/// <summary>
    /// Performs action on given subscriber.
    /// </summary>
    /// <param name="subscriberId">Id of subscriber</param>
    /// <param name="actionName">Name of action</param>
    private void DoSubscriberAction(int subscriberId, string actionName)
    {
        try
        {
            // Check manage subscribers permission
            var subscriber = SubscriberInfoProvider.GetSubscriberInfo(subscriberId);
            if (!subscriber.CheckPermissions(PermissionsEnum.Modify, SiteContext.CurrentSiteName, MembershipContext.AuthenticatedUser))
            {
                RedirectToAccessDenied(subscriber.TypeInfo.ModuleName, "ManageSubscribers");
            }

            switch (actionName.ToUpperInvariant())
            {
            // Subscribe
            case SUBSCRIBE:
                var subscription = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, mNewsletter.NewsletterID);
                if ((subscription == null) || subscription.SubscriptionEnabled)
                {
                    return;
                }

                SubscriberInfoProvider.RenewSubscription(subscriberId, mNewsletter.NewsletterID, chkSendConfirmation.Checked);
                SubscriberNewsletterInfoProvider.SetApprovalStatus(subscriberId, mNewsletter.NewsletterID, !chkRequireOptIn.Checked);
                if (chkRequireOptIn.Checked)
                {
                    IssueInfoProvider.SendDoubleOptInEmail(subscriberId, mNewsletter.NewsletterID);
                }
                break;

            // Unsubscribe
            case UNSUBSCRIBE:
                SubscriberInfoProvider.Unsubscribe(subscriberId, mNewsletter.NewsletterID, chkSendConfirmation.Checked);
                break;

            // Remove subscription
            case REMOVE:
                SubscriberInfoProvider.DeleteSubscription(subscriberId, mNewsletter.NewsletterID, chkSendConfirmation.Checked);
                break;

            // Approve subscription
            case APPROVE:
                SubscriberNewsletterInfoProvider.ApproveSubscription(subscriberId, mNewsletter.NewsletterID);
                break;

            // Block selected subscriber
            case BLOCK:
                SubscriberInfoProvider.BlockSubscriber(subscriberId);
                break;

            // Un-block selected subscriber
            case UNBLOCK:
                SubscriberInfoProvider.UnblockSubscriber(subscriberId);
                break;
            }
        }
        catch (Exception e)
        {
            LogAndShowError("Newsletter subscriber", "NEWSLETTERS", e);
        }
    }
Example #2
0
    /// <summary>
    /// Uniselector item selected event handler.
    /// </summary>
    protected void UniSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.newsletter", "ManageSubscribers"))
        {
            RedirectToAccessDenied("cms.newsletter", "ManageSubscribers");
        }

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

        string[] newItems = newValues.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        // Get all selected newsletters
        foreach (string item in newItems)
        {
            int newsletterId = ValidationHelper.GetInteger(item, 0);

            // Get subscription
            SubscriberNewsletterInfo subscription = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(mSubscriberId, newsletterId);

            // If not already subscribed
            if (subscription == null)
            {
                // Send confirmation only when subscriber is not Role or CG type and only when Double opt-in is enabled.
                SubscriberInfoProvider.Subscribe(mSubscriberId, newsletterId, DateTime.Now, chkSendConfirmation.Checked, !mIsMultipleSubscriber && chkRequireOptIn.Checked);
            }
        }

        selectNewsletter.Value = null;
        unigridNewsletters.ReloadData();
        pnlUpdate.Update();
    }
