コード例 #1
0
    /// <summary>
    /// Saves contact.
    /// </summary>
    private ContactInfo SaveContact()
    {
        ContactInfo contact = null;

        // Handle authenticated user when user subscription is allowed
        if (AllowUserSubscribersIsAuthenticated)
        {
            CurrentUserInfo currentUser = MembershipContext.AuthenticatedUser;
            contact = mContactProvider.GetContactForSubscribing(currentUser);
        }
        // Work with non-authenticated user or authenticated user when user subscription is disabled
        else if (SaveDataForm())
        {
            if (String.IsNullOrWhiteSpace(subscriber.SubscriberEmail))
            {
                lblError.Visible = true;
                lblError.Text    = GetString("NewsletterSubscription.ErrorInvalidEmail");
                return(null);
            }

            var firstName = TextHelper.LimitLength(subscriber.SubscriberFirstName, 100);
            var lastName  = TextHelper.LimitLength(subscriber.SubscriberLastName, 100);

            contact = mContactProvider.GetContactForSubscribing(subscriber.SubscriberEmail, firstName, lastName);
        }

        return(contact);
    }
コード例 #2
0
        public ActionResult Subscribe(SubscribeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(PartialView("_Subscribe", model));
            }

            var newsletter = NewsletterInfoProvider.GetNewsletterInfo("DancingGoatMvcNewsletter", SiteContext.CurrentSiteID);
            var contact    = mContactProvider.GetContactForSubscribing(model.Email);

            string resultMessage;

            if (!mSubscriptionService.IsMarketable(contact, newsletter))
            {
                mSubscriptionService.Subscribe(contact, newsletter, NewsletterSubscriptionSettings);

                // The subscription service is configured to use double opt-in, but newsletter must allow for it
                resultMessage = ResHelper.GetString(newsletter.NewsletterEnableOptIn ? "DancingGoatMvc.News.ConfirmationSent" : "DancingGoatMvc.News.Subscribed");
            }
            else
            {
                resultMessage = ResHelper.GetString("DancingGoatMvc.News.AlreadySubscribed");
            }

            return(Content(resultMessage));
        }
コード例 #3
0
        public ActionResult Subscribe(NewsletterSubscriptionSubscribeModel model)
        {
            if (!ValidationHelper.IsEmail(model.Email))
            {
                ModelState.AddModelError("Email", "Invalid email.");

                return(PartialView("~/Components/Widgets/NewsletterSubscriptionWidget/_Subscribe.cshtml", model));
            }

            var properties = componentPropertiesRetriever.Retrieve <NewsletterSubscriptionProperties>();
            var newsletter = newsletterInfoProvider.Get(properties.Newsletter, SiteContext.CurrentSiteID);
            var contact    = contactProvider.GetContactForSubscribing(model.Email);

            string resultMessage;

            if (!subscriptionService.IsMarketable(contact, newsletter))
            {
                subscriptionService.Subscribe(contact, newsletter, NewsletterSubscriptionSettings);

                // The subscription service is configured to use double opt-in, but newsletter must allow for it
                resultMessage = localizer[newsletter.NewsletterEnableOptIn ?
                                          "A confirmation link has been sent to your email." :
                                          "You have been subscribed."];
            }
            else
            {
                resultMessage = localizer["You are already subscribed."];
            }

            return(Content(resultMessage));
        }
コード例 #4
0
    /// <summary>
    /// Load data.
    /// </summary>
    public void LoadData()
    {
        if (StopProcessing)
        {
            // Hide control
            Visible = false;
        }
        else
        {
            SetContext();

            InitializeUser();

            usNewsletters.WhereCondition = new WhereCondition().WhereEquals("NewsletterSiteID", SiteID).WhereEquals("NewsletterType", (int)EmailCommunicationTypeEnum.Newsletter).ToString(true);

            if (IsUserIdentified())
            {
                usNewsletters.Visible = true;

                var contact = mContactProvider.GetContactForSubscribing(userInfo);
                InitializeSubscriber(contact, SiteID);

                LoadNewslettersForUser(contact);
            }
            else
            {
                usNewsletters.Visible = false;

                if ((UserID > 0) && (MembershipContext.AuthenticatedUser.UserID == UserID))
                {
                    ShowInformation(GetString("MySubscriptions.CannotIdentify"));
                }
                else
                {
                    if (!IsLiveSite)
                    {
                        // It's located in Admin/Users/Subscriptions
                        lblText.ResourceString = "MySubscriptions.EmailCommunicationDisabled";
                    }
                    else
                    {
                        ShowInformation(GetString("MySubscriptions.CannotIdentifyUser"));
                    }
                }
            }

            ReleaseContext();
        }
    }
