-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#8752: Hide/show menu items on the frond-end #8753
base: 1.10.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
using System.Reflection; | ||
using System.Web.Mvc; | ||
using System.Web.Routing; | ||
using System.Web.UI.WebControls; | ||
using Orchard.Caching; | ||
using Orchard.ContentManagement; | ||
using Orchard.ContentManagement.Aspects; | ||
using Orchard.ContentManagement.MetaData; | ||
|
@@ -12,7 +14,11 @@ | |
using Orchard.Core.Containers.Models; | ||
using Orchard.Core.Contents.Settings; | ||
using Orchard.Core.Contents.ViewModels; | ||
using Orchard.Core.Navigation.Models; | ||
using Orchard.Core.Navigation.Services; | ||
using Orchard.Data; | ||
using Orchard.Data.Bags; | ||
using Orchard.Environment; | ||
using Orchard.Localization; | ||
using Orchard.Localization.Services; | ||
using Orchard.Logging; | ||
|
@@ -32,21 +38,24 @@ public class AdminController : ContentControllerBase, IUpdateModel { | |
private readonly ISiteService _siteService; | ||
private readonly ICultureManager _cultureManager; | ||
private readonly ICultureFilter _cultureFilter; | ||
private readonly ISignals _signals; | ||
|
||
public AdminController( | ||
IOrchardServices orchardServices, | ||
IContentDefinitionManager contentDefinitionManager, | ||
ISiteService siteService, | ||
ICultureManager cultureManager, | ||
ICultureFilter cultureFilter) : base(orchardServices.ContentManager) { | ||
ICultureFilter cultureFilter, | ||
ISignals signals | ||
) : base(orchardServices.ContentManager) { | ||
Services = orchardServices; | ||
_contentManager = orchardServices.ContentManager; | ||
_transactionManager = orchardServices.TransactionManager; | ||
_contentDefinitionManager = contentDefinitionManager; | ||
_siteService = siteService; | ||
_cultureManager = cultureManager; | ||
_cultureFilter = cultureFilter; | ||
|
||
_signals = signals; | ||
T = NullLocalizer.Instance; | ||
Logger = NullLogger.Instance; | ||
Shape = orchardServices.New; | ||
|
@@ -394,31 +403,27 @@ public class AdminController : ContentControllerBase, IUpdateModel { | |
|
||
[HttpPost] | ||
public ActionResult Clone(int id, string returnUrl) { | ||
var contentItem = _contentManager.GetLatest(id); | ||
|
||
if (contentItem == null) | ||
return HttpNotFound(); | ||
var originalContentItem = _contentManager.GetLatest(id); | ||
|
||
if (!Services.Authorizer.Authorize(Permissions.CreateContent, contentItem, T("Couldn't clone content"))) | ||
if (!Services.Authorizer.Authorize(Permissions.ViewContent, originalContentItem, T("Couldn't open original content"))) | ||
return new HttpUnauthorizedResult(); | ||
|
||
// pass a dummy content to the authorization check to check for "own" variations | ||
var dummyContent = _contentManager.New(contentItem.ContentType); | ||
var dummyContent = _contentManager.New(originalContentItem.ContentType); | ||
|
||
if (!Services.Authorizer.Authorize(Permissions.EditContent, dummyContent, T("You do not have permission to edit (or create) content."))) | ||
if (!Services.Authorizer.Authorize(Permissions.CreateContent, dummyContent, T("Couldn't create clone content"))) | ||
return new HttpUnauthorizedResult(); | ||
|
||
try { | ||
Services.ContentManager.Clone(contentItem); | ||
var cloneContentItem = _contentManager.Clone(originalContentItem); | ||
|
||
Services.Notifier.Information(T("Successfully cloned. The clone was saved as a draft.")); | ||
if (string.IsNullOrWhiteSpace(returnUrl)) { | ||
var adminRouteValues = _contentManager.GetItemMetadata(cloneContentItem).AdminRouteValues; | ||
return RedirectToRoute(adminRouteValues); | ||
} | ||
catch (InvalidOperationException) { | ||
Services.Notifier.Warning(T("Could not clone the content item.")); | ||
else { | ||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List")); | ||
} | ||
|
||
Services.Notifier.Information(T("Successfully cloned. The clone was saved as a draft.")); | ||
|
||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List")); | ||
} | ||
|
||
[HttpPost] | ||
|
@@ -477,6 +482,60 @@ public class AdminController : ContentControllerBase, IUpdateModel { | |
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) { | ||
ModelState.AddModelError(key, errorMessage.ToString()); | ||
} | ||
|
||
/// <summary> | ||
/// Set VisibleAtFrontEnd flag for menuPart | ||
/// </summary> | ||
/// <param name="menu">The menu item</param> | ||
/// <param name="value">Show->true or Hide->false</param> | ||
private void VisibleAtFrontEnd(MenuPart menu, Boolean value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this method be here? It's working on MenuPart, but this controller is the one for ContentItems |
||
//var menuItems = _contentManager | ||
// .Query<MenuPart, MenuPartRecord>() | ||
// .Where(x => x.MenuId == menu.Menu.Id) | ||
// .List().Where(x => x.MenuPosition.StartsWith(menu.MenuPosition + ".")) | ||
// .Select(x => x.As<MenuPart>()) | ||
// .ToList(); | ||
//foreach (var menuItem in menuItems.Concat(new[] { menu })) { | ||
// menuItem.VisibleAtFrontEnd = value; | ||
//} | ||
menu.VisibleAtFrontEnd = value; | ||
//Trigger the change of NavigationContentItems in modules that cache this type of content | ||
_signals.Trigger("NavigationContentItems.Changed"); | ||
} | ||
|
||
/// <summary> | ||
/// Set VisibleAtFrontEnd to false on content's menuPart (for itself not for its children) | ||
/// </summary> | ||
/// <param name="id">ContentItem Id</param> | ||
/// <param name="returnUrl">return Url</param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public ActionResult Hide(int id, string returnUrl) { | ||
var contentItem = _contentManager.Get(id, VersionOptions.Latest); | ||
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Couldn't edit content"))) | ||
return new HttpUnauthorizedResult(); | ||
MenuPart menuPart = contentItem.As<MenuPart>(); | ||
if (menuPart != null) | ||
VisibleAtFrontEnd(menuPart, false); | ||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List")); | ||
} | ||
|
||
/// <summary> | ||
/// Set VisibleAtFrontEnd to true on content's menuPart (for itself not for its children) | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <param name="returnUrl"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public ActionResult Show(int id, string returnUrl) { | ||
var contentItem = _contentManager.Get(id, VersionOptions.Latest); | ||
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Couldn't edit content"))) | ||
return new HttpUnauthorizedResult(); | ||
MenuPart menuPart = contentItem.As<MenuPart>(); | ||
if (menuPart != null) | ||
VisibleAtFrontEnd(menuPart, true); | ||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List")); | ||
} | ||
} | ||
|
||
[Obsolete("Use Orchard.Mvc.FormValueRequiredAttribute instead.")] | ||
|
@@ -492,4 +551,6 @@ public class FormValueRequiredAttribute : ActionMethodSelectorAttribute { | |
return !string.IsNullOrEmpty(value); | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
using Orchard.Core.Contents.Settings; | ||
using Orchard.Data; | ||
using System.Web.Routing; | ||
using Orchard.Time; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
|
||
namespace Orchard.Core.Navigation.Controllers { | ||
[ValidateInput(false)] | ||
|
@@ -129,6 +130,7 @@ public class AdminController : Controller, IUpdateModel { | |
IsMenuItem = menuPart.Is<MenuItemPart>(), | ||
Text = menuPart.MenuText, | ||
Position = menuPart.MenuPosition, | ||
VisibleAtFrontEnd = menuPart.VisibleAtFrontEnd, | ||
Url = menuPart.Is<MenuItemPart>() | ||
? menuPart.As<MenuItemPart>().Url | ||
: _navigationManager.GetUrl(null, Services.ContentManager.GetItemMetadata(menuPart).DisplayRouteValues), | ||
|
@@ -248,6 +250,8 @@ public class AdminController : Controller, IUpdateModel { | |
return View(model); | ||
} | ||
|
||
|
||
|
||
[HttpPost, ActionName("Edit")] | ||
[Mvc.FormValueRequired("submit.Save")] | ||
public ActionResult EditPOST(int id, string returnUrl) { | ||
|
@@ -295,5 +299,6 @@ public class AdminController : Controller, IUpdateModel { | |
|
||
return this.RedirectLocal(returnUrl, () => RedirectToAction("Edit", new RouteValueDictionary { { "Id", contentItem.Id } })); | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,15 @@ | |
using Orchard.Core.Navigation.Models; | ||
using Orchard.Localization; | ||
using Orchard.UI.Navigation; | ||
using System.Collections; | ||
using System.Linq; | ||
|
||
namespace Orchard.Core.Navigation.Services { | ||
public class DefaultMenuProvider : IMenuProvider { | ||
private readonly IContentManager _contentManager; | ||
|
||
public DefaultMenuProvider(IContentManager contentManager) { | ||
_contentManager = contentManager; | ||
|
||
_menuPartsMemory = new Dictionary<int, IEnumerable<MenuPart>>(); | ||
} | ||
|
||
|
@@ -27,7 +28,23 @@ public class DefaultMenuProvider : IMenuProvider { | |
.Where(x => x.MenuId == menu.Id) | ||
.List(); | ||
} | ||
var menuParts = _menuPartsMemory[menu.Id]; | ||
var menuParts = _menuPartsMemory[menu.Id].ToList<MenuPart>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ToList is unnecessary. Change the type of the dictionary instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless you think the enumeration is executing the query again. |
||
|
||
//List of hidden items | ||
var menuPartsHidden = _contentManager | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The menus are already loaded from database, why do another query? You can just filter in Linq. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried but the code didn't return the list I expected, this is the committed code I added in my version
|
||
.Query<MenuPart, MenuPartRecord>() | ||
.Where(x => x.MenuId == menu.Id && !x.VisibleAtFrontEnd) | ||
.List(); | ||
|
||
//Removing from menuList the items with VisibleAtFrontEnd set to false | ||
foreach (var itemHidden in menuPartsHidden) { | ||
//Copy the list | ||
MenuPart[] menuParts1 = new MenuPart[menuParts.Count]; | ||
menuParts.CopyTo(menuParts1); | ||
foreach (var item in menuParts1) | ||
if (item.MenuPosition.StartsWith(itemHidden.MenuPosition)) | ||
menuParts.Remove(item); | ||
} | ||
|
||
foreach (var menuPart in menuParts) { | ||
if (menuPart != null) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why these permissions are changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We started from a fork version of LaserOrchard which already has these changes, it is a part of code that I did not write but which I had to integrate to remain aligned with the version we started from. Ours mod is just a proposal, which unfortunately integrates changes that were not made by us, if too different from the code, as far as I'm concerned it can be rejected entirely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are from the dev branch.