Example #3
0
            public void ConfirmSubscription_Hash_DateTime_SubscriptionIsConfirmed()
            {
                var email    = "a-guid-" + Guid.NewGuid() + "@domain.com";
                var dateTime = DateTime.Now;
                var contact  = GetContact(email);

                mNewsletterSubscriptionService.Subscribe(contact, mNewsletter, mNewsletterSubscriptionSettings);

                // Get the expected subscriber and binding to newsletter so that hash for their subscription confirmation can be faked
                // (the newsletters API allows to retrieve current hash, but not the corresponding timestamp which is necessary to pass the validation).
                var expectedSubscriber = SubscriberInfoProvider.GetSubscriberByEmail(email, mSite.SiteID);
                var confirmationHash   = SecurityHelper.GenerateConfirmationEmailHash(mNewsletter.NewsletterGUID + "|" + expectedSubscriber.SubscriberGUID, dateTime);

                var expectedSubscriberNewsletter = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(expectedSubscriber.SubscriberID, mNewsletter.NewsletterID);

                expectedSubscriberNewsletter.SubscriptionApprovalHash = confirmationHash;
                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(expectedSubscriberNewsletter);

                var approvalResult = mNewsletterSubscriptionService.ConfirmSubscription(expectedSubscriberNewsletter.SubscriptionApprovalHash, false, dateTime);

                // Retrieve the subscriber-newsletter binding again so that approval state can be validated
                expectedSubscriberNewsletter = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(expectedSubscriber.SubscriberID, mNewsletter.NewsletterID);

                Assert.AreEqual(ApprovalResult.Success, approvalResult, "Subscription confirmation was not successful.");
                Assert.IsTrue(expectedSubscriberNewsletter.SubscriptionApproved, $"Subscription is not approved after confirmation.");
            }
Example #4
0
    /// <summary>
    /// Handles multiple selector actions.
    /// </summary>
    protected void btnOk_Clicked(object sender, EventArgs e)
    {
        // Check permissions
        CheckAuthorization();

        if (drpActions.SelectedValue != "SELECT")
        {
            // Go through all selected items
            ArrayList list = UniGridSubscribers.SelectedItems;
            if (list.Count > 0)
            {
                SubscriberNewsletterInfo sni = null;
                foreach (object subscriberId in list)
                {
                    sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(ValidationHelper.GetInteger(subscriberId, 0), newsletterId);
                    if (sni != null)
                    {
                        switch (drpActions.SelectedValue)
                        {
                        // Remove subscription
                        case "REMOVE":
                            SubscriberProvider.Unsubscribe(sni, chkSendConfirmation.Checked);
                            break;

                        // Approve subscription
                        case "APPROVE":
                            if (!sni.SubscriptionApproved)
                            {
                                sni.SubscriptionApproved     = true;
                                sni.SubscriptionApprovedWhen = DateTime.Now;
                                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(sni);
                            }
                            break;

                        // Reject subscription
                        case "REJECT":
                            if (sni.SubscriptionApproved)
                            {
                                sni.SubscriptionApproved     = false;
                                sni.SubscriptionApprovedWhen = DateTime.MinValue;
                                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(sni);
                            }
                            break;
                        }
                    }
                }
            }
            else
            {
                ltlScript.Text += ScriptHelper.GetAlertScript(GetString("general.noitems"));
            }
        }
        UniGridSubscribers.ResetSelection();
        UniGridSubscribers.ReloadData();
    }
    /// <summary>
    /// Unigrid newsletters action handler.
    /// </summary>
    protected void unigridNewsletters_OnAction(string actionName, object actionArgument)
    {
        // Check 'configure' permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "ManageSubscribers"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "ManageSubscribers");
        }

        int newsletterId = ValidationHelper.GetInteger(actionArgument, 0);

        switch (actionName.ToLowerCSafe())
        {
        // Renew subscription
        case "subscribe":
            SubscriberInfoProvider.RenewSubscription(subscriberId, newsletterId, chkSendConfirmation.Checked);
            break;

        // Unsubscribe selected subscriber
        case "unsubscribe":
            SubscriberInfoProvider.Unsubscribe(subscriberId, newsletterId, chkSendConfirmation.Checked);
            break;

        // Unsubscribe selected subscriber
        case "remove":
            SubscriberInfoProvider.DeleteSubscription(subscriberId, newsletterId, chkSendConfirmation.Checked);
            break;

        // Approve selected subscription
        case "approve":
        {
            SubscriberNewsletterInfo subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
            if ((subscriptionInfo != null) && (!subscriptionInfo.SubscriptionApproved))
            {
                subscriptionInfo.SubscriptionApproved     = true;
                subscriptionInfo.SubscriptionApprovedWhen = DateTime.Now;
                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
            }
        }
        break;

        // Reject selected subscription
        case "reject":
        {
            SubscriberNewsletterInfo subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
            if ((subscriptionInfo != null) && (subscriptionInfo.SubscriptionApproved))
            {
                subscriptionInfo.SubscriptionApproved     = false;
                subscriptionInfo.SubscriptionApprovedWhen = DateTime.MinValue;
                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
            }
        }
        break;
        }
    }
