/// <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(); } }