mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +08:00
Hooked up a default value for position when a new menu item is added or a menu part is turned on for a content item
--HG-- branch : dev
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
using System.Web.Mvc;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Core.Navigation.Drivers;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
using Orchard.Core.Navigation.ViewModels;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Navigation;
|
||||
using Orchard.Utility;
|
||||
using MenuItem=Orchard.Core.Navigation.Models.MenuItem;
|
||||
|
||||
namespace Orchard.Core.Navigation.Controllers {
|
||||
@@ -24,7 +27,7 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.ManageMainMenu, T("Not allowed to manage the main menu")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var model = new ViewModels.NavigationManagementViewModel { Menu = _navigationManager.BuildMenu("main") };
|
||||
var model = new NavigationManagementViewModel { Menu = _navigationManager.BuildMenu("main") };
|
||||
|
||||
return View(model);
|
||||
}
|
||||
@@ -50,7 +53,10 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
return Index();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(menuItem.As<MenuPart>().MenuPosition))
|
||||
menuItem.As<MenuPart>().MenuPosition = Position.GetNext(_navigationManager.BuildMenu("main"));
|
||||
menuItem.As<MenuPart>().OnMainMenu = true;
|
||||
|
||||
_services.ContentManager.Create(model.MenuItem.Item.ContentItem);
|
||||
|
||||
return RedirectToAction("Index");
|
||||
|
@@ -1,21 +1,20 @@
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
using Orchard.Security;
|
||||
|
||||
namespace Orchard.Core.Navigation.Models {
|
||||
namespace Orchard.Core.Navigation.Drivers {
|
||||
[UsedImplicitly]
|
||||
public class MenuItemDriver : ContentItemDriver<MenuItem> {
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
|
||||
public readonly static ContentType ContentType = new ContentType {
|
||||
Name = "menuitem",
|
||||
DisplayName = "Menu Item"
|
||||
};
|
||||
Name = "menuitem",
|
||||
DisplayName = "Menu Item"
|
||||
};
|
||||
|
||||
public MenuItemDriver(IOrchardServices orchardServices, IAuthorizationService authorizationService) {
|
||||
_orchardServices = orchardServices;
|
||||
public MenuItemDriver(IAuthorizationService authorizationService) {
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,20 @@
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
using Orchard.Security;
|
||||
using Orchard.UI.Navigation;
|
||||
using Orchard.Utility;
|
||||
|
||||
namespace Orchard.Core.Navigation.Models {
|
||||
namespace Orchard.Core.Navigation.Drivers {
|
||||
[UsedImplicitly]
|
||||
public class MenuPartDriver : ContentPartDriver<MenuPart> {
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly INavigationManager _navigationManager;
|
||||
|
||||
public MenuPartDriver(IAuthorizationService authorizationService) {
|
||||
public MenuPartDriver(IAuthorizationService authorizationService, INavigationManager navigationManager) {
|
||||
_authorizationService = authorizationService;
|
||||
_navigationManager = navigationManager;
|
||||
}
|
||||
|
||||
public virtual IUser CurrentUser { get; set; }
|
||||
@@ -25,6 +30,9 @@ namespace Orchard.Core.Navigation.Models {
|
||||
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, CurrentUser, part))
|
||||
return null;
|
||||
|
||||
if (string.IsNullOrEmpty(part.MenuPosition))
|
||||
part.MenuPosition = Position.GetNext(_navigationManager.BuildMenu("main"));
|
||||
|
||||
updater.TryUpdateModel(part, Prefix, null, null);
|
||||
return ContentPartTemplate(part, "Parts/Navigation.EditMenuPart").Location("primary", "9");
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Orchard.Core.Navigation.Records;
|
||||
using Orchard.Core.Navigation.Drivers;
|
||||
using Orchard.Core.Navigation.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
|
||||
|
@@ -344,6 +344,7 @@
|
||||
<Compile Include="Storage\IStorageFolder.cs" />
|
||||
<Compile Include="Storage\IStorageProvider.cs" />
|
||||
<Compile Include="UI\Zones\ZoneManager.cs" />
|
||||
<Compile Include="Utility\Position.cs" />
|
||||
<Compile Include="Utility\Reflect.cs" />
|
||||
<Compile Include="Utility\ReflectOn.cs" />
|
||||
<Compile Include="Validation\JetBrains.Annotations.cs" />
|
||||
|
25
src/Orchard/Utility/Position.cs
Normal file
25
src/Orchard/Utility/Position.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.UI.Navigation;
|
||||
|
||||
namespace Orchard.Utility {
|
||||
public static class Position {
|
||||
public static string GetNext(IEnumerable<MenuItem> menuItems) {
|
||||
//TODO: (erikpo) Clean up query to not hardcode to exclude 99, which is the admin menu item as of right now
|
||||
var maxMenuItem = menuItems.FirstOrDefault().Items.Where(mi => mi.Position != "99").OrderByDescending(mi => mi.Position, new PositionComparer()).FirstOrDefault();
|
||||
|
||||
if (maxMenuItem != null) {
|
||||
var positionParts = maxMenuItem.Position.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim() != "");
|
||||
if (positionParts.Count() > 0) {
|
||||
int result;
|
||||
if (int.TryParse(positionParts.ElementAt(0), out result)) {
|
||||
return (result + 1).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "1";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user