/// <summary> /// Gets the default layout code for the web part /// </summary> protected string GetDefaultCode() { string fileFullPath = WebPartInfoProvider.GetFullPhysicalPath(webPartInfo); if (File.Exists(fileFullPath)) { return(File.ReadAllText(fileFullPath)); } return(null); }
/// <summary> /// Creates new web part. /// </summary> protected void btnOK_Click(object sender, EventArgs e) { // Validate the text box fields string errorMessage = new Validator().IsCodeName(txtWebPartName.Text, GetString("general.invalidcodename")).Result; // Validate and trim file name textbox only if it's visible if (radNewWebPart.Checked && radNewFile.Checked) { if (String.IsNullOrEmpty(errorMessage)) { errorMessage = new Validator().IsFileName(Path.GetFileName(txtCodeFileName.Text), GetString("WebPart_Clone.InvalidFileName")).Result; } } // Check file name if (radExistingFile.Checked && radNewWebPart.Checked) { if (String.IsNullOrEmpty(errorMessage)) { string webpartPath = WebPartInfoProvider.GetWebPartPhysicalPath(FileSystemSelector.Value.ToString()); errorMessage = new Validator().IsFileName(Path.GetFileName(webpartPath), GetString("WebPart_Clone.InvalidFileName")).Result; } } if (!String.IsNullOrEmpty(errorMessage)) { ShowError(HTMLHelper.HTMLEncode(errorMessage)); return; } // Run in transaction using (var tr = new CMSTransactionScope()) { WebPartInfo wi = new WebPartInfo(); // Check if new name is unique WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo(txtWebPartName.Text); if (webpart != null) { ShowError(GetString("Development.WebParts.WebPartNameAlreadyExist").Replace("%%name%%", txtWebPartName.Text)); return; } string filename = String.Empty; if (radNewWebPart.Checked) { if (radExistingFile.Checked) { filename = FileSystemSelector.Value.ToString().Trim(); if (filename.ToLowerCSafe().StartsWithCSafe("~/cmswebparts/")) { filename = filename.Substring("~/cmswebparts/".Length); } } else { filename = txtCodeFileName.Text.Trim(); if (!filename.EndsWithCSafe(".ascx")) { filename += ".ascx"; } } } wi.WebPartDisplayName = txtWebPartDisplayName.Text.Trim(); wi.WebPartFileName = filename; wi.WebPartName = txtWebPartName.Text.Trim(); wi.WebPartCategoryID = QueryHelper.GetInteger("parentobjectid", 0); wi.WebPartDescription = ""; wi.WebPartDefaultValues = "<form></form>"; // Initialize WebPartType - fill it with the default value wi.WebPartType = wi.WebPartType; // Inherited web part if (radInherited.Checked) { // Check if is selected webpart and isn't category item if (ValidationHelper.GetInteger(webpartSelector.Value, 0) <= 0) { ShowError(GetString("WebPartNew.InheritedCategory")); return; } int parentId = ValidationHelper.GetInteger(webpartSelector.Value, 0); var parent = WebPartInfoProvider.GetWebPartInfo(parentId); if (parent != null) { wi.WebPartType = parent.WebPartType; wi.WebPartResourceID = parent.WebPartResourceID; wi.WebPartSkipInsertProperties = parent.WebPartSkipInsertProperties; } wi.WebPartParentID = parentId; // Create empty default values definition wi.WebPartProperties = "<defaultvalues></defaultvalues>"; } else { // Check if filename was added if (FileSystemSelector.Visible && !FileSystemSelector.IsValid()) { ShowError(FileSystemSelector.ValidationError); return; } wi.WebPartProperties = "<form></form>"; wi.WebPartParentID = 0; } // Save the web part WebPartInfoProvider.SetWebPartInfo(wi); if (radNewFile.Checked && radNewWebPart.Checked) { string physicalFile = WebPartInfoProvider.GetFullPhysicalPath(wi); if (!File.Exists(physicalFile)) { string ascx; string code; string designer; // Write the files try { WebPartInfoProvider.GenerateWebPartCode(wi, null, out ascx, out code, out designer); string folder = Path.GetDirectoryName(physicalFile); // Ensure the folder if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } File.WriteAllText(physicalFile, ascx); File.WriteAllText(physicalFile + ".cs", code); // Designer file if (!String.IsNullOrEmpty(designer)) { File.WriteAllText(physicalFile + ".designer.cs", designer); } } catch (Exception ex) { LogAndShowError("WebParts", "GENERATEFILES", ex, true); return; } } else { ShowError(String.Format(GetString("General.FileExistsPath"), physicalFile)); return; } } // Refresh web part tree ScriptHelper.RegisterStartupScript(this, typeof(string), "reloadframee", ScriptHelper.GetScript( "parent.location = '" + UIContextHelper.GetElementUrl("cms.design", "Development.Webparts", false, wi.WebPartID) + "';")); PageBreadcrumbs.Items[1].Text = HTMLHelper.HTMLEncode(wi.WebPartDisplayName); ShowChangesSaved(); plcTable.Visible = false; tr.Commit(); } }
/// <summary> /// Handles form's after data load event. /// </summary> protected void EditForm_OnAfterDataLoad(object sender, EventArgs e) { etaCode.Language = LanguageEnum.HTML; cssLayoutEditor.Editor.Language = LanguageEnum.CSS; cssLayoutEditor.Editor.ShowBookmarks = true; // Do not check changes DocumentManager.RegisterSaveChangesScript = false; EditForm.OnBeforeSave += EditForm_OnBeforeSave; etaCode.Language = LanguageEnum.HTML; wpli = UIContext.EditedObject as WebPartLayoutInfo; layoutID = QueryHelper.GetInteger("layoutid", 0); isSiteManager = ((MembershipContext.AuthenticatedUser.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Admin) && layoutID != 0) || QueryHelper.GetBoolean("sitemanager", false)); isNew = (LayoutCodeName == "|new|"); isDefault = (LayoutCodeName == "|default|") || (!isSiteManager && string.IsNullOrEmpty(LayoutCodeName)); if ((wpli == null) || (wpli.WebPartLayoutID <= 0)) { isNew |= isSiteManager; editMenuElem.ObjectManager.ObjectType = WebPartLayoutInfo.OBJECT_TYPE; } ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), "PreviewHierarchyPerformAction", ScriptHelper.GetScript("function actionPerformed(action) { if (action == 'saveandclose') { document.getElementById('" + hdnClose.ClientID + "').value = '1'; } " + editMenuElem.ObjectManager.GetJSFunction(ComponentEvents.SAVE, null, null) + "; }")); currentUser = MembershipContext.AuthenticatedUser; // Get web part instance (if edited in administration) if ((webpartId != "") && !isSiteManager) { // Get page info pi = CMSWebPartPropertiesPage.GetPageInfo(aliasPath, templateId, culture); if (pi == null) { ShowInformation(GetString("WebPartProperties.WebPartNotFound"), persistent: false); } else { // Get page template pti = pi.UsedPageTemplateInfo; if ((pti != null) && ((pti.TemplateInstance != null))) { webPart = pti.TemplateInstance.GetWebPart(instanceGuid, zoneVariantId, variantId) ?? pti.TemplateInstance.GetWebPart(webpartId); } } } // If the web part is not found, try web part ID if (webPart == null) { wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(webpartId, 0)); if (wpi == null) { ShowError(GetString("WebPartProperties.WebPartNotFound")); return; } } else { // CMS desk wpi = WebPartInfoProvider.GetWebPartInfo(webPart.WebPartType); if (string.IsNullOrEmpty(LayoutCodeName)) { // Get the current layout name LayoutCodeName = ValidationHelper.GetString(webPart.GetValue("WebPartLayout"), ""); } } if (wpi != null) { // Load the web part information webPartInfo = wpi; bool loaded = false; if (!RequestHelper.IsPostBack()) { if (wpli != null) { editMenuElem.MenuPanel.Visible = true; // Read-only code text area etaCode.Editor.ReadOnly = false; loaded = true; } if (!loaded) { string fileName = WebPartInfoProvider.GetFullPhysicalPath(webPartInfo); // Check if filename exist if (!FileHelper.FileExists(fileName)) { ShowError(GetString("WebPartProperties.FileNotExist")); pnlContent.Visible = false; editMenuElem.ObjectEditMenu.Visible = false; } else { // Load default web part layout code etaCode.Text = File.ReadAllText(Server.MapPath(fileName)); // Load default web part CSS cssLayoutEditor.Text = wpi.WebPartCSS; } } } } if (((wpli == null) || (wpli.WebPartLayoutID <= 0)) && isSiteManager) { editMenuElem.Title.Breadcrumbs.AddBreadcrumb(new BreadcrumbItem { Text = GetString("WebParts.Layout"), RedirectUrl = String.Format("{0}&parentobjectid={1}&displaytitle={2}", UIContextHelper.GetElementUrl("CMS.Design", "WebPart.Layout"), QueryHelper.GetInteger("webpartid", 0), false) }); editMenuElem.Title.Breadcrumbs.AddBreadcrumb(new BreadcrumbItem { Text = GetString("webparts_layout_newlayout"), }); } ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "ApplyButton", ScriptHelper.GetScript( "function SetRefresh(refreshpage) { document.getElementById('" + hidRefresh.ClientID + @"').value = refreshpage; } function OnApplyButton(refreshpage) { SetRefresh(refreshpage); actionPerformed('save');refreshPreview(); } function OnOKButton(refreshpage) { SetRefresh(refreshpage); actionPerformed('saveandclose'); } ")); InitLayoutForm(); }