Example #6
0
            public void ConfirmSubscription_SubscriptionIsNotConfirmedByDefault()
            {
                var email   = "a-guid-" + Guid.NewGuid() + "@domain.com";
                var contact = GetContact(email);

                mNewsletterSubscriptionService.Subscribe(contact, mNewsletter, mNewsletterSubscriptionSettings);

                var expectedSubscriber = SubscriberInfoProvider.GetSubscriberByEmail(email, mSite.SiteID);

                var expectedSubscriberNewsletter = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(expectedSubscriber.SubscriberID, mNewsletter.NewsletterID);

                Assert.IsFalse(expectedSubscriberNewsletter.SubscriptionApproved, $"Subscription must not be approved when double opt-in is enabled.");
            }
Example #7
0
    /// <summary>
    /// Handles the UniGrid's OnAction event.
    /// </summary>
    /// <param name="actionName">Name of item (button) that threw event</param>
    /// <param name="actionArgument">ID (value of Primary key) of corresponding data row</param>
    protected void UniGridSubscribers_OnAction(string actionName, object actionArgument)
    {
        // Check 'manage subscribers' permission
        CheckAuthorization();

        int subscriberId = ValidationHelper.GetInteger(actionArgument, 0);

        switch (actionName.ToLower())
        {
        // Unsubscribe selected subscriber
        case "remove":
            SubscriberProvider.Unsubscribe(subscriberId, newsletterId, chkSendConfirmation.Checked);
            break;

        // Approve selected subscription
        case "approve":
        {
            SubscriberNewsletterInfo subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
            if ((subscriptionInfo != null) && (!subscriptionInfo.SubscriptionApproved))
            {
                subscriptionInfo.SubscriptionApproved     = true;
                subscriptionInfo.SubscriptionApprovedWhen = DateTime.Now;
                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
            }
        }
        break;

        // Reject selected subscription
        case "reject":
        {
            SubscriberNewsletterInfo subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
            if ((subscriptionInfo != null) && (subscriptionInfo.SubscriptionApproved))
            {
                subscriptionInfo.SubscriptionApproved     = false;
                subscriptionInfo.SubscriptionApprovedWhen = DateTime.MinValue;
                SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
            }
        }
        break;

        // Block selected subscriber
        case "block":
            SubscriberProvider.BlockSubscriber(subscriberId);
            break;

        // Un-block selected subscriber
        case "unblock":
            SubscriberProvider.UnblockSubscriber(subscriberId);
            break;
        }
    }
