Example #1
0
    /// <summary>
    /// Create schedule task.
    /// </summary>
    private void CreateTask()
    {
        try
        {
            var editedObject = TypedEditedObject;

            TaskInfo task = new TaskInfo();
            task.TaskAssemblyName = "CMS.Newsletters";
            task.TaskClass        = "CMS.Newsletters.DynamicNewsletterSender";
            task.TaskDisplayName  = GetString("DynamicNewsletter.TaskName") + editedObject.NewsletterDisplayName;
            task.TaskEnabled      = true;
            task.TaskInterval     = ScheduleInterval.ScheduleInterval;
            task.TaskLastResult   = string.Empty;
            task.TaskName         = "DynamicNewsletter." + ValidationHelper.GetCodeName(editedObject.NewsletterName, "_");
            task.TaskSiteID       = SiteContext.CurrentSiteID;
            task.TaskNextRunTime  = SchedulingHelper.GetNextTime(task.TaskInterval, new DateTime(), new DateTime());
            task.TaskData         = editedObject.NewsletterGUID.ToString();
            // Set task for processing in external service
            task.TaskAllowExternalService = true;
            task.TaskUseExternalService   = (SchedulingHelper.UseExternalService && NewsletterHelper.UseExternalServiceForDynamicNewsletters(SiteContext.CurrentSiteName));
            task.TaskType = ScheduledTaskTypeEnum.System;

            TaskInfoProvider.SetTaskInfo(task);

            editedObject.NewsletterDynamicScheduledTaskID = task.TaskID;
            NewsletterInfoProvider.SetNewsletterInfo(editedObject);

            Redirect();
        }
        catch (Exception ex)
        {
            ShowError(GetString(ex.Message));
        }
    }
Example #2
0
    /// <summary>
    /// Sets data to database.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Check 'configure' permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "configure"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "configure");
        }

        // Validate base inputs (newsletter display name, code name, sender name, sender e-mail)
        string errorMessage = new Validator().NotEmpty(txtNewsletterDisplayName.Text.Trim(), GetString("general.requiresdisplayname")).NotEmpty(txtNewsletterName.Text.Trim(), GetString("Newsletter_Edit.ErrorEmptyName")).NotEmpty(txtNewsletterSenderName.Text.Trim(), GetString("Newsletter_Edit.ErrorEmptySenderName")).NotEmpty(txtNewsletterSenderEmail.Text.Trim(), GetString("Newsletter_Edit.ErrorEmptySenderEmail")).IsEmail(txtNewsletterSenderEmail.Text.Trim(), GetString("Newsletter_Edit.ErrorEmailFormat")).IsCodeName(txtNewsletterName.Text.Trim(), GetString("general.invalidcodename")).Result;

        if (String.IsNullOrEmpty(errorMessage))
        {
            if (String.IsNullOrEmpty((string)subscriptionTemplate.Value))
            {
                // No subscription template selected
                lblSubscriptionError.Visible = true;
                return;
            }

            if (String.IsNullOrEmpty((string)unsubscriptionTemplate.Value))
            {
                // No unsubscription template selected
                lblUnsubscriptionError.Visible = true;
                return;
            }

            if ((radTemplateBased.Checked) && (String.IsNullOrEmpty((string)issueTemplate.Value)))
            {
                // No issue template selected for template based newsletter
                lblIssueError.Visible = true;
                return;
            }

            if ((radDynamic.Checked) && (String.IsNullOrEmpty(txtNewsletterDynamicURL.Text.Trim())))
            {
                // Dynamic URL cannot be empty
                ShowError(GetString("menuitem_edit.errorpageurlempty"));
                return;
            }

            if ((radDynamic.Checked) && (chkSchedule.Checked) && (!ScheduleInterval.CheckOneDayMinimum()))
            {
                // Problem occurred while setting schedule interval for dynamic newsletter
                ShowError(GetString("Newsletter_Edit.NoDaySelected"));
                return;
            }

            if ((radDynamic.Checked) && (chkSchedule.Checked) && (SchedulingHelper.DecodeInterval(ScheduleInterval.ScheduleInterval).StartTime == DateTime.MinValue))
            {
                ShowError(GetString("Newsletter.IncorrectDate"));
                return;
            }

            // Newsletter code name must to be unique per site...
            NewsletterInfo newsletterObj = NewsletterInfoProvider.GetNewsletterInfo(txtNewsletterName.Text.Trim(), CMSContext.CurrentSiteID);

            // If newsletter code name is unique...
            if (newsletterObj == null)
            {
                // Create new item -> insert
                newsletterObj = new NewsletterInfo();

                newsletterObj.NewsletterDisplayName = txtNewsletterDisplayName.Text.Trim();
                newsletterObj.NewsletterName        = txtNewsletterName.Text.Trim();

                int selectedValue = ValidationHelper.GetInteger(subscriptionTemplate.Value, 0);
                if (selectedValue != 0)
                {
                    newsletterObj.NewsletterSubscriptionTemplateID = selectedValue;
                }
                else
                {
                    ShowError(GetString("Newsletter_Edit.NoSubscriptionTemplateSelected"));
                    return;
                }

                selectedValue = ValidationHelper.GetInteger(unsubscriptionTemplate.Value, 0);
                if (selectedValue != 0)
                {
                    newsletterObj.NewsletterUnsubscriptionTemplateID = selectedValue;
                }
                else
                {
                    ShowError(GetString("Newsletter_Edit.NoUnsubscriptionTemplateSelected"));
                    return;
                }
                newsletterObj.NewsletterSenderName  = txtNewsletterSenderName.Text.Trim();
                newsletterObj.NewsletterSenderEmail = txtNewsletterSenderEmail.Text.Trim();
                if (radDynamic.Checked)
                {
                    newsletterObj.NewsletterDynamicURL = txtNewsletterDynamicURL.Text.Trim();
                    newsletterObj.NewsletterType       = NewsletterType.Dynamic;
                }
                else
                {
                    selectedValue = ValidationHelper.GetInteger(issueTemplate.Value, 0);
                    if (selectedValue != 0)
                    {
                        newsletterObj.NewsletterTemplateID = selectedValue;
                    }
                    else
                    {
                        ShowError(GetString("Newsletter_Edit.NoEmailTemplateSelected"));
                        return;
                    }
                    newsletterObj.NewsletterType = NewsletterType.TemplateBased;
                }
                newsletterObj.NewsletterSiteID          = CMSContext.CurrentSiteID;
                newsletterObj.NewsletterGUID            = Guid.NewGuid();
                newsletterObj.NewsletterLogActivity     = true;
                newsletterObj.NewsletterEnableResending = true;

                try
                {
                    // Create new newsletter
                    NewsletterInfoProvider.SetNewsletterInfo(newsletterObj);

                    if (radDynamic.Checked)
                    {
                        if (chkSchedule.Checked)
                        {
                            // Create new task to schedule the dynamic newsletter
                            TaskInfo task = new TaskInfo();
                            task.TaskAssemblyName = "CMS.Newsletter";
                            task.TaskClass        = "CMS.Newsletter.DynamicNewsletterSender";
                            task.TaskDisplayName  = GetString("DynamicNewsletter.TaskName") + newsletterObj.NewsletterDisplayName;
                            task.TaskEnabled      = true;
                            task.TaskInterval     = ScheduleInterval.ScheduleInterval;
                            task.TaskLastResult   = string.Empty;
                            task.TaskName         = "DynamicNewsletter." + ValidationHelper.GetCodeName(newsletterObj.NewsletterName, "_");
                            task.TaskSiteID       = CMSContext.CurrentSiteID;
                            task.TaskNextRunTime  = SchedulingHelper.GetNextTime(task.TaskInterval, new DateTime(), new DateTime());
                            task.TaskData         = newsletterObj.NewsletterGUID.ToString();
                            // Set task for processing in external service
                            task.TaskAllowExternalService = true;
                            task.TaskUseExternalService   = (SchedulingHelper.UseExternalService && NewsletterHelper.UseExternalServiceForDynamicNewsletters(CMSContext.CurrentSiteName));
                            task.TaskType = ScheduledTaskTypeEnum.System;

                            TaskInfoProvider.SetTaskInfo(task);

                            newsletterObj.NewsletterDynamicScheduledTaskID = task.TaskID;
                            NewsletterInfoProvider.SetNewsletterInfo(newsletterObj);
                        }
                    }
                    URLHelper.Redirect("Newsletter_Frameset.aspx?newsletterid=" + ValidationHelper.GetString(newsletterObj.NewsletterID, null) + "&saved=1");
                }
                catch (Exception ex)
                {
                    ShowError(GetString(ex.Message));
                }
            }
            else
            {
                ShowError(GetString("Newsletter_Edit.NewsletterNameExists"));
            }
        }
        else
        {
            ShowError(errorMessage);
        }
    }
Example #3
0
    /// <summary>
    /// Saves configuration changes.
    /// </summary>
    protected void SaveData()
    {
        // Check "configure" permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "configure"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "configure");
        }

        string scheduledInterval = null;

        if (isDynamic && chkSchedule.Checked)
        {
            // Get scheduled interval for dynamic newsletter
            scheduledInterval = schedulerInterval.ScheduleInterval;
        }

        string errorMessage = ValidateNewsletterValues();

        if (!string.IsNullOrEmpty(errorMessage))
        {
            ShowError(errorMessage);
            return;
        }

        NewsletterInfo newsletterObj = NewsletterInfoProvider.GetNewsletterInfo(txtNewsletterName.Text.Trim(), CMSContext.CurrentSiteID);

        // Newsletter's code name must be unique
        if (newsletterObj != null && newsletterObj.NewsletterID != newsletterId)
        {
            ShowError(GetString("Newsletter_Edit.NewsletterNameExists"));
            return;
        }

        if (newsletterObj == null)
        {
            newsletterObj = NewsletterInfoProvider.GetNewsletterInfo(newsletterId);
        }

        SetNewsletterValues(newsletterObj);

        // Check if subscription template was selected
        int subscriptionTemplateValue = ValidationHelper.GetInteger(subscriptionTemplate.Value, 0);

        if (subscriptionTemplateValue == 0)
        {
            ShowError(GetString("Newsletter_Edit.NoSubscriptionTemplateSelected"));
            return;
        }
        newsletterObj.NewsletterSubscriptionTemplateID = subscriptionTemplateValue;

        // Check if double opt-in template was selected
        int optInTemplateValue = ValidationHelper.GetInteger(optInSelector.Value, 0);

        if (newsletterObj.NewsletterEnableOptIn && optInTemplateValue == 0)
        {
            ShowError(GetString("Newsletter_Edit.NoOptInTemplateSelected"));
            return;
        }
        newsletterObj.NewsletterOptInTemplateID = optInTemplateValue;

        // Check if unsubscription template was selected
        int unsubscriptionTemplateValue = ValidationHelper.GetInteger(unsubscriptionTemplate.Value, 0);

        if (unsubscriptionTemplateValue == 0)
        {
            ShowError(GetString("Newsletter_Edit.NoUnsubscriptionTemplateSelected"));
            return;
        }
        newsletterObj.NewsletterUnsubscriptionTemplateID = unsubscriptionTemplateValue;

        // ID of scheduled task which should be deleted
        int deleteScheduledTaskId = 0;

        if (isDynamic)
        {
            newsletterObj.NewsletterType           = NewsletterType.Dynamic;
            newsletterObj.NewsletterDynamicSubject = radFollowing.Checked ? txtSubject.Text : string.Empty;

            if ((String.IsNullOrEmpty(txtNewsletterDynamicURL.Text.Trim())))
            {
                // Dynamic URL cannot be empty
                ShowError(GetString("menuitem_edit.errorpageurlempty"));
                return;
            }

            newsletterObj.NewsletterDynamicURL = txtNewsletterDynamicURL.Text.Trim();

            if (chkSchedule.Checked)
            {
                // Set info for scheduled task
                TaskInfo task = GetDynamicNewsletterTask(newsletterObj);

                if (!schedulerInterval.CheckOneDayMinimum())
                {
                    // If problem occurred while setting schedule interval
                    ShowError(GetString("Newsletter_Edit.NoDaySelected"));
                    return;
                }

                if (!IsValidDate(SchedulingHelper.DecodeInterval(scheduledInterval).StartTime))
                {
                    ShowError(GetString("Newsletter.IncorrectDate"));
                    return;
                }

                task.TaskInterval = scheduledInterval;

                task.TaskNextRunTime = SchedulingHelper.GetNextTime(task.TaskInterval, new DateTime(), new DateTime());
                task.TaskDisplayName = GetString("DynamicNewsletter.TaskName") + newsletterObj.NewsletterDisplayName;
                task.TaskName        = "DynamicNewsletter_" + newsletterObj.NewsletterName;
                // Set task for processing in external service
                task.TaskAllowExternalService = true;
                task.TaskUseExternalService   = (SchedulingHelper.UseExternalService && NewsletterHelper.UseExternalServiceForDynamicNewsletters(CMSContext.CurrentSiteName));
                TaskInfoProvider.SetTaskInfo(task);
                newsletterObj.NewsletterDynamicScheduledTaskID = task.TaskID;
            }
            else
            {
                if (newsletterObj.NewsletterDynamicScheduledTaskID > 0)
                {
                    // Store task ID for deletion
                    deleteScheduledTaskId = newsletterObj.NewsletterDynamicScheduledTaskID;
                }
                newsletterObj.NewsletterDynamicScheduledTaskID = 0;
                schedulerInterval.Visible = false;
            }
        }
        else
        {
            newsletterObj.NewsletterType = NewsletterType.TemplateBased;

            // Check if issue template was selected
            int issueTemplateValue = ValidationHelper.GetInteger(issueTemplate.Value, 0);
            if (issueTemplateValue == 0)
            {
                ShowError(GetString("Newsletter_Edit.NoEmailTemplateSelected"));
                return;
            }
            newsletterObj.NewsletterTemplateID = issueTemplateValue;
        }

        // Save changes to DB
        NewsletterInfoProvider.SetNewsletterInfo(newsletterObj);
        if (deleteScheduledTaskId > 0)
        {
            // Delete scheduled task if schedule mail-outs were unchecked
            TaskInfoProvider.DeleteTaskInfo(deleteScheduledTaskId);
        }

        ShowChangesSaved();

        // Refresh header with display name
        ScriptHelper.RefreshTabHeader(Page, GetString("Newsletter_Header.Configuration"));
    }