/// <summary>
    /// Returns initialized clone settings based on filled controls.
    /// </summary>
    private CloneSettings InitializeCloneSettings()
    {
        var settings = new CloneSettings
        {
            KeepFieldsTranslated = chkKeepFieldsTranslated.Checked,
            CloneBase            = InfoToClone,
            CodeName             = txtCodeName.Text,
            DisplayName          = txtDisplayName.Text,
            IncludeBindings      = chkBindings.Checked,
            IncludeOtherBindings = chkBindings.Checked,
            IncludeChildren      = chkChildren.Checked,
            IncludeMetafiles     = chkMetafiles.Checked,
            IncludeSiteBindings  = chkSiteBindings.Checked,
            MaxRelativeLevel     = ValidationHelper.GetInteger(drpMaxRelativeLevel.SelectedValue, -1),
            CloneToSiteID        = (plcCloneUnderSite.Visible && siteElem.Visible) ? siteElem.SiteID : InfoToClone.Generalized.ObjectSiteID
        };

        if (plcAssignToCurrentSite.Visible)
        {
            settings.AssignToSiteID = (chkAssignToCurrentSite.Checked ? SiteContext.CurrentSiteID : 0);
        }

        if (customProperties != null)
        {
            if (customProperties.IsValid(settings))
            {
                var customParameters = customProperties.CustomParameters;
                if (customParameters != null)
                {
                    settings.CustomParameters = customParameters;
                }

                settings.ExcludedChildTypes.AddRange(excludedChildren);
                settings.ExcludedBindingTypes.AddRange(excludedBindings);
                settings.ExcludedOtherBindingTypes.AddRange(excludedOtherBindings);
            }
            else
            {
                return(null);
            }
        }

        if (InfoToClone.Parent != null)
        {
            settings.ParentID = InfoToClone.Parent.Generalized.ObjectID;
        }

        return(settings);
    }
Example #2
0
    /// <summary>
    /// Clones the object to the DB according to provided settings.
    /// </summary>
    public CloneResult CloneObject()
    {
        if (InfoToClone != null)
        {
            TransferExcludedTypes();

            // Check code name
            if (plcCodeName.Visible)
            {
                bool checkCodeName = true;
                if (customProperties != null)
                {
                    checkCodeName = customProperties.ValidateCodeName;
                }

                if (checkCodeName && !ValidationHelper.IsCodeName(txtCodeName.Text))
                {
                    ShowError(GetString("general.invalidcodename"));
                    return(null);
                }
            }

            // Check permissions
            string targetSiteName = SiteContext.CurrentSiteName;
            if (plcCloneUnderSite.Visible && siteElem.Visible)
            {
                int targetSiteId = siteElem.SiteID;
                if (targetSiteId > 0)
                {
                    targetSiteName = SiteInfoProvider.GetSiteName(targetSiteId);
                }
            }

            // Check object permissions (Create & Modify)
            try
            {
                InfoToClone.CheckPermissions(PermissionsEnum.Create, targetSiteName, CurrentUser, true);
                InfoToClone.CheckPermissions(PermissionsEnum.Modify, targetSiteName, CurrentUser, true);
            }
            catch (PermissionCheckException ex)
            {
                RedirectToAccessDenied(ex.ModuleName, ex.PermissionFailed);
            }

            CloneSettings settings = new CloneSettings();
            settings.KeepFieldsTranslated = chkKeepFieldsTranslated.Checked;
            settings.CloneBase            = InfoToClone;
            settings.CodeName             = txtCodeName.Text;
            settings.DisplayName          = txtDisplayName.Text;
            settings.IncludeBindings      = chkBindings.Checked;
            settings.IncludeOtherBindings = chkBindings.Checked;
            settings.IncludeChildren      = chkChildren.Checked;
            settings.IncludeMetafiles     = chkMetafiles.Checked;
            settings.IncludeSiteBindings  = chkSiteBindings.Checked;
            if (plcAssignToCurrentSite.Visible)
            {
                settings.AssignToSiteID = (chkAssignToCurrentSite.Checked ? SiteContext.CurrentSiteID : 0);
            }
            settings.MaxRelativeLevel = ValidationHelper.GetInteger(drpMaxRelativeLevel.SelectedValue, -1);
            if (plcCloneUnderSite.Visible && siteElem.Visible)
            {
                settings.CloneToSiteID = siteElem.SiteID;
            }
            else
            {
                settings.CloneToSiteID = InfoToClone.Generalized.ObjectSiteID;
            }
            if (customProperties != null)
            {
                if (customProperties.IsValid(settings))
                {
                    Hashtable p = customProperties.CustomParameters;
                    if (p != null)
                    {
                        settings.CustomParameters = p;
                    }

                    settings.ExcludedChildTypes.AddRange(excludedChildren);
                    settings.ExcludedBindingTypes.AddRange(excludedBindings);
                    settings.ExcludedOtherBindingTypes.AddRange(excludedOtherBindings);
                }
                else
                {
                    return(null);
                }
            }
            if (InfoToClone.Parent != null)
            {
                settings.ParentID = InfoToClone.Parent.Generalized.ObjectID;
            }

            CloneResult result = new CloneResult();
            BaseInfo    clone  = null;

            if (chkUseTransaction.Checked)
            {
                using (var transaction = new CMSTransactionScope())
                {
                    clone = InfoToClone.Generalized.InsertAsClone(settings, result);
                    transaction.Commit();
                }
            }
            else
            {
                clone = InfoToClone.Generalized.InsertAsClone(settings, result);
            }

            if (customProperties != null)
            {
                string script = customProperties.CloseScript;
                if (!string.IsNullOrEmpty(script))
                {
                    mCloseScript = script.Replace("{0}", clone.Generalized.ObjectID.ToString());
                }
            }

            return(result);
        }
        return(null);
    }