private List <Subscriber> GetSubscribers() { NewslettersManager manager = NewslettersManager.GetManager(); var subscribers = manager.GetSubscribers().ToList(); return(subscribers); }
/// <inheritDoc/> public virtual void ExecuteAction(string subscriberId, string issueId, bool shouldSubscribe) { var newslettersManager = NewslettersManager.GetManager(this.ProviderName); var issueGuid = Guid.Empty; var subscriberGuid = Guid.Empty; if (!string.IsNullOrEmpty(subscriberId) && Guid.TryParse(subscriberId, out subscriberGuid) && (!string.IsNullOrEmpty(issueId) && Guid.TryParse(issueId, out issueGuid))) { Guid mailingListId = Guid.Empty; var issue = newslettersManager.GetIssue(issueGuid); if (issue != null) { mailingListId = issue.List.Id; } var subscriber = newslettersManager.GetSubscriber(subscriberGuid); if (shouldSubscribe) { this.Subscribe(newslettersManager, subscriber, mailingListId); } else { this.Unsubscribe(newslettersManager, subscriber, mailingListId, issue); } } }
protected void ChangeCustomField_Click(object sender, EventArgs e) { NewslettersManager manager = NewslettersManager.GetManager(); Subscriber subscriber = manager.GetSubscriber(new Guid(SubscriberList.SelectedValue)); DataExtensions.SetValue((IDynamicFieldsContainer)subscriber, "Company", ComboBoxValueField.Text); SubscriberField.Text = String.Format("User {0} now works for Company {1}", SubscriberList.SelectedItem.Text, DataExtensions.GetValue <String>(subscriber, "Company")); }
/// <inheritdoc /> public virtual bool AddSubscriber(SubscribeFormViewModel viewModel, out string error) { error = string.Empty; if (NewsletterValidator.IsValidEmail(viewModel.Email)) { var newslettersManager = NewslettersManager.GetManager(this.Provider); // check if subscriber exists var email = viewModel.Email.ToLower(); IQueryable <Subscriber> matchingSubscribers = newslettersManager.GetSubscribers().Where(s => s.Email == email); bool subscriberAlreadyInList = false; foreach (Subscriber s in matchingSubscribers) { if (s.Lists.Any(ml => ml.Id == this.SelectedMailingListId)) { subscriberAlreadyInList = true; break; } } if (subscriberAlreadyInList) { // If the email has already been subscribed, consider it as success. return(true); } else { Subscriber subscriber = matchingSubscribers.FirstOrDefault(); if (subscriber == null) { subscriber = newslettersManager.CreateSubscriber(true); subscriber.Email = viewModel.Email; subscriber.FirstName = viewModel.FirstName != null ? viewModel.FirstName : string.Empty; subscriber.LastName = viewModel.LastName != null ? viewModel.LastName : string.Empty; } // check if the mailing list exists if (newslettersManager.Subscribe(subscriber, this.SelectedMailingListId)) { if (this.SuccessfullySubmittedForm == SuccessfullySubmittedForm.OpenSpecificPage) { viewModel.RedirectPageUrl = HyperLinkHelpers.GetFullPageUrl(this.PageId); } newslettersManager.SaveChanges(); return(true); } } } error = Res.Get <SubscribeFormResources>().EmailAddressErrorMessageResourceName; return(false); }
/// <summary> /// Handles the notifications events sent by SendGrid. /// </summary> /// <param name="events">An array of the events that have occurred.</param> public void Post(SendGridEvent[] events) { if (this.IsToRequireAuthentication()) { ServiceUtility.RequestBackendUserAuthentication(); } // TODO: provider name should be configurable // TODO: get the manager only if an event that we are interested in is present in the events variable. // Could be done with a property but I am not aware of the this object lifecycle at the moment. // TODO: Ideally the whole data related operations like getting subscribers, campaigns and writing statistics // should be in a separate component and not in the service itself. var newslettersManager = NewslettersManager.GetManager(); var isToSaveChanges = false; // TODO: Use dynamic objects and inspect them for the required properties instead of // this strongly typed approach since the different events have very different properties // and this hook should be able to handle all and not throw exceptions if an event that is // not supported is received. foreach (var sendGridEvent in events) { // TODO: rewrite this with chain of responsibility so that additional event handlers can be easily added. if (this.IsToHanlde(sendGridEvent)) { BounceStatus bounceStatus = this.ResolveStatus(sendGridEvent); Campaign campaign; Subscriber subscriber; try { campaign = newslettersManager.GetCampaign(sendGridEvent.SitefinityCampaignId); subscriber = newslettersManager.GetSubscriber(sendGridEvent.SitefinitySubscriberId); } catch (Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException) { // If the campaign or the subscriber is not found we skip to the next event. break; } this.WriteStatistics(newslettersManager, campaign, subscriber, bounceStatus); this.PerformBounceAction(newslettersManager, subscriber, bounceStatus); isToSaveChanges = true; } } if (isToSaveChanges) { newslettersManager.SaveChanges(); } this.Response.StatusCode = (int)HttpStatusCode.OK; }
/// <summary> /// Gets the merged tags. /// </summary> /// <returns></returns> public static IEnumerable <MergeTag> GetMergedTags() { var newslettersManager = NewslettersManager.GetManager(); foreach (var mergeTag in newslettersManager.GetMergeTags()) { if (mergeTag.PropertyName == "UnsubscribeLink") { continue; } yield return(mergeTag); } yield return(new MergeTag("Subscribe link", "SubscribeLink", "MergeContextItems")); }
/// <inheritDoc/> public virtual bool Unsubscribe(UnsubscribeFormViewModel viewModel, out string error) { error = string.Empty; var newslettersManager = NewslettersManager.GetManager(this.ProviderName); string email = viewModel.Email.ToLower(); IQueryable <Subscriber> subscribers = newslettersManager.GetSubscribers().Where(s => s.Email == email); if (subscribers.Count() == 0) { error = string.Format(Res.Get <UnsubscribeFormResources>().YouDontBelongToTheMailingList, email); return(false); } var hasUnsubscribedUser = false; foreach (Subscriber subscriber in subscribers) { if (subscriber != null) { var isUnsubscribed = newslettersManager.Unsubscribe(subscriber, this.ListId); hasUnsubscribedUser = hasUnsubscribedUser || isUnsubscribed; } } if (hasUnsubscribedUser) { newslettersManager.SaveChanges(); if (this.SuccessfullySubmittedForm == SuccessfullySubmittedForm.OpenSpecificPage) { viewModel.RedirectPageUrl = HyperLinkHelpers.GetFullPageUrl(this.PageId); } this.Message = string.Format(Res.Get <UnsubscribeFormResources>().UnsubscribedFromMailingListSuccessMessage, email); return(true); } else { error = string.Format(Res.Get <UnsubscribeFormResources>().YouDontBelongToTheMailingList, email); return(false); } }
/// <inheritDoc/> public virtual void ExecuteAction(string subscriberId, string issueId, string listId, bool shouldSubscribe) { var newslettersManager = NewslettersManager.GetManager(this.ProviderName); var issueGuid = Guid.Empty; var subscriberGuid = Guid.Empty; var mailingListGuid = Guid.Empty; if (Guid.TryParse(subscriberId, out subscriberGuid)) { Guid mailingListId = Guid.Empty; Campaign issue = null; if (Guid.TryParse(listId, out mailingListGuid) && mailingListGuid != Guid.Empty) { mailingListId = mailingListGuid; } else if (Guid.TryParse(issueId, out issueGuid) && issueGuid != Guid.Empty) { issue = newslettersManager.GetIssues().FirstOrDefault(i => i.Id == issueGuid); if (issue != null) { mailingListId = issue.List.Id; } } var subscriber = newslettersManager.GetSubscriber(subscriberGuid); if (shouldSubscribe) { this.Subscribe(newslettersManager, subscriber, mailingListId); } else { this.Unsubscribe(newslettersManager, subscriber, mailingListId, issue); } } }
public void SubscribeForm_CheckForParamInjections() { this.pageOperations = new PagesOperations(); this.newslettersManager = NewslettersManager.GetManager(); Guid mailingListId = Guid.NewGuid(); var subscribers = this.newslettersManager.GetSubscribers().Where(s => s.Email == this.testEmail).ToList(); bool deleteSubsciber = (subscribers.Count > 0) ? false : true; try { ////Create a new mailing list string mailingListName = this.mailingListBaseName + this.mailingListIndex; SitefinityTestUtilities.ServerOperations.NewsLetter().CreateMailingList(mailingListId, mailingListName, string.Empty, string.Empty, string.Empty); ////Check if the mailing list was created successfully MailingList mailingList = this.newslettersManager.GetMailingLists().Where(l => l.Id == mailingListId).SingleOrDefault(); Assert.IsNotNull(mailingList, "New mailing list was not created successfully!"); ////Create simple page with a SubscribeForm widget that has SelectedMailingListId with the newly created mailing list var mvcControllerProxy = new MvcControllerProxy(); mvcControllerProxy.ControllerName = typeof(SubscribeFormController).FullName; var subscribeFormController = new SubscribeFormController(); subscribeFormController.Model.SelectedMailingListId = mailingListId; subscribeFormController.Model.SuccessfullySubmittedForm = SuccessfullySubmittedForm.OpenSpecificPage; mvcControllerProxy.Settings = new ControllerSettings(subscribeFormController); this.pageOperations.CreatePageWithControl( mvcControllerProxy, this.pageNamePrefix, this.pageTitlePrefix, this.urlNamePrefix, this.pageIndex); ////Create simple page with a content block to redirect on it mvcControllerProxy.ControllerName = typeof(ContentBlockController).FullName; var contentBlockController = new ContentBlockController(); contentBlockController.Content = this.searchValueText; mvcControllerProxy.Settings = new ControllerSettings(contentBlockController); this.pageOperations.CreatePageWithControl( mvcControllerProxy, this.pageNamePrefixContentBlockPage, this.pageTitlePrefixContentBlockPage, this.urlNamePrefixContentBlockPage, this.pageIndexContentBlockPage); string subscribeFormPageUrl = UrlPath.ResolveAbsoluteUrl("~/" + this.urlNamePrefix + this.pageIndex); string redirectUrl = UrlPath.ResolveAbsoluteUrl("~/" + this.urlNamePrefixContentBlockPage + this.pageIndexContentBlockPage); string postString = "Email=" + this.testEmail; ////Make an initial request to register the subscriber var responseContent = PageInvoker.PostWebRequest(subscribeFormPageUrl, postString, false); Assert.IsTrue(responseContent.Contains(this.subscribeValueText), "User was not successfully subscribed!"); ////Make a secondary request to inject the RedirectPageUrl value postString += "&RedirectPageUrl=" + redirectUrl; responseContent = PageInvoker.PostWebRequest(subscribeFormPageUrl, postString, false); Assert.IsFalse(responseContent.Contains(this.searchValueText), "RedirectPageUrl parameter was injected into the model!"); } finally { ////Delete created pages this.pageOperations.DeletePages(); ////Delete the created subsciber if he was created by the test if (deleteSubsciber) { Subscriber subscriber = this.newslettersManager.GetSubscribers().Where(s => s.Email == this.testEmail).FirstOrDefault(); if (subscriber != null) { this.newslettersManager.DeleteSubscriber(subscriber.Id); } } ////Delete created mailing list this.newslettersManager.DeleteMailingList(mailingListId); this.newslettersManager.SaveChanges(); } }
/// <inheritdoc /> public virtual bool AddSubscriber(SubscribeFormViewModel viewModelOld, out string error) { var agreed = SystemManager.CurrentHttpContext.Request.Params["TermsAgreement"].Split(',').Count() > 1; var viewModel = new SubscribeFormViewModelCustom() { FirstName = viewModelOld.FirstName, LastName = viewModelOld.LastName, Email = viewModelOld.Email, CssClass = viewModelOld.CssClass, RedirectPageUrl = viewModelOld.RedirectPageUrl, // custom - getting the property values from the request TermsAgreement = agreed }; error = string.Empty; if (viewModel.TermsAgreement) { if (NewsletterValidator.IsValidEmail(viewModel.Email)) { var newslettersManager = NewslettersManager.GetManager(this.Provider); // check if subscriber exists var email = viewModel.Email.ToLower(); IQueryable <Subscriber> matchingSubscribers = newslettersManager.GetSubscribers().Where(s => s.Email == email); bool subscriberAlreadyInList = false; foreach (Subscriber s in matchingSubscribers) { if (s.Lists.Any(ml => ml.Id == this.SelectedMailingListId)) { subscriberAlreadyInList = true; break; } } if (subscriberAlreadyInList) { Subscriber subscriber = matchingSubscribers.FirstOrDefault(); if (subscriber != null) { //update their name subscriber.FirstName = viewModel.FirstName; subscriber.LastName = viewModel.LastName; //update their terms acceptance subscriber.SetValue("TermsAccepted", viewModel.TermsAgreement); newslettersManager.SaveChanges(); } // If the email has already been subscribed, consider it as success. return(true); } else { Subscriber subscriber = matchingSubscribers.FirstOrDefault(); if (subscriber == null) { subscriber = newslettersManager.CreateSubscriber(true); subscriber.Email = viewModel.Email; subscriber.FirstName = viewModel.FirstName ?? string.Empty; subscriber.LastName = viewModel.LastName ?? string.Empty; // custom - setting the property values subscriber.SetValue("TermsAccepted", viewModel.TermsAgreement); } else { subscriber.FirstName = viewModel.FirstName ?? string.Empty; subscriber.LastName = viewModel.LastName ?? string.Empty; //update acceptance of terms subscriber.SetValue("TermsAccepted", viewModel.TermsAgreement); } // check if the mailing list exists if (newslettersManager.Subscribe(subscriber, this.SelectedMailingListId)) { if (this.SuccessfullySubmittedForm == SuccessfullySubmittedForm.OpenSpecificPage) { viewModel.RedirectPageUrl = HyperLinkHelpers.GetFullPageUrl(this.PageId); } newslettersManager.SaveChanges(); return(true); } } } error = Res.Get <SubscribeFormResources>().EmailAddressErrorMessageResourceName; } error = "You must agree to the terms"; return(false); }