コード例 #5
0
        public ActionResult Subscribe(NewsletterSubscribeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // If the subscription data is not valid, displays the subscription form with error messages
                return(View(model));
            }

            // Gets the default Kentico contact provider
            IContactProvider contactProvider = Service <IContactProvider> .Entry();

            // Either gets an existing contact by email or creates a new contact object with the given email
            ContactInfo contact = contactProvider.GetContactForSubscribing(model.Email);

            // Gets a newsletter
            // Fill in the code name of your newsletter object in Kentico
            NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("SampleNewsletter", SiteContext.CurrentSiteID);

            // Prepares settings that configure the subscription behavior
            var subscriptionSettings = new NewsletterSubscriptionSettings()
            {
                RemoveAlsoUnsubscriptionFromAllNewsletters = true, // Subscription removes the given email address from the global opt-out list for all marketing emails (if present)
                SendConfirmationEmail = true,                      // Allows sending of confirmation emails for the subscription
                AllowOptIn            = true                       // Allows handling of double opt-in subscription for newsletters that have it enabled
            };

            // Subscribes the contact with the specified email address to the given newsletter
            SubscriptionService.Subscribe(contact, newsletter, subscriptionSettings);

            // Passes information to the view, indicating whether the newsletter requires double opt-in for subscription
            model.RequireDoubleOptIn = newsletter.NewsletterEnableOptIn;

            // Displays a view to inform the user that the subscription was successful
            return(View("SubscribeSuccess", model));
        }
コード例 #6
0
        public ActionResult Register(RegistrationModel model)
        {
            if (!ValidationHelper.IsEmail(model.Email))
            {
                ModelState.AddModelError("Email", "Invalid email.");

                return(PartialView("~/Components/Widgets/EventRegistrationFormWidget/_EventRegistrationForm.cshtml", model));
            }

            string result  = "";
            var    page    = pageDataContext.Retrieve <Event>().Page;
            var    contact = contactProvider.GetContactForSubscribing(model.Email);

            if (contact != null)
            {
                // Update contact info
                contact.ContactFirstName = model.FirstName;
                contact.ContactLastName  = model.LastName;
                contact.Update();

                // Check if contact is already registered
                var existingAttendee = EventAttendeeInfoProvider.ProviderObject.Get()
                                       .WhereEquals("ContactID", contact.ContactID)
                                       .WhereEquals("NodeID", page.NodeID)
                                       .TopN(1)
                                       .FirstOrDefault();

                if (existingAttendee != null)
                {
                    result = "You are already registered for this event!";
                }
                else
                {
                    // Successful registration
                    EventAttendeeInfoProvider.ProviderObject.Set(new EventAttendeeInfo()
                    {
                        ContactID = contact.ContactID,
                        NodeID    = page.NodeID,
                        EventAttendeeRegisteredOn = DateTime.Now
                    });

                    if (model.SubmitAction.Equals("text"))
                    {
                        result = model.ActionRelatedData;
                    }
                    else
                    {
                        return(Redirect(model.ActionRelatedData));
                    }
                }
            }
            else
            {
                result = "Unable to register for event: contact not found.";
            }

            return(Content(result));
        }
    /// <summary>
    /// Saves contact.
    /// </summary>
    /// <returns>Contact info object</returns>
    private ContactInfo SaveContact()
    {
        ContactInfo     contact;
        CurrentUserInfo currentUser = MembershipContext.AuthenticatedUser;

        // Handle authenticated user when user subscription is allowed
        if (AllowUserSubscribers && AuthenticationHelper.IsAuthenticated() && (currentUser != null))
        {
            contact = mContactProvider.GetContactForSubscribing(currentUser);
        }
        // Work with non-authenticated user or authenticated user when user subscription is disabled
        else
        {
            var firstName = TextHelper.LimitLength(txtFirstName.Text.Trim(), 100);
            var lastName  = TextHelper.LimitLength(txtLastName.Text.Trim(), 100);
            contact = mContactProvider.GetContactForSubscribing(txtEmail.Text.Trim(), firstName, lastName);
        }
        return(contact);
    }
    private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
    {
        if (RaiseOnCheckPermissions("ManageSubscribers", this))
        {
            if (StopProcessing)
            {
                return;
            }
        }

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

        if (subscriber != null)
        {
            if (!String.IsNullOrEmpty(items))
            {
                string[] newItems = items.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                var newsletterIds = newItems.Select(item => ValidationHelper.GetInteger(item, 0)).ToArray();

                foreach (var newsletterId in newsletterIds)
                {
                    mSubscriptionService.RemoveSubscription(subscriber.SubscriberID, newsletterId, SendConfirmationEmail);
                    LogUnsubscriptionActivity(newsletterId);
                }
            }
        }

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

                try
                {
                    var contact    = mContactProvider.GetContactForSubscribing(userInfo);
                    var newsletter = NewsletterInfoProvider.GetNewsletterInfo(newsletterId);
                    mSubscriptionService.Subscribe(contact, newsletter, new SubscribeSettings()
                    {
                        SendConfirmationEmail = SendConfirmationEmail,
                        AllowOptIn            = true,
                        RemoveUnsubscriptionFromNewsletter         = true,
                        RemoveAlsoUnsubscriptionFromAllNewsletters = true,
                    });
                }
                catch (Exception ex)
                {
                    EventLogProvider.LogException(ex.Source, "SUBSCRIBE", ex, SiteID);
                    // Can occur e.g. when newsletter is deleted while the user is selecting it for subscription.
                    // This is rare scenario, the main purpose of this catch is to avoid YSOD on the live site.
                }
            }
        }

        // Display information about successful (un)subscription
        ShowChangesSaved();
    }
コード例 #9
0
        private static ContactInfo GetContact(string email)
        {
            IContactProvider contactProvider = Service.Resolve <IContactProvider>();

            return(contactProvider.GetContactForSubscribing(email));
        }