示例#1
0
    /// <summary>
    /// Handles import button click.
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">Event arguments</param>
    protected void btnImport_Click(object sender, EventArgs e)
    {
        // Check 'Manage subscribers' permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "managesubscribers"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "managesubscribers");
        }

        // Get selected newsletters
        ArrayList newsletterIds = new ArrayList();
        string    values        = ValidationHelper.GetString(usNewsletters.Value, null);

        if (!String.IsNullOrEmpty(values))
        {
            string[] newItems = values.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                foreach (string item in newItems)
                {
                    newsletterIds.Add(ValidationHelper.GetInteger(item, 0));
                }
            }
        }

        // Import subscribers
        try
        {
            if (CMSContext.CurrentSite != null)
            {
                int       siteId = CMSContext.CurrentSiteID;
                ArrayList errors = new ArrayList();
                // Add subscribers to site and subscribe them to selected newsletters
                if (radSubscribe.Checked)
                {
                    errors = SubscriberProvider.ImportSubscribersToSite(txtImportSub.Text, newsletterIds, siteId, true, chkSendConfirmation.Checked, chkDoNotSubscribe.Checked, chkRequireOptIn.Checked);
                    if (errors.Count == 0)
                    {
                        lblInfo.Text    = GetString("Subscriber_Import.SubscribersImported");
                        lblInfo.Visible = true;
                    }
                }
                // Unsubscribe inserted subscribers from selected newsletters
                else if (radUnsubscribe.Checked)
                {
                    errors = SubscriberProvider.UnsubscribeFromNewsletters(txtImportSub.Text, newsletterIds, siteId, chkSendConfirmation.Checked);
                    if (errors.Count == 0)
                    {
                        lblInfo.Text    = GetString("Subscriber_Import.SubscribersUnsubscribed");
                        lblInfo.Visible = true;
                    }
                }
                // Delete inserted subscribers
                else if (radDelete.Checked)
                {
                    errors = SubscriberProvider.DeleteSubscribers(txtImportSub.Text, siteId);
                    if (errors.Count == 0)
                    {
                        lblInfo.Text    = GetString("Subscriber_Import.SubscribersDeleted");
                        lblInfo.Visible = true;
                    }
                }

                if (errors.Count > 0)
                {
                    lblError.Text    = CreateErrorString(errors);
                    lblError.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text    = ex.Message;
            lblError.Visible = true;
        }
    }