Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Episerver Component Gadget for administrating user subscriptions
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Profile;
using EPiServer.Personalization;
using EPiServer.Shell.ViewComposition;
namespace Gosso.Episerver.Plugins.Gadget
{
[IFrameComponent( Url= "SubscriptionComponent",
Categories = "dashboard",
Title = "Administrera prenumeranter till nyhetsbrev (upgraded)", MinHeight = 400)]
public class SubscriptionComponentController : Controller
{
[Authorize(Roles = "WebEditors,WebAdmins")]
public ActionResult Index()
{
return Content(GetHtmlHeader() + GetHtmlFooter());
}
[HttpPost]
[Authorize]
public JsonResult RemoveSubscriber(string profilename)
{
//replace | with @
profilename = profilename.Replace("|", "@");
EPiServerProfile profile = EPiServerProfile.Get(profilename);
if (profile != null && !string.IsNullOrEmpty(profile.UserName))
{
var allsubsriptionpages = GetSubscriptionPages();
foreach (var subscriptionpage in allsubsriptionpages)
{
if (profile.SubscriptionInfo.IsSubscribingTo(subscriptionpage.PageLink) &&
profile.SubscriptionInfo.CanSubscribeTo(subscriptionpage.PageLink))
{
profile.SubscriptionInfo.Interval = 0;
//Set property to empty string
profile.SetPropertyValue("SubscriptionStatus", false);
//Subscribe to page
profile.SubscriptionInfo.UnSubscribe(subscriptionpage.PageLink);
profile.Save();
}
}
}
var profiles = GetProfiles();
return Json(GetHtmlView(profiles));
}
[HttpPost]
[Authorize]
public JsonResult GetAll()
{
var profiles = GetProfiles();
return Json(GetHtmlView(profiles));
}
[HttpPost]
[Authorize]
public JsonResult SearchSubscriber(string profilename)
{
var profiles = GetProfiles();
List<EPiServerProfile> users;
if (!string.IsNullOrEmpty(profilename))
{
users = profiles.Where(p => p.UserName.Contains(profilename)).ToList();
}
else
{
users = profiles.ToList();
}
return Json(GetHtmlView(users));
}
/// <summary>
/// Get all EPiServerProfiles that are subscribing
/// </summary>
/// <returns></returns>
private IEnumerable<EPiServerProfile> GetProfiles()
{
var listofsubscriptionprofile = new List<EPiServerProfile>();
var allsubsriptionpages = GetSubscriptionPages();
var profiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
foreach (ProfileInfo profile in profiles)
{
var episerverprofile = EPiServerProfile.Get(profile.UserName);
foreach (var subscriptionpage in allsubsriptionpages)
{
if (episerverprofile.SubscriptionInfo.IsSubscribingTo(subscriptionpage.PageLink) && !listofsubscriptionprofile.Contains(episerverprofile))
{
listofsubscriptionprofile.Add(episerverprofile);
}
}
}
return listofsubscriptionprofile.OrderBy(p => p.UserName);
}
private string GetHtmlHeader()
{
var sb = new StringBuilder();
sb.AppendLine("<html><head><title>Subcription management iframepage</title>");
sb.AppendLine("<link href=\"/content/bootstrap/bootstrap.min.css\" rel=\"stylesheet\" />");
sb.AppendLine("<script src=\"/Scripts/nuget/jquery-1.12.4/jquery-1.12.4.js\"></script>");
sb.AppendLine("<script src=\"/Scripts/Application/subscription.js\"></script></head><body>");
sb.AppendLine("<br/><div class=\"container-fluid\">");
sb.Append("<div class=\"form-group padding-base-horizontal\">");
sb.Append("<input type=\"email\" class=\"form-control\" id=\"InputEmail1\" placeholder=\"Ange epostadress\"/>");
sb.Append("</div>");
sb.Append("<button class=\"btn btn-default\" onclick=\"searchsubscriber();\">Sök nu</button>&nbsp;");
sb.Append("<button class=\"btn btn-default\" onclick=\"getAllUsers();\">Visa alla</button>");
sb.Append("<div id=\"resultview\">");
return sb.ToString();
}
private string GetHtmlFooter()
{
var sb = new StringBuilder();
sb.Append("</div>");
sb.Append("</div></body></html>");
return sb.ToString();
}
private string GetHtmlView(IEnumerable<EPiServerProfile> listofsubscriptionprofile)
{
var sb = new StringBuilder();
sb.Append("<div class=\"highlight info-panel\">");
sb.Append("<span>Antal träffar:</span><label class=\"control-label\">" + listofsubscriptionprofile.Count().ToString() + "</label>");
sb.Append("</div>");
sb.Append("<table class=\"table table-striped\">");
sb.Append("<thead>");
sb.Append("<tr>");
sb.Append("<th>");
sb.Append("<strong>Användarnamn</strong>");
sb.Append("</th>");
sb.Append("<th>");
sb.Append("</th>");
sb.Append("</tr>");
sb.Append("</thead>");
sb.Append("<tbody>");
foreach (var episerverProfile in listofsubscriptionprofile)
{
var username = episerverProfile.UserName.Replace("@", "|");
sb.Append("<tr>");
sb.Append("<td>");
sb.Append(episerverProfile.UserName);
sb.Append("</td>");
sb.Append("<td class=\"text-right\">");
sb.Append("<button class=\"btn btn-default\" onclick=\"removesubscriber('" + username + "');\">Avsluta prenumeration</button>");
sb.Append("</td>");
sb.Append("</tr>");
}
sb.Append("</tbody>");
sb.Append("</table>");
return sb.ToString();
}
public static IEnumerable<PageData> GetSubscriptionPages()
{
var criterias = new PropertyCriteriaCollection
{
new PropertyCriteria()
{
Type = PropertyDataType.Boolean,
Value = "True",
Required = true,
Name = "EPSUBSCRIBE"
}
};
return DataFactory.Instance.FindPagesWithCriteria(ContentReference.StartPage, criterias);
}
}
}
//subscription.js from example on http://devblog.gosso.se/?p=385
function removesubscriber(profilename) {
if (!confirm("Är du säker på att du vill ta bort användare?")) {
e.preventDefault();
return false;
}
removeuser(profilename);
return true;
}
function searchsubscriber() {
var profilename = getemailvalue();
$.ajax({
url: "/SubscriptionComponent/SearchSubscriber",
type: 'POST',
async: false,
data: {
profilename: profilename
},
dataType: 'json',
success: function (data) {
$("#resultview").empty();
$("#resultview").append(data);
}
});
}
function removeuser(profilename) {
$.ajax({
url: "/SubscriptionComponent/RemoveSubscriber",
type: 'POST',
async: false,
data: {
profilename: profilename
},
dataType: 'json',
success: function(data) {
$("#resultview").empty();
$("#resultview").append(data);
}
});
}
function getAllUsers() {
$.ajax({
url: "/SubscriptionComponent/GetAll",
type: 'POST',
async: false,
data: {
},
dataType: 'json',
success: function (data) {
$("#resultview").empty();
$("#resultview").append(data);
}
});
}
function getemailvalue() {
return $("#InputEmail1").val();
}
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.