/// <summary>
    /// Saves webpart properties.
    /// </summary>
    public bool Save()
    {
        // Check MVT/CP security
        if (VariantID > 0)
        {
            // Check OnlineMarketing permissions.
            if (!CheckPermissions("Manage"))
            {
                lblError.Visible = true;
                lblError.Text    = GetString("general.modifynotallowed");
                return(false);
            }
        }

        // Save the data
        if ((pi != null) && (pti != null) && (templateInstance != null) && SaveForm(form))
        {
            // Add web part if new
            if (IsNewWebPart)
            {
                AddWebPart();
            }

            WebPartInstance originalWebPartInstance = webPartInstance;
            if (IsNewVariant)
            {
                webPartInstance             = webPartInstance.Clone();
                webPartInstance.VariantMode = VariantModeFunctions.GetVariantModeEnum(QueryHelper.GetString("variantmode", String.Empty).ToLower());
            }

            // Get basicform's datarow and update webpart
            SaveFormToWebPart(form);

            bool isWebPartVariant = (VariantID > 0) || (ZoneVariantID > 0) || IsNewVariant;
            if (!isWebPartVariant)
            {
                // Save the changes
                CMSPortalManager.SaveTemplateChanges(pi, pti, templateInstance, WidgetZoneTypeEnum.None, ViewModeEnum.Design, tree);
            }
            else
            {
                // Save the variant properties
                if ((webPartInstance != null) &&
                    (webPartInstance.ParentZone != null) &&
                    (!webPartInstance.ParentZone.HasVariants) && // Save only if the parent zone does not have any variants
                    (webPartInstance.ParentZone.ParentTemplateInstance != null) &&
                    (webPartInstance.ParentZone.ParentTemplateInstance.ParentPageTemplate != null))
                {
                    string      variantName             = string.Empty;
                    string      variantDisplayName      = string.Empty;
                    string      variantDisplayCondition = string.Empty;
                    string      variantDescription      = string.Empty;
                    bool        variantEnabled          = true;
                    string      zoneId       = webPartInstance.ParentZone.ZoneID;
                    int         templateId   = webPartInstance.ParentZone.ParentTemplateInstance.ParentPageTemplate.PageTemplateId;
                    Guid        instanceGuid = Guid.Empty;
                    XmlDocument doc          = new XmlDocument();
                    XmlNode     xmlWebParts  = null;

                    if (ZoneVariantID > 0)
                    {
                        // This webpart is in a zone variant therefore save the whole variant webparts
                        xmlWebParts = webPartInstance.ParentZone.GetXmlNode(doc);
                        if (VariantMode == VariantModeEnum.MVT)
                        {
                            // MVT variant
                            ModuleCommands.OnlineMarketingSaveMVTVariantWebParts(ZoneVariantID, xmlWebParts);
                        }
                        else if (VariantMode == VariantModeEnum.ContentPersonalization)
                        {
                            // Content personalization variant
                            ModuleCommands.OnlineMarketingSaveContentPersonalizationVariantWebParts(ZoneVariantID, xmlWebParts);
                        }
                    }
                    else
                    {
                        // web part/widget variant
                        xmlWebParts  = webPartInstance.GetXmlNode(doc);
                        instanceGuid = InstanceGUID;

                        Hashtable properties = WindowHelper.GetItem("variantProperties") as Hashtable;
                        if (properties != null)
                        {
                            variantName        = ValidationHelper.GetString(properties["codename"], string.Empty);
                            variantDisplayName = ValidationHelper.GetString(properties["displayname"], string.Empty);
                            variantDescription = ValidationHelper.GetString(properties["description"], string.Empty);
                            variantEnabled     = ValidationHelper.GetBoolean(properties["enabled"], true);
                            if (VariantMode == VariantModeEnum.ContentPersonalization)
                            {
                                variantDisplayCondition = ValidationHelper.GetString(properties["condition"], string.Empty);
                            }
                        }

                        // Save the web part variant properties
                        if (VariantMode == VariantModeEnum.MVT)
                        {
                            // MVT variant
                            ModuleCommands.OnlineMarketingSaveMVTVariant(VariantID, variantName, variantDisplayName, variantDescription, variantEnabled, zoneId, webPartInstance.InstanceGUID, templateId, 0, xmlWebParts);
                        }
                        else if (VariantMode == VariantModeEnum.ContentPersonalization)
                        {
                            // Content personalization variant
                            ModuleCommands.OnlineMarketingSaveContentPersonalizationVariant(VariantID, variantName, variantDisplayName, variantDescription, variantEnabled, variantDisplayCondition, zoneId, webPartInstance.InstanceGUID, templateId, 0, xmlWebParts);
                        }

                        // The variants are cached -> Reload
                        if (originalWebPartInstance != null)
                        {
                            originalWebPartInstance.LoadVariants(true, VariantMode);
                        }
                    }
                }
            }

            // Reload the form (because of macro values set only by JS)
            form.ReloadData();

            // Clear the cached web part
            if (InstanceGUID != null)
            {
                CacheHelper.TouchKey("webpartinstance|" + InstanceGUID.ToString().ToLower());
            }

            return(true);
        }
        else if ((webPartInstance != null) && (webPartInstance.ParentZone != null))
        {
            // Reload the zone/web part variants when saving of the form fails
            webPartInstance.ParentZone.LoadVariants(true, VariantModeEnum.None);
        }

        return(false);
    }
