custom_pages.aspx.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.Web.UI.WebControls;
  17. using System.Text.RegularExpressions;
  18. using System.Web.Security;
  19. using System.Data;
  20. using iNethinkCMS.Command;
  21. using iNethinkCMS.Web.UI;
  22. namespace iNethinkCMS.Web.admin.custom
  23. {
  24. public partial class custom_pages : Admin_BasePage
  25. {
  26. iNethinkCMS.BLL.BLL_iNethinkCMS_Custom_Pages bll = new iNethinkCMS.BLL.BLL_iNethinkCMS_Custom_Pages();
  27. iNethinkCMS.Model.Model_iNethinkCMS_Custom_Pages model = new iNethinkCMS.Model.Model_iNethinkCMS_Custom_Pages();
  28. iNethinkCMS.BLL.BLL_iNethinkCMS_Upload bll_upload = new iNethinkCMS.BLL.BLL_iNethinkCMS_Upload();
  29. private string vNavInfo = "当前位置:";
  30. private string vAct = "";
  31. private int vPage = 1;
  32. private int vID = 0;
  33. private string vSQL = "";
  34. private string vKeyType = "";
  35. private string vKey = "";
  36. private string vTitle;
  37. private string vTemplatePath;
  38. private string vDir;
  39. private string vKeywords;
  40. private string vDescription;
  41. private string vHtml;
  42. protected void Page_Load(object sender, EventArgs e)
  43. {
  44. CheckUserPower("d");
  45. #region
  46. vAct = Request.QueryString["Act"] != null ? Request.QueryString["Act"] : "";
  47. if (Request.QueryString["Page"] != null && Request.QueryString["Page"].Trim() != "")
  48. {
  49. if (!int.TryParse(Request.QueryString["Page"], out vPage))
  50. {
  51. vPage = 1;
  52. }
  53. }
  54. vKeyType = Request.QueryString["sKeyType"] != null ? Request.QueryString["sKeyType"] : "";
  55. vKey = Request.QueryString["sKey"] != null ? Request.QueryString["sKey"] : "";
  56. vKey = vKey.Replace("'", "");
  57. if (Request.QueryString["ID"] != null && Request.QueryString["ID"].Trim() != "")
  58. {
  59. if (!int.TryParse(Request.QueryString["ID"], out vID))
  60. {
  61. Response.Write("ID Error");
  62. Response.End();
  63. }
  64. }
  65. if (vKey.Length > 0)
  66. {
  67. if (vKeyType == "ID")
  68. {
  69. if (iNethinkCMS.Command.Command_Validate.IsNumber(vKey) == true)
  70. {
  71. vSQL += vKeyType + " = " + vKey + "";
  72. }
  73. else
  74. {
  75. vKey = "";
  76. }
  77. }
  78. else
  79. {
  80. vSQL += vKeyType + " Like '%" + vKey + "%'";
  81. }
  82. }
  83. #endregion
  84. this.mainID.Visible = false;
  85. this.guideID.Visible = false;
  86. this.MakeAllPageID.Visible = false;
  87. switch (vAct)
  88. {
  89. case "guide":
  90. this.guideID.Visible = true;
  91. if (vID == 0)
  92. {
  93. this.navInfoID.InnerText = vNavInfo + "自定页面添加";
  94. this.Button_Submit.Click += new EventHandler(Button_Submit_Click_Add);
  95. }
  96. else
  97. {
  98. this.navInfoID.InnerText = vNavInfo + "自定页面修改";
  99. this.Button_Submit.Click += new EventHandler(Button_Submit_Click_Edit);
  100. }
  101. if (!IsPostBack)
  102. {
  103. if (Request.UrlReferrer != null)
  104. {
  105. ViewState["reJumpUrl"] = Request.UrlReferrer.AbsoluteUri;
  106. }
  107. Fun_SetValue(vID); //获取frm基本信息
  108. }
  109. break;
  110. case "delete":
  111. Fun_Delete(vID);
  112. break;
  113. case "makepage":
  114. this.searchInfoID.Visible = false;
  115. string vMsgInfo = CreateFile(vID);
  116. MessageBox.ShowAndRedirect(this, vMsgInfo, Request.UrlReferrer.AbsoluteUri);
  117. break;
  118. case "makeallpage":
  119. this.searchInfoID.Visible = false;
  120. this.navInfoID.InnerText = vNavInfo + "生成全部自定页面";
  121. this.MakeAllPageID.Visible = true;
  122. break;
  123. case "makeallpage_do":
  124. //读取系统内所有自定义模板
  125. int tmpCountNum = 0;
  126. DataTable dt = bll.GetList("").Tables[0];
  127. for (int i = 0; i < dt.Rows.Count; i++)
  128. {
  129. int tmpID = (int)dt.Rows[i]["ID"];
  130. string tmpInfo = CreateFile(tmpID);
  131. if (string.IsNullOrEmpty(tmpInfo))
  132. {
  133. tmpCountNum++;
  134. }
  135. }
  136. Response.Write(dt.Rows.Count.ToString() + "|&&|" + tmpCountNum.ToString());
  137. Response.End();
  138. break;
  139. default:
  140. this.navInfoID.InnerText = vNavInfo + "自定页面管理";
  141. this.mainID.Visible = true;
  142. if (!IsPostBack)
  143. {
  144. this.sKeyType.SelectedValue = vKeyType;
  145. this.sKey.Text = vKey;
  146. }
  147. PageListInfo();
  148. break;
  149. }
  150. }
  151. #region mainID 列表
  152. protected void PageListInfo()
  153. {
  154. int vPageSize = int.Parse(siteConfig.PageListNum);
  155. int vRecordCount = bll.GetRecordCount(vSQL);
  156. Repeater.DataSource = bll.GetListByPage(vSQL, "ID Desc", (vPage - 1) * vPageSize, vPage * vPageSize);
  157. Repeater.DataBind();
  158. string pTemp = "";
  159. int vPageCount = 1;
  160. if (vRecordCount > 0)
  161. {
  162. vPageCount = (int)Math.Ceiling((double)vRecordCount / (double)vPageSize);
  163. pTemp = WebUI_PageList.GetPagingInfo_Manage(vPageCount, vRecordCount, vPage, vPageSize);
  164. }
  165. this.pagelist.InnerHtml = pTemp;
  166. this.iNoInfo.Visible = vRecordCount == 0 ? true : false;
  167. }
  168. protected void Button_Search_Click(object sender, EventArgs e)
  169. {
  170. Response.Redirect(Request.Path + "?skeytype=" + this.sKeyType.SelectedValue + "&skey=" + this.sKey.Text);
  171. }
  172. #endregion
  173. #region guideID 信息修改/添加
  174. private void Fun_SetValue(int byID)
  175. {
  176. if (byID == 0)
  177. {
  178. this.txtTitle.Text = "";
  179. this.txtTemplatePath.Text = "";
  180. this.txtDir.Text = "";
  181. this.txtKeywords.Text = "";
  182. this.txtDescription.Text = "";
  183. this.txtHtml.Text = "";
  184. }
  185. else
  186. {
  187. this.txtDir.ReadOnly = true;
  188. model = bll.GetModel(byID);
  189. this.txtTitle.Text = model.Title;
  190. this.txtTemplatePath.Text = model.TemplatePath;
  191. this.txtDir.Text = model.Dir;
  192. this.txtKeywords.Text = model.Keywords;
  193. this.txtDescription.Text = model.Description;
  194. this.txtHtml.Text = model.Html;
  195. }
  196. }
  197. private bool Fun_GetValue()
  198. {
  199. if (this.txtTitle.Text.Trim().Length == 0)
  200. {
  201. MessageBox.Show(this, "请输入页面标题!");
  202. return false;
  203. }
  204. if (this.txtDir.Text.Trim().Length == 0)
  205. {
  206. MessageBox.Show(this, "请输入保存路径!");
  207. return false;
  208. }
  209. if (Command.Command_StringPlus.Left(this.txtDir.Text.Trim(), 1) != "/")
  210. {
  211. MessageBox.Show(this, "保存路径输入错误,必须以“/”开头!");
  212. return false;
  213. }
  214. vTitle = this.txtTitle.Text.Trim();
  215. vTemplatePath = this.txtTemplatePath.Text.Trim();
  216. vDir = this.txtDir.Text.Trim();
  217. vKeywords = Command.Command_StringPlus.LostHTML(this.txtKeywords.Text.Trim());
  218. vDescription = Command.Command_StringPlus.LostHTML(this.txtDescription.Text.Trim());
  219. vHtml = this.txtHtml.Text;
  220. return true;
  221. }
  222. protected void Button_Submit_Click_Add(object sender, EventArgs e)
  223. {
  224. if (Fun_GetValue() == true)
  225. {
  226. if (bll.GetRecordCount("Dir = '" + vDir + "'") > 0)
  227. {
  228. MessageBox.Show(this, "保存路径 [" + vDir + "] 已经存在!");
  229. return;
  230. }
  231. model.Title = vTitle;
  232. model.TemplatePath = vTemplatePath;
  233. model.Dir = vDir;
  234. model.Keywords = vKeywords;
  235. model.Description = vDescription;
  236. model.Html = vHtml;
  237. bll.Add(model);
  238. int vAid = bll.GetMaxID();
  239. bll_upload.UpdateUploadFile(vHtml, 3, vAid, 0);
  240. string vMsgInfo = CreateFile(vAid); //创建文件
  241. MessageBox.ShowAndRedirect(this, vMsgInfo, Request.Path);
  242. }
  243. }
  244. protected void Button_Submit_Click_Edit(object sender, EventArgs e)
  245. {
  246. if (Fun_GetValue() == true)
  247. {
  248. if (bll.GetRecordCount("ID = " + vID) == 0) //判断该ID是否被删除
  249. {
  250. MessageBox.ShowAndRedirect(this, "你所需要更新的记录 [" + vID + "] 不存在!", ViewState["reJumpUrl"].ToString());
  251. return;
  252. }
  253. if (bll.GetRecordCount("ID <> " + vID + " And Dir = '" + vDir + "'") > 0)
  254. {
  255. MessageBox.Show(this, "保存路径 [" + vDir + "] 已经存在!");
  256. return;
  257. }
  258. model.ID = vID;
  259. model.Title = vTitle;
  260. model.TemplatePath = vTemplatePath;
  261. model.Dir = vDir;
  262. model.Keywords = vKeywords;
  263. model.Description = vDescription;
  264. model.Html = vHtml;
  265. bll.Update(model);
  266. bll_upload.UpdateUploadFile_Reset(3, vID, 0); //重置上传表中的数据
  267. bll_upload.UpdateUploadFile(vHtml, 3, vID, 0);
  268. string vMsgInfo = CreateFile(vID); //创建文件
  269. MessageBox.ShowAndRedirect(this, vMsgInfo, ViewState["reJumpUrl"].ToString());
  270. }
  271. }
  272. #endregion
  273. #region Delete 数据删除
  274. protected void Fun_Delete(int byID)
  275. {
  276. model = bll.GetModel(byID);
  277. string vPath = Server.MapPath(model.Dir);
  278. if (System.IO.File.Exists(vPath))
  279. {
  280. System.IO.File.Delete(vPath);
  281. }
  282. if (bll.Delete(byID) == true)
  283. {
  284. bll_upload.UpdateUploadFile_Reset(3, byID, 0); //重置上传表中的数据
  285. Response.Redirect(Request.UrlReferrer.AbsoluteUri);
  286. }
  287. }
  288. #endregion
  289. ///
  290. /// 自定义页面文件创建
  291. ///
  292. public string CreateFile(int byID)
  293. {
  294. string vBackInfo = "";
  295. model = bll.GetModel(byID);
  296. string isTemplatePath = model.TemplatePath;
  297. string isHtml = model.Html;
  298. string isDir = HttpContext.Current.Server.MapPath(model.Dir);
  299. string isPath = System.IO.Path.GetDirectoryName(isDir);
  300. string vHtmlInfo = "";
  301. //模板文件模式
  302. if (isTemplatePath != null && isTemplatePath.Length > 0)
  303. {
  304. isTemplatePath = HttpContext.Current.Server.MapPath(siteConfig.TemplateDir + isTemplatePath);
  305. if (System.IO.File.Exists(isTemplatePath))
  306. {
  307. //读取模板文件
  308. try
  309. {
  310. System.IO.StreamReader sr = new System.IO.StreamReader(isTemplatePath, System.Text.Encoding.UTF8);
  311. vHtmlInfo = sr.ReadToEnd();
  312. sr.Close();
  313. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase);
  314. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase);
  315. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase);
  316. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase);
  317. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:sitepath}"), "<a href=\"/\">首页</a> > " + model.Title, RegexOptions.IgnoreCase);
  318. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagetitle}"), model.Title, RegexOptions.IgnoreCase);
  319. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagekeywords}"), model.Keywords, RegexOptions.IgnoreCase);
  320. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagedescription}"), model.Description, RegexOptions.IgnoreCase);
  321. vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagehtml}"), isHtml, RegexOptions.IgnoreCase);
  322. WebUI_Template wt = new WebUI_Template();
  323. wt.vContent = vHtmlInfo;
  324. wt.Parser_MyTag();
  325. wt.Parser_List();
  326. wt.Parser_Page();
  327. wt.Parser_IF();
  328. vHtmlInfo = wt.vContent;
  329. }
  330. catch (Exception ex)
  331. {
  332. string exMessage = ex.Message.Replace(System.Environment.NewLine, "");
  333. exMessage = Command_Validate.SqlTextClear(exMessage);
  334. vBackInfo = @"自定义页面生成发生错误!\n" + exMessage;
  335. }
  336. }
  337. else
  338. {
  339. vBackInfo = @"自定义页面生成发生错误!\n模板文件不存在!";
  340. }
  341. }
  342. else
  343. {
  344. vHtmlInfo = isHtml;
  345. }
  346. //无错误时,生成静态文件
  347. if (string.IsNullOrEmpty(vBackInfo))
  348. {
  349. System.IO.Directory.CreateDirectory(isPath);
  350. System.IO.StreamWriter sw = null;
  351. try
  352. {
  353. sw = new System.IO.StreamWriter(isDir, false, System.Text.Encoding.UTF8);
  354. sw.Write(vHtmlInfo);
  355. sw.Flush();
  356. }
  357. catch (Exception ex)
  358. {
  359. vBackInfo = @"自定义页面生成发生错误!\n" + ex.ToString();
  360. }
  361. finally
  362. {
  363. sw.Close();
  364. }
  365. }
  366. return vBackInfo;
  367. }
  368. }
  369. }