protected void btnSaveAll_Click(object sender, EventArgs e) { try { // Save all the document transformations DataSet ds = DataClassInfoProvider.GetAllDataClass(); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { int classId = ValidationHelper.GetInteger(dr["ClassID"], 0); string className = ValidationHelper.GetString(dr["ClassName"], ""); bool isDocumentType = ValidationHelper.GetBoolean(dr["ClassIsDocumentType"], false); if (isDocumentType) { ProcessTransformations(classId, className); } } } // Save all the custom table transformations ds = DataClassInfoProvider.GetCustomTableClasses(null, null, 0, "ClassID,ClassName,ClassIsCustomTable"); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { int classId = ValidationHelper.GetInteger(dr["ClassID"], 0); string className = ValidationHelper.GetString(dr["ClassName"], ""); bool isCustomTable = ValidationHelper.GetBoolean(dr["ClassIsCustomTable"], false); if (isCustomTable) { ProcessTransformations(classId, className); } } } // Save all the layouts ds = LayoutInfoProvider.GetAllLayouts(); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { string layoutName = ValidationHelper.GetString(dr["LayoutCodeName"], ""); string layoutCode = ValidationHelper.GetString(dr["LayoutCode"], ""); int checkedOutByUserId = ValidationHelper.GetInteger(dr["LayoutCheckedOutByUserID"], 0); string checkedOutMachineName = ValidationHelper.GetString(dr["LayoutCheckedOutMachineName"], ""); if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())) { string filename = LayoutInfoProvider.GetLayoutUrl(layoutName, null); // Prepare the code string code = layoutCode; code = LayoutInfoProvider.AddLayoutDirectives(code); SiteManagerFunctions.SaveCodeFile(filename, code); } } } // Save all the page template layouts ds = PageTemplateInfoProvider.GetAllTemplates(); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { string templateName = ValidationHelper.GetString(dr["PageTemplateCodeName"], ""); string templateLayout = ValidationHelper.GetString(dr["PageTemplateLayout"], ""); bool isReusable = ValidationHelper.GetBoolean(dr["PageTemplateIsReusable"], false); int checkedOutByUserId = ValidationHelper.GetInteger(dr["PageTemplateLayoutCheckedOutByUserID"], 0); string checkedOutMachineName = ValidationHelper.GetString(dr["PageTemplateLayoutCheckedOutMachineName"], ""); bool isPortalTemplate = ValidationHelper.GetBoolean(dr["PageTemplateIsPortal"], false); string pageTemplateType = ValidationHelper.GetString(dr["PageTemplateType"], ""); bool isDashboard = pageTemplateType.Equals(PageTemplateInfoProvider.GetPageTemplateTypeCode(PageTemplateTypeEnum.Dashboard)); if ((isPortalTemplate || isDashboard) && !String.IsNullOrEmpty(templateLayout) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))) { string filename = null; if (isReusable) { filename = PageTemplateInfoProvider.GetLayoutUrl(templateName, null); } else { filename = PageTemplateInfoProvider.GetAdhocLayoutUrl(templateName, null); } // Prepare the code string code = templateLayout; code = LayoutInfoProvider.AddLayoutDirectives(code); SiteManagerFunctions.SaveCodeFile(filename, code); } } } // Save all the web part layouts ds = WebPartLayoutInfoProvider.GetWebPartLayouts(null, null); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { string webPartLayoutCodeName = ValidationHelper.GetString(dr["WebPartLayoutCodeName"], ""); string webPartLayoutCode = ValidationHelper.GetString(dr["WebPartLayoutCode"], ""); WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(dr["WebPartLayoutWebPartID"], 0)); if (wpi != null) { int checkedOutByUserId = ValidationHelper.GetInteger(dr["WebPartLayoutCheckedOutByUserID"], 0); string checkedOutMachineName = ValidationHelper.GetString(dr["WebPartLayoutCheckedOutMachineName"], ""); if (!String.IsNullOrEmpty(webPartLayoutCode) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))) { // Get layout filename string filename = WebPartLayoutInfoProvider.GetWebPartLayoutUrl(wpi.WebPartName, webPartLayoutCodeName, ""); // Get path to the code file string codeFilePath = URLHelper.GetVirtualPath(filename) + ".cs"; // Get path to the original code behind file string originalCodeFilePath = String.Empty; if (codeFileRegex.IsMatch(webPartLayoutCode, 0)) { originalCodeFilePath = codeFileRegex.Match(webPartLayoutCode).Result("$1"); } // Get original class name string originalClassName = String.Empty; if (inheritsRegex.IsMatch(webPartLayoutCode)) { originalClassName = inheritsRegex.Match(webPartLayoutCode).Result("$1"); } if (codeFileRegex.IsMatch(webPartLayoutCode)) { webPartLayoutCode = codeFileRegex.Replace(webPartLayoutCode, "CodeFile=\"" + codeFilePath + "\""); } if (inheritsRegex.IsMatch(webPartLayoutCode)) { webPartLayoutCode = inheritsRegex.Replace(webPartLayoutCode, "Inherits=\"$1_Web_Deployment\""); } // Read original codefile and change classname string codeFileCode = String.Empty; if (!String.IsNullOrEmpty(originalCodeFilePath) && FileHelper.FileExists(originalCodeFilePath)) { codeFileCode = File.ReadAllText(Server.MapPath(originalCodeFilePath)); codeFileCode = codeFileCode.Replace(originalClassName, originalClassName + "_Web_Deployment"); // Save code file SiteManagerFunctions.SaveCodeFile(filename, webPartLayoutCode); // Save code behind file SiteManagerFunctions.SaveCodeFile(codeFilePath, codeFileCode); } } } } } lblResult.Text = GetString("Deployment.ObjectsSaved"); } catch (Exception ex) { CMS.EventLog.EventLogProvider ep = new CMS.EventLog.EventLogProvider(); ep.LogEvent("System deployment", "E", ex); lblError.Visible = true; lblError.Text = ex.Message; } }
/// <summary> /// Loads data of edited layout from DB into TextBoxes. /// </summary> protected void LoadData() { if (li != null) { this.pnlCheckOutInfo.Visible = true; tbLayoutDisplayName.Text = li.LayoutDisplayName; tbLayoutDescription.Text = li.LayoutDescription; tbCodeName.Text = li.LayoutCodeName; drpType.SelectedIndex = (li.LayoutType == LayoutTypeEnum.Html ? 1 : 0); tbLayoutCode.Text = li.LayoutCode; txtLayoutCSS.Text = li.LayoutCSS; if (li.LayoutCheckedOutByUserID > 0) { this.tbLayoutCode.ReadOnly = true; this.txtLayoutCSS.ReadOnly = true; string username = null; UserInfo ui = UserInfoProvider.GetUserInfo(li.LayoutCheckedOutByUserID); if (ui != null) { username = HTMLHelper.HTMLEncode(ui.FullName); } // Checked out by current machine if (li.LayoutCheckedOutMachineName.ToLower() == HTTPHelper.MachineName.ToLower()) { this.lblCheckOutInfo.Text = String.Format(GetString("PageLayout.CheckedOut"), Server.MapPath(li.LayoutCheckedOutFilename)); } else { this.lblCheckOutInfo.Text = String.Format(GetString("PageLayout.CheckedOutOnAnotherMachine"), li.LayoutCheckedOutMachineName, username); } } else { this.lblCheckOutInfo.Text = String.Format(GetString("PageLayout.CheckOutInfo"), Server.MapPath(LayoutInfoProvider.GetLayoutUrl(li.LayoutCodeName, null))); } } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); bool isAscx = (this.drpType.SelectedValue.ToLower() == "ascx"); if (!SettingsKeyProvider.UsingVirtualPathProvider && (li != null)) { if (isAscx) { this.tbLayoutCode.Editor.ReadOnly = true; this.txtLayoutCSS.Enabled = false; } this.pnlCheckOutInfo.Visible = false; this.plcVirtualInfo.Visible = true; this.lblVirtualInfo.Text = String.Format(GetString("PageLayout.VirtualPathProviderNotRunning"), LayoutInfoProvider.GetLayoutUrl(li.LayoutCodeName, null)); } // Setup the information and code type if (isAscx) { tbLayoutCode.Editor.Language = LanguageEnum.ASPNET; tbLayoutCode.UseAutoComplete = false; ltlHint.Text = GetString("Administration-PageLayout_New.Hint"); } else { tbLayoutCode.Editor.Language = LanguageEnum.HTMLMixed; tbLayoutCode.UseAutoComplete = true; ltlHint.Text = GetString("EditLayout.HintHtml"); } this.plcDirectives.Visible = isAscx; }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); bool editingEnabled = true; LayoutInfo li = this.PagePlaceholder.LayoutInfo; PageTemplateInfo pti = this.PagePlaceholder.PageTemplateInfo; // Layout save button script ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "LayoutSave", ScriptHelper.GetScript( "function SaveDocument() { layoutChanged = true; " + this.Page.ClientScript.GetPostBackEventReference(this.btnSaveLayout, "") + "; } \n" )); string tmpLayoutCode = ""; int tmpCheckUser = 0; string tmpMachine = ""; string tmpFilename = ""; string tmpLayoutUrl = ""; string layoutUrl = ""; LayoutTypeEnum layoutType = LayoutTypeEnum.Ascx; if (li != null) { tmpLayoutCode = li.LayoutCode; tmpCheckUser = li.LayoutCheckedOutByUserID; tmpMachine = li.LayoutCheckedOutMachineName; tmpFilename = li.LayoutCheckedOutFilename; tmpLayoutUrl = LayoutInfoProvider.GetLayoutUrl(li.LayoutCodeName, null, li.LayoutType); layoutUrl = tmpLayoutUrl; layoutType = li.LayoutType; } // Get layout information if ((li == null) && (pti != null)) { tmpLayoutCode = pti.PageTemplateLayout; tmpCheckUser = pti.PageTemplateLayoutCheckedOutByUserID; tmpMachine = pti.PageTemplateLayoutCheckedOutMachineName; tmpFilename = pti.PageTemplateLayoutCheckedOutFileName; layoutUrl = PageTemplateInfoProvider.GetLayoutUrl(pti.CodeName, null, pti.PageTemplateLayoutType); layoutType = pti.PageTemplateLayoutType; if (pti.IsReusable) { tmpLayoutUrl = PageTemplateInfoProvider.GetLayoutUrl(pti.CodeName, null, pti.PageTemplateLayoutType); } else { tmpLayoutUrl = PageTemplateInfoProvider.GetAdhocLayoutUrl(pti.CodeName, null, pti.PageTemplateLayoutType); } } if (!RequestHelper.IsPostBack()) { drpType.SelectedIndex = (layoutType == LayoutTypeEnum.Html ? 1 : 0); this.txtLayout.Text = tmpLayoutCode; } this.plcUndoCheckOut.Visible = false; this.plcCheckOut.Visible = false; this.plcCheckIn.Visible = false; // Check checked-out status if (tmpCheckUser > 0) { this.txtLayout.ReadOnly = true; string username = null; UserInfo ui = UserInfoProvider.GetUserInfo(tmpCheckUser); if (ui != null) { username = HTMLHelper.HTMLEncode(ui.FullName); } // Checked out by current machine if ((HttpContext.Current != null) && (tmpMachine.ToLower() == HTTPHelper.MachineName.ToLower())) { this.plcCheckIn.Visible = true; this.lblCheckOutInfo.Text = String.Format(ResHelper.GetString("PageLayout.CheckedOut", culture), HttpContext.Current.Server.MapPath(tmpFilename)); } else { this.lblCheckOutInfo.Text = String.Format(ResHelper.GetString("PageLayout.CheckedOutOnAnotherMachine", culture), HTMLHelper.HTMLEncode(tmpMachine), HTMLHelper.HTMLEncode(username)); } //this.lblCheckOutInfo.Text = String.Format(ResHelper.GetString("PageLayout.CheckedOutOnAnotherMachine"), li.LayoutCheckedOutMachineName, username); if (user.IsGlobalAdministrator) { this.plcUndoCheckOut.Visible = true; } } else { if (HttpContext.Current != null) { this.lblCheckOutInfo.Text = String.Format(ResHelper.GetString("PageLayout.CheckOutInfo", culture), HttpContext.Current.Server.MapPath(tmpLayoutUrl)); } this.plcCheckOut.Visible = true; this.txtLayout.ReadOnly = false; } bool isAscx = (this.drpType.SelectedValue.ToLower() == "ascx"); this.btnSaveLayout.Visible = !isAscx || SettingsKeyProvider.UsingVirtualPathProvider; // Disable items when virtual path provider is disabled if (isAscx && !SettingsKeyProvider.UsingVirtualPathProvider && (pti != null)) { this.lblVirtualInfo.Text = String.Format(ResHelper.GetString("TemplateLayout.VirtualPathProviderNotRunning", culture), PageTemplateInfoProvider.GetLayoutUrl(pti.CodeName, null)); this.plcVirtualInfo.Visible = true; this.pnlCheckOutInfo.Visible = false; editingEnabled = false; } string info = null; // Setup the information and code type if (isAscx) { txtLayout.Editor.Language = LanguageEnum.ASPNET; txtLayout.UseAutoComplete = false; info = ResHelper.GetString("Administration-PageLayout_New.Hint", culture); // Check the edit code permission if (!user.IsAuthorizedPerResource("CMS.Design", "EditCode")) { editingEnabled = false; info = ResHelper.GetString("EditCode.NotAllowed", culture); } } else { txtLayout.Editor.Language = LanguageEnum.HTMLMixed; txtLayout.UseAutoComplete = true; info = ResHelper.GetString("EditLayout.HintHtml", culture); } if (!String.IsNullOrEmpty(lblLayoutInfo.Text)) { lblLayoutInfo.Text += " "; } lblLayoutInfo.Text += info; this.lblLayoutInfo.Visible = (this.lblLayoutInfo.Text != ""); this.txtLayout.ReadOnly = !editingEnabled; this.plcActions.Visible = editingEnabled; // Disable editor for layout or template checked out if (li != null) { if (li.LayoutCheckedOutByUserID > 0) { txtLayout.ReadOnly = true; } } else if (pti != null) { if (pti.PageTemplateLayoutCheckedOutByUserID > 0) { txtLayout.ReadOnly = true; } } }
/// <summary> /// Check out layout event handler. /// </summary> protected void btnCheckOut_Click(object sender, EventArgs e) { // Ensure version before check-out using (CMSActionContext context = new CMSActionContext()) { context.AllowAsyncActions = false; // Save first if (!SaveLayout()) { return; } } LayoutInfo li = this.PagePlaceholder.LayoutInfo; PageTemplateInfo pti = this.PagePlaceholder.PageTemplateInfo; if ((li != null) || (pti != null)) { try { string filename = ""; string tmpCode = ""; LayoutTypeEnum layoutType = LayoutTypeEnum.Ascx; if (li != null) { filename = LayoutInfoProvider.GetLayoutUrl(li.LayoutCodeName, null, li.LayoutType); layoutType = li.LayoutType; tmpCode = li.LayoutCode; } else { filename = PageTemplateInfoProvider.GetLayoutUrl(pti.CodeName, null, pti.PageTemplateLayoutType); layoutType = pti.PageTemplateLayoutType; tmpCode = pti.PageTemplateLayout; } // Write the code to the file string fullfilename = ""; if (HttpContext.Current != null) { fullfilename = HttpContext.Current.Server.MapPath(filename); DirectoryHelper.EnsureDiskPath(fullfilename, HttpContext.Current.Server.MapPath("~/")); } StringBuilder sb = new StringBuilder(); if (layoutType == LayoutTypeEnum.Ascx) { sb.Append(LayoutInfoProvider.GetLayoutDirectives()); } sb.Append(tmpCode); string content = HTMLHelper.EnsureLineEnding(sb.ToString(), "\r\n"); File.WriteAllText(fullfilename, content); // Set the layout data if (li != null) { // Shared layout li.LayoutCheckedOutByUserID = user.UserID; li.LayoutCheckedOutMachineName = ""; if (HttpContext.Current != null) { li.LayoutCheckedOutMachineName = HTTPHelper.MachineName; } li.LayoutCheckedOutFilename = filename; LayoutInfoProvider.SetLayoutInfo(li); } else { // Page template layout pti.PageTemplateLayoutCheckedOutByUserID = user.UserID; pti.PageTemplateLayoutCheckedOutMachineName = ""; if (HttpContext.Current != null) { pti.PageTemplateLayoutCheckedOutMachineName = HTTPHelper.MachineName; } pti.PageTemplateLayoutCheckedOutFileName = filename; PageTemplateInfoProvider.SetPageTemplateInfo(pti); } } catch { return; } } }
/// <summary> /// Saves the layout code. /// </summary> /// <returns>Returns true if save successful</returns> private bool SaveLayout() { LayoutInfo li = this.PagePlaceholder.LayoutInfo; PageTemplateInfo pti = this.PagePlaceholder.PageTemplateInfo; if ((li != null) || (pti != null)) { string mPath = ""; if ((HttpContext.Current != null) && (HttpContext.Current.Server != null)) { mPath = HttpContext.Current.Server.UrlPathEncode(URLHelper.ApplicationPath); } LayoutTypeEnum layoutType = LayoutInfoProvider.GetLayoutTypeEnum(this.drpType.SelectedValue); // Check the permissions if ((layoutType != LayoutTypeEnum.Ascx) || user.IsAuthorizedPerResource("CMS.Design", "EditCode")) { if (li != null) { // Shared layout li.LayoutType = layoutType; li.LayoutCode = HTMLHelper.UnResolveUrls(this.txtLayout.Text, mPath); LayoutInfoProvider.SetLayoutInfo(li); } else { // Custom layout pti.PageTemplateLayoutType = layoutType; pti.PageTemplateLayout = HTMLHelper.UnResolveUrls(this.txtLayout.Text, mPath); PageTemplateInfoProvider.SetPageTemplateInfo(pti); } } this.lblLayoutInfo.Text = ResHelper.GetString("General.ChangesSaved", culture); // Resave page template XML if (layoutType == LayoutTypeEnum.Ascx) { PageTemplateInfo ti = this.PagePlaceholder.PageInfo.PageTemplateInfo; if ((ti != null) && (ti.PageTemplateId > 0)) { try { // Load the layout CMSAbstractLayout layout = (CMSAbstractLayout)this.Page.LoadControl(LayoutInfoProvider.GetLayoutUrl(li.LayoutCodeName, li.LayoutVersionGUID)); layout.ID = "abstractLayout"; // Remove zones without web parts ArrayList removeZones = new ArrayList(); foreach (WebPartZoneInstance zone in ti.WebPartZones) { if (zone.WebParts.Count <= 0) { removeZones.Add(zone); } } foreach (WebPartZoneInstance zone in removeZones) { ti.WebPartZones.Remove(zone); } // Ensure all zones foreach (DictionaryEntry zone in layout.WebPartZones) { ti.EnsureZone(((CMSWebPartZone)zone.Value).ID); } this.Controls.Remove(layout); // Save the page template PageTemplateInfoProvider.SetPageTemplateInfo(ti); } catch { } } } return(true); } else { return(false); } }