using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Web.Models.Trees;
using umbraco;
using umbraco.BusinessLogic.Actions;
namespace Umbraco.Web.Trees
{
public abstract class ContentTreeControllerBase : TreeController
{
///
/// Returns the
///
protected abstract int RecycleBinId { get; }
///
/// Returns true if the recycle bin has items in it
///
protected abstract bool RecycleBinSmells { get; }
///
/// Returns the user's start node for this tree
///
protected abstract int UserStartNode { get; }
protected abstract TreeNodeCollection PerformGetTreeNodes(string id, FormDataCollection queryStrings);
protected abstract MenuItemCollection PerformGetMenuForNode(string id, FormDataCollection queryStrings);
protected abstract UmbracoObjectTypes UmbracoObjectType { get; }
protected IEnumerable GetChildEntities(string id)
{
int iid;
if (int.TryParse(id, out iid) == false)
{
throw new InvalidCastException("The id for the media tree must be an integer");
}
//if a request is made for the root node data but the user's start node is not the default, then
// we need to return their start node data
if (iid == Constants.System.Root && UserStartNode != Constants.System.Root)
{
//just return their single start node, it will show up under the 'Content' label
var startNode = Services.EntityService.Get(UserStartNode, UmbracoObjectType);
return new[] {startNode};
}
return Services.EntityService.GetChildren(iid, UmbracoObjectType).ToArray();
}
///
/// This will automatically check if the recycle bin needs to be rendered (i.e. its the first level)
/// and will automatically append it to the result of GetChildNodes.
///
///
///
///
protected sealed override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
if (id == Constants.System.Root.ToInvariantString() && UserStartNode == Constants.System.Root)
{
//we need to append the recycle bin to the end (if not in dialog mode)
var nodes = PerformGetTreeNodes(id, queryStrings);
if (!IsDialog(queryStrings))
{
nodes.Add(CreateTreeNode(
RecycleBinId.ToInvariantString(),
id,
queryStrings,
ui.GetText("general", "recycleBin"),
"icon-trash",
RecycleBinSmells,
queryStrings.GetValue("application") + TreeAlias.EnsureStartsWith('/') + "/recyclebin"));
}
return nodes;
}
//before we get the children we need to see if this is a container node
var current = Services.EntityService.Get(int.Parse(id), UmbracoObjectType);
if (current != null && current.AdditionalData.ContainsKey("IsContainer") && current.AdditionalData["IsContainer"] is bool && (bool)current.AdditionalData["IsContainer"])
{
//no children!
return new TreeNodeCollection();
}
return PerformGetTreeNodes(id, queryStrings);
}
///
/// Checks if the menu requested is for the recycle bin and renders that, otherwise renders the result of PerformGetMenuForNode
///
///
///
///
protected sealed override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
{
if (RecycleBinId.ToInvariantString() == id)
{
var menu = new MenuItemCollection();
menu.Items.Add(ui.Text("actions", "emptyTrashcan"));
menu.Items.Add(ui.Text("actions", ActionRefresh.Instance.Alias), true);
return menu;
}
return PerformGetMenuForNode(id, queryStrings);
}
///
/// Based on the allowed actions, this will filter the ones that the current user is allowed
///
///
///
///
protected void FilterUserAllowedMenuItems(MenuItemCollection menuWithAllItems, IEnumerable