/// <summary>
    /// Subscriber control items changed event.
    /// </summary>
    protected void SubscriberSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        int subscriberID = 0;

        string[]       newItems = null;
        SubscriberInfo sb       = null;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(selectSubscriber.Value, null);

        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            // Add all new items to site
            foreach (string item in newItems)
            {
                subscriberID = ValidationHelper.GetInteger(item, 0);

                // Get subscriber
                sb = SubscriberInfoProvider.GetSubscriberInfo(subscriberID);

                // If subscriber exists and is not subscribed, subscribe him
                if ((sb != null) && (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
                {
                    try
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                    }
                    catch
                    {
                    }
                }
            }
        }

        selectSubscriber.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
예제 #2
0
    /// <summary>
    /// Saves the data.
    /// </summary>
    private bool Save(string newsletterName, SubscriberInfo sb)
    {
        bool toReturn = false;
        int  siteId   = SiteContext.CurrentSiteID;

        // Check if sunscriber info object exists
        if ((sb == null) || string.IsNullOrEmpty(newsletterName))
        {
            return(false);
        }

        // Get nesletter info
        NewsletterInfo news = NewsletterInfoProvider.GetNewsletterInfo(newsletterName, siteId);

        if (news != null)
        {
            try
            {
                // Check if subscriber is not allready subscribed
                if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberGUID, news.NewsletterGUID, siteId))
                {
                    toReturn = true;

                    // Subscribe to the site
                    SubscriberInfoProvider.Subscribe(sb.SubscriberID, news.NewsletterID, DateTime.Now, SendConfirmationEmail);

                    // Info message
                    if (!chooseMode)
                    {
                        lblInfo.Visible = true;
                        lblInfo.Text    = GetString("NewsletterSubscription.Subscribed");
                    }

                    // Track successful subscription conversion
                    if (TrackConversionName != string.Empty)
                    {
                        string siteName = SiteContext.CurrentSiteName;

                        if (AnalyticsHelper.AnalyticsEnabled(siteName) && AnalyticsHelper.TrackConversionsEnabled(siteName) && !AnalyticsHelper.IsIPExcluded(siteName, RequestContext.UserHostAddress))
                        {
                            // Log conversion
                            HitLogProvider.LogConversions(siteName, LocalizationContext.PreferredCultureCode, TrackConversionName, 0, ConversionValue);
                        }
                    }

                    // Log newsletter subscription activity if double opt-in is not required
                    if (!news.NewsletterEnableOptIn)
                    {
                        Activity activity = new ActivityNewsletterSubscribing(sb, news, AnalyticsContext.ActivityEnvironmentVariables);
                        activity.Log();
                    }
                }
                else
                {
                    // Info message - subscriber is allready in site
                    if (!chooseMode)
                    {
                        lblInfo.Visible = true;
                        lblInfo.Text    = GetString("NewsletterSubscription.SubscriberIsAlreadySubscribed");
                    }
                    else
                    {
                        lblInfo.Visible = true;
                        lblInfo.Text   += GetString("NewsletterSubscription.SubscriberIsAlreadySubscribedXY") + " " + HTMLHelper.HTMLEncode(ResHelper.LocalizeString(news.NewsletterDisplayName)) + ".<br />";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text    = ex.Message;
            }
        }
        else
        {
            lblError.Visible = true;
            lblError.Text    = GetString("NewsletterSubscription.NewsletterDoesNotExist");
        }

        return(toReturn);
    }
