/// <summary> /// Control ID validation. /// </summary> private void formElem_OnItemValidation(object sender, ref string errorMessage) { Control ctrl = (Control)sender; if (string.Equals("webpartcontrolid", ctrl.ID, StringComparison.OrdinalIgnoreCase)) { FormEngineUserControl ctrlTextbox = (FormEngineUserControl)ctrl; // New web part control id string newControlId = ValidationHelper.GetString(ctrlTextbox.Value, null); // Load the web part variants if not loaded yet if ((PortalContext.MVTVariantsEnabled && !templateInstance.MVTVariantsLoaded) || (PortalContext.ContentPersonalizationEnabled && !templateInstance.ContentPersonalizationVariantsLoaded)) { templateInstance.LoadVariants(false, VariantModeEnum.None); } // Check control ID validity if (!ValidationHelper.IsIdentifier(newControlId)) { errorMessage = GetString("webpartproperties.controlid.allowedcharacters"); } // New or changed web part control id bool checkIdUniqueness = IsNewWebPart || IsNewVariant || (webPartInstance == null) || (webPartInstance.ControlID != newControlId); // Try to find a web part with the same web part control id amongst all the web parts and their variants if (checkIdUniqueness && (templateInstance.GetWebPart(newControlId, true) != null)) { // Error - duplicity IDs errorMessage = GetString("WebPartProperties.ErrorUniqueID"); } else { string uniqueId = GetUniqueWebPartId(newControlId); if (!string.Equals(uniqueId, newControlId, StringComparison.InvariantCultureIgnoreCase)) { // Check if there is already a widget with the same id in the page WebPartInstance foundWidget = pi.TemplateInstance.GetWebPart(newControlId); if ((foundWidget != null) && foundWidget.IsWidget) { // Error - the ID collide with another widget which is already in the page errorMessage = ResHelper.GetString("WidgetProperties.ErrorUniqueID"); } } } } }
/// <summary> /// Loads the web part form. /// </summary> protected void LoadForm() { // Load settings if (!string.IsNullOrEmpty(Request.Form[hdnIsNewWebPart.UniqueID])) { IsNewWebPart = ValidationHelper.GetBoolean(Request.Form[hdnIsNewWebPart.UniqueID], false); } if (!string.IsNullOrEmpty(Request.Form[hdnInstanceGUID.UniqueID])) { InstanceGUID = ValidationHelper.GetGuid(Request.Form[hdnInstanceGUID.UniqueID], Guid.Empty); } // Indicates whether the new variant should be chosen when closing this dialog selectNewVariant = IsNewVariant; // Try to find the web part variant in the database and set its VariantID if (IsNewVariant) { Hashtable varProperties = WindowHelper.GetItem("variantProperties") as Hashtable; if (varProperties != null) { // Get the variant code name from the WindowHelper string variantName = ValidationHelper.GetString(varProperties["codename"], string.Empty); // Check if the variant exists in the database int variantIdFromDB = VariantHelper.GetVariantID(VariantMode, PageTemplateID, variantName, true); // Set the variant id from the database if (variantIdFromDB > 0) { VariantID = variantIdFromDB; IsNewVariant = false; } } } if (!String.IsNullOrEmpty(WebPartID)) { // Get the page info pi = CMSWebPartPropertiesPage.GetPageInfo(AliasPath, PageTemplateID, CultureCode); if (pi != null) { // Get template pti = pi.UsedPageTemplateInfo; // Get template instance templateInstance = pti.TemplateInstance; if (!IsNewWebPart) { // Standard zone webPartInstance = templateInstance.GetWebPart(InstanceGUID, WebPartID); // If the web part not found, try to find it among the MVT/CP variants if (webPartInstance == null) { // MVT/CP variant templateInstance.LoadVariants(false, VariantModeEnum.None); webPartInstance = templateInstance.GetWebPart(InstanceGUID, -1, 0); // Set the VariantMode according to the selected web part/zone variant if ((webPartInstance != null) && (webPartInstance.ParentZone != null)) { VariantMode = (webPartInstance.VariantMode != VariantModeEnum.None) ? webPartInstance.VariantMode : webPartInstance.ParentZone.VariantMode; } else { VariantMode = VariantModeEnum.None; } } else { // Ensure that the ZoneVariantID is not set when the web part was found in a regular zone ZoneVariantID = 0; } if ((VariantID > 0) && (webPartInstance != null) && (webPartInstance.PartInstanceVariants != null)) { // Check OnlineMarketing permissions. if (CheckPermissions("Read")) { webPartInstance = webPartInstance.FindVariant(VariantID); } else { // Not authorized for OnlineMarketing - Manage. RedirectToInformation(String.Format(GetString("general.permissionresource"), "Read", (VariantMode == VariantModeEnum.ContentPersonalization) ? "CMS.ContentPersonalization" : "CMS.MVTest")); } } if (webPartInstance == null) { UIContext.EditedObject = null; return; } } mainWebPartInstance = webPartInstance; // Keep xml version if (webPartInstance != null) { xmlVersion = webPartInstance.XMLVersion; // If data source ID set, edit the data source if (NestedWebPartID > 0) { webPartInstance = webPartInstance.NestedWebParts[NestedWebPartKey] ?? new WebPartInstance() { InstanceGUID = Guid.NewGuid() }; } } // Get the form info FormInfo fi = GetWebPartFormInfo(); // Get the form definition if (fi != null) { fi.ContextResolver.Settings.RelatedObject = templateInstance; form.AllowMacroEditing = ((WebPartTypeEnum)wpi.WebPartType != WebPartTypeEnum.Wireframe); // Get data row with required columns DataRow dr = fi.GetDataRow(); if (IsNewWebPart || (xmlVersion > 0)) { fi.LoadDefaultValues(dr); } // Load values from existing web part LoadDataRowFromWebPart(dr, webPartInstance, fi); // Set a unique WebPartControlID for the new variant if (IsNewVariant || IsNewWebPart) { // Set control ID string webPartControlId = ValidationHelper.GetCodeName(wpi.WebPartDisplayName); dr["WebPartControlID"] = WebPartZoneInstance.GetUniqueWebPartId(webPartControlId, templateInstance); } // Init the form InitForm(form, dr, fi); AddExportLink(); } else { UIContext.EditedObject = null; } } } }
/// <summary> /// Init event handler. /// </summary> protected override void OnInit(EventArgs e) { base.OnInit(e); // Load settings if (!string.IsNullOrEmpty(Request.Form[hdnIsNewWebPart.UniqueID])) { IsNewWebPart = ValidationHelper.GetBoolean(Request.Form[hdnIsNewWebPart.UniqueID], false); } if (!string.IsNullOrEmpty(Request.Form[hdnInstanceGUID.UniqueID])) { InstanceGUID = ValidationHelper.GetGuid(Request.Form[hdnInstanceGUID.UniqueID], Guid.Empty); } // Try to find the web part variant in the database and set its VariantID if (IsNewVariant) { Hashtable properties = WindowHelper.GetItem("variantProperties") as Hashtable; if (properties != null) { // Get the variant code name from the WindowHelper string variantName = ValidationHelper.GetString(properties["codename"], string.Empty); // Check if the variant exists in the database int variantIdFromDB = 0; if (VariantMode == VariantModeEnum.MVT) { variantIdFromDB = ModuleCommands.OnlineMarketingGetMVTVariantId(PageTemplateId, variantName); } else if (VariantMode == VariantModeEnum.ContentPersonalization) { variantIdFromDB = ModuleCommands.OnlineMarketingGetContentPersonalizationVariantId(PageTemplateId, variantName); } // Set the variant id from the database if (variantIdFromDB > 0) { VariantID = variantIdFromDB; IsNewVariant = false; } } } if (!String.IsNullOrEmpty(WebpartId)) { // Get the page info pi = CMSWebPartPropertiesPage.GetPageInfo(this.AliasPath, this.PageTemplateId); if (pi != null) { // Get template pti = pi.PageTemplateInfo; // Get template instance templateInstance = pti.TemplateInstance; // Parent webpart WebPartInfo parentWpi = null; //Before FormInfo FormInfo beforeFI = null; //After FormInfo FormInfo afterFI = null; // Webpart form info FormInfo fi = null; if (!IsNewWebPart) { // Standard zone webPartInstance = pti.GetWebPart(InstanceGUID, WebpartId); // If the web part not found, try to find it among the MVT/CP variants if (webPartInstance == null) { // MVT/CP variant templateInstance.LoadVariants(false, VariantModeEnum.None); webPartInstance = templateInstance.GetWebPart(InstanceGUID, true); // Set the VariantMode according to the selected web part/zone variant if ((webPartInstance != null) && (webPartInstance.ParentZone != null)) { VariantMode = (webPartInstance.VariantMode != VariantModeEnum.None) ? webPartInstance.VariantMode : webPartInstance.ParentZone.VariantMode; } else { VariantMode = VariantModeEnum.None; } } else { // Ensure that the ZoneVarianID is not set when the web part was found in a regural zone. ZoneVariantID = 0; } if ((VariantID > 0) && (webPartInstance != null) && (webPartInstance.PartInstanceVariants != null)) { // Check OnlineMarketing permissions. if (CheckPermissions("Read")) { webPartInstance = webPartInstance.FindVariant(VariantID); } else { // Not authorised for OnlineMarketing - Manage. RedirectToInformation(String.Format(GetString("general.permissionresource"), "Read", (VariantMode == VariantModeEnum.ContentPersonalization) ? "CMS.ContentPersonalization" : "CMS.MVTest")); } } if (webPartInstance == null) { lblInfo.Text = GetString("WebPartProperties.WebPartNotFound"); pnlFormArea.Visible = false; return; } wpi = WebPartInfoProvider.GetWebPartInfo(webPartInstance.WebPartType); form.Mode = FormModeEnum.Update; } // Webpart instance hasn't created yet else { wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(WebpartId, 0)); form.Mode = FormModeEnum.Insert; } // Load parent if (wpi != null) { if (wpi.WebPartParentID > 0) { parentWpi = WebPartInfoProvider.GetWebPartInfo(wpi.WebPartParentID); } } // Get the form definition string wpProperties = "<form></form>"; if (wpi != null) { wpProperties = wpi.WebPartProperties; // Use parent webpart if is defined if (parentWpi != null) { wpProperties = parentWpi.WebPartProperties; } // Get before FormInfo if (BeforeFormDefinition == null) { beforeFI = PortalHelper.GetPositionFormInfo((WebPartTypeEnum)wpi.WebPartType, PropertiesPosition.Before); } else { beforeFI = new FormInfo(BeforeFormDefinition); } // Get after FormInfo if (AfterFormDefinition == null) { afterFI = PortalHelper.GetPositionFormInfo((WebPartTypeEnum)wpi.WebPartType, PropertiesPosition.After); } else { afterFI = new FormInfo(AfterFormDefinition); } } // Add 'General' category at the beginning if no one is specified if (!string.IsNullOrEmpty(wpProperties) && (!wpProperties.StartsWith("<form><category", StringComparison.InvariantCultureIgnoreCase))) { wpProperties = wpProperties.Insert(6, "<category name=\"" + GetString("general.general") + "\" />"); } // Get merged web part FormInfo fi = FormHelper.GetWebPartFormInfo(wpi.WebPartName, wpProperties, beforeFI, afterFI, true); // Get datarow with required columns DataRow dr = fi.GetDataRow(); if (IsNewWebPart) { // Load default properties values fi.LoadDefaultValues(dr); // Load overriden system values fi.LoadDefaultValues(dr, wpi.WebPartDefaultValues); // Set control ID FormFieldInfo ffi = fi.GetFormField("WebPartControlID"); if (ffi != null) { ffi.DefaultValue = WebPartZoneInstance.GetUniqueWebPartId(wpi.WebPartName, templateInstance); fi.UpdateFormField("WebPartControlID", ffi); } } // Load values from existing webpart LoadDataRowFromWebPart(dr, webPartInstance); // Set a unique WebPartControlID for athe new variant if (IsNewVariant) { // Set control ID dr["WebPartControlID"] = WebPartZoneInstance.GetUniqueWebPartId(wpi.WebPartName, templateInstance); } // Init the form InitForm(form, dr, fi); AddExportLink(); } } }
/// <summary> /// Loads the web part form. /// </summary> protected void LoadForm() { // Load settings if (!string.IsNullOrEmpty(Request.Form[hdnIsNewWebPart.UniqueID])) { IsNewWebPart = ValidationHelper.GetBoolean(Request.Form[hdnIsNewWebPart.UniqueID], false); } if (!string.IsNullOrEmpty(Request.Form[hdnInstanceGUID.UniqueID])) { InstanceGUID = ValidationHelper.GetGuid(Request.Form[hdnInstanceGUID.UniqueID], Guid.Empty); } // Indicates whether the new variant should be chosen when closing this dialog selectNewVariant = IsNewVariant; // Try to find the web part variant in the database and set its VariantID if (IsNewVariant) { Hashtable varProperties = WindowHelper.GetItem("variantProperties") as Hashtable; if (varProperties != null) { // Get the variant code name from the WindowHelper string variantName = ValidationHelper.GetString(varProperties["codename"], string.Empty); // Check if the variant exists in the database int variantIdFromDB = VariantHelper.GetVariantID(VariantMode, PageTemplateID, variantName, true); // Set the variant id from the database if (variantIdFromDB > 0) { VariantID = variantIdFromDB; IsNewVariant = false; } } } if (!String.IsNullOrEmpty(WebPartID)) { // Get the page info pi = CMSWebPartPropertiesPage.GetPageInfo(AliasPath, PageTemplateID, CultureCode); if (pi == null) { ShowError(GetString("general.pagenotfound")); pnlExport.Visible = false; return; } // Get template pti = pi.UsedPageTemplateInfo; // Get template instance templateInstance = pti.TemplateInstance; if (!IsNewWebPart) { // Standard zone webPartInstance = templateInstance.GetWebPart(InstanceGUID, WebPartID); // If the web part not found, try to find it among the MVT/CP variants if (webPartInstance == null) { // MVT/CP variant templateInstance.LoadVariants(false, VariantModeEnum.None); webPartInstance = templateInstance.GetWebPart(InstanceGUID, -1, 0); // Set the VariantMode according to the selected web part/zone variant if ((webPartInstance != null) && (webPartInstance.ParentZone != null)) { VariantMode = (webPartInstance.VariantMode != VariantModeEnum.None) ? webPartInstance.VariantMode : webPartInstance.ParentZone.VariantMode; } else { VariantMode = VariantModeEnum.None; } } else { // Ensure that the ZoneVariantID is not set when the web part was found in a regular zone ZoneVariantID = 0; } if ((VariantID > 0) && (webPartInstance != null) && (webPartInstance.PartInstanceVariants != null)) { // Check OnlineMarketing permissions. if (CheckPermissions("Read")) { webPartInstance = webPartInstance.FindVariant(VariantID); } else { // Not authorized for OnlineMarketing - Manage. RedirectToInformation(String.Format(GetString("general.permissionresource"), "Read", (VariantMode == VariantModeEnum.ContentPersonalization) ? "CMS.ContentPersonalization" : "CMS.MVTest")); } } if (webPartInstance == null) { UIContext.EditedObject = null; return; } } mainWebPartInstance = webPartInstance; // Keep xml version if (webPartInstance != null) { xmlVersion = webPartInstance.XMLVersion; // If data source ID set, edit the data source if (NestedWebPartID > 0) { webPartInstance = webPartInstance.NestedWebParts[NestedWebPartKey] ?? new WebPartInstance() { InstanceGUID = Guid.NewGuid() }; } } // Get the form info FormInfo fi = GetWebPartFormInfo(); // Get the form definition if (fi != null) { fi.ContextResolver.Settings.RelatedObject = templateInstance; form.AllowMacroEditing = true; // Get data row with required columns DataRow dr = fi.GetDataRow(); if (IsNewWebPart || (xmlVersion > 0)) { fi.LoadDefaultValues(dr); } // Load values from existing web part LoadDataRowFromWebPart(dr, webPartInstance, fi); // Set a unique WebPartControlID for the new variant if (IsNewVariant || IsNewWebPart) { // Set control ID string webPartControlId = ValidationHelper.GetCodeName(wpi.WebPartDisplayName); dr["WebPartControlID"] = WebPartZoneInstance.GetUniqueWebPartId(webPartControlId, templateInstance); } // Init the form InitForm(form, dr, fi); AddExportLink(); } else { UIContext.EditedObject = null; } } }