Example #8
0
    /// <summary>
    /// Uniselector item selected event handler.
    /// </summary>
    protected void UniSelector_OnItemsSelected(object sender, EventArgs e)
    {
        // Check permissions
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "ManageSubscribers"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "ManageSubscribers");
        }

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

        string[] newItems = null;
        SubscriberNewsletterInfo subscription = null;
        Newsletter newsletter   = null;
        int        newsletterId = 0;

        newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (newItems != null)
        {
            // Get all selected newsletters
            foreach (string item in newItems)
            {
                newsletterId = ValidationHelper.GetInteger(item, 0);

                // Get subscription
                subscription = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);

                // If not already subscribed
                if (subscription == null)
                {
                    newsletter = NewsletterProvider.GetNewsletter(newsletterId);

                    // Subscribe role
                    if (isRoleORContactGroup)
                    {
                        SubscriberProvider.Subscribe(subscriberId, newsletterId, DateTime.Now, chkSendConfirmation.Checked, false);
                    }
                    // Subscribe users and subscribers
                    else
                    {
                        SubscriberProvider.Subscribe(subscriberId, newsletterId, DateTime.Now, chkSendConfirmation.Checked, chkRequireOptIn.Checked);
                    }
                }
            }
        }

        selectNewsletter.Value = null;
        unigridNewsletters.ReloadData();
        pnlUpdate.Update();
    }
Example #9
0
    /// <summary>
    /// Performs action on given subscriber.
    /// </summary>
    /// <param name="newsletter">Newsletter info</param>
    /// <param name="actionName">Name of action</param>
    private void DoSubscriberAction(NewsletterInfo newsletter, string actionName)
    {
        switch (actionName.ToUpperInvariant())
        {
        // Renew subscription
        case SUBSCRIBE:
            var subscriberNewsletterInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(mSubscriberId, newsletter.NewsletterID);
            if (subscriberNewsletterInfo.SubscriptionEnabled)
            {
                return;
            }

            bool requireDoubleOptIn = chkRequireOptIn.Checked && newsletter.NewsletterEnableOptIn;

            SubscriberInfoProvider.RenewSubscription(mSubscriberId, newsletter.NewsletterID, chkSendConfirmation.Checked && !requireDoubleOptIn);

            bool approved = true;
            // Check if double opt-in is enabled and if confirmation e-mail checkbox was checked
            if (requireDoubleOptIn)
            {
                IssueInfoProvider.SendDoubleOptInEmail(mSubscriberId, newsletter.NewsletterID);
                approved = false;
            }

            SubscriberNewsletterInfoProvider.SetApprovalStatus(mSubscriberId, newsletter.NewsletterID, approved);
            break;

        // Unsubscribe
        case UNSUBSCRIBE:
            SubscriberInfoProvider.Unsubscribe(mSubscriberId, newsletter.NewsletterID, chkSendConfirmation.Checked);
            break;

        // Remove subscription
        case REMOVE:
            SubscriberInfoProvider.DeleteSubscription(mSubscriberId, newsletter.NewsletterID, chkSendConfirmation.Checked);
            break;

        // Approve subscription
        case APPROVE:
            SubscriberNewsletterInfoProvider.ApproveSubscription(mSubscriberId, newsletter.NewsletterID);
            break;
        }
    }
    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 = SubscriberInfoProvider.GetSubscribersFromView(email, siteId);
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    SubscriberInfo sb = new SubscriberInfo(dr);
                    if ((sb != null) && (string.IsNullOrEmpty(sb.SubscriberType) || sb.SubscriberType.EqualsCSafe(PredefinedObjectType.CONTACT, true) || sb.SubscriberType.EqualsCSafe(SiteObjectType.USER, true)))
                    {
                        // Get newsletter information
                        NewsletterInfo news = NewsletterInfoProvider.GetNewsletterInfo(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) && (sni.SubscriptionEnabled))
                            {
                                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(ErrorText))
                {
                    result = GetString("unsubscribe.notsubscribed");
                }
                else
                {
                    result = 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(ResultText))
            {
                // Use default message if none was specified
                lblInfo.Text = GetString("unsubscribe.confirmtext");
            }
            else
            {
                lblInfo.Text = ResultText;
            }
            lblError.Visible  = false;
            txtEmail.Visible  = false;
            btnSubmit.Visible = false;
        }
    }
    /// <summary>
    /// Handles multiple selector actions.
    /// </summary>
    protected void btnOk_Clicked(object sender, EventArgs e)
    {
        // Check permissions
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.newsletter", "ManageSubscribers"))
        {
            RedirectToCMSDeskAccessDenied("cms.newsletter", "ManageSubscribers");
        }
        // Return if no action was selected
        if (drpActions.SelectedValue.EqualsCSafe(SELECT, true))
        {
            return;
        }

        // Get selected items
        List <string> list = unigridNewsletters.SelectedItems;

        if (list.Count == 0)
        {
            ltlScript.Text += ScriptHelper.GetAlertScript(GetString("general.noitems"));
            return;
        }

        int newsletterId;
        SubscriberNewsletterInfo subscriptionInfo;

        foreach (string id in list)
        {
            newsletterId = ValidationHelper.GetInteger(id, 0);

            switch (drpActions.SelectedValue)
            {
            // Subscribe
            case SUBSCRIBE:
                SubscriberInfoProvider.RenewSubscription(subscriberId, newsletterId, chkSendConfirmation.Checked);
                break;

            // Unsubscribe
            case UNSUBSCRIBE:
                SubscriberInfoProvider.Unsubscribe(subscriberId, newsletterId, chkSendConfirmation.Checked);
                break;


            // Remove subscription
            case "REMOVE":
                SubscriberInfoProvider.DeleteSubscription(subscriberId, newsletterId, chkSendConfirmation.Checked);
                break;

            // Approve subscription
            case "APPROVE":
                subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
                if ((subscriptionInfo != null) && (!subscriptionInfo.SubscriptionApproved))
                {
                    subscriptionInfo.SubscriptionApproved     = true;
                    subscriptionInfo.SubscriptionApprovedWhen = DateTime.Now;
                    SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
                }
                break;

            // Reject subscription
            case "REJECT":
                subscriptionInfo = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriberId, newsletterId);
                if ((subscriptionInfo != null) && (subscriptionInfo.SubscriptionApproved))
                {
                    subscriptionInfo.SubscriptionApproved     = false;
                    subscriptionInfo.SubscriptionApprovedWhen = DateTime.MinValue;
                    SubscriberNewsletterInfoProvider.SetSubscriberNewsletterInfo(subscriptionInfo);
                }
                break;
            }
        }

        unigridNewsletters.ResetSelection();
        unigridNewsletters.ReloadData();
    }
