Extending Core/Navigation/Controllers/AdminController to be able to publish/unpublish menuitems

This commit is contained in:
Benedek Farkas 2024-11-15 18:21:26 +01:00
parent 7b503709a5
commit bf028e0316
3 changed files with 57 additions and 4 deletions

View File

@ -249,6 +249,44 @@ namespace Orchard.Core.Navigation.Controllers {
});
}
[HttpPost]
// Copy of Contents/AdminController/Publish, but with different permission check and redirect.
public ActionResult Publish(int id) {
var menuPart = _contentManager.GetLatest<MenuPart>(id);
if (menuPart == null)
return HttpNotFound();
if (!_authorizer.Authorize(Permissions.ManageMenus, menuPart.Menu, T("Couldn't manage the menu")))
return new HttpUnauthorizedResult();
_contentManager.Publish(menuPart.ContentItem);
_notifier.Information(string.IsNullOrWhiteSpace(menuPart.TypeDefinition.DisplayName)
? T("Your content has been published.")
: T("Your {0} has been published.", menuPart.TypeDefinition.DisplayName));
return RedirectToAction("Index", new { menuId = menuPart.Menu.Id });
}
[HttpPost]
// Copy of Contents/AdminController/Unpublish, but with different permission check and redirect.
public ActionResult Unpublish(int id) {
var menuPart = _contentManager.GetLatest<MenuPart>(id);
if (menuPart == null)
return HttpNotFound();
if (!_authorizer.Authorize(Permissions.ManageMenus, menuPart.Menu, T("Couldn't manage the menu")))
return new HttpUnauthorizedResult();
_contentManager.Unpublish(menuPart.ContentItem);
_notifier.Information(string.IsNullOrWhiteSpace(menuPart.TypeDefinition.DisplayName)
? T("Your content has been unpublished.")
: T("Your {0} has been unpublished.", menuPart.TypeDefinition.DisplayName));
return RedirectToAction("Index", new { menuId = menuPart.Menu.Id });
}
private MenuItemEntry CreateMenuItemEntries(MenuPart menuPart) {
return new MenuItemEntry {
MenuItemId = menuPart.Id,

View File

@ -19,8 +19,8 @@ namespace Orchard.Core.Navigation.Services {
public IEnumerable<MenuPart> GetMenuParts(int menuId) {
return _contentManager
.Query<MenuPart, MenuPartRecord>()
.Where( x => x.MenuId == menuId)
.Query<MenuPart, MenuPartRecord>(VersionOptions.Latest)
.Where(x => x.MenuId == menuId)
.List();
}

View File

@ -1,4 +1,6 @@
@model NavigationManagementViewModel
@using Orchard.ContentManagement;
@using Orchard.Core.Navigation.ViewModels;
@using Orchard.Utility.Extensions;
@ -115,8 +117,21 @@
<span class="navigation-position"><input type="text" class="text" name="@Html.NameOf(m => m.MenuItemEntries[i].Position)" value="@menuPartEntry.Position" /></span>
<span class="navigation-actions">
<input type="hidden" name="@Html.NameOf(m => m.MenuItemEntries[i].MenuItemId)" value="@menuPartEntry.MenuItemId" />
@Html.ItemEditLink(T("Edit").Text, menuPartEntry.ContentItem, new { returnUrl = Request.RawUrl })@T(" | ")
@Html.Link(T("Delete").Text, Url.ItemRemoveUrl(menuPartEntry.ContentItem,null), new { itemprop = "RemoveUrl UnsafeUrl" })
@{
var menuItemHasPublished = menuPartEntry.ContentItem.HasPublished();
}
@Html.Link(
menuItemHasPublished ? T("Unpublish").Text : T("Publish").Text,
Url.Action(
menuItemHasPublished ? "Unpublish": "Publish",
"Admin",
new { area = "Navigation", id = menuPartEntry.ContentItem.Id }),
new { itemprop = "UnsafeUrl" })
@T(" | ")
@Html.ItemEditLink(T("Edit").Text, menuPartEntry.ContentItem, new { returnUrl = Request.RawUrl })
@T(" | ")
@Html.Link(T("Delete").Text, Url.ItemRemoveUrl(menuPartEntry.ContentItem, null), new { itemprop = "RemoveUrl UnsafeUrl" })
</span>
</div>