mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#18797, 18799: Fixing MenuPart and NavigationPart
Work Items: 18797, 18799
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using JetBrains.Annotations;
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Drivers;
|
using Orchard.ContentManagement.Drivers;
|
||||||
using Orchard.Core.Navigation.Models;
|
using Orchard.Core.Navigation.Models;
|
||||||
@@ -32,6 +31,12 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
|
|
||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
|
protected override string Prefix {
|
||||||
|
get {
|
||||||
|
return "MenuPart";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override DriverResult Editor(MenuPart part, dynamic shapeHelper) {
|
protected override DriverResult Editor(MenuPart part, dynamic shapeHelper) {
|
||||||
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, _orchardServices.WorkContext.CurrentUser, part))
|
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, _orchardServices.WorkContext.CurrentUser, part))
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Drivers;
|
using Orchard.ContentManagement.Drivers;
|
||||||
using Orchard.Core.Navigation.Models;
|
using Orchard.Core.Navigation.Models;
|
||||||
|
using Orchard.Core.Navigation.Services;
|
||||||
using Orchard.Core.Navigation.ViewModels;
|
using Orchard.Core.Navigation.ViewModels;
|
||||||
|
using Orchard.Localization;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
|
using Orchard.UI.Navigation;
|
||||||
|
using Orchard.Utility;
|
||||||
|
|
||||||
namespace Orchard.Core.Navigation.Drivers {
|
namespace Orchard.Core.Navigation.Drivers {
|
||||||
|
|
||||||
@@ -10,14 +14,30 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
private readonly IAuthorizationService _authorizationService;
|
private readonly IAuthorizationService _authorizationService;
|
||||||
private readonly IWorkContextAccessor _workContextAccessor;
|
private readonly IWorkContextAccessor _workContextAccessor;
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly IMenuService _menuService;
|
||||||
|
private readonly INavigationManager _navigationManager;
|
||||||
|
|
||||||
public NavigationPartDriver(
|
public NavigationPartDriver(
|
||||||
IAuthorizationService authorizationService,
|
IAuthorizationService authorizationService,
|
||||||
IWorkContextAccessor workContextAccessor,
|
IWorkContextAccessor workContextAccessor,
|
||||||
IContentManager contentManager) {
|
IContentManager contentManager,
|
||||||
|
IMenuService menuService,
|
||||||
|
INavigationManager navigationManager) {
|
||||||
_authorizationService = authorizationService;
|
_authorizationService = authorizationService;
|
||||||
_workContextAccessor = workContextAccessor;
|
_workContextAccessor = workContextAccessor;
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
|
_menuService = menuService;
|
||||||
|
_navigationManager = navigationManager;
|
||||||
|
|
||||||
|
T = NullLocalizer.Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
|
protected override string Prefix {
|
||||||
|
get {
|
||||||
|
return "NavigationPart";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DriverResult Editor(NavigationPart part, dynamic shapeHelper) {
|
protected override DriverResult Editor(NavigationPart part, dynamic shapeHelper) {
|
||||||
@@ -28,14 +48,48 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
return ContentShape("Parts_Navigation_Edit",
|
return ContentShape("Parts_Navigation_Edit",
|
||||||
() => {
|
() => {
|
||||||
// loads all menu part of type ContentMenuItem linking to the current content item
|
// loads all menu part of type ContentMenuItem linking to the current content item
|
||||||
var model = new NavigationPartViewModel() {
|
var model = new NavigationPartViewModel {
|
||||||
Part = part,
|
Part = part,
|
||||||
ContentMenuItems = _contentManager.Query<MenuPart>()
|
ContentMenuItems = _contentManager
|
||||||
.Join<ContentMenuItemPartRecord>().Where(x => x.ContentMenuItemRecord == part.ContentItem.Record).List()
|
.Query<MenuPart>()
|
||||||
|
.Join<ContentMenuItemPartRecord>()
|
||||||
|
.Where(x => x.ContentMenuItemRecord == part.ContentItem.Record)
|
||||||
|
.List(),
|
||||||
|
Menus = _menuService.GetMenus(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return shapeHelper.EditorTemplate(TemplateName: "Parts.Navigation.Edit", Model: model, Prefix: Prefix);
|
return shapeHelper.EditorTemplate(TemplateName: "Parts.Navigation.Edit", Model: model, Prefix: Prefix);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override DriverResult Editor(NavigationPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||||
|
var currentUser = _workContextAccessor.GetContext().CurrentUser;
|
||||||
|
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, currentUser, part))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var model = new NavigationPartViewModel();
|
||||||
|
|
||||||
|
if (updater.TryUpdateModel(model, Prefix, null, null)) {
|
||||||
|
if(model.AddMenuItem) {
|
||||||
|
if (string.IsNullOrEmpty(model.MenuText)) {
|
||||||
|
updater.AddModelError("MenuText", T("The MenuText field is required"));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var menu = _contentManager.Get(model.CurrentMenuId);
|
||||||
|
|
||||||
|
if(menu != null) {
|
||||||
|
var menuItem = _contentManager.Create<ContentMenuItemPart>("ContentMenuItem");
|
||||||
|
menuItem.Content = part.ContentItem;
|
||||||
|
|
||||||
|
menuItem.As<MenuPart>().MenuText = model.MenuText;
|
||||||
|
menuItem.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||||
|
menuItem.As<MenuPart>().Menu = menu;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Editor(part, shapeHelper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,16 +25,21 @@ namespace Orchard.Core.Navigation.Handlers {
|
|||||||
OnActivated<MenuPart>(PropertySetHandlers);
|
OnActivated<MenuPart>(PropertySetHandlers);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void PropertySetHandlers(ActivatedContentContext context, MenuPart menuPart) {
|
protected void PropertySetHandlers(ActivatedContentContext context, MenuPart menuPart) {
|
||||||
menuPart.MenuField.Setter(menu => {
|
menuPart.MenuField.Setter(menu => {
|
||||||
menuPart.Record.MenuId = menu.ContentItem.Id;
|
if(menu == null || menu.ContentItem == null) {
|
||||||
|
menuPart.Record.MenuId = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
menuPart.Record.MenuId = menu.ContentItem.Id;
|
||||||
|
}
|
||||||
|
|
||||||
return menu;
|
return menu;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
protected void LazyLoadHandlers(MenuPart menuPart) {
|
|
||||||
menuPart.MenuField.Loader(ctx =>
|
menuPart.MenuField.Loader(ctx =>
|
||||||
_contentManager.Get(menuPart.Record.MenuId, menuPart.IsPublished() ? VersionOptions.Published : VersionOptions.Latest));
|
_contentManager.Get(menuPart.Record.MenuId, menuPart.IsPublished() ? VersionOptions.Published : VersionOptions.Latest)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace Orchard.Core.Navigation {
|
|||||||
public int Create() {
|
public int Create() {
|
||||||
ContentDefinitionManager.AlterPartDefinition("MenuPart", builder => builder.Attachable());
|
ContentDefinitionManager.AlterPartDefinition("MenuPart", builder => builder.Attachable());
|
||||||
ContentDefinitionManager.AlterPartDefinition("NavigationPart", builder => builder.Attachable());
|
ContentDefinitionManager.AlterPartDefinition("NavigationPart", builder => builder.Attachable());
|
||||||
ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg.WithPart("MenuPart"));
|
ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg.WithPart("NavigationPart"));
|
||||||
|
|
||||||
SchemaBuilder.CreateTable("MenuItemPartRecord",
|
SchemaBuilder.CreateTable("MenuItemPartRecord",
|
||||||
table => table
|
table => table
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
using Orchard.Core.Navigation.Models;
|
using Orchard.Core.Navigation.Models;
|
||||||
|
|
||||||
namespace Orchard.Core.Navigation.ViewModels {
|
namespace Orchard.Core.Navigation.ViewModels {
|
||||||
public class NavigationPartViewModel {
|
public class NavigationPartViewModel {
|
||||||
public IEnumerable<MenuPart> ContentMenuItems { get; set; }
|
public IEnumerable<MenuPart> ContentMenuItems { get; set; }
|
||||||
public NavigationPart Part { get; set; }
|
public NavigationPart Part { get; set; }
|
||||||
|
public IEnumerable<ContentItem> Menus { get; set; }
|
||||||
|
public string MenuText { get; set; }
|
||||||
|
public bool AddMenuItem { get; set; }
|
||||||
|
public int CurrentMenuId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,18 +9,35 @@
|
|||||||
<span class="hint">@T("The menu items linking to this content item.")</span>
|
<span class="hint">@T("The menu items linking to this content item.")</span>
|
||||||
@if(Model.ContentMenuItems.Any()) {
|
@if(Model.ContentMenuItems.Any()) {
|
||||||
<ul>
|
<ul>
|
||||||
@foreach(var menuPart in Model.ContentMenuItems) {
|
@foreach(var menuPart in Model.ContentMenuItems) {
|
||||||
var menuContentItem = contentManager.Get(menuPart.Menu.Id);
|
var menuContentItem = contentManager.Get(menuPart.Menu.Id);
|
||||||
var menuName = Html.ItemDisplayText(menuContentItem).ToString();
|
var menuName = Html.ItemDisplayText(menuContentItem).ToString();
|
||||||
<li>
|
<li>
|
||||||
<div><span>@menuPart.MenuText</span> @T("on") <span>@Html.ActionLink(menuName, "Index", "Admin", new { area = "Navigation", menuId = menuContentItem.Id }, new {}) </span></div>
|
<div><span>@menuPart.MenuText</span> @T("on") <span>@Html.ActionLink(menuName, "Index", "Admin", new { area = "Navigation", menuId = menuContentItem.Id }, new {}) </span></div>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
</ul>
|
</ul>
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@T("Not displayed in any menu.")
|
@T("Not displayed in any menu.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
@Html.EditorFor(m => m.AddMenuItem)
|
||||||
|
<label for="@Html.FieldIdFor(m => m.AddMenuItem)" class="forcheckbox">@T("Add on a menu")</label>
|
||||||
|
<div data-controllerid="@Html.FieldIdFor(m => m.AddMenuItem)">
|
||||||
|
<select id="@Html.FieldIdFor(m => m.CurrentMenuId)" name="@Html.FieldNameFor(m => m.CurrentMenuId)">
|
||||||
|
@foreach (ContentItem menu in Model.Menus) {
|
||||||
|
@Html.SelectOption(Model.CurrentMenuId, menu.Id, Html.ItemDisplayText(menu).ToString())
|
||||||
|
}
|
||||||
|
</select>
|
||||||
|
<span class="hint">@T("Select which menu you want the content item to be added on.")</span>
|
||||||
|
|
||||||
|
<label for="MenuText">@T("Menu text")</label>
|
||||||
|
@Html.TextBoxFor(m => m.MenuText, new { @class = "text-box single-line" })
|
||||||
|
<span class="hint">@T("The text that should appear in the menu.")</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
</fieldset>
|
</fieldset>
|
||||||
@@ -91,9 +91,11 @@ namespace Orchard.Blogs.Commands {
|
|||||||
var menu = _menuService.GetMenu(MenuName);
|
var menu = _menuService.GetMenu(MenuName);
|
||||||
|
|
||||||
if (menu != null) {
|
if (menu != null) {
|
||||||
blog.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
var menuItem = _contentManager.Create<ContentMenuItemPart>("ContentMenuItem");
|
||||||
blog.As<MenuPart>().MenuText = MenuText;
|
menuItem.Content = blog;
|
||||||
blog.As<MenuPart>().Menu = menu.ContentItem;
|
menuItem.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||||
|
menuItem.As<MenuPart>().MenuText = MenuText;
|
||||||
|
menuItem.As<MenuPart>().Menu = menu;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,9 +80,11 @@ namespace Orchard.Pages.Commands {
|
|||||||
var menu = _menuService.GetMenu(MenuName);
|
var menu = _menuService.GetMenu(MenuName);
|
||||||
|
|
||||||
if (menu != null) {
|
if (menu != null) {
|
||||||
page.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
var menuItem = _contentManager.Create<ContentMenuItemPart>("ContentMenuItem");
|
||||||
page.As<MenuPart>().MenuText = MenuText;
|
menuItem.Content = page;
|
||||||
page.As<MenuPart>().Menu = menu.ContentItem;
|
menuItem.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||||
|
menuItem.As<MenuPart>().MenuText = MenuText;
|
||||||
|
menuItem.As<MenuPart>().Menu = menu;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user