2021-01-13 11:02:29 +01:00
|
|
|
using System.Linq;
|
2021-01-14 19:41:32 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-09 07:49:26 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-01-14 19:41:32 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Actions;
|
2021-02-23 16:09:36 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models.Trees;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Trees;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2016-03-23 22:56:42 +01:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Trees
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
2020-11-19 19:23:41 +11:00
|
|
|
[Authorize(Policy = AuthorizationPolicies.TreeAccessMacros)]
|
2019-01-22 09:31:47 +01:00
|
|
|
[Tree(Constants.Applications.Settings, Constants.Trees.Macros, TreeTitle = "Macros", SortOrder = 4, TreeGroup = Constants.Trees.Groups.Settings)]
|
2020-09-23 08:55:25 +02:00
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeTreeArea)]
|
2019-01-22 09:31:47 +01:00
|
|
|
[CoreTree]
|
2018-10-10 15:03:14 +01:00
|
|
|
public class MacrosTreeController : TreeController
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
2020-02-17 14:58:34 +01:00
|
|
|
private readonly IMenuItemCollectionFactory _menuItemCollectionFactory;
|
2020-06-09 07:49:26 +02:00
|
|
|
private readonly IMacroService _macroService;
|
2020-02-17 14:58:34 +01:00
|
|
|
|
2021-02-23 16:09:36 +01:00
|
|
|
public MacrosTreeController(ILocalizedTextService localizedTextService, UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, IMenuItemCollectionFactory menuItemCollectionFactory, IMacroService macroService, IEventAggregator eventAggregator) : base(localizedTextService, umbracoApiControllerTypeCollection, eventAggregator)
|
2020-02-17 14:58:34 +01:00
|
|
|
{
|
|
|
|
|
_menuItemCollectionFactory = menuItemCollectionFactory;
|
2020-06-09 07:49:26 +02:00
|
|
|
_macroService = macroService;
|
2020-02-17 14:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 19:41:32 +01:00
|
|
|
protected override ActionResult<TreeNode> CreateRootNode(FormCollection queryStrings)
|
2018-10-23 09:34:02 +02:00
|
|
|
{
|
2021-01-14 19:41:32 +01:00
|
|
|
var rootResult = base.CreateRootNode(queryStrings);
|
|
|
|
|
if (!(rootResult.Result is null))
|
|
|
|
|
{
|
|
|
|
|
return rootResult;
|
|
|
|
|
}
|
|
|
|
|
var root = rootResult.Value;
|
|
|
|
|
|
2018-10-23 09:34:02 +02:00
|
|
|
//check if there are any macros
|
2020-06-09 07:49:26 +02:00
|
|
|
root.HasChildren = _macroService.GetAll().Any();
|
2018-10-23 09:34:02 +02:00
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 19:41:32 +01:00
|
|
|
protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, FormCollection queryStrings)
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
|
|
|
|
var nodes = new TreeNodeCollection();
|
|
|
|
|
|
2019-03-29 12:06:23 +00:00
|
|
|
if (id == Constants.System.RootString)
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
2020-06-09 07:49:26 +02:00
|
|
|
foreach (var macro in _macroService.GetAll().OrderBy(m => m.Name))
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
|
|
|
|
nodes.Add(CreateTreeNode(
|
2016-09-01 19:06:08 +02:00
|
|
|
macro.Id.ToString(),
|
|
|
|
|
id,
|
|
|
|
|
queryStrings,
|
|
|
|
|
macro.Name,
|
2019-06-07 17:02:55 +01:00
|
|
|
Constants.Icons.Macro,
|
2019-01-07 08:31:06 +01:00
|
|
|
false));
|
2016-03-23 22:56:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 11:02:29 +01:00
|
|
|
protected override ActionResult<MenuItemCollection> GetMenuForNode(string id, FormCollection queryStrings)
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
2020-02-17 14:58:34 +01:00
|
|
|
var menu = _menuItemCollectionFactory.Create();
|
2016-03-23 22:56:42 +01:00
|
|
|
|
2019-03-29 12:06:23 +00:00
|
|
|
if (id == Constants.System.RootString)
|
2016-03-23 22:56:42 +01:00
|
|
|
{
|
|
|
|
|
//Create the normal create action
|
2020-06-09 07:49:26 +02:00
|
|
|
menu.Items.Add<ActionNew>(LocalizedTextService);
|
2019-01-21 10:44:11 +01:00
|
|
|
|
2016-03-23 22:56:42 +01:00
|
|
|
//refresh action
|
2020-06-09 07:49:26 +02:00
|
|
|
menu.Items.Add(new RefreshNode(LocalizedTextService, true));
|
2016-03-23 22:56:42 +01:00
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 07:49:26 +02:00
|
|
|
var macro = _macroService.GetById(int.Parse(id));
|
2020-02-17 14:58:34 +01:00
|
|
|
if (macro == null) return menu;
|
2016-03-23 22:56:42 +01:00
|
|
|
|
2016-03-23 22:58:52 +01:00
|
|
|
//add delete option for all macros
|
2020-06-09 07:49:26 +02:00
|
|
|
menu.Items.Add<ActionDelete>(LocalizedTextService, opensDialog: true);
|
2016-03-23 22:56:42 +01:00
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|