Example #12
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;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // If StopProcessing flag is set, do nothing
        if (StopProcessing)
        {
            Visible = false;
            return;
        }

        string subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
        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
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
                return;
            }
        }

        if (string.IsNullOrEmpty(subscriptionHash))
        {
            Visible = false;
            return;
        }

        // Try to approve subscription
        SubscriberInfoProvider.ApprovalResult result = SubscriberInfoProvider.ApproveSubscription(subscriptionHash, false, CMSContext.CurrentSiteName, datetime);

        switch (result)
        {
        // Approving subscription was successful
        case SubscriberInfoProvider.ApprovalResult.Success:
            if (!String.IsNullOrEmpty(SuccessfulApprovalText))
            {
                lblInfo.Text = SuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.successful_approval");
            }

            // Log newsletter subscription activity
            SubscriberNewsletterInfo sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriptionHash);
            if ((sni != null) && sni.SubscriptionEnabled)
            {
                NewsletterInfo news = NewsletterInfoProvider.GetNewsletterInfo(sni.NewsletterID);
                SubscriberInfo sb   = SubscriberInfoProvider.GetSubscriberInfo(sni.SubscriberID);
                if (sb != null)
                {
                    // Under what contacs this subscriber belogs to?
                    int contactId = ActivityTrackingHelper.GetContactID(sb);
                    if (contactId > 0)
                    {
                        Activity activity = new ActivityNewsletterSubscribing(sb, news, CMSContext.ActivityEnvironmentVariables);
                        if (activity.Data != null)
                        {
                            activity.Data.ContactID = contactId;
                            activity.Log();
                        }
                    }
                }
            }
            break;

        // Subscription was already approved
        case SubscriberInfoProvider.ApprovalResult.Failed:
            if (!String.IsNullOrEmpty(UnsuccessfulApprovalText))
            {
                lblInfo.Text = UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
            }
            break;

        case SubscriberInfoProvider.ApprovalResult.TimeExceeded:
            if (!String.IsNullOrEmpty(UnsuccessfulApprovalText))
            {
                lblInfo.Text = UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_timeexceeded");
            }
            break;


        // Subscription not found
        default:
        case SubscriberInfoProvider.ApprovalResult.NotFound:
            if (!String.IsNullOrEmpty(UnsuccessfulApprovalText))
            {
                lblInfo.Text = UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_invalid");
            }
            break;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // If StopProcessing flag is set, do nothing
        if (StopProcessing)
        {
            Visible = false;
            return;
        }

        string subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
        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
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
                return;
            }
        }

        if (string.IsNullOrEmpty(subscriptionHash))
        {
            this.Visible = false;
            return;
        }

        // Try to approve subscription
        SubscriberProvider.ApprovalResult result = SubscriberProvider.ApproveSubscription(subscriptionHash, false, CMSContext.CurrentSiteName, datetime);

        switch (result)
        {
        // Approving subscription was successful
        case SubscriberProvider.ApprovalResult.Success:
            if (!String.IsNullOrEmpty(this.SuccessfulApprovalText))
            {
                lblInfo.Text = this.SuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.successful_approval");
            }

            // Log newsletter subscription activity
            if ((CMSContext.ViewMode == ViewModeEnum.LiveSite))
            {
                SubscriberNewsletterInfo sni = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriptionHash);
                if (sni != null)
                {
                    // Load subscriber info and make sure activity modul is enabled
                    Subscriber sb = SubscriberProvider.GetSubscriber(sni.SubscriberID);
                    if ((sb != null) && ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(sb.SubscriberSiteID))
                    {
                        int        siteId = sb.SubscriberSiteID;
                        Newsletter news   = NewsletterProvider.GetNewsletter(sni.NewsletterID);
                        if (news.NewsletterLogActivity && ActivitySettingsHelper.NewsletterSubscribeEnabled(siteId))
                        {
                            // Under what contacs this subscriber belogs to?
                            int contactId = ActivityTrackingHelper.GetContactID(sb);

                            if (contactId > 0)
                            {
                                ModuleCommands.OnlineMarketingUpdateContactFromExternalData(sb, contactId);
                                ModuleCommands.OnlineMarketingCreateRelation(sb.SubscriberID, MembershipType.NEWSLETTER_SUBSCRIBER, contactId);
                                var data = new ActivityData()
                                {
                                    ContactID = contactId,
                                    SiteID    = sb.SubscriberSiteID,
                                    Type      = PredefinedActivityType.NEWSLETTER_SUBSCRIBING,
                                    TitleData = news.NewsletterName,
                                    ItemID    = news.NewsletterID,
                                    URL       = URLHelper.CurrentRelativePath,
                                    Campaign  = CMSContext.Campaign
                                };
                                ActivityLogProvider.LogActivity(data);
                            }
                        }
                    }
                }
            }
            break;

        // Subscription was already approved
        case SubscriberProvider.ApprovalResult.Failed:
            if (!String.IsNullOrEmpty(this.UnsuccessfulApprovalText))
            {
                lblInfo.Text = this.UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
            }
            break;

        case SubscriberProvider.ApprovalResult.TimeExceeded:
            if (!String.IsNullOrEmpty(this.UnsuccessfulApprovalText))
            {
                lblInfo.Text = this.UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_timeexceeded");
            }
            break;


        // Subscription not found
        default:
        case SubscriberProvider.ApprovalResult.NotFound:
            if (!String.IsNullOrEmpty(this.UnsuccessfulApprovalText))
            {
                lblInfo.Text = this.UnsuccessfulApprovalText;
            }
            else
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_invalid");
            }
            break;
        }
    }