Beispiel #2
0
    /// <summary>
    /// Handles UniGrid's OnAction event.
    /// </summary>
    /// <param name="actionName">Name of the action which should be performed</param>
    /// <param name="actionArgument">ID of the item the action should be performed with</param>
    protected void gridElem_OnAction(string actionName, object actionArgument)
    {
        if (!CheckPermissions("CMS.ContentPersonalization", PERMISSION_MODIFY))
        {
            return;
        }

        int variantId = ValidationHelper.GetInteger(actionArgument, 0);

        if (variantId > 0)
        {
            string action = actionName.ToLowerCSafe();
            switch (action)
            {
            case "delete":
            {
                // Get the instance in order to clear the cache
                ContentPersonalizationVariantInfo vi = ContentPersonalizationVariantInfoProvider.GetContentPersonalizationVariant(variantId);

                // Delete the object
                ContentPersonalizationVariantInfoProvider.DeleteContentPersonalizationVariant(variantId);
                RaiseOnAction(string.Empty, null);

                // Log widget variant synchronization
                if ((vi != null) && (vi.VariantDocumentID > 0))
                {
                    // Log synchronization
                    DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, tree);
                }
            }
            break;

            case "up":
            case "down":
            {
                // Get the instance in order to clear the cache
                ContentPersonalizationVariantInfo vi = ContentPersonalizationVariantInfoProvider.GetContentPersonalizationVariant(variantId);

                // Use try/catch due to license check
                try
                {
                    if (action == "up")
                    {
                        // Move up
                        ContentPersonalizationVariantInfoProvider.MoveVariantUp(variantId);
                    }
                    else
                    {
                        // Move down
                        ContentPersonalizationVariantInfoProvider.MoveVariantDown(variantId);
                    }

                    // Reload the variants in the current cached pageInfo
                    if ((Node != null) && (vi != null))
                    {
                        // Get the PageInfo of the current document
                        PageInfo pi = PageInfoProvider.GetPageInfo(CMSContext.CurrentSiteName, Node.NodeAliasPath, CMSContext.PreferredCultureCode, null, Node.NodeID, tree.CombineWithDefaultCulture);

                        // Reload the zone/web part variants when moving up/down
                        if ((pi != null) && (pi.UsedPageTemplateInfo != null) && (pi.UsedPageTemplateInfo.TemplateInstance != null))
                        {
                            // Reload web part, widget variants
                            if (vi.VariantInstanceGUID != Guid.Empty)
                            {
                                WebPartInstance webPartInstance = pi.UsedPageTemplateInfo.TemplateInstance.GetWebPart(vi.VariantInstanceGUID);
                                if (webPartInstance != null)
                                {
                                    webPartInstance.LoadVariants(true, VariantModeEnum.ContentPersonalization);
                                }
                            }
                            // Reload zone variants
                            else
                            {
                                WebPartZoneInstance zoneInstance = pi.UsedPageTemplateInfo.TemplateInstance.GetZone(vi.VariantZoneID);
                                if (zoneInstance != null)
                                {
                                    zoneInstance.LoadVariants(true, VariantModeEnum.ContentPersonalization);
                                }
                            }
                        }

                        RaiseOnAction(string.Empty, null);

                        // Log widget variant synchronization
                        if (vi.VariantDocumentID > 0)
                        {
                            // Log synchronization
                            DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, tree);
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblError.Visible = true;
                    lblError.Text    = ex.Message;
                }
            }
            break;
            }
        }
    }