Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Source for the new members subscription provider for Newsletter Studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using NewsletterStudio.Bll.Providers.Model;
using NewsletterStudio.Model;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
using umbraco.BusinessLogic;
using Member = umbraco.cms.businesslogic.member.Member;
namespace NewsletterStudio.Bll.Providers
{
public class UmbracoNewSubscriptionProvider : SubscriptionProviderBase
{
public override string UniqueName { get { return "UmbracoNewSubscriptionProvider"; } }
public override string DisplayName { get { return "Umbraco members"; }}
private IMemberGroupService _memberGroupService;
private IMemberService _memberService;
public UmbracoNewSubscriptionProvider()
{
_memberGroupService = UmbracoContext.Current.Application.Services.MemberGroupService;
_memberService = UmbracoContext.Current.Application.Services.MemberService;
}
public override IEnumerable<ListItem> GetListItems()
{
var listItems = new List<ListItem>();
foreach (var group in _memberGroupService.GetAll())
{
ListItem item = new ListItem
{
Text = group.Name,
Value = group.Name
};
listItems.Add(item);
}
return listItems;
}
public override List<Receiver> GetSubscribersForSendOut(string listItemValue, SendOutParams parameters)
{
bool useCustomData = ConfigManager.NewsletterStudio.ActivateCustomDataCollection;
var receivers = new List<Receiver>();
var members = _memberService.GetMembersInRole(listItemValue);
foreach (var member in members)
{
if (ShouldHaveNewsletter(member))
{
// Validate email and username
if (!String.IsNullOrEmpty(member.Email) && !String.IsNullOrEmpty(member.Name))
{
var receiver = new Receiver()
{
Fullname = member.Name,
Email = member.Email,
DataProviderKey = member.Id.ToString(),
};
if(useCustomData)
{
var data = new Dictionary<string, object>();
data.Add("email", member.Email);
data.Add("username", member.Username);
data.Add("loginname", member.Username); // was called login name in older versions
data.Add("login", member.Username); // UI calles it login
data.Add("name", member.Name); // UI calles it login
foreach (var property in member.PropertyTypes)
{
if(!data.ContainsKey(property.Alias))
data.Add(property.Alias, member.GetValue(property.Alias));
}
receiver.Data = data;
}
receivers.Add(receiver);
}
}
}
return receivers;
}
private bool ShouldHaveNewsletter(IMember member)
{
if (member == null)
return false;
if (!member.HasProperty("newsletter"))
return true;
return member.GetValue<bool>("newsletter");
}
public override bool Unsubscribe(string email)
{
// Get user
var member = _memberService.GetByEmail(email);
if (member != null)
{
// If we're using the newsletter-propery set it to false / 0;
if (member.HasProperty("newsletter"))
{
member.SetValue("newsletter", false);
_memberService.Save(member);
return true;
}
}
return false;
}
public override bool Unsubscribe(string email, string listItemId)
{
return Unsubscribe(email);
}
public override bool CanEditSubsciber
{
get { return false; }
}
public override string GetEditUrl(string subscriptionId, string email)
{
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.