Example #15
0
    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))
        {
            Subscriber subscriber = SubscriberProvider.GetSubscriber(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.Equals(SiteObjectType.ROLE, StringComparison.InvariantCultureIgnoreCase))
            {
                ShowError(GetString("Unsubscribe.CannotUnsubscribeRole"));
                return;
            }

            Newsletter newsletter = NewsletterProvider.GetNewsletter(newsletterGuid, siteId);
            if (newsletter == null)
            {
                ShowError(GetString("Unsubscribe.NewsletterDoesNotExist"));
                return;
            }

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

                if (string.IsNullOrEmpty(subscriber.SubscriberType) || !subscriber.SubscriberType.Equals(PredefinedObjectType.CONTACTGROUP, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Unsubscribe action
                    SubscriberProvider.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, issueGuid, siteId, 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)
            {
                SubscriberProvider.ApprovalResult result = SubscriberProvider.Unsubscribe(subscriptionHash, true, CMSContext.CurrentSiteName, datetime);

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

                    // Get subscriber
                    Subscriber subscriber = SubscriberProvider.GetSubscriber(sni.SubscriberID);
                    if ((subscriber != null) && !string.IsNullOrEmpty(subscriber.SubscriberType) && subscriber.SubscriberType.Equals(PredefinedObjectType.CONTACTGROUP, StringComparison.InvariantCultureIgnoreCase))
                    {
                        // 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, issueGuid, siteId, 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 SubscriberProvider.ApprovalResult.Failed:
                    ShowError(GetString("newsletter.unsubscribefailed"));
                    break;

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

                // Subscription not found
                default:
                case SubscriberProvider.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)
            {
                IssueProvider.Unsubscribe(issueID);
                return;
            }
            // Otherwise try using the Issue GUID
            if (issueGuid != Guid.Empty)
            {
                Issue issue = IssueProvider.GetIssue(issueGuid, siteId);
                if (issue == null)
                {
                    return;
                }

                IssueProvider.Unsubscribe(issue.IssueID);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // If StopProcessing flag is set, do nothing
        if (StopProcessing)
        {
            Visible = false;
            return;
        }

        string   subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
        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
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
                return;
            }
        }

        if (string.IsNullOrEmpty(subscriptionHash))
        {
            Visible = false;
            return;
        }

        var approvalService = Service <ISubscriptionApprovalService> .Entry();

        var result = approvalService.ApproveSubscription(subscriptionHash, false, SiteContext.CurrentSiteName, datetime);

        switch (result)
        {
        case ApprovalResult.Success:
            lblInfo.Text = !String.IsNullOrEmpty(SuccessfulApprovalText) ? SuccessfulApprovalText : ResHelper.GetString("newsletter.successful_approval");

            var subscription = SubscriberNewsletterInfoProvider.GetSubscriberNewsletterInfo(subscriptionHash);
            if (subscription == null)
            {
                return;
            }

            var newsletter = NewsletterInfoProvider.GetNewsletterInfo(subscription.NewsletterID);
            var subscriber = SubscriberInfoProvider.GetSubscriberInfo(subscription.SubscriberID);

            if ((newsletter == null) || (subscriber == null))
            {
                return;
            }

            LogNewsletterSubscriptionActivity(subscriber, newsletter);
            break;

        case ApprovalResult.Failed:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_failed");
            break;

        case ApprovalResult.TimeExceeded:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_timeexceeded");
            break;

        case ApprovalResult.AlreadyApproved:
            lblInfo.Text = !String.IsNullOrEmpty(SuccessfulApprovalText) ? SuccessfulApprovalText : ResHelper.GetString("newsletter.successful_approval");
            break;

        // Subscription not found
        default:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_invalid");
            break;
        }
    }