migrate to new git
This commit is contained in:
511
web/admin/news/news_reg.aspx.cs
Normal file
511
web/admin/news/news_reg.aspx.cs
Normal file
@@ -0,0 +1,511 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.Web;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class admin_news_news_reg : MyWeb.config
|
||||
{
|
||||
private Model.ezEntities _db = new Model.ezEntities();
|
||||
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (!IsPostBack)
|
||||
{
|
||||
if (isStrNull(Request["num"]))
|
||||
{
|
||||
uptime.Text = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
addNewRow();
|
||||
}
|
||||
else
|
||||
{
|
||||
int _num = Val(Request["num"]);
|
||||
var qry = _db.news.AsEnumerable();
|
||||
//var prod = _db.news.Where(q => q.num == _num).ToList();
|
||||
var prod = qry.Where(q => q.num == _num).FirstOrDefault();
|
||||
|
||||
if (prod != null)
|
||||
{
|
||||
subject.Text = prod.subject;
|
||||
if (prod.kind.HasValue)
|
||||
{
|
||||
kind_txt.Value = prod.news_kind.kind.ToString();
|
||||
kind.Value = prod.kind.ToString();
|
||||
}
|
||||
status.SelectedValue = prod.status;
|
||||
topping.Checked = ValString(prod.topping).Equals("Y");
|
||||
|
||||
if (prod.selltime1.HasValue)
|
||||
{
|
||||
start_day.Text = prod.selltime1.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
if (prod.selltime2.HasValue)
|
||||
{
|
||||
end_day.Text = prod.selltime2.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
if (prod.uptime.HasValue)
|
||||
{
|
||||
uptime.Text = prod.uptime.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
if (prod.author.HasValue)
|
||||
author.Text = prod.admin.u_name;
|
||||
|
||||
if (prod.activity_num.HasValue)
|
||||
{
|
||||
activity_num_txt.Value = prod.activity.subject.ToString();
|
||||
activity_num.Value = prod.activity_num.ToString();
|
||||
}
|
||||
|
||||
|
||||
word.Text = prod.word;
|
||||
demo.Text = prod.demo;
|
||||
|
||||
initNewsFiles(_num);
|
||||
addNewRow();
|
||||
|
||||
edit.Visible = true;
|
||||
goback.Visible = true;
|
||||
add.Visible = false;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Response.Redirect("index.aspx");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected void goback_Click(object sender, EventArgs e)
|
||||
{
|
||||
Response.Redirect("index.aspx?page=" + Convert.ToString(Request["page"]));
|
||||
}
|
||||
|
||||
|
||||
#region 資料新增
|
||||
|
||||
protected void add_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (Page.IsValid) {
|
||||
L_msg.Text = "";
|
||||
|
||||
MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
||||
string[] pic_name2 = { };
|
||||
string[] pic_type = { };
|
||||
string[] pic_name = fileSystem.UploadNewsFile(Model.news.Dir, 800, admin.info.num, ref pic_name2, ref pic_type); //縮圖的寬高不得超過800象素,如果不是圖片也會傳
|
||||
|
||||
|
||||
Model.news news = new Model.news();//新增
|
||||
news.subject = subject.Text;
|
||||
if (!isStrNull(kind.Value)) { news.kind = Val(kind.Value); }
|
||||
if (!isStrNull(start_day.Text)) { news.selltime1 = ValDate(start_day.Text); } else { news.selltime1 = null; }
|
||||
if (!isStrNull(end_day.Text)) { news.selltime2 = ValDate(end_day.Text); } else { news.selltime2 = null; }
|
||||
news.word = word.Text;
|
||||
news.demo = demo.Text;
|
||||
if (!isStrNull(uptime.Text)) { news.uptime = ValDate(uptime.Text); }
|
||||
if (admin.info.num > 0) { news.author = admin.info.num; } else { news.author =null; }
|
||||
if (!isStrNull(status.SelectedValue)) { news.status = status.SelectedValue; }
|
||||
news.topping = topping.Checked ? "Y" : "N";
|
||||
if (!isStrNull(activity_num.Value)) { news.activity_num = Val(activity_num.Value); }
|
||||
|
||||
|
||||
|
||||
_db.news.Add(news);
|
||||
_db.SaveChanges();
|
||||
int _id = news.num;
|
||||
if( _id > 0)
|
||||
{
|
||||
//檔案上傳
|
||||
if( SaveFiles(_id, pic_name, pic_name2, pic_type))
|
||||
{
|
||||
Model.admin_log admin_log = new Model.admin_log();
|
||||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.News, (int)Model.admin_log.Status.Insert, subject.Text);
|
||||
Response.Redirect("index.aspx");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
L_msg.Type = alert_type.danger;
|
||||
L_msg.Text = "Error";
|
||||
}
|
||||
//using (Model.ezEntities _db = new Model.ezEntities())
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 資料修改
|
||||
|
||||
protected void edit_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (Page.IsValid)
|
||||
{
|
||||
L_msg.Text = "";
|
||||
MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
||||
string[] pic_name2 = { };
|
||||
string[] pic_type = { };
|
||||
string[] pic_name = fileSystem.UploadNewsFile(Model.news.Dir, 800,admin.info.num, ref pic_name2, ref pic_type); //縮圖的寬高不得超過800象素,如果不是圖片也會傳
|
||||
int _num = Val(Request["num"]);
|
||||
Model.news news = _db.news.Where(q => q.num == _num).FirstOrDefault();//修改
|
||||
if (news != null)
|
||||
{
|
||||
news.subject = subject.Text;
|
||||
if (!isStrNull(kind.Value)) { news.kind = Val(kind.Value); }
|
||||
if (!isStrNull(start_day.Text)) { news.selltime1 = ValDate(start_day.Text); } else { news.selltime1 = null; }
|
||||
if (!isStrNull(end_day.Text)) { news.selltime2 = ValDate(end_day.Text); } else { news.selltime2 = null; }
|
||||
news.word = word.Text;
|
||||
news.demo = demo.Text;
|
||||
if (!isStrNull(uptime.Text)) { news.uptime = ValDate(uptime.Text); }
|
||||
if (admin.info.num > 0) { news.author = admin.info.num; }
|
||||
if (!isStrNull(status.SelectedValue)) { news.status = status.SelectedValue; }
|
||||
if (admin.info.num > 0) { news.author = admin.info.num; } else { news.author = null; }
|
||||
news.topping = topping.Checked ? "Y" : "N";
|
||||
if (!isStrNull(activity_num.Value)) { news.activity_num = Val(activity_num.Value); }
|
||||
|
||||
|
||||
_db.SaveChanges();
|
||||
//檔案
|
||||
if (SaveFiles(Val(Request["num"]), pic_name, pic_name2, pic_type))
|
||||
{
|
||||
Model.admin_log admin_log = new Model.admin_log();
|
||||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.News, (int)Model.admin_log.Status.Update, subject.Text);
|
||||
Response.Redirect("index.aspx?page=" + Convert.ToString(Request["page"]));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
L_msg.Type = alert_type.danger;
|
||||
L_msg.Text = "查無資料";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 檔案管理
|
||||
|
||||
#region 儲存
|
||||
//public void SaveFiles(int news_id, string[] pic_name , string[] _pic_name2 , string[] _pic_type)
|
||||
//{
|
||||
// DataTable picDt = new DataTable();
|
||||
|
||||
// IEnumerable<DataRow> queryFiles = (IEnumerable<DataRow>)from dTc in _db.news_files.AsEnumerable() where dTc.news_id == news_id select dTc;
|
||||
// // int aa = queryTeacher.Count<DataRow>(); //Count=1
|
||||
// if (queryFiles.Count<DataRow>() > 0) { picDt = queryFiles.CopyToDataTable<DataRow>(); }
|
||||
|
||||
// if (picDt.Rows.Count > 0)
|
||||
// {
|
||||
|
||||
// }
|
||||
// //處理本次更新的檔案
|
||||
// MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
||||
// List<string> nums = new List<string>();
|
||||
|
||||
// DataTable dt = GetFileRows();
|
||||
// if (dt.Rows.Count > 0)
|
||||
// {
|
||||
// int p = 1;
|
||||
// foreach (DataRow row in dt.Rows)
|
||||
// {
|
||||
// bool _change = false;
|
||||
// string orgPic = GiftOrgPic(ValString(row["num"]), ref picDt);
|
||||
// if (!isStrNull(pic_name[p]))
|
||||
// {
|
||||
// if (!isStrNull(orgPic)) { fileSystem.Delete(MyWeb.news.Dir + "/" + orgPic); }
|
||||
// _change = true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// pic_name[p] = orgPic;
|
||||
// }
|
||||
|
||||
// Model.news_files ff = _db.news_files.Where(q => q.news_id == news_id && q.num == ValString(row["num"]) ).FirstOrDefault();//修改
|
||||
// if (ff != null)
|
||||
// {
|
||||
// ff.pic1 = pic_name[p];
|
||||
// if (_change)
|
||||
// {
|
||||
// ff.reg_time = DateTime.Now;
|
||||
// ff.pic_name = _pic_name2[p];
|
||||
// ff.pic_type = _pic_type[p];
|
||||
// }
|
||||
|
||||
// _db.SaveChanges();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (!isStrNull(pic_name[p]))
|
||||
// {
|
||||
// Model.news_files ffNew = new Model.news_files();
|
||||
// ffNew.num= ValString(row["num"]);
|
||||
// ffNew.pic1 = pic_name[p];
|
||||
// ffNew.news_id = news_id;
|
||||
// ffNew.pic_name = _pic_name2[p];
|
||||
// ffNew.pic_type = _pic_type[p];
|
||||
// _db.news_files.Add(ffNew);
|
||||
// _db.SaveChanges();
|
||||
// }
|
||||
// }
|
||||
|
||||
// p++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (picDt.Rows.Count > 0)
|
||||
// {
|
||||
// foreach (DataRow row in picDt.Rows)
|
||||
// {
|
||||
// //刪掉不要的檔案
|
||||
// var prod = _db.news_files.AsEnumerable().Where(q => q.news_id == news_id && q.num == ValString(row["num"])).FirstOrDefault(); //刪除該筆資料
|
||||
// if (prod != null)
|
||||
// {
|
||||
// _db.news_files.Remove(prod);
|
||||
// _db.SaveChanges(); //執行
|
||||
// }
|
||||
|
||||
// if (!isStrNull(row["pic1"])) { fileSystem.Delete(MyWeb.news.Dir + "/" + ValString(row["pic1"])); }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//}
|
||||
public bool SaveFiles(int news_id, string[] pic_name, string[] _pic_name2, string[] _pic_type)
|
||||
{
|
||||
|
||||
bool success = true;
|
||||
|
||||
MyWeb.sql sql = new MyWeb.sql();
|
||||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||||
try
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
//載入原有的檔案
|
||||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||||
sqlCmd.CommandText = "select [num],pic1 from [news_files] where news_id=? order by [num]";
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("news_id", news_id));
|
||||
DataTable picDt = sql.dataTable(sqlCmd);
|
||||
|
||||
//處理本次更新的檔案
|
||||
MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
||||
List<string> nums = new List<string>();
|
||||
|
||||
DataTable dt = GetFileRows();
|
||||
if (dt.Rows.Count > 0)
|
||||
{
|
||||
int p = 1;
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
bool _change = false;
|
||||
string orgPic = GiftOrgPic(ValString(row["num"]), ref picDt);
|
||||
if (!isStrNull(pic_name[p]))
|
||||
{
|
||||
if (!isStrNull(orgPic)) { fileSystem.Delete(Model.news.Dir + "/" + orgPic); }
|
||||
_change = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
pic_name[p] = orgPic;
|
||||
}
|
||||
|
||||
sqlCmd = new OleDbCommand("", sqlConn);
|
||||
sqlCmd.CommandText = "UPDATE [news_files] SET pic1=?" + (_change ? ",reg_time=?,pic_name=?,pic_type=?" : "") + " WHERE news_id=? and num=? ";
|
||||
if (!isStrNull(pic_name[p]))
|
||||
{
|
||||
sqlCmd.CommandText += " IF @@ROWCOUNT = 0 ";
|
||||
sqlCmd.CommandText += "INSERT INTO [news_files] (num,pic1,news_id,pic_name,pic_type) VALUES (?,?,?,?,?)";
|
||||
}
|
||||
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic1", pic_name[p]));
|
||||
if (_change)
|
||||
{
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("reg_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic_name", _pic_name2[p]));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic_type", _pic_type[p]));
|
||||
}
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("news_id", news_id));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("num", ValString(row["num"])));
|
||||
if (!isStrNull(pic_name[p]))
|
||||
{
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("num", ValString(row["num"])));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic1", pic_name[p]));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("news_id", news_id));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic_name", _pic_name2[p]));
|
||||
sqlCmd.Parameters.Add(new OleDbParameter("pic_type", _pic_type[p]));
|
||||
|
||||
}
|
||||
|
||||
sqlCmd.ExecuteNonQuery();
|
||||
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
if (picDt.Rows.Count > 0)
|
||||
{
|
||||
foreach (DataRow row in picDt.Rows)
|
||||
{
|
||||
//刪掉不要的檔案
|
||||
OleDbCommand sqlCmd2 = new OleDbCommand("", sqlConn);
|
||||
sqlCmd2.CommandText = "delete from [news_files] where news_id=? and num=?";
|
||||
sqlCmd2.Parameters.Add(new OleDbParameter("news_id", news_id));
|
||||
sqlCmd2.Parameters.Add(new OleDbParameter("num", ValString(row["num"])));
|
||||
sqlCmd2.ExecuteNonQuery();
|
||||
|
||||
if (!isStrNull(row["pic1"])) { fileSystem.Delete(Model.news.Dir + "/" + ValString(row["pic1"])); }
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Response.Write(ex.Message);
|
||||
success = false;
|
||||
L_msg.Type = alert_type.danger;
|
||||
L_msg.Text = ex.Message;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
sqlConn.Close(); sqlConn.Dispose();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
protected string GiftOrgPic(string id, ref DataTable dt)
|
||||
{
|
||||
if (dt.Rows.Count > 0)
|
||||
foreach (DataRow row in dt.Rows)
|
||||
if (row["num"].ToString() == id)
|
||||
{
|
||||
string pic = row["pic1"].ToString();
|
||||
dt.Rows.Remove(row);
|
||||
return pic;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 載入
|
||||
protected void initNewsFiles(int num = 0)
|
||||
{
|
||||
var qry = _db.news_files.AsEnumerable();
|
||||
if (num > 0)
|
||||
qry = qry.Where(q => q.news_id == num).ToList();
|
||||
else
|
||||
qry = qry.ToList();
|
||||
|
||||
fileRepeater.DataSource = qry;
|
||||
fileRepeater.DataBind();
|
||||
}
|
||||
protected void fileRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
|
||||
{
|
||||
//DataRowView row = (DataRowView)e.Item.DataItem;
|
||||
|
||||
|
||||
((HiddenField)e.Item.FindControl("id")).Value = DataBinder.Eval(e.Item.DataItem, "num").ToString();
|
||||
((HiddenField)e.Item.FindControl("pic")).Value = DataBinder.Eval(e.Item.DataItem, "pic1").ToString();
|
||||
((Literal)e.Item.FindControl("pic_name")).Text = DataBinder.Eval(e.Item.DataItem, "pic_name").ToString();
|
||||
((Literal)e.Item.FindControl("pic_type")).Text = DataBinder.Eval(e.Item.DataItem, "pic_type").ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected DataTable GetFileRows()
|
||||
{
|
||||
DataTable dt = new DataTable();
|
||||
dt.Columns.Add("num");
|
||||
dt.Columns.Add("pic1");
|
||||
dt.Columns.Add("pic_name");
|
||||
dt.Columns.Add("pic_type");
|
||||
|
||||
|
||||
if (fileRepeater.Items.Count > 0)
|
||||
{
|
||||
|
||||
foreach (RepeaterItem item in fileRepeater.Items)
|
||||
{
|
||||
DataRow row = dt.NewRow();
|
||||
|
||||
row["pic1"] = ((HiddenField)item.FindControl("pic")).Value;
|
||||
row["num"] = ((HiddenField)item.FindControl("id")).Value;
|
||||
row["pic_name"] = ((Literal)item.FindControl("pic_name")).Text;
|
||||
row["pic_type"] = ((Literal)item.FindControl("pic_type")).Text;
|
||||
dt.Rows.Add(row);
|
||||
}
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
#region 新增
|
||||
protected void addNewRow()
|
||||
{
|
||||
string id = DateTime.Now.ToString("yyyyMMddHHmmss"); ;
|
||||
DataTable dt = GetFileRows();
|
||||
DataRow row = dt.NewRow();
|
||||
row["num"] = id;
|
||||
row["pic1"] = "";
|
||||
row["pic_name"] = "";
|
||||
row["pic_type"] = "";
|
||||
dt.Rows.Add(row);
|
||||
fileRepeater.DataSource = dt;
|
||||
fileRepeater.DataBind();
|
||||
|
||||
ScriptJS("$('#pics').append('<input type=\"file\" data-id=\"" + id + "\" name=\"picUpload" + id + "\" id=\"picUpload" + id + "\">');bindPics();");
|
||||
|
||||
}
|
||||
protected void AddBtn_Click(object sender, EventArgs e)
|
||||
{
|
||||
addNewRow();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 刪除
|
||||
|
||||
protected void DelBtn_Click(object sender, EventArgs e)
|
||||
{
|
||||
RepeaterItem rItem = (RepeaterItem)((LinkButton)sender).NamingContainer;
|
||||
string id = ((HiddenField)rItem.FindControl("id")).Value;
|
||||
DataTable dt = GetFileRows();
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
if (ValString(row["num"]) == id)
|
||||
{
|
||||
dt.Rows.Remove(row);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fileRepeater.DataSource = dt;
|
||||
fileRepeater.DataBind();
|
||||
ScriptJS("$('#picUpload" + id + "').remove();bindPics(false);");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user