Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Tree Controller for Newsletter Studio (Newsletter)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web;
using NewsletterStudio.Core.Extensions;
using NewsletterStudio.Core.Interfaces.Data;
using NewsletterStudio.Core.Model;
using NewsletterStudio.Infrastucture;
using NewsletterStudio.Infrastucture.Services;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees;
using umbraco.BusinessLogic.Actions;
namespace NewsletterStudio.Umbraco
{
[PluginController(NewsletterStudioApplication.Name)]
[Tree("NewsletterStudio", "Newsletter", "Newsletters", "icon-message", "icon-message", sortOrder: 5)]
public class NewsletterTreeController : TreeController
{
private INewsletterRepository _newsletterRepository;
private LocalizationService _localization;
public NewsletterTreeController()
{
_newsletterRepository = GlobalFactory.Current.NewsletterRepository;
_localization = new LocalizationService();
}
protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
{
var node = base.CreateRootNode(queryStrings);
node.Name = _localization.Get("ns_newsletters");
return node;
}
protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
var nodes = new TreeNodeCollection();
if(id == "-1")
{
var nodeDraft = this.CreateTreeNode("draft", id, queryStrings, _localization.Get("ns_draft"), "icon-outbox", true);
nodeDraft.RoutePath = "NewsletterStudio";
nodes.Add(nodeDraft);
var nodeSent = this.CreateTreeNode("sent", id, queryStrings, _localization.Get("ns_sent"), "icon-mailbox", true);
nodeSent.RoutePath = "NewsletterStudio";
nodes.Add(nodeSent);
var nodeArchive = this.CreateTreeNode("archive", id, queryStrings, _localization.Get("ns_archive"), "icon-file-cabinet", true);
nodeArchive.RoutePath = "NewsletterStudio";
nodes.Add(nodeArchive);
}
else
{
IList<Newsletter> letterList;
switch (id)
{
case "draft":
letterList = _newsletterRepository.GetDraft();
break;
case "sent":
letterList = _newsletterRepository.GetSent();
break;
case "archive":
letterList = _newsletterRepository.GetArchived();
break;
default:
letterList = new List<Newsletter>();
break;
}
foreach (var newsletter in letterList)
{
var item = this.CreateTreeNode(newsletter.Id.ToString(), id, queryStrings, newsletter.Name, "icon-message", false);
if (newsletter.Status == NewsletterStatus.Completed)
{
item.Icon = "icon-pie-chart";
item.RoutePath = "NewsletterStudio/Newsletter/analytics/" + newsletter.Id;
}
else
{
if (newsletter.ScheduledSendDate.HasValue)
item.CssClasses.Add("ns-is-scheduled");
}
if(newsletter.Status == NewsletterStatus.Error)
item.CssClasses.Add("ns-has-error");
nodes.Add(item);
}
}
return nodes;
}
protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
{
var menu = new MenuItemCollection();
if (id == "-1" || id =="draft")
{
menu.DefaultMenuAlias = ActionNew.Instance.Alias;
menu.Items.Add<ActionNew>(_localization.Get("general_create"));
}
else if (id.IsNumeric())
{
menu.Items.Add<CopyNewsletterAction>(_localization.Get("ns_copyToNew"));
menu.Items.Add<ActionDelete>(_localization.Get("general_delete"));
}
menu.Items.Add<ActionRefresh>(_localization.Get("actions_refreshNode"));
return menu;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment