public void Subscribe_Contact_OnlyOnceIsSubscribed() { var email = "a-guid-" + Guid.NewGuid() + "@domain.com"; var contact = GetContact(email); // Subscribe contact twice mNewsletterSubscriptionService.Subscribe(contact, mNewsletter, mNewsletterSubscriptionSettings); var result = mNewsletterSubscriptionService.Subscribe(contact, mNewsletter, mNewsletterSubscriptionSettings); var subscribers = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters().TypedResult; var contacts = ContactInfoProvider.GetContacts(); CMSAssert.All( () => Assert.IsFalse(result, "No new subscription is expected."), () => Assert.AreEqual(1, subscribers.Count(), "One subscription only should be present.") ); }
private void LoadNewslettersForSubscriber() { if (subscriber == null) { return; } // Get selected newsletters DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters().WhereEquals("SubscriberID", subscriber.SubscriberID).Column("NewsletterID"); if (!DataHelper.DataSourceIsEmpty(ds)) { currentValues = TextHelper.Join(";", DataHelper.GetStringValues(ds.Tables[0], "NewsletterID")); } // Load selected newsletters if (!RequestHelper.IsPostBack() || !string.IsNullOrEmpty(DataHelper.GetNewItemsInList(SelectorValue, currentValues))) { usNewsletters.Value = currentValues; } }
private void LoadSelection(bool force) { currentValues = string.Empty; var sb = GetSubscriber(); if (sb != null) { // Get selected newsletters DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters().WhereEquals("SubscriberID", sb.SubscriberID).Column("NewsletterID"); if (!DataHelper.DataSourceIsEmpty(ds)) { currentValues = TextHelper.Join(";", DataHelper.GetStringValues(ds.Tables[0], "NewsletterID")); } if (!RequestHelper.IsPostBack() || force) { // Load selected newsletters usNewsletters.Value = currentValues; } } }
private void LoadSelection(bool force) { currentValues = string.Empty; Subscriber sb = SubscriberProvider.GetSubscriber(email, siteId); if (sb != null) { // Get selected newsletters DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters(sb.SubscriberID, null, -1, "NewsletterID"); if (!DataHelper.DataSourceIsEmpty(ds)) { currentValues = TextHelper.Join(";", SqlHelperClass.GetStringValues(ds.Tables[0], "NewsletterID")); } if (!RequestHelper.IsPostBack() || force) { // Load selected newsletters usNewsletters.Value = currentValues; } } }
/// <summary> /// Load data. /// </summary> public void LoadData() { if (this.StopProcessing) { // Hide control this.Visible = false; } else { this.SetContext(); // Get specified user if used instead of current user UserInfo ui = null; if (this.UserID > 0) { ui = UserInfoProvider.GetUserInfo(this.UserID); } else { ui = CMSContext.CurrentUser; } // Get specified site ID instead of current site ID int siteId = 0; if (this.SiteID > 0) { siteId = this.SiteID; } else { siteId = CMSContext.CurrentSiteID; } usNewsletters.WhereCondition = "NewsletterSiteID = " + siteId; usNewsletters.ButtonRemoveSelected.CssClass = "XLongButton"; usNewsletters.ButtonAddItems.CssClass = "XLongButton"; usNewsletters.OnSelectionChanged += new EventHandler(usNewsletters_OnSelectionChanged); usNewsletters.IsLiveSite = this.IsLiveSite; this.userIsIdentified = (ui != null) && (!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName)); if (this.userIsIdentified) { usNewsletters.Visible = true; // Try to get subsriber info with specified e-mail sb = SubscriberProvider.GetSubscriber(ui.Email, siteId); if (sb == null) { // Try to get subscriber info according to user info sb = SubscriberProvider.GetSubscriber(SiteObjectType.USER, ui.UserID, siteId); } // Get user e-mail address if (sb != null) { subscriberEmail = sb.SubscriberEmail; // Get selected newsletters DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters(sb.SubscriberID, null, -1, "NewsletterID"); if (!DataHelper.DataSourceIsEmpty(ds)) { currentValues = TextHelper.Join(";", SqlHelperClass.GetStringValues(ds.Tables[0], "NewsletterID")); } // Load selected newsletters if (!string.IsNullOrEmpty(currentValues)) { usNewsletters.Value = currentValues; } } // Try to get email address from user data if (string.IsNullOrEmpty(subscriberEmail)) { if (ValidationHelper.IsEmail(ui.Email)) { subscriberEmail = ui.Email; } else if (ValidationHelper.IsEmail(ui.UserName)) { subscriberEmail = ui.UserName; } } } else { usNewsletters.Visible = false; lblInfo.Visible = true; if ((this.UserID > 0) && (CMSContext.CurrentUser.UserID == this.UserID)) { lblInfo.Text = GetString("MySubscriptions.CannotIdentify"); } else { lblInfo.Text = GetString("MySubscriptions.CannotIdentifyUser"); } } this.ReleaseContext(); } }
private void usNewsletters_OnSelectionChanged(object sender, EventArgs e) { if (RaiseOnCheckPermissions("ManageSubscribers", this)) { if (StopProcessing) { return; } } // Get specified site ID instead of current site ID int siteId = 0; if (SiteID > 0) { siteId = SiteID; } else { siteId = SiteContext.CurrentSiteID; } if ((subscriber == null) && (userInfo != null)) { // Create new subsciber (bind to existing user account) if ((!userInfo.IsPublic()) && (ValidationHelper.IsEmail(userInfo.Email) || ValidationHelper.IsEmail(userInfo.UserName))) { subscriber = new SubscriberInfo(); if (userInfo != null) { if (!string.IsNullOrEmpty(userInfo.FirstName) && !string.IsNullOrEmpty(userInfo.LastName)) { subscriber.SubscriberFirstName = userInfo.FirstName; subscriber.SubscriberLastName = userInfo.LastName; } else { subscriber.SubscriberFirstName = userInfo.FullName; } // Full name consists of "user " and user full name subscriber.SubscriberFullName = new SubscriberFullNameFormater().GetUserSubscriberName(userInfo.FullName); } subscriber.SubscriberSiteID = siteId; subscriber.SubscriberType = UserInfo.OBJECT_TYPE; subscriber.SubscriberRelatedID = userInfo.UserID; // Save subscriber to DB SubscriberInfoProvider.SetSubscriberInfo(subscriber); } } if (subscriber == null) { return; } // Create membership between current contact and subscriber ModuleCommands.OnlineMarketingCreateRelation(subscriber.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); var ids = newItems.Select(item => ValidationHelper.GetInteger(item, 0)).ToArray(); var subscriptions = SubscriberNewsletterInfoProvider .GetSubscriberNewsletters() .WhereEquals("SubscriberID", subscriber.SubscriberID) .WhereIn("NewsletterID", ids); foreach (var subscription in subscriptions) { subscription.Delete(); LogActivity(userInfo, 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); try { // If subscriber is not subscribed, subscribe him if (!mSubscriptionService.IsSubscribed(subscriber.SubscriberID, newsletterId)) { mSubscriptionService.Subscribe(subscriber.SubscriberID, newsletterId, new SubscribeSettings() { SendConfirmationEmail = SendConfirmationEmail, RequireOptIn = true, RemoveAlsoUnsubscriptionFromAllNewsletters = true, }); // Log activity LogActivity(userInfo, newsletterId, true); } } catch { // 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(); }
/// <summary> /// Load data. /// </summary> public void LoadData() { if (StopProcessing) { // Hide control Visible = false; } else { SetContext(); // Get specified user if used instead of current user if (UserID > 0) { userInfo = UserInfoProvider.GetUserInfo(UserID); } else { userInfo = MembershipContext.AuthenticatedUser; } // Get specified site ID instead of current site ID int siteId = 0; if (SiteID > 0) { siteId = SiteID; } else { siteId = SiteContext.CurrentSiteID; } usNewsletters.WhereCondition = "NewsletterSiteID = " + siteId; usNewsletters.OnSelectionChanged += new EventHandler(usNewsletters_OnSelectionChanged); usNewsletters.IsLiveSite = IsLiveSite; userIsIdentified = (userInfo != null) && (!userInfo.IsPublic()) && (ValidationHelper.IsEmail(userInfo.Email) || ValidationHelper.IsEmail(userInfo.UserName)); if (userIsIdentified) { usNewsletters.Visible = true; // Try to get subscriber info with specified e-mail subscriber = SubscriberInfoProvider.GetSubscriberInfo(userInfo.Email, siteId); if (subscriber == null) { // Try to get subscriber info according to user info subscriber = SubscriberInfoProvider.GetSubscriberInfo(UserInfo.OBJECT_TYPE, userInfo.UserID, siteId); } // Get user e-mail address if (subscriber != null) { subscriberEmail = subscriber.SubscriberEmail; // Get selected newsletters DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters().WhereEquals("SubscriberID", subscriber.SubscriberID).Column("NewsletterID"); if (!DataHelper.DataSourceIsEmpty(ds)) { currentValues = TextHelper.Join(";", DataHelper.GetStringValues(ds.Tables[0], "NewsletterID")); } // Load selected newsletters if (!RequestHelper.IsPostBack() || !string.IsNullOrEmpty(DataHelper.GetNewItemsInList(SelectorValue, currentValues))) { usNewsletters.Value = currentValues; } } // Try to get email address from user data if (string.IsNullOrEmpty(subscriberEmail)) { if (ValidationHelper.IsEmail(userInfo.Email)) { subscriberEmail = userInfo.Email; } else if (ValidationHelper.IsEmail(userInfo.UserName)) { subscriberEmail = userInfo.UserName; } } } 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(); } }