Fixing that Core.Containers "Show on admin menu" functionality shouldn't depend on the Orchard.Lists feature

This commit is contained in:
Benedek Farkas 2024-11-20 12:55:36 +01:00
parent 81570fab35
commit 8f46425555
3 changed files with 104 additions and 90 deletions

View File

@ -0,0 +1,96 @@
using System.Linq;
using Orchard.ContentManagement;
using Orchard.Core.Containers.Models;
using Orchard.Core.Containers.Services;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Navigation;
using Orchard.Utility.Extensions;
namespace Orchard.Core.Containers {
public class AdminMenu : INavigationProvider {
private readonly IContainerService _containerService;
private readonly IContentManager _contentManager;
private readonly IAuthorizationService _authorizationService;
private readonly IWorkContextAccessor _workContextAccessor;
public AdminMenu(
IContainerService containerService,
IContentManager contentManager,
IAuthorizationService authorizationService,
IWorkContextAccessor workContextAccessor) {
_containerService = containerService;
_contentManager = contentManager;
_authorizationService = authorizationService;
_workContextAccessor = workContextAccessor;
}
public Localizer T { get; set; }
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
builder.AddImageSet("list");
var containers = _containerService
.GetContainersQuery(VersionOptions.Latest)
.Where<ContainerPartRecord>(x => x.ShowOnAdminMenu)
.List()
.Where(content => _authorizationService.TryCheckAccess(
Contents.Permissions.EditContent,
_workContextAccessor.GetContext().CurrentUser,
content))
.ToList();
foreach (var container in containers) {
var closureContainer = container;
if (!string.IsNullOrWhiteSpace(container.AdminMenuImageSet)) {
builder.AddImageSet(container.AdminMenuImageSet.Trim());
}
builder.Add(T(container.AdminMenuText), container.AdminMenuPosition, item => {
var containedItems = _containerService.GetContentItems(closureContainer.Id, VersionOptions.Latest).ToList();
var actualContainer = closureContainer;
var position = 0;
// If the list has just a single item that happens to be a container itself,
// we will treat that one as the actual container to provide a nice & quick way to manage that list.
if (containedItems.Count == 1) {
var containedItem = containedItems.First().As<ContainerPart>();
if (containedItem != null) {
actualContainer = containedItem;
foreach (var itemContentType in containedItem.ItemContentTypes) {
var closureItemContentType = itemContentType;
item.Add(T("New {0}", itemContentType.DisplayName), string.Format("1.{0}", position++), subItem => subItem
.Action("Create", "Admin", new {
id = closureItemContentType.Name,
containerid = containedItem.Id,
area = "Contents"
}));
}
}
}
var containerMetadata = _contentManager.GetItemMetadata(actualContainer);
item.Action(containerMetadata.AdminRouteValues);
item.Action(containerMetadata.AdminRouteValues);
item.AddClass("nav-list");
item.AddClass(closureContainer.AdminMenuText.HtmlClassify());
item.LinkToFirstChild(false);
foreach (var itemContentType in closureContainer.ItemContentTypes) {
var closureItemContentType = itemContentType;
item.Add(T("New {0}", itemContentType.DisplayName), string.Format("1.{0}", position++), subItem => subItem
.Action("Create", "Admin", new {
id = closureItemContentType.Name,
containerid = container.Id,
area = "Contents"
}));
}
});
}
}
}
}

View File

@ -119,6 +119,7 @@
<Compile Include="Common\ViewModels\DateTimeEditor.cs" />
<Compile Include="Common\ViewModels\TextFieldDriverViewModel.cs" />
<Compile Include="Common\ViewModels\TextFieldSettingsEventsViewModel.cs" />
<Compile Include="Containers\AdminMenu.cs" />
<Compile Include="Containers\Controllers\ItemController.cs" />
<Compile Include="Containers\Drivers\ContainablePartDriver.cs" />
<Compile Include="Containers\Drivers\ContainerPartDriver.cs" />

View File

@ -1,98 +1,15 @@
using System;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.Core.Containers.Models;
using Orchard.Core.Containers.Services;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Localization;
using Orchard.UI.Navigation;
using Orchard.Utility.Extensions;
namespace Orchard.Lists {
public class AdminMenu : INavigationProvider {
private readonly IContainerService _containerService;
private readonly IContentManager _contentManager;
private readonly IAuthorizationService _authorizationService;
private readonly IWorkContextAccessor _workContextAccessor;
public AdminMenu(
IContainerService containerService,
IContentManager contentManager,
IAuthorizationService authorizationService,
IWorkContextAccessor workContextAccessor
) {
_containerService = containerService;
_contentManager = contentManager;
_authorizationService = authorizationService;
_workContextAccessor = workContextAccessor;
}
public Localizer T { get; set; }
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
builder.AddImageSet("list");
CreateListManagementMenuItem(builder);
CreateListMenuItems(builder);
}
private void CreateListManagementMenuItem(NavigationBuilder builder) {
builder.Add(T("Lists"), "11", item => item
.Action("Index", "Admin", new {area = "Orchard.Lists"}).Permission(Permissions.ManageLists)
);
}
private void CreateListMenuItems(NavigationBuilder builder) {
var containers = _containerService
.GetContainersQuery(VersionOptions.Latest)
.Where<ContainerPartRecord>(x => x.ShowOnAdminMenu)
.List()
.Where(x => _authorizationService.TryCheckAccess(Orchard.Core.Contents.Permissions.EditContent, _workContextAccessor.GetContext().CurrentUser, x))
.ToList();
foreach (var container in containers) {
var closureContainer = container;
if (!String.IsNullOrWhiteSpace(container.AdminMenuImageSet)) {
builder.AddImageSet(container.AdminMenuImageSet.Trim());
}
builder.Add(T(container.AdminMenuText), container.AdminMenuPosition, item => {
var containedItems = _containerService.GetContentItems(closureContainer.Id, VersionOptions.Latest).ToList();
var actualContainer = closureContainer;
var position = 0;
// If the list has just a single item that happens to be a container itself,
// we will treat that one as the actual container to provide a nice & quick way to manage that list.
if (containedItems.Count == 1) {
var containedItem = containedItems.First().As<ContainerPart>();
if (containedItem != null) {
actualContainer = containedItem;
foreach (var itemContentType in containedItem.ItemContentTypes) {
var closureItemContentType = itemContentType;
item.Add(T("New {0}", itemContentType.DisplayName), String.Format("1.{0}", position++), subItem => subItem
.Action("Create", "Admin", new { id = closureItemContentType.Name, containerid = containedItem.Id, area = "Contents" }));
}
}
}
var containerMetadata = _contentManager.GetItemMetadata(actualContainer);
item.Action(containerMetadata.AdminRouteValues);
item.Action(containerMetadata.AdminRouteValues);
item.AddClass("nav-list");
item.AddClass(closureContainer.AdminMenuText.HtmlClassify());
item.LinkToFirstChild(false);
foreach (var itemContentType in closureContainer.ItemContentTypes) {
var closureItemContentType = itemContentType;
item.Add(T("New {0}", itemContentType.DisplayName), String.Format("1.{0}", position++), subItem => subItem
.Action("Create", "Admin", new { id = closureItemContentType.Name, containerid = container.Id, area = "Contents" }));
}
});
}
}
public void GetNavigation(NavigationBuilder builder) =>
builder
.AddImageSet("list")
.Add(T("Lists"), "11", item => item
.Action("Index", "Admin", new { area = "Orchard.Lists" }).Permission(Permissions.ManageLists));
}
}
}