PageRenderTime 22ms CodeModel.GetById 58ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/Content/CMSDesk/MasterPage/PageEdit.aspx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 400 lines | 291 code | 66 blank | 43 comment | 58 complexity | e5ea248c945140e6bf0a064576da6550 MD5 | raw file
  1. using System;
  2. using System.Web;
  3. using System.Web.UI.WebControls;
  4. using CMS.CMSHelper;
  5. using CMS.ExtendedControls;
  6. using CMS.GlobalHelper;
  7. using CMS.IO;
  8. using CMS.PortalEngine;
  9. using CMS.SettingsProvider;
  10. using CMS.SiteProvider;
  11. using CMS.TreeEngine;
  12. using CMS.UIControls;
  13. using CMS.WorkflowEngine;
  14. using TreeNode = CMS.TreeEngine.TreeNode;
  15. public partial class CMSModules_Content_CMSDesk_MasterPage_PageEdit : CMSContentPage
  16. {
  17. #region "Variables"
  18. protected string mSave = null;
  19. protected int nodeId = 0;
  20. protected TreeNode node = null;
  21. protected TreeProvider tree = null;
  22. protected CurrentUserInfo user = null;
  23. protected string mHead = null;
  24. protected string mBeforeLayout = null;
  25. protected string mAfterLayout = null;
  26. protected string mBody = null;
  27. #endregion
  28. #region "Methods"
  29. protected void Page_Load(object sender, EventArgs e)
  30. {
  31. CMSContext.ViewMode = ViewModeEnum.MasterPage;
  32. // Keep current user
  33. user = CMSContext.CurrentUser;
  34. // Check UIProfile
  35. if (!user.IsAuthorizedPerUIElement("CMS.Content", "MasterPage"))
  36. {
  37. RedirectToCMSDeskUIElementAccessDenied("CMS.Content", "MasterPage");
  38. }
  39. // Check "Design" permission
  40. if (!user.IsAuthorizedPerResource("CMS.Design", "Design"))
  41. {
  42. RedirectToAccessDenied("CMS.Design", "Design");
  43. }
  44. // Register the scripts
  45. ScriptHelper.RegisterProgress(this);
  46. ScriptHelper.RegisterSaveShortcut(btnSave, null, false);
  47. // Save changes support
  48. bool confirmChanges = SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSConfirmChanges");
  49. string script = string.Empty;
  50. if (confirmChanges)
  51. {
  52. script = "var confirmLeave='" + ResHelper.GetString("Content.ConfirmLeave", user.PreferredUICultureCode) + "'; \n";
  53. }
  54. else
  55. {
  56. script += "confirmChanges = false;";
  57. }
  58. ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "saveChangesScript", ScriptHelper.GetScript(script));
  59. nodeId = QueryHelper.GetInteger("NodeId", 0);
  60. try
  61. {
  62. CMSContext.CurrentPageInfo = PageInfoProvider.GetPageInfo(CMSContext.CurrentSiteName, "/", CMSContext.PreferredCultureCode, null, false);
  63. // Title
  64. string title = CMSContext.CurrentTitle;
  65. if (!string.IsNullOrEmpty(title))
  66. {
  67. title = "<title>" + title + "</title>";
  68. }
  69. // Body class
  70. string bodyCss = CMSContext.CurrentBodyClass;
  71. if (bodyCss != null && bodyCss.Trim() != "")
  72. {
  73. bodyCss = "class=\"" + bodyCss + "\"";
  74. }
  75. else
  76. {
  77. bodyCss = "";
  78. }
  79. // metadata
  80. string meta = "<meta http-equiv=\"pragma\" content=\"no-cache\" />";
  81. string description = CMSContext.CurrentDescription;
  82. if (description != "")
  83. {
  84. meta += "<meta name=\"description\" content=\"" + description + "\" />";
  85. }
  86. string keywords = CMSContext.CurrentKeyWords;
  87. if (keywords != "")
  88. {
  89. meta += "<meta name=\"keywords\" content=\"" + keywords + "\" />";
  90. }
  91. // Site style sheet
  92. string cssSiteSheet = "";
  93. CssStylesheetInfo cssInfo = null;
  94. int stylesheetId = CMSContext.CurrentPageInfo.DocumentStylesheetID;
  95. cssInfo = CssStylesheetInfoProvider.GetCssStylesheetInfo((stylesheetId > 0) ? stylesheetId : CMSContext.CurrentSite.SiteDefaultStylesheetID);
  96. if (cssInfo != null)
  97. {
  98. cssSiteSheet = CSSHelper.GetCSSFileLink(CSSHelper.GetStylesheetUrl(cssInfo.StylesheetName));
  99. }
  100. // Theme css files
  101. string themeCssFiles = "";
  102. if (cssInfo != null)
  103. {
  104. try
  105. {
  106. string directory = URLHelper.GetPhysicalPath(string.Format("~/App_Themes/{0}/", cssInfo.StylesheetName));
  107. if (Directory.Exists(directory))
  108. {
  109. foreach (string file in Directory.GetFiles(directory, "*.css"))
  110. {
  111. themeCssFiles += CSSHelper.GetCSSFileLink(CSSHelper.GetPhysicalCSSUrl(cssInfo.StylesheetName, Path.GetFileName(file)));
  112. }
  113. }
  114. }
  115. catch
  116. {
  117. }
  118. }
  119. // Add values to page
  120. mHead = FormatHTML(HighlightHTML(title + meta + cssSiteSheet + themeCssFiles), 2);
  121. mBody = bodyCss;
  122. }
  123. catch
  124. {
  125. lblError.Visible = true;
  126. lblError.Text = GetString("MasterPage.PageEditErr");
  127. }
  128. // Prepare the hints and typw dropdown
  129. lblType.Text = ResHelper.GetString("PageLayout.Type");
  130. if (drpType.Items.Count == 0)
  131. {
  132. drpType.Items.Add(new ListItem(ResHelper.GetString("TransformationType.Ascx"), TransformationTypeEnum.Ascx.ToString()));
  133. drpType.Items.Add(new ListItem(ResHelper.GetString("TransformationType.Html"), TransformationTypeEnum.Html.ToString()));
  134. }
  135. string lang = ValidationHelper.GetString(SettingsHelper.AppSettings["CMSProgrammingLanguage"], "C#");
  136. ltlDirectives.Text = "&lt;%@ Control Language=\"" + lang + "\" ClassName=\"Simple\" Inherits=\"CMS.PortalControls.CMSAbstractLayout\" %&gt;<br />&lt;%@ Register Assembly=\"CMS.PortalControls\" Namespace=\"CMS.PortalControls\" TagPrefix=\"cc1\" %&gt;";
  137. if (!SettingsKeyProvider.UsingVirtualPathProvider)
  138. {
  139. lblChecked.Visible = true;
  140. lblChecked.Text = "<br />" + AddSpaces(2) + GetString("MasterPage.VirtualPathProviderNotRunning");
  141. txtLayout.ReadOnly = true;
  142. }
  143. LoadData();
  144. // Register synchronization script for split mode
  145. if (CMSContext.DisplaySplitMode)
  146. {
  147. RegisterSplitModeSync(true, false);
  148. }
  149. }
  150. protected override void OnPreRender(EventArgs e)
  151. {
  152. base.OnPreRender(e);
  153. bool editingEnabled = true;
  154. string info = null;
  155. // Setup the information and code type
  156. bool isAscx = (drpType.SelectedValue.ToLower() == "ascx");
  157. if (isAscx)
  158. {
  159. txtLayout.Language = LanguageEnum.ASPNET;
  160. info = ResHelper.GetString("Administration-PageLayout_New.Hint");
  161. // Check the edit code permission
  162. if (!user.IsAuthorizedPerResource("CMS.Design", "EditCode"))
  163. {
  164. editingEnabled = false;
  165. info = ResHelper.GetString("EditCode.NotAllowed");
  166. }
  167. }
  168. else
  169. {
  170. txtLayout.Language = LanguageEnum.HTMLMixed;
  171. info = ResHelper.GetString("EditLayout.HintHtml");
  172. }
  173. if (String.IsNullOrEmpty(lblLayoutInfo.Text))
  174. {
  175. lblLayoutInfo.Text = info;
  176. }
  177. lblLayoutInfo.Visible = (lblLayoutInfo.Text != "");
  178. txtLayout.ReadOnly = !editingEnabled;
  179. }
  180. public void LoadData()
  181. {
  182. if (nodeId > 0)
  183. {
  184. // Get the document
  185. tree = new TreeProvider(user);
  186. node = tree.SelectSingleNode(nodeId, user.PreferredCultureCode);
  187. if (node != null)
  188. {
  189. PageTemplateInfo pti = PageTemplateInfoProvider.GetPageTemplateInfo(node.DocumentPageTemplateID);
  190. if (pti != null)
  191. {
  192. // Get shared layout
  193. LayoutInfo li = LayoutInfoProvider.GetLayoutInfo(pti.LayoutID);
  194. if (li != null)
  195. {
  196. // Load shared layout
  197. if (!RequestHelper.IsPostBack())
  198. {
  199. txtLayout.Text = li.LayoutCode;
  200. drpType.SelectedIndex = (li.LayoutType == LayoutTypeEnum.Html ? 1 : 0);
  201. }
  202. if (li.LayoutCheckedOutByUserID > 0)
  203. {
  204. lblChecked.Visible = true;
  205. lblChecked.Text = "<br />" + AddSpaces(2) + GetString("MasterPage.LayoutCheckedOut");
  206. if (!RequestHelper.IsPostBack())
  207. {
  208. txtLayout.Text = li.LayoutCode;
  209. txtLayout.ReadOnly = true;
  210. }
  211. }
  212. }
  213. else
  214. {
  215. // Load custom layout
  216. if (!RequestHelper.IsPostBack())
  217. {
  218. txtLayout.Text = ValidationHelper.GetString(pti.PageTemplateLayout, "");
  219. drpType.SelectedIndex = (pti.PageTemplateLayoutType == LayoutTypeEnum.Html ? 1 : 0);
  220. }
  221. if (pti.PageTemplateLayoutCheckedOutByUserID > 0)
  222. {
  223. lblChecked.Visible = true;
  224. lblChecked.Text = "<br />" + AddSpaces(2) + GetString("MasterPage.LayoutCheckedOut");
  225. if (!RequestHelper.IsPostBack())
  226. {
  227. txtLayout.ReadOnly = true;
  228. }
  229. }
  230. }
  231. }
  232. else
  233. {
  234. txtLayout.ReadOnly = true;
  235. }
  236. // Load node data
  237. if (!RequestHelper.IsPostBack())
  238. {
  239. txtBodyCss.Text = node.NodeBodyElementAttributes;
  240. txtDocType.Text = node.NodeDocType;
  241. txtHeadTags.Value = node.NodeHeadTags;
  242. }
  243. }
  244. }
  245. lblAfterDocType.Text = HighlightHTML("<html>") + "<br />" + AddSpaces(1) + HighlightHTML("<head>");
  246. lblAfterHeadTags.Text = AddSpaces(1) + HighlightHTML("</head>");
  247. lblAfterLayout.Text = AddSpaces(1) + HighlightHTML("</body>") + "<br />" + HighlightHTML("</html>");
  248. lblBodyEnd.Text = HighlightHTML(">");
  249. lblBodyStart.Text = AddSpaces(1) + HighlightHTML("<body " + HttpUtility.HtmlDecode(mBody));
  250. }
  251. /// <summary>
  252. /// Format HTML text.
  253. /// </summary>
  254. /// <param name="inputHTML">Input HTML</param>
  255. /// <param name="level">Indentation level</param>
  256. public string FormatHTML(string inputHTML, int level)
  257. {
  258. return AddSpaces(level) + inputHTML.Replace(HttpUtility.HtmlEncode(">"), HttpUtility.HtmlEncode(">") + "<br />" + AddSpaces(level));
  259. }
  260. /// <summary>
  261. /// Add spaces.
  262. /// </summary>
  263. /// <param name="level">Indentation level</param>
  264. public string AddSpaces(int level)
  265. {
  266. string toReturn = "";
  267. for (int i = 0; i < level * 2; i++)
  268. {
  269. toReturn += "&nbsp;";
  270. }
  271. return toReturn;
  272. }
  273. /// <summary>
  274. /// Highlight HTML.
  275. /// </summary>
  276. /// <param name="inputHtml">Input HTML</param>
  277. public string HighlightHTML(string inputHtml)
  278. {
  279. return HTMLHelper.HighlightHTML(inputHtml);
  280. }
  281. protected void btnSave_Click(object sender, EventArgs e)
  282. {
  283. if ((nodeId > 0) && (node != null))
  284. {
  285. LayoutTypeEnum layoutType = LayoutInfoProvider.GetLayoutTypeEnum(drpType.SelectedValue);
  286. // Check the permissions
  287. if ((layoutType != LayoutTypeEnum.Ascx) || user.IsAuthorizedPerResource("CMS.Design", "EditCode"))
  288. {
  289. // Update the layout
  290. if (node.DocumentPageTemplateID > 0)
  291. {
  292. PageTemplateInfo pti = PageTemplateInfoProvider.GetPageTemplateInfo(node.DocumentPageTemplateID);
  293. if (pti != null)
  294. {
  295. // Get shared layout
  296. LayoutInfo li = LayoutInfoProvider.GetLayoutInfo(pti.LayoutID);
  297. if (li != null)
  298. {
  299. // Update shared layout
  300. li.LayoutCode = txtLayout.Text;
  301. li.LayoutType = layoutType;
  302. LayoutInfoProvider.SetLayoutInfo(li);
  303. }
  304. else if (pti.PageTemplateLayoutCheckedOutByUserID <= 0)
  305. {
  306. // Update custom layout
  307. pti.PageTemplateLayout = txtLayout.Text;
  308. pti.PageTemplateLayoutType = layoutType;
  309. PageTemplateInfoProvider.SetPageTemplateInfo(pti);
  310. }
  311. }
  312. }
  313. }
  314. // Update fields
  315. node.NodeBodyElementAttributes = txtBodyCss.Text;
  316. node.NodeDocType = txtDocType.Text;
  317. node.NodeHeadTags = txtHeadTags.Value.ToString();
  318. // Update the node
  319. node.Update();
  320. // Update search index
  321. if ((node.PublishedVersionExists) && (SearchIndexInfoProvider.SearchEnabled))
  322. {
  323. SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Update, PredefinedObjectType.DOCUMENT, SearchHelper.ID_FIELD, node.GetSearchID());
  324. }
  325. // Log synchronization
  326. DocumentSynchronizationHelper.LogDocumentChange(node, TaskTypeEnum.UpdateDocument, tree);
  327. lblInfo.Visible = true;
  328. lblInfo.Text = GetString("General.ChangesSaved");
  329. // Clear cache
  330. PageInfoProvider.RemoveAllPageInfosFromCache();
  331. }
  332. }
  333. #endregion
  334. }