示例#1
0
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // Check email format
        string email  = txtEmail.Text.Trim();
        string result = new Validator().IsEmail(email, GetString("unsubscribe.invalidemailformat")).Result;

        if (String.IsNullOrEmpty(result))
        {
            bool requestSent = false;
            int  siteId      = 0;
            if (CMSContext.CurrentSite != null)
            {
                siteId = CMSContext.CurrentSiteID;
            }

            // Try to get all subscriber infos with given e-mail
            DataSet ds = SubscriberProvider.GetSubscribersFromView(email, siteId);
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Subscriber sb = new Subscriber(dr);
                    if ((sb != null) && ((sb.SubscriberType == null) || (sb.SubscriberRelatedID > 0) && (sb.SubscriberType == SiteObjectType.USER)))
                    {
                        // Get newsletter information
                        Newsletter news = NewsletterProvider.GetNewsletter(this.NewsletterName, siteId);
                        if (news != null)
                        {
                            // Get subscription info
                            SubscriberNewsletterInfo sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(sb.SubscriberID, news.NewsletterID);
                            // Send e-mail to subscribed user only
                            if (sni != null)
                            {
                                SendUnsubscriptionRequest(sb, news, sni, CMSContext.CurrentSiteName);
                                requestSent = true;  // At least one request was sent
                            }
                        }
                    }
                }
            }

            // Unsubscription failed if none confirmation e-mail was sent
            if (!requestSent)
            {
                // Use default error message if none is specified
                if (String.IsNullOrEmpty(this.ErrorText))
                {
                    result = GetString("unsubscribe.notsubscribed");
                }
                else
                {
                    result = this.ErrorText;
                }
            }
        }

        // Display error message if set
        if (!string.IsNullOrEmpty(result))
        {
            lblError.Text    = result;
            lblError.Visible = true;
        }
        else
        {
            // Display unsubscription confirmation
            lblInfo.Visible = true;
            if (String.IsNullOrEmpty(this.ResultText))
            {
                // Use default message if none was specified
                lblInfo.Text = GetString("unsubscribe.confirmtext");
            }
            else
            {
                lblInfo.Text = this.ResultText;
            }
            lblError.Visible  = false;
            txtEmail.Visible  = false;
            btnSubmit.Visible = false;
        }
    }