예제 #3
0
    private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
    {
        // Check 'EcommerceModify' permission
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.Ecommerce", "EcommerceModify"))
        {
            // Check 'ModifyCustomers' permission if don't have general ecommerce modify permission
            if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.Ecommerce", "ModifyCustomers"))
            {
                RedirectToAccessDenied("CMS.Ecommerce", "EcommerceModify OR ModifyCustomers");
            }
        }

        // Check if a customer has a valid email
        if ((email != null) && (email.Trim() != string.Empty) && (ValidationHelper.IsEmail(email)))
        {
            // Check whether subscriber already exist
            var sb = GetSubscriber();
            if (sb == null)
            {
                // Create new subscriber
                sb = new SubscriberInfo
                {
                    SubscriberEmail     = email,
                    SubscriberFirstName = firstName,
                    SubscriberLastName  = lastName,
                    SubscriberFullName  = (firstName + " " + lastName).Trim(),
                    SubscriberSiteID    = siteId,
                    SubscriberGUID      = Guid.NewGuid()
                };
                SubscriberInfoProvider.SetSubscriberInfo(sb);
            }

            var changed = false;

            // Remove old items
            string newValues = ValidationHelper.GetString(usNewsletters.Value, null);
            string items     = DataHelper.GetNewItemsInList(newValues, currentValues);
            if (!String.IsNullOrEmpty(items))
            {
                string[] newItems = items.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in newItems)
                {
                    int newsletterId = ValidationHelper.GetInteger(item, 0);

                    // If subscriber is subscribed, unsubscribe him
                    if (SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
                    {
                        SubscriberInfoProvider.Unsubscribe(sb.SubscriberID, newsletterId);

                        changed = true;
                    }
                }
            }

            // Add new items
            items = DataHelper.GetNewItemsInList(currentValues, newValues);
            if (!String.IsNullOrEmpty(items))
            {
                string[] newItems = items.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in newItems)
                {
                    int newsletterId = ValidationHelper.GetInteger(item, 0);

                    // If subscriber is not subscribed, subscribe him
                    if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now);

                        changed = true;
                    }
                }
            }

            // Display information about successful (un)subscription if occurred
            if (changed)
            {
                ShowChangesSaved();
            }
        }
    }
    private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
    {
        if (RaiseOnCheckPermissions("ManageSubscribers", this))
        {
            if (StopProcessing)
            {
                return;
            }
        }

        // Get specified user if used instead of current user
        UserInfo ui = null;

        if (UserID > 0)
        {
            ui = UserInfoProvider.GetUserInfo(UserID);
        }
        else
        {
            ui = CMSContext.CurrentUser;
        }

        // Get specified site ID instead of current site ID
        int siteId = 0;

        if (SiteID > 0)
        {
            siteId = SiteID;
        }
        else
        {
            siteId = CMSContext.CurrentSiteID;
        }

        if ((sb == null) && (ui != null))
        {
            // Create new subsciber (bind to existing user account)
            if ((!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName)))
            {
                sb = new SubscriberInfo();
                if (ui != null)
                {
                    if (!string.IsNullOrEmpty(ui.FirstName) && !string.IsNullOrEmpty(ui.LastName))
                    {
                        sb.SubscriberFirstName = ui.FirstName;
                        sb.SubscriberLastName  = ui.LastName;
                    }
                    else
                    {
                        sb.SubscriberFirstName = ui.FullName;
                    }
                    // Full name consists of "user " and user full name
                    sb.SubscriberFullName = "User '" + ui.FullName + "'";
                }

                sb.SubscriberSiteID    = siteId;
                sb.SubscriberType      = SiteObjectType.USER;
                sb.SubscriberRelatedID = ui.UserID;
                // Save subscriber to DB
                SubscriberInfoProvider.SetSubscriberInfo(sb);
            }
        }

        if (sb == null)
        {
            return;
        }

        // Create membership between current contact and subscriber
        ModuleCommands.OnlineMarketingCreateRelation(sb.SubscriberID, MembershipType.NEWSLETTER_SUBSCRIBER, ModuleCommands.OnlineMarketingGetCurrentContactID());

        // Remove old items
        int    newsletterId = 0;
        string newValues    = ValidationHelper.GetString(usNewsletters.Value, null);
        string items        = DataHelper.GetNewItemsInList(newValues, currentValues);

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

                    // If subscriber is subscribed, unsubscribe him
                    if (SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
                    {
                        SubscriberInfoProvider.Unsubscribe(sb.SubscriberID, newsletterId, SendConfirmationEmail);
                        // Log activity
                        LogActivity(ui, newsletterId, false);
                    }
                }
            }
        }

        // Add new items
        items = DataHelper.GetNewItemsInList(currentValues, newValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                foreach (string item in newItems)
                {
                    newsletterId = ValidationHelper.GetInteger(item, 0);

                    // If subscriber is not subscribed, subscribe him
                    if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
                    {
                        try
                        {
                            SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, SendConfirmationEmail);
                            // Log activity
                            LogActivity(ui, newsletterId, true);
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }

        // Display information about successful (un)subscription
        ShowChangesSaved();
    }
예제 #5
0
    /// <summary>
    /// Saves the data.
    /// </summary>
    /// <param name="newsletterName">Newsletter name</param>
    /// <param name="sb">Subscriber</param>
    private bool Save(string newsletterName, SubscriberInfo sb)
    {
        bool toReturn = false;
        int  siteId   = SiteContext.CurrentSiteID;

        // Check if sunscriber info object exists
        if ((sb == null) || string.IsNullOrEmpty(newsletterName))
        {
            return(false);
        }

        // Get nesletter info
        NewsletterInfo news = NewsletterInfoProvider.GetNewsletterInfo(newsletterName, siteId);

        if (news != null)
        {
            // Init webpart resolver
            object[] data = new object[3];
            data[0] = news;
            data[1] = sb;

            MacroResolver resolver = ContextResolver;
            resolver.SetNamedSourceData("Newsletter", news);
            resolver.SetNamedSourceData("Subscriber", sb);
            if (AllowUserSubscribers && isAuthenticated)
            {
                data[2] = CurrentUser;
                resolver.SetNamedSourceData("User", CurrentUser);
            }
            resolver.SetAnonymousSourceData(data);

            try
            {
                // Check if subscriber is not allready subscribed
                if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberGUID, news.NewsletterGUID, siteId))
                {
                    toReturn = true;

                    // Subscribe to the site
                    SubscriberInfoProvider.Subscribe(sb.SubscriberID, news.NewsletterID, DateTime.Now, SendConfirmationEmail);

                    if (!chooseMode)
                    {
                        // Display message about successful subscription
                        lblInfo.Visible = true;
                        lblInfo.Text    = SubscriptionConfirmationMessage;
                    }

                    // Track successful subscription conversion
                    if (!string.IsNullOrEmpty(TrackConversionName))
                    {
                        string siteName = SiteContext.CurrentSiteName;

                        if (AnalyticsHelper.AnalyticsEnabled(siteName) && AnalyticsHelper.TrackConversionsEnabled(siteName) && !AnalyticsHelper.IsIPExcluded(siteName, RequestContext.UserHostAddress))
                        {
                            // Log conversion
                            HitLogProvider.LogConversions(siteName, LocalizationContext.PreferredCultureCode, TrackConversionName, 0, ConversionValue);
                        }
                    }

                    // Log newsletter subscription activity if double opt-in is not required
                    if (!news.NewsletterEnableOptIn)
                    {
                        Activity activity = new ActivityNewsletterSubscribing(sb, news, AnalyticsContext.ActivityEnvironmentVariables);
                        activity.Log();
                    }
                }
                else
                {
                    lblInfo.Visible = true;
                    string message = null;

                    if (string.IsNullOrEmpty(MessageForAlreadySubscribed))
                    {
                        if (!chooseMode)
                        {
                            message = GetString("NewsletterSubscription.SubscriberIsAlreadySubscribed");
                        }
                        else
                        {
                            message = string.Format("{0} {1}.<br />", GetString("NewsletterSubscription.SubscriberIsAlreadySubscribedXY"), HTMLHelper.HTMLEncode(ResHelper.LocalizeString(news.NewsletterDisplayName)));
                        }
                    }
                    else
                    {
                        message = MessageForAlreadySubscribed;
                    }

                    // Info message - subscriber is allready in site
                    if (!chooseMode)
                    {
                        lblInfo.Text = message;
                    }
                    else
                    {
                        lblInfo.Text += message;
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text    = ex.Message;
            }
        }
        else
        {
            lblError.Visible = true;
            lblError.Text    = GetString("NewsletterSubscription.NewsletterDoesNotExist");
        }

        return(toReturn);
    }
예제 #6
0
    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
        }
        else
        {
            int siteId = 0;

            // Get current id
            if (SiteContext.CurrentSite != null)
            {
                siteId = SiteContext.CurrentSiteID;
            }

            // Get subscriber and newsletter guid from query string
            Guid   subscriberGuid   = QueryHelper.GetGuid("subscriberguid", Guid.Empty);
            Guid   newsletterGuid   = QueryHelper.GetGuid("newsletterguid", Guid.Empty);
            Guid   issueGuid        = QueryHelper.GetGuid("issueguid", Guid.Empty);
            string subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
            int    issueId          = QueryHelper.GetInteger("issueid", 0);
            int    contactId        = QueryHelper.GetInteger("contactid", 0);

            string   requestTime = QueryHelper.GetString("datetime", string.Empty);
            DateTime datetime    = DateTimeHelper.ZERO_TIME;

            // Get date and time
            if (!string.IsNullOrEmpty(requestTime))
            {
                try
                {
                    datetime = DateTime.ParseExact(requestTime, SecurityHelper.EMAIL_CONFIRMATION_DATETIME_FORMAT, null);
                }
                catch
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("newsletter.unsubscribefailed");
                    return;
                }
            }

            // Check whether both guid exists
            if ((subscriberGuid != Guid.Empty) && (newsletterGuid != Guid.Empty))
            {
                SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(subscriberGuid, siteId);
                if (subscriber == null)
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("Unsubscribe.SubscriberDoesNotExist");
                    return;
                }
                // Show error message if subscriber type is 'Role'
                if (!string.IsNullOrEmpty(subscriber.SubscriberType) && subscriber.SubscriberType.EqualsCSafe(RoleInfo.OBJECT_TYPE, true))
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("Unsubscriber.CannotUnsubscribeRole");
                    return;
                }

                NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo(newsletterGuid, siteId);
                if (newsletter == null)
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("Unsubscribe.NewsletterDoesNotExist");
                    return;
                }

                // Check whether subscription is valid
                if (SubscriberInfoProvider.IsSubscribed(subscriber.SubscriberID, newsletter.NewsletterID))
                {
                    bool isSubscribed = true;

                    if (string.IsNullOrEmpty(subscriber.SubscriberType) ||
                        (!subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.CONTACTGROUP, true) &&
                         !subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.PERSONA, true)))
                    {
                        // Unsubscribe action
                        SubscriberInfoProvider.Unsubscribe(subscriber.SubscriberID, newsletter.NewsletterID, SendConfirmationEmail);
                    }
                    else
                    {
                        // Check if the contact group member has unsubscription activity for the specified newsletter
                        isSubscribed = (contactId > 0) && !ModuleCommands.OnlineMarketingIsContactUnsubscribed(contactId, newsletter.NewsletterID, siteId);
                    }

                    if (isSubscribed)
                    {
                        // Log newsletter unsubscription activity
                        LogActivity(subscriber, subscriber.SubscriberID, newsletter.NewsletterID, siteId, issueId, issueGuid, contactId);

                        // Display confirmation
                        DisplayConfirmation();
                    }
                    else
                    {
                        // Contact group member is already unsubscribed
                        lblError.Visible = true;
                        lblError.Text    = GetString("Unsubscribe.NotSubscribed");
                    }
                }
                else
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("Unsubscribe.NotSubscribed");
                }
            }
            // Check if subscriptionGUID is supplied
            else if (!string.IsNullOrEmpty(subscriptionHash))
            {
                // Check if given subscription exists
                SubscriberNewsletterInfo sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriptionHash);
                if ((sni != null) && sni.SubscriptionEnabled)
                {
                    SubscriberInfoProvider.ApprovalResult result = SubscriberInfoProvider.Unsubscribe(subscriptionHash, SendConfirmationEmail, SiteContext.CurrentSiteName, datetime);

                    switch (result)
                    {
                    // Approving subscription was successful
                    case SubscriberInfoProvider.ApprovalResult.Success:
                        bool isSubscribed = true;

                        // Get subscriber
                        SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(sni.SubscriberID);
                        if ((subscriber != null) && !string.IsNullOrEmpty(subscriber.SubscriberType) && (subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.CONTACTGROUP, true) || subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.PERSONA, true)))
                        {
                            // Check if the contact group member has unsubscription activity for the specified newsletter
                            isSubscribed = (contactId > 0) && !ModuleCommands.OnlineMarketingIsContactUnsubscribed(contactId, sni.NewsletterID, siteId);
                        }

                        if (isSubscribed)
                        {
                            // Log newsletter unsubscription activity
                            LogActivity(subscriber, subscriber.SubscriberID, sni.NewsletterID, siteId, issueId, issueGuid, contactId);

                            // Display confirmation
                            DisplayConfirmation();
                        }
                        else
                        {
                            // Contact group member is already unsubscribed
                            lblError.Visible = true;
                            lblError.Text    = GetString("Unsubscribe.NotSubscribed");
                        }
                        break;

                    // Subscription was already approved
                    case SubscriberInfoProvider.ApprovalResult.Failed:
                        lblError.Visible = true;
                        lblError.Text    = GetString("newsletter.unsubscribefailed");
                        break;

                    case SubscriberInfoProvider.ApprovalResult.TimeExceeded:
                        lblError.Visible = true;
                        lblError.Text    = GetString("newsletter.approval_timeexceeded");
                        break;

                    // Subscription not found
                    default:
                    case SubscriberInfoProvider.ApprovalResult.NotFound:
                        lblError.Visible = true;
                        lblError.Text    = GetString("Unsubscribe.NotSubscribed");
                        break;
                    }
                }
                else
                {
                    lblError.Visible = true;
                    lblError.Text    = GetString("Unsubscribe.NotSubscribed");
                }
            }
            else
            {
                Visible = false;
            }
        }
    }
    /// <summary>
    /// Persona items selected event handler.
    /// </summary>
    protected void PersonaSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (personaSelector == null)
        {
            return;
        }

        int siteId = SiteContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(personaSelector.Value, null);

        // Get added items
        string[] newItems = newValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in newItems)
        {
            // Check limited number of subscribers
            if (!SubscriberInfoProvider.LicenseVersionCheck(RequestContext.CurrentDomain, FeatureEnum.Personas, ObjectActionEnum.Insert))
            {
                ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                break;
            }

            // Get persona ID
            int personaID = ValidationHelper.GetInteger(item, 0);

            // Try to get subscriber
            SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(PredefinedObjectType.PERSONA, personaID, siteId);
            if (subscriber == null)
            {
                // Get persona display name
                var    persona     = BaseAbstractInfoProvider.GetInfoById(PredefinedObjectType.PERSONA, personaID);
                string personaName = ValidationHelper.GetString(persona.GetValue("PersonaDisplayName"), string.Empty);
                if (string.IsNullOrEmpty(personaName))
                {
                    continue;
                }

                // Create new subscriber of persona type
                subscriber = new SubscriberInfo();

                subscriber.SubscriberFirstName = personaName;

                // Full name consists of "persona" and display name
                subscriber.SubscriberFullName = new SubscriberFullNameFormater().GetPersonaSubscriberName(personaName);

                subscriber.SubscriberSiteID    = siteId;
                subscriber.SubscriberType      = PredefinedObjectType.PERSONA;
                subscriber.SubscriberRelatedID = personaID;

                CheckPermissionsForSubscriber(subscriber);

                SubscriberInfoProvider.SetSubscriberInfo(subscriber);
            }

            // If subscriber exists and is not subscribed, subscribe him
            if (!SubscriberInfoProvider.IsSubscribed(subscriber.SubscriberID, mNewsletter.NewsletterID))
            {
                try
                {
                    SubscriberInfoProvider.Subscribe(subscriber.SubscriberID, mNewsletter.NewsletterID, DateTime.Now, chkSendConfirmation.Checked, false);
                }
                catch (InvalidOperationException ex)
                {
                    ShowError(ex.Message);
                }
            }
        }

        personaSelector.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// Contact items selected event handler.
    /// </summary>
    protected void ContactSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (cSelector == null)
        {
            return;
        }
        int siteId = SiteContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(cSelector.Value, null);

        // Get added items
        string[] newItems = newValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in newItems)
        {
            // Check limited number of subscribers
            if (!SubscriberInfoProvider.LicenseVersionCheck(RequestContext.CurrentDomain, FeatureEnum.Subscribers, ObjectActionEnum.Insert))
            {
                ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                break;
            }

            // Get contact ID
            int contactID = ValidationHelper.GetInteger(item, 0);

            // Try to get subscriber
            SubscriberInfo sb = SubscriberInfoProvider.GetSubscriberInfo(PredefinedObjectType.CONTACT, contactID, siteId);
            if (sb == null)
            {
                // Get contact's info
                DataSet contactData = ModuleCommands.OnlineMarketingGetContactForNewsletters(contactID, "ContactFirstName,ContactMiddleName,ContactLastName,ContactEmail");
                if (DataHelper.DataSourceIsEmpty(contactData))
                {
                    continue;
                }

                string firstName  = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactFirstName"], string.Empty);
                string lastName   = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactLastName"], string.Empty);
                string middleName = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactMiddleName"], string.Empty);
                string email      = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactEmail"], string.Empty);

                // Create new subscriber of contact type
                sb = new SubscriberInfo();
                sb.SubscriberFirstName = firstName;
                sb.SubscriberLastName  = lastName;
                sb.SubscriberEmail     = email;
                sb.SubscriberFullName  = new SubscriberFullNameFormater().GetContactSubscriberName(firstName, middleName, lastName);
                sb.SubscriberSiteID    = siteId;
                sb.SubscriberType      = PredefinedObjectType.CONTACT;
                sb.SubscriberRelatedID = contactID;

                CheckPermissionsForSubscriber(sb);

                SubscriberInfoProvider.SetSubscriberInfo(sb);
            }

            // Subscribe the existing or created subscriber
            if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, mNewsletter.NewsletterID))
            {
                try
                {
                    SubscriberInfoProvider.Subscribe(sb.SubscriberID, mNewsletter.NewsletterID, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                }
                catch (InvalidOperationException ex)
                {
                    ShowError(ex.Message);
                }
            }
        }

        cSelector.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// Contact group items selected event handler.
    /// </summary>
    protected void CGSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (cgSelector == null)
        {
            return;
        }
        int siteId = SiteContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(cgSelector.Value, null);

        // Get added items
        string[] newItems = newValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in newItems)
        {
            // Check limited number of subscribers
            if (!SubscriberInfoProvider.LicenseVersionCheck(RequestContext.CurrentDomain, FeatureEnum.Subscribers, ObjectActionEnum.Insert))
            {
                ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                break;
            }

            // Get group ID
            int groupID = ValidationHelper.GetInteger(item, 0);

            // Try to get subscriber
            SubscriberInfo sb = SubscriberInfoProvider.GetSubscriberInfo(PredefinedObjectType.CONTACTGROUP, groupID, siteId);
            if (sb == null)
            {
                // Get contact group display name
                string cgName = ModuleCommands.OnlineMarketingGetContactGroupName(groupID);
                if (string.IsNullOrEmpty(cgName))
                {
                    continue;
                }

                // Create new subscriber of contact group type
                sb = new SubscriberInfo();
                sb.SubscriberFirstName = cgName;
                // Full name consists of "contact group " and display name
                sb.SubscriberFullName  = new SubscriberFullNameFormater().GetContactGroupSubscriberName(cgName);
                sb.SubscriberSiteID    = siteId;
                sb.SubscriberType      = PredefinedObjectType.CONTACTGROUP;
                sb.SubscriberRelatedID = groupID;

                CheckPermissionsForSubscriber(sb);

                SubscriberInfoProvider.SetSubscriberInfo(sb);
            }

            // If subscriber exists and is not subscribed, subscribe him
            if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, mNewsletter.NewsletterID))
            {
                try
                {
                    SubscriberInfoProvider.Subscribe(sb.SubscriberID, mNewsletter.NewsletterID, DateTime.Now, chkSendConfirmation.Checked, false);
                }
                catch (InvalidOperationException ex)
                {
                    ShowError(ex.Message);
                }
            }
        }

        cgSelector.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// User control items changed event.
    /// </summary>
    protected void UserSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        int siteId = SiteContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(selectUser.Value, null);

        string[] newItems = newValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in newItems)
        {
            // Check limited number of subscribers
            if (!SubscriberInfoProvider.LicenseVersionCheck(RequestContext.CurrentDomain, FeatureEnum.Subscribers, ObjectActionEnum.Insert))
            {
                ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                break;
            }

            int userID = ValidationHelper.GetInteger(item, 0);

            // Get subscriber
            SubscriberInfo sb = SubscriberInfoProvider.GetSubscriberInfo(UserInfo.OBJECT_TYPE, userID, siteId);
            if (sb == null)
            {
                // Get user info
                UserInfo ui = UserInfoProvider.GetUserInfo(userID);
                if (ui == null)
                {
                    continue;
                }

                // Create new subscriber of user type
                sb = new SubscriberInfo();
                sb.SubscriberFirstName = ui.FullName;
                sb.SubscriberFullName  = new SubscriberFullNameFormater().GetUserSubscriberName(ui.FullName);
                sb.SubscriberSiteID    = siteId;
                sb.SubscriberType      = UserInfo.OBJECT_TYPE;
                sb.SubscriberRelatedID = userID;

                CheckPermissionsForSubscriber(sb);

                SubscriberInfoProvider.SetSubscriberInfo(sb);
            }

            // If subscriber exists and is not subscribed, subscribe him
            if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, mNewsletter.NewsletterID))
            {
                try
                {
                    SubscriberInfoProvider.Subscribe(sb.SubscriberID, mNewsletter.NewsletterID, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                }
                catch (InvalidOperationException ex)
                {
                    ShowError(ex.Message);
                }
            }
        }

        selectUser.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get data from query string
        Guid   subscriberGuid   = QueryHelper.GetGuid("subscriberguid", Guid.Empty);
        Guid   newsletterGuid   = QueryHelper.GetGuid("newsletterguid", Guid.Empty);
        string subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
        Guid   issueGuid        = QueryHelper.GetGuid("issueGuid", Guid.Empty);
        int    issueID          = QueryHelper.GetInteger("issueid", 0);
        int    contactId        = QueryHelper.GetInteger("contactid", 0);
        bool   unsubscribed     = false;

        string   requestTime = QueryHelper.GetString("datetime", string.Empty);
        DateTime datetime    = DateTimeHelper.ZERO_TIME;

        // Get date and time
        if (!string.IsNullOrEmpty(requestTime))
        {
            try
            {
                datetime = DateTime.ParseExact(requestTime, SecurityHelper.EMAIL_CONFIRMATION_DATETIME_FORMAT, null);
            }
            catch
            {
                ShowError(GetString("newsletter.unsubscribefailed"));
                return;
            }
        }

        // Get site ID
        int siteId = 0;

        if (CMSContext.CurrentSite != null)
        {
            siteId = CMSContext.CurrentSiteID;
        }

        if ((subscriberGuid != Guid.Empty) && (newsletterGuid != Guid.Empty) && (siteId != 0))
        {
            SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(subscriberGuid, siteId);
            if (subscriber == null)
            {
                ShowError(GetString("Unsubscribe.SubscriberDoesNotExist"));
                return;
            }
            // Show error message if subscriber type is 'Role'
            if (!string.IsNullOrEmpty(subscriber.SubscriberType) && subscriber.SubscriberType.EqualsCSafe(SiteObjectType.ROLE, true))
            {
                ShowError(GetString("Unsubscribe.CannotUnsubscribeRole"));
                return;
            }

            NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo(newsletterGuid, siteId);
            if (newsletter == null)
            {
                ShowError(GetString("Unsubscribe.NewsletterDoesNotExist"));
                return;
            }

            // Check if subscriber with given GUID is subscribed to specified newsletter
            if (SubscriberInfoProvider.IsSubscribed(subscriber.SubscriberID, newsletter.NewsletterID))
            {
                bool isSubscribed = true;

                if (string.IsNullOrEmpty(subscriber.SubscriberType) || !subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.CONTACTGROUP, true))
                {
                    // Unsubscribe action
                    SubscriberInfoProvider.Unsubscribe(subscriber.SubscriberID, newsletter.NewsletterID);
                }
                else
                {
                    // Check if the contact group member has unsubscription activity for the specified newsletter
                    isSubscribed = (contactId > 0) && !ModuleCommands.OnlineMarketingIsContactUnsubscribed(contactId, newsletter.NewsletterID, siteId);
                }

                if (isSubscribed)
                {
                    // Log newsletter unsubscription activity
                    LogActivity(subscriber, 0, newsletter.NewsletterID, issueID, contactId);

                    // Display confirmation
                    ShowInformation(GetString("Unsubscribe.Unsubscribed"));
                    unsubscribed = true;
                }
                else
                {
                    // Contact group member is already unsubscribed
                    ShowError(GetString("Unsubscribe.NotSubscribed"));
                }
            }
            else
            {
                ShowError(GetString("Unsubscribe.NotSubscribed"));
            }
        }
        // Check if subscription approval hash is supplied
        else if (!string.IsNullOrEmpty(subscriptionHash))
        {
            SubscriberNewsletterInfo sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriptionHash);
            // Check if hash is valid
            if ((sni != null) && sni.SubscriptionEnabled)
            {
                SubscriberInfoProvider.ApprovalResult result = SubscriberInfoProvider.Unsubscribe(subscriptionHash, true, CMSContext.CurrentSiteName, datetime);

                switch (result)
                {
                // Approving subscription was successful
                case SubscriberInfoProvider.ApprovalResult.Success:
                    bool isSubscribed = true;

                    // Get subscriber
                    SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(sni.SubscriberID);
                    if ((subscriber != null) && !string.IsNullOrEmpty(subscriber.SubscriberType) && subscriber.SubscriberType.EqualsCSafe(PredefinedObjectType.CONTACTGROUP, true))
                    {
                        // Check if the contact group member has unsubscription activity for the specified newsletter
                        isSubscribed = (contactId > 0) && !ModuleCommands.OnlineMarketingIsContactUnsubscribed(contactId, sni.NewsletterID, siteId);
                    }

                    if (isSubscribed)
                    {
                        // Log newsletter unsubscription activity
                        LogActivity(null, sni.SubscriberID, sni.NewsletterID, issueID, contactId);

                        // Display confirmation
                        ShowInformation(GetString("Unsubscribe.Unsubscribed"));
                        unsubscribed = true;
                    }
                    else
                    {
                        // Contact group member is already unsubscribed
                        ShowError(GetString("Unsubscribe.NotSubscribed"));
                    }
                    break;

                // Subscription was already approved
                case SubscriberInfoProvider.ApprovalResult.Failed:
                    ShowError(GetString("newsletter.unsubscribefailed"));
                    break;

                case SubscriberInfoProvider.ApprovalResult.TimeExceeded:
                    ShowError(GetString("newsletter.approval_timeexceeded"));
                    break;

                // Subscription not found
                default:
                case SubscriberInfoProvider.ApprovalResult.NotFound:
                    ShowError(GetString("Unsubscribe.NotSubscribed"));
                    break;
                }
            }
            else
            {
                ShowError(GetString("Unsubscribe.NotSubscribed"));
            }
        }
        else
        {
            if (subscriberGuid == Guid.Empty)
            {
                ShowError(GetString("Unsubscribe.SubscriberDoesNotExist"));
            }
            if (newsletterGuid == Guid.Empty)
            {
                ShowError(GetString("Unsubscribe.NewsletterDoesNotExist"));
            }
        }

        // Increase unsubscribed count
        if (unsubscribed)
        {
            // If Issue ID was provided
            if (issueID > 0)
            {
                IssueInfoProvider.Unsubscribe(issueID);
                return;
            }
            // Otherwise try using the Issue GUID
            if (issueGuid != Guid.Empty)
            {
                IssueInfo issue = IssueInfoProvider.GetIssueInfo(issueGuid, siteId);
                if (issue == null)
                {
                    return;
                }

                IssueInfoProvider.Unsubscribe(issue.IssueID);
            }
        }
    }
    /// <summary>
    /// Contact items selected event handler.
    /// </summary>
    protected void ContactSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (cSelector == null)
        {
            return;
        }
        int contactID = 0;

        string[]       newItems    = null;
        SubscriberInfo sb          = null;
        int            siteId      = CMSContext.CurrentSiteID;
        string         cType       = PredefinedObjectType.CONTACT;
        DataSet        contactData = null;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(cSelector.Value, null);

        // Get added items
        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            foreach (string item in newItems)
            {
                // Check limited number of subscribers
                if (!SubscriberInfoProvider.LicenseVersionCheck(URLHelper.GetCurrentDomain(), FeatureEnum.Subscribers, VersionActionEnum.Insert))
                {
                    ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                    break;
                }

                // Get contact ID
                contactID = ValidationHelper.GetInteger(item, 0);

                // Try to get subscriber
                sb = SubscriberInfoProvider.GetSubscriberInfo(cType, contactID, siteId);
                if (sb == null)
                {
                    // Get contact's info
                    contactData = ModuleCommands.OnlineMarketingGetContactForNewsletters(contactID, "ContactFirstName,ContactMiddleName,ContactLastName");
                    if (DataHelper.DataSourceIsEmpty(contactData))
                    {
                        continue;
                    }

                    string firstName  = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactFirstName"], string.Empty);
                    string lastName   = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactLastName"], string.Empty);
                    string middleName = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactMiddleName"], string.Empty);

                    // Create new subscriber of contact type
                    sb = new SubscriberInfo();
                    sb.SubscriberFirstName = firstName;
                    sb.SubscriberLastName  = lastName;
                    sb.SubscriberFullName  = string.Format("Contact '{0}{1}{2}'", string.IsNullOrEmpty(firstName) ? string.Empty : firstName + " ", string.IsNullOrEmpty(middleName) ? string.Empty : middleName + " ", lastName);
                    sb.SubscriberSiteID    = siteId;
                    sb.SubscriberType      = cType;
                    sb.SubscriberRelatedID = contactID;
                    SubscriberInfoProvider.SetSubscriberInfo(sb);
                }

                // If subscriber exists and is not subscribed, subscribe him
                if ((sb != null) && (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
                {
                    try
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                    }
                    catch
                    {
                    }
                }
            }
        }

        cSelector.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// Contact group items selected event handler.
    /// </summary>
    protected void CGSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (cgSelector == null)
        {
            return;
        }
        int groupID = 0;

        string[]       newItems = null;
        SubscriberInfo sb       = null;
        int            siteId   = CMSContext.CurrentSiteID;
        string         cgType   = "om.contactgroup";
        string         cgName   = null;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(cgSelector.Value, null);

        // Get added items
        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            foreach (string item in newItems)
            {
                // Check limited number of subscribers
                if (!SubscriberInfoProvider.LicenseVersionCheck(URLHelper.GetCurrentDomain(), FeatureEnum.Subscribers, VersionActionEnum.Insert))
                {
                    ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                    break;
                }

                // Get group ID
                groupID = ValidationHelper.GetInteger(item, 0);

                // Try to get subscriber
                sb = SubscriberInfoProvider.GetSubscriberInfo(cgType, groupID, siteId);
                if (sb == null)
                {
                    // Get contact group display name
                    cgName = ModuleCommands.OnlineMarketingGetContactGroupName(groupID);
                    if (string.IsNullOrEmpty(cgName))
                    {
                        continue;
                    }

                    // Create new subscriber of contact group type
                    sb = new SubscriberInfo();
                    sb.SubscriberFirstName = cgName;
                    // Full name consists of "contact group " and display name
                    sb.SubscriberFullName  = string.Concat("Contact group '", cgName, "'");
                    sb.SubscriberSiteID    = siteId;
                    sb.SubscriberType      = cgType;
                    sb.SubscriberRelatedID = groupID;
                    SubscriberInfoProvider.SetSubscriberInfo(sb);
                }

                // If subscriber exists and is not subscribed, subscribe him
                if ((sb != null) && (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
                {
                    try
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, chkSendConfirmation.Checked, false);
                    }
                    catch
                    {
                    }
                }
            }
        }

        cgSelector.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// User control items changed event.
    /// </summary>
    protected void UserSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        int userID = 0;

        string[]       newItems = null;
        SubscriberInfo sb       = null;
        UserInfo       ui       = null;
        int            siteId   = CMSContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(selectUser.Value, null);

        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            foreach (string item in newItems)
            {
                // Check limited number of subscribers
                if (!SubscriberInfoProvider.LicenseVersionCheck(URLHelper.GetCurrentDomain(), FeatureEnum.Subscribers, VersionActionEnum.Insert))
                {
                    ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                    break;
                }

                userID = ValidationHelper.GetInteger(item, 0);

                // Get subscriber
                sb = SubscriberInfoProvider.GetSubscriberInfo(SiteObjectType.USER, userID, siteId);
                if (sb == null)
                {
                    // Get user info
                    ui = UserInfoProvider.GetUserInfo(userID);
                    if (ui == null)
                    {
                        continue;
                    }

                    // Create new subscriber of user type
                    sb = new SubscriberInfo();
                    sb.SubscriberFirstName = ui.FullName;
                    sb.SubscriberFullName  = "User '" + ui.FullName + "'";
                    sb.SubscriberSiteID    = siteId;
                    sb.SubscriberType      = SiteObjectType.USER;
                    sb.SubscriberRelatedID = userID;
                    SubscriberInfoProvider.SetSubscriberInfo(sb);
                }

                // If subscriber exists and is not subscribed, subscribe him
                if ((sb != null) && (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
                {
                    try
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                    }
                    catch
                    {
                    }
                }
            }
        }

        selectUser.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }
    /// <summary>
    /// Roles control items changed event.
    /// </summary>
    protected void RolesSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        int roleID = 0;

        string[]       newItems = null;
        SubscriberInfo sb       = null;
        RoleInfo       ri       = null;
        int            siteId   = CMSContext.CurrentSiteID;

        // Get new items from selector
        string newValues = ValidationHelper.GetString(selectRole.Value, null);

        // Get added items
        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            foreach (string item in newItems)
            {
                // Check limited number of subscribers
                if (!SubscriberInfoProvider.LicenseVersionCheck(URLHelper.GetCurrentDomain(), FeatureEnum.Subscribers, VersionActionEnum.Insert))
                {
                    ShowError(GetString("licenselimitations.subscribers.errormultiple"));
                    break;
                }

                roleID = ValidationHelper.GetInteger(item, 0);

                // Get subscriber
                sb = SubscriberInfoProvider.GetSubscriberInfo(SiteObjectType.ROLE, roleID, siteId);
                if (sb == null)
                {
                    // Get role info and copy display name to new subscriber
                    ri = RoleInfoProvider.GetRoleInfo(roleID);
                    if ((ri == null) || (ri.SiteID != siteId))
                    {
                        continue;
                    }

                    // Create new subscriber of role type
                    sb = new SubscriberInfo();
                    sb.SubscriberFirstName = ri.DisplayName;
                    // Full name consists of "role " and role display name
                    sb.SubscriberFullName  = string.Concat("Role '", ri.DisplayName, "'");
                    sb.SubscriberSiteID    = siteId;
                    sb.SubscriberType      = SiteObjectType.ROLE;
                    sb.SubscriberRelatedID = roleID;
                    SubscriberInfoProvider.SetSubscriberInfo(sb);
                }

                // If subscriber exists and is not subscribed, subscribe him
                if ((sb != null) && (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
                {
                    try
                    {
                        SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, chkSendConfirmation.Checked, false);
                    }
                    catch
                    {
                    }
                }
            }
        }

        selectRole.Value = null;
        UniGridSubscribers.ReloadData();
        pnlUpdate.Update();
    }