2014-11-20 19:51:42 +11:00
|
|
|
using System;
|
2017-09-13 17:35:20 +02:00
|
|
|
using System.Collections.Generic;
|
2014-11-20 19:51:42 +11:00
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http.Formatting;
|
2017-09-12 16:22:16 +02:00
|
|
|
using AutoMapper;
|
2014-11-20 19:51:42 +11:00
|
|
|
using Umbraco.Core;
|
2016-03-15 16:58:13 +01:00
|
|
|
using Umbraco.Core.Services;
|
2014-11-20 19:51:42 +11:00
|
|
|
using Umbraco.Core.Models;
|
2018-01-15 11:32:30 +01:00
|
|
|
using Umbraco.Core.Models.Entities;
|
2018-10-29 17:27:33 +11:00
|
|
|
using Umbraco.Web.Actions;
|
2017-09-12 16:22:16 +02:00
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2014-11-20 19:51:42 +11:00
|
|
|
using Umbraco.Web.Models.Trees;
|
|
|
|
|
using Umbraco.Web.Mvc;
|
2017-09-12 16:22:16 +02:00
|
|
|
using Umbraco.Web.Search;
|
2014-11-20 19:51:42 +11:00
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2018-10-29 17:27:33 +11:00
|
|
|
|
2014-11-20 19:51:42 +11:00
|
|
|
using Constants = Umbraco.Core.Constants;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Trees
|
|
|
|
|
{
|
|
|
|
|
[UmbracoTreeAuthorize(Constants.Trees.Templates)]
|
2018-10-05 11:51:34 +02:00
|
|
|
[Tree(Constants.Applications.Settings, Constants.Trees.Templates, null, sortOrder:6)]
|
2014-11-20 19:51:42 +11:00
|
|
|
[PluginController("UmbracoTrees")]
|
2018-10-05 13:11:38 +01:00
|
|
|
[CoreTree(TreeGroup = Constants.Trees.Groups.Templating)]
|
2017-09-12 16:22:16 +02:00
|
|
|
public class TemplatesTreeController : TreeController, ISearchableTree
|
2014-11-20 19:51:42 +11:00
|
|
|
{
|
2018-11-04 22:54:25 +01:00
|
|
|
protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
|
|
|
|
|
{
|
|
|
|
|
var root = base.CreateRootNode(queryStrings);
|
|
|
|
|
//check if there are any templates
|
2018-11-05 10:36:17 +01:00
|
|
|
root.HasChildren = Services.FileService.GetTemplates(-1).Any();
|
2018-11-04 22:54:25 +01:00
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 19:51:42 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// The method called to render the contents of the tree structure
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="queryStrings">
|
|
|
|
|
/// All of the query string parameters passed from jsTree
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// We are allowing an arbitrary number of query strings to be pased in so that developers are able to persist custom data from the front-end
|
|
|
|
|
/// to the back end to be used in the query for model data.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
|
|
|
|
|
{
|
|
|
|
|
var nodes = new TreeNodeCollection();
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
var found = id == Constants.System.Root.ToInvariantString()
|
|
|
|
|
? Services.FileService.GetTemplates(-1)
|
2014-11-20 19:51:42 +11:00
|
|
|
: Services.FileService.GetTemplates(int.Parse(id));
|
|
|
|
|
|
|
|
|
|
nodes.AddRange(found.Select(template => CreateTreeNode(
|
|
|
|
|
template.Id.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
//TODO: Fix parent ID stuff for templates
|
|
|
|
|
"-1",
|
|
|
|
|
queryStrings,
|
|
|
|
|
template.Name,
|
|
|
|
|
template.IsMasterTemplate ? "icon-newspaper" : "icon-newspaper-alt",
|
|
|
|
|
template.IsMasterTemplate,
|
2017-05-12 14:49:44 +02:00
|
|
|
GetEditorPath(template, queryStrings),
|
2018-01-15 11:32:30 +01:00
|
|
|
Udi.Create(ObjectTypes.GetUdiType(Constants.ObjectTypes.TemplateType), template.Key)
|
2017-05-12 14:49:44 +02:00
|
|
|
)));
|
2014-11-20 19:51:42 +11:00
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the menu structure for the node
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="queryStrings"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
|
|
|
|
|
{
|
|
|
|
|
var menu = new MenuItemCollection();
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
//Create the normal create action
|
2018-10-30 00:34:43 +11:00
|
|
|
var item = menu.Items.Add<ActionNew>(Services.TextService, opensDialog: true);
|
2018-10-05 11:51:34 +02:00
|
|
|
item.NavigateToRoute($"{queryStrings.GetValue<string>("application")}/templates/edit/{id}?create=true");
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2014-11-20 19:51:42 +11:00
|
|
|
if (id == Constants.System.Root.ToInvariantString())
|
|
|
|
|
{
|
|
|
|
|
//refresh action
|
2018-10-29 17:27:33 +11:00
|
|
|
menu.Items.Add(new RefreshNode(Services.TextService, true));
|
2014-11-20 19:51:42 +11:00
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var template = Services.FileService.GetTemplate(int.Parse(id));
|
|
|
|
|
if (template == null) return new MenuItemCollection();
|
2014-11-25 17:36:02 +11:00
|
|
|
var entity = FromTemplate(template);
|
2014-11-20 19:51:42 +11:00
|
|
|
|
2014-11-25 17:36:02 +11:00
|
|
|
//don't allow delete if it has child layouts
|
2014-11-20 19:51:42 +11:00
|
|
|
if (template.IsMasterTemplate == false)
|
|
|
|
|
{
|
|
|
|
|
//add delete option if it doesn't have children
|
2018-10-30 00:34:43 +11:00
|
|
|
menu.Items.Add<ActionDelete>(Services.TextService, true, opensDialog: true);
|
2014-11-20 19:51:42 +11:00
|
|
|
}
|
|
|
|
|
|
2014-11-25 17:36:02 +11:00
|
|
|
//add refresh
|
2018-10-29 17:27:33 +11:00
|
|
|
menu.Items.Add(new RefreshNode(Services.TextService, true));
|
2016-09-01 19:06:08 +02:00
|
|
|
|
2014-11-20 19:51:42 +11:00
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-15 11:32:30 +01:00
|
|
|
private EntitySlim FromTemplate(ITemplate template)
|
2014-11-20 19:51:42 +11:00
|
|
|
{
|
2018-01-15 11:32:30 +01:00
|
|
|
return new EntitySlim
|
2014-11-25 17:36:02 +11:00
|
|
|
{
|
|
|
|
|
CreateDate = template.CreateDate,
|
|
|
|
|
Id = template.Id,
|
|
|
|
|
Key = template.Key,
|
|
|
|
|
Name = template.Name,
|
2018-01-12 10:48:36 +01:00
|
|
|
NodeObjectType = Constants.ObjectTypes.Template,
|
2014-11-25 17:36:02 +11:00
|
|
|
//TODO: Fix parent/paths on templates
|
|
|
|
|
ParentId = -1,
|
|
|
|
|
Path = template.Path,
|
|
|
|
|
UpdateDate = template.UpdateDate
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-11-20 19:51:42 +11:00
|
|
|
|
2014-11-25 17:36:02 +11:00
|
|
|
private string GetEditorPath(ITemplate template, FormDataCollection queryStrings)
|
|
|
|
|
{
|
2014-11-20 19:51:42 +11:00
|
|
|
//TODO: Rebuild the language editor in angular, then we dont need to have this at all (which is just a path to the legacy editor)
|
|
|
|
|
|
2015-01-14 12:09:30 +11:00
|
|
|
return Services.FileService.DetermineTemplateRenderingEngine(template) == RenderingEngine.WebForms
|
2014-11-20 19:51:42 +11:00
|
|
|
? "/" + queryStrings.GetValue<string>("application") + "/framed/" +
|
2015-10-29 16:26:31 +01:00
|
|
|
Uri.EscapeDataString("settings/editTemplate.aspx?templateID=" + template.Id)
|
2017-05-12 14:49:44 +02:00
|
|
|
: null;
|
2014-11-20 19:51:42 +11:00
|
|
|
}
|
2017-09-12 16:22:16 +02:00
|
|
|
|
2018-12-04 14:25:37 +11:00
|
|
|
public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
|
2017-09-12 16:22:16 +02:00
|
|
|
{
|
2018-01-15 11:32:30 +01:00
|
|
|
var results = Services.EntityService.GetPagedDescendants(UmbracoObjectTypes.Template, pageIndex, pageSize, out totalFound, filter: query);
|
2018-12-04 14:25:37 +11:00
|
|
|
return Mapper.Map<IEnumerable<SearchResultEntity>>(results);
|
2017-09-12 16:22:16 +02:00
|
|
|
}
|
2014-11-20 19:51:42 +11:00
|
|
|
}
|
2015-10-29 16:26:31 +01:00
|
|
|
}
|