2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
2018-10-08 13:33:14 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-11-26 07:49:32 +01:00
|
|
|
|
using System.Diagnostics;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Formatting;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Web.Http;
|
2018-09-06 14:10:10 +02:00
|
|
|
|
using Umbraco.Core;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Web.Models.Trees;
|
|
|
|
|
|
using Umbraco.Web.Mvc;
|
|
|
|
|
|
using Umbraco.Web.WebApi;
|
|
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
|
|
|
|
|
using Constants = Umbraco.Core.Constants;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Trees
|
|
|
|
|
|
{
|
|
|
|
|
|
[AngularJsonOnlyConfiguration]
|
|
|
|
|
|
[PluginController("UmbracoTrees")]
|
|
|
|
|
|
public class ApplicationTreeController : UmbracoAuthorizedApiController
|
2018-11-26 07:49:32 +01:00
|
|
|
|
{
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the tree nodes for an application
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="application">The application to load tree for</param>
|
|
|
|
|
|
/// <param name="tree">An optional single tree alias, if specified will only load the single tree for the request app</param>
|
|
|
|
|
|
/// <param name="queryStrings"></param>
|
|
|
|
|
|
/// <param name="onlyInitialized">An optional bool (defaults to true), if set to false it will also load uninitialized trees</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpQueryStringFilter("queryStrings")]
|
2018-10-16 16:41:06 +11:00
|
|
|
|
public async Task<TreeRootNode> GetApplicationTrees(string application, string tree, FormDataCollection queryStrings, bool onlyInitialized = true)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-09-06 13:07:09 +02:00
|
|
|
|
application = application.CleanForXss();
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
if (string.IsNullOrEmpty(application)) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
|
|
//find all tree definitions that have the current application alias
|
2018-10-23 18:28:55 +11:00
|
|
|
|
var groupedTrees = Services.ApplicationTreeService.GetGroupedApplicationTrees(application, onlyInitialized);
|
|
|
|
|
|
var allTrees = groupedTrees.Values.SelectMany(x => x).ToList();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-10-25 09:06:53 +02:00
|
|
|
|
if (string.IsNullOrEmpty(tree) == false || allTrees.Count == 1)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-10-23 18:28:55 +11:00
|
|
|
|
var apptree = !tree.IsNullOrWhiteSpace()
|
|
|
|
|
|
? allTrees.FirstOrDefault(x => x.Alias == tree)
|
|
|
|
|
|
: allTrees.FirstOrDefault();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (apptree == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await GetRootForSingleAppTree(
|
|
|
|
|
|
apptree,
|
|
|
|
|
|
Constants.System.Root.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
|
queryStrings,
|
|
|
|
|
|
application);
|
|
|
|
|
|
|
2018-10-16 16:41:06 +11:00
|
|
|
|
//this will be null if it cannot convert to a single root section
|
2018-06-29 19:52:40 +02:00
|
|
|
|
if (result != null)
|
2018-10-08 13:33:14 +01:00
|
|
|
|
{
|
2018-10-16 16:41:06 +11:00
|
|
|
|
return result;
|
2018-10-08 13:33:14 +01:00
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-23 18:28:55 +11:00
|
|
|
|
//Don't apply fancy grouping logic futher down, if we only have one group of items
|
|
|
|
|
|
var hasGroups = groupedTrees.Count > 1;
|
|
|
|
|
|
if (!hasGroups)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-10-23 18:28:55 +11:00
|
|
|
|
var collection = new TreeNodeCollection();
|
|
|
|
|
|
foreach (var apptree in allTrees)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-10-23 18:28:55 +11:00
|
|
|
|
//return the root nodes for each tree in the app
|
|
|
|
|
|
var rootNode = await GetRootForMultipleAppTree(apptree, queryStrings);
|
|
|
|
|
|
//This could be null if the tree decides not to return it's root (i.e. the member type tree does this when not in umbraco membership mode)
|
|
|
|
|
|
if (rootNode != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
collection.Add(rootNode);
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-11 14:09:44 +00:00
|
|
|
|
if(collection.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var multiTree = TreeRootNode.CreateMultiTreeRoot(collection);
|
|
|
|
|
|
multiTree.Name = Services.TextService.Localize("sections/" + application);
|
|
|
|
|
|
|
|
|
|
|
|
return multiTree;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Otherwise its a application/section with no trees (aka a full screen app)
|
|
|
|
|
|
//For example we do not have a Forms tree definied in C# & can not attribute with [Tree(isSingleNodeTree:true0]
|
|
|
|
|
|
var rootId = Constants.System.Root.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
var section = Services.TextService.Localize("sections/" + application);
|
2018-10-08 13:33:14 +01:00
|
|
|
|
|
2018-12-11 14:09:44 +00:00
|
|
|
|
return TreeRootNode.CreateSingleTreeRoot(rootId, null, null, section, TreeNodeCollection.Empty, true);
|
2018-10-08 13:33:14 +01:00
|
|
|
|
}
|
2018-10-16 16:41:06 +11:00
|
|
|
|
|
|
|
|
|
|
var rootNodeGroups = new List<TreeRootNode>();
|
|
|
|
|
|
|
2018-10-11 09:16:44 +01:00
|
|
|
|
//Group trees by [CoreTree] attribute with a TreeGroup property
|
2018-10-23 18:28:55 +11:00
|
|
|
|
foreach (var treeSectionGroup in groupedTrees)
|
2018-10-08 13:33:14 +01:00
|
|
|
|
{
|
|
|
|
|
|
var treeGroupName = treeSectionGroup.Key;
|
|
|
|
|
|
|
2018-10-08 14:10:11 +01:00
|
|
|
|
var groupNodeCollection = new TreeNodeCollection();
|
2018-10-23 18:28:55 +11:00
|
|
|
|
foreach (var appTree in treeSectionGroup.Value)
|
2018-10-08 13:33:14 +01:00
|
|
|
|
{
|
2018-10-23 18:28:55 +11:00
|
|
|
|
var rootNode = await GetRootForMultipleAppTree(appTree, queryStrings);
|
|
|
|
|
|
if (rootNode != null)
|
2018-10-08 13:33:14 +01:00
|
|
|
|
{
|
2018-10-23 18:28:55 +11:00
|
|
|
|
//Add to a new list/collection
|
|
|
|
|
|
groupNodeCollection.Add(rootNode);
|
2018-10-08 13:33:14 +01:00
|
|
|
|
}
|
2018-10-08 14:10:11 +01:00
|
|
|
|
}
|
2018-10-08 13:33:14 +01:00
|
|
|
|
|
2018-10-08 14:10:11 +01:00
|
|
|
|
//If treeGroupName == null then its third party
|
2018-10-23 18:28:55 +11:00
|
|
|
|
if (treeGroupName.IsNullOrWhiteSpace())
|
2018-10-08 14:10:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
//This is used for the localisation key
|
|
|
|
|
|
//treeHeaders/thirdPartyGroup
|
|
|
|
|
|
treeGroupName = "thirdPartyGroup";
|
2018-10-08 13:33:14 +01:00
|
|
|
|
}
|
2018-10-08 14:10:11 +01:00
|
|
|
|
|
2018-10-11 09:16:44 +01:00
|
|
|
|
if (groupNodeCollection.Count > 0)
|
2018-10-10 12:25:19 +01:00
|
|
|
|
{
|
2018-10-16 18:08:57 +11:00
|
|
|
|
var groupRoot = TreeRootNode.CreateGroupNode(groupNodeCollection, application);
|
2018-10-10 12:25:19 +01:00
|
|
|
|
groupRoot.Name = Services.TextService.Localize("treeHeaders/" + treeGroupName);
|
2018-10-08 14:10:11 +01:00
|
|
|
|
|
2018-10-10 12:25:19 +01:00
|
|
|
|
rootNodeGroups.Add(groupRoot);
|
|
|
|
|
|
}
|
2018-10-08 13:33:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 16:41:06 +11:00
|
|
|
|
return TreeRootNode.CreateGroupedMultiTreeRoot(new TreeNodeCollection(rootNodeGroups.OrderBy(x => x.Name)));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the root node for an application with multiple trees
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="configTree"></param>
|
|
|
|
|
|
/// <param name="queryStrings"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private async Task<TreeNode> GetRootForMultipleAppTree(ApplicationTree configTree, FormDataCollection queryStrings)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (configTree == null) throw new ArgumentNullException(nameof(configTree));
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var byControllerAttempt = await configTree.TryGetRootNodeFromControllerTree(queryStrings, ControllerContext);
|
|
|
|
|
|
if (byControllerAttempt.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
return byControllerAttempt.Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (HttpResponseException)
|
|
|
|
|
|
{
|
|
|
|
|
|
//if this occurs its because the user isn't authorized to view that tree, in this case since we are loading multiple trees we
|
|
|
|
|
|
//will just return null so that it's not added to the list.
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new ApplicationException("Could not get root node for tree type " + configTree.Alias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the root node for an application with one tree
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="configTree"></param>
|
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
|
/// <param name="queryStrings"></param>
|
|
|
|
|
|
/// <param name="application"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2018-10-16 16:41:06 +11:00
|
|
|
|
private async Task<TreeRootNode> GetRootForSingleAppTree(ApplicationTree configTree, string id, FormDataCollection queryStrings, string application)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var rootId = Constants.System.Root.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
if (configTree == null) throw new ArgumentNullException(nameof(configTree));
|
|
|
|
|
|
var byControllerAttempt = configTree.TryLoadFromControllerTree(id, queryStrings, ControllerContext);
|
|
|
|
|
|
if (byControllerAttempt.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rootNode = await configTree.TryGetRootNodeFromControllerTree(queryStrings, ControllerContext);
|
|
|
|
|
|
if (rootNode.Success == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
//This should really never happen if we've successfully got the children above.
|
|
|
|
|
|
throw new InvalidOperationException("Could not create root node for tree " + configTree.Alias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-25 08:54:17 +02:00
|
|
|
|
var treeAttribute = configTree.GetTreeAttribute();
|
|
|
|
|
|
|
2018-10-16 16:41:06 +11:00
|
|
|
|
var sectionRoot = TreeRootNode.CreateSingleTreeRoot(
|
2018-06-29 19:52:40 +02:00
|
|
|
|
rootId,
|
|
|
|
|
|
rootNode.Result.ChildNodesUrl,
|
|
|
|
|
|
rootNode.Result.MenuUrl,
|
|
|
|
|
|
rootNode.Result.Name,
|
2018-10-25 08:54:17 +02:00
|
|
|
|
byControllerAttempt.Result,
|
2018-10-26 14:18:42 +11:00
|
|
|
|
treeAttribute.IsSingleNodeTree);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-10-16 16:41:06 +11:00
|
|
|
|
//assign the route path based on the root node, this means it will route there when the section is navigated to
|
|
|
|
|
|
//and no dashboards will be available for this section
|
|
|
|
|
|
sectionRoot.RoutePath = rootNode.Result.RoutePath;
|
2018-11-26 07:49:32 +01:00
|
|
|
|
sectionRoot.Path = rootNode.Result.Path;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
foreach (var d in rootNode.Result.AdditionalData)
|
|
|
|
|
|
{
|
|
|
|
|
|
sectionRoot.AdditionalData[d.Key] = d.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
return sectionRoot;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new ApplicationException("Could not render a tree for type " + configTree.Alias);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|