예제 #1
0
    /// <summary>
    /// Creates new variant and raises "Add" event if specified.
    /// </summary>
    /// <param name="name">Name of new variant</param>
    /// <param name="issueId">ID of source issue on which the new variant will be based</param>
    private void RaiseOnAddEvent(string name, int issueId)
    {
        // Get main issue (original)
        int       currentIssuedId = IssueID;
        IssueInfo parentIssue     = IssueInfoProvider.GetOriginalIssue(currentIssuedId);

        // Allow modifying issues in idle state only
        if ((parentIssue == null) || (parentIssue.IssueStatus != IssueStatusEnum.Idle))
        {
            return;
        }

        // Get issue content specified by ID (if not found use original)
        IssueInfo contentIssue = null;

        if (issueId > 0)
        {
            if (issueId == parentIssue.IssueID)
            {
                contentIssue = parentIssue;
            }
            else
            {
                contentIssue = IssueInfoProvider.GetIssueInfo(issueId);
            }
        }

        NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo(parentIssue.IssueNewsletterID);

        // ID of the first child (if new A/B test is being created (i.e. parent and 2 children)
        int origVariantId = 0;

        // Check if current issue is variant issue
        if (!parentIssue.IssueIsABTest)
        {
            // Variant issue has not been created yet => create original and 2 child variants
            parentIssue.IssueIsABTest = true;

            // Create 1st variant based on parent issue, the 2nd variant will be created as ordinary variant below
            IssueInfo issueOrigVariant = parentIssue.Clone(true);
            issueOrigVariant.IssueVariantOfIssueID = parentIssue.IssueID;
            issueOrigVariant.IssueVariantName      = GetString("newsletter.abvariantoriginal");
            issueOrigVariant.IssueScheduledTaskID  = 0;
            IssueInfoProvider.SetIssueInfo(issueOrigVariant);
            // Create scheduled task for variant mail-out and update issue variant
            issueOrigVariant.IssueScheduledTaskID = CreateScheduledTask(issueOrigVariant);
            IssueInfoProvider.SetIssueInfo(issueOrigVariant);
            // Update parent issue
            IssueInfoProvider.SetIssueInfo(parentIssue);
            try
            {
                ObjectVersionManager.DestroyObjectHistory(parentIssue.ObjectType, parentIssue.IssueID);
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException("Newsletter-AddVariant", "EXCEPTION", ex);
            }

            origVariantId = issueOrigVariant.IssueID;
        }

        // Variant issue has been created => create new variant only
        IssueInfo issueVariant = (contentIssue != null ? contentIssue.Clone(true) : parentIssue.Clone(true));

        issueVariant.IssueVariantName      = name;
        issueVariant.IssueVariantOfIssueID = parentIssue.IssueID;

        // Prepare content with empty regions if empty content will be used
        string[] regions = null;
        if ((contentIssue == null) && (newsletter != null))
        {
            EmailTemplateInfo template = EmailTemplateInfoProvider.GetEmailTemplateInfo(newsletter.NewsletterTemplateID);
            if (template != null)
            {
                bool          isValidRegionName;
                List <string> regionNames = new List <string>();
                EmailTemplateHelper.ValidateEditableRegions(template.TemplateBody, out isValidRegionName, out isValidRegionName, regionNames);
                for (int i = regionNames.Count - 1; i >= 0; i--)
                {
                    regionNames[i] = regionNames[i] + "::";
                }
                regions = regionNames.ToArray();
                // Set template ID (i.e. this template with these regions is used for current issue)
                issueVariant.IssueTemplateID = template.TemplateID;
            }
        }

        issueVariant.IssueText            = (contentIssue != null ? contentIssue.IssueText : IssueHelper.GetContentXML(regions));
        issueVariant.IssueScheduledTaskID = 0;
        IssueInfoProvider.SetIssueInfo(issueVariant);

        // Duplicate attachments and replace old guids with new guids in issue text if current variant issue is based on content of another
        if (contentIssue != null)
        {
            List <Guid> guids = new List <Guid>();
            MetaFileInfoProvider.CopyMetaFiles(contentIssue.IssueID, issueVariant.IssueID,
                                               (contentIssue.IssueIsVariant ? IssueInfo.OBJECT_TYPE_VARIANT : IssueInfo.OBJECT_TYPE),
                                               ObjectAttachmentsCategories.ISSUE, IssueInfo.OBJECT_TYPE_VARIANT, ObjectAttachmentsCategories.ISSUE, guids);
            if (guids.Count > 0)
            {
                for (int i = 0; i < guids.Count; i += 2)
                {
                    issueVariant.IssueText = LinkConverter.ReplaceInLink(issueVariant.IssueText, guids[i].ToString(), guids[i + 1].ToString());
                }
            }
        }

        // Create scheduled task for variant mail-out
        issueVariant.IssueScheduledTaskID = CreateScheduledTask(issueVariant);
        // Update issue variant
        IssueInfoProvider.SetIssueInfo(issueVariant);

        if (origVariantId > 0)
        {
            // New A/B test issue created => create new A/B test info
            ABTestInfo abi = new ABTestInfo
            {
                TestIssueID = parentIssue.IssueID, TestSizePercentage = 50, TestWinnerOption = ABTestWinnerSelectionEnum.OpenRate, TestSelectWinnerAfter = 60
            };
            ABTestInfoProvider.SetABTestInfo(abi);

            // Move attachments (meta files) from parent issue to first variant
            MetaFileInfoProvider.MoveMetaFiles(parentIssue.IssueID, origVariantId, IssueInfo.OBJECT_TYPE, ObjectAttachmentsCategories.ISSUE, IssueInfo.OBJECT_TYPE_VARIANT, ObjectAttachmentsCategories.ISSUE);
            MetaFileInfoProvider.MoveMetaFiles(parentIssue.IssueID, origVariantId, IssueInfo.OBJECT_TYPE_VARIANT, ObjectAttachmentsCategories.ISSUE, IssueInfo.OBJECT_TYPE_VARIANT, ObjectAttachmentsCategories.ISSUE);
        }

        if (OnAddVariant != null)
        {
            VariantEventArgs args = new VariantEventArgs(name, issueVariant.IssueID);
            OnAddVariant(this, args);
        }
    }
    /// <summary>
    /// Actions handler.
    /// </summary>
    protected void HeaderActions_ActionPerformed(object sender, CommandEventArgs e)
    {
        switch (e.CommandName.ToLowerCSafe())
        {
        case "save":
            // Check 'Manage templates' permission
            if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.newsletter", "managetemplates"))
            {
                RedirectToAccessDenied("cms.newsletter", "managetemplates");
            }

            string errorMessage;
            // Check template code name
            if (!ValidationHelper.IsCodeName(txtTemplateName.Text))
            {
                errorMessage = GetString("General.ErrorCodeNameInIdentifierFormat");
            }
            else
            {
                // Check code and display name for emptiness
                errorMessage = new Validator().NotEmpty(txtTemplateDisplayName.Text, GetString("general.requiresdisplayname")).NotEmpty(txtTemplateName.Text, GetString("NewsletterTemplate_Edit.ErrorEmptyName")).Result;
            }

            if (string.IsNullOrEmpty(errorMessage))
            {
                // TemplateName must to be unique
                EmailTemplateInfo emailTemplateObj = EmailTemplateInfoProvider.GetEmailTemplateInfo(txtTemplateName.Text.Trim(), SiteContext.CurrentSiteID);

                // If templateName value is unique
                if ((emailTemplateObj == null) || (emailTemplateObj.TemplateID == templateid))
                {
                    // If templateName value is unique -> determine whether it is update or insert
                    if ((emailTemplateObj == null))
                    {
                        // Get EmailTemplate object by primary key
                        emailTemplateObj = EmailTemplateInfoProvider.GetEmailTemplateInfo(templateid) ?? new EmailTemplateInfo();
                    }

                    // Check region names validity
                    bool   isValidRegionName;
                    bool   isValid;
                    string templateBody = htmlTemplateBody.ResolvedValue.Trim();

                    EmailTemplateHelper.ValidateEditableRegions(templateBody, out isValid, out isValidRegionName, null);
                    if (isValid)
                    {
                        if (isValidRegionName)
                        {
                            // Set template object
                            emailTemplateObj.TemplateBody           = templateBody;
                            emailTemplateObj.TemplateName           = txtTemplateName.Text.Trim();
                            emailTemplateObj.TemplateHeader         = ValidationHelper.GetString(txtTemplateHeader.Value, "").Trim();
                            emailTemplateObj.TemplateFooter         = ValidationHelper.GetString(txtTemplateFooter.Value, "").Trim();
                            emailTemplateObj.TemplateDisplayName    = txtTemplateDisplayName.Text.Trim();
                            emailTemplateObj.TemplateStylesheetText = txtTemplateStyleSheetText.Text.Trim();

                            // Set temaplte subject only for 'subscription' and 'unsubscription' template types
                            if (plcSubject.Visible)
                            {
                                emailTemplateObj.TemplateSubject = txtTemplateSubject.Text.Trim();
                            }

                            // Save the template object and display info message
                            EmailTemplateInfoProvider.SetEmailTemplateInfo(emailTemplateObj);
                            ShowChangesSaved();

                            // Reload header if changes were saved
                            ScriptHelper.RefreshTabHeader(Page, emailTemplateObj.TemplateDisplayName);
                        }
                        else
                        {
                            ShowError(GetString("NewsletterTemplate_Edit.EditableRegionNameError"));
                        }
                    }
                    else
                    {
                        ShowError(GetString("NewsletterTemplate_Edit.EditableRegionError"));
                    }
                }
                else
                {
                    ShowError(GetString("NewsletterTemplate_Edit.TemplateNameExists"));
                }
            }
            else
            {
                ShowError(errorMessage);
            }
            break;
        }
    }