Skip to content
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

Update placement GUI. #7877

Merged
merged 8 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Descriptors.ShapePlacementStrategy;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Navigation;
using OrchardCore.Placements.Services;
using OrchardCore.Placements.ViewModels;
using OrchardCore.Routing;
using OrchardCore.Settings;

namespace OrchardCore.Placements.Controllers
{
Expand All @@ -24,40 +29,72 @@ public class AdminController : Controller
private readonly IHtmlLocalizer H;
private readonly IStringLocalizer S;
private readonly INotifier _notifier;
private readonly ISiteService _siteService;
private readonly dynamic New;

public AdminController(
ILogger<AdminController> logger,
IAuthorizationService authorizationService,
PlacementsManager placementsManager,
IHtmlLocalizer<AdminController> htmlLocalizer,
IStringLocalizer<AdminController> stringLocalizer,
INotifier notifier)
INotifier notifier,
ISiteService siteService,
IShapeFactory shapeFactory)
{
_logger = logger;
_authorizationService = authorizationService;
_placementsManager = placementsManager;
_notifier = notifier;
_siteService = siteService;

New = shapeFactory;
H = htmlLocalizer;
S = stringLocalizer;
}

public async Task<IActionResult> Index()
public async Task<IActionResult> Index(ContentOptions options, PagerParameters pagerParameters)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManagePlacements))
{
return Forbid();
}

var siteSettings = await _siteService.GetSiteSettingsAsync();
var pager = new Pager(pagerParameters, siteSettings.PageSize);

var shapeTypes = await _placementsManager.ListShapePlacementsAsync();

return View(new ListShapePlacementsViewModel
var shapeList = shapeTypes.Select(entry => new ShapePlacementViewModel
{
ShapePlacements = shapeTypes.Select(entry => new ShapePlacementViewModel
{
ShapeType = entry.Key
})
});
ShapeType = entry.Key
}).ToList();

if (!string.IsNullOrWhiteSpace(options.Search))
{
shapeList = shapeList.Where(x => x.ShapeType.Contains(options.Search)).ToList();
Skrypt marked this conversation as resolved.
Show resolved Hide resolved
}

var count = shapeList.Count();

shapeList = shapeList.OrderBy(x => x.ShapeType)
.Skip(pager.GetStartIndex())
.Take(pager.PageSize).ToList();

var pagerShape = (await New.Pager(pager)).TotalItemCount(count);

var model = new ListShapePlacementsViewModel
{
ShapePlacements = shapeList,
Pager = pagerShape,
Options = options,
};

model.Options.ContentsBulkAction = new List<SelectListItem>() {
new SelectListItem() { Text = S["Delete"], Value = nameof(ContentsBulkAction.Remove) }
};

return View("Index", model);
}

public async Task<IActionResult> Create(string suggestion, string returnUrl = null)
Expand Down Expand Up @@ -196,6 +233,36 @@ public async Task<IActionResult> Delete(string shapeType, string returnUrl = nul
return RedirectToReturnUrlOrIndex(returnUrl);
}

[HttpPost, ActionName("Index")]
[FormValueRequired("submit.BulkAction")]
public async Task<ActionResult> IndexPost(ViewModels.ContentOptions options, IEnumerable<string> itemIds)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManagePlacements))
{
return Forbid();
}

if (itemIds?.Count() > 0)
{
switch (options.BulkAction)
{
case ContentsBulkAction.None:
break;
case ContentsBulkAction.Remove:
foreach (var item in itemIds)
{
await _placementsManager.RemoveShapePlacementsAsync(item);
}
_notifier.Success(H["Placements successfully removed."]);
break;
default:
throw new ArgumentOutOfRangeException();
}
}

return RedirectToAction("Index");
}

private IActionResult RedirectToReturnUrlOrIndex(string returnUrl)
{
if ((String.IsNullOrEmpty(returnUrl) == false) && (Url.IsLocalUrl(returnUrl)))
Expand Down Expand Up @@ -232,6 +299,7 @@ private static bool IsEmpty(PlacementNode node)
&& (node.Wrappers == null || node.Wrappers.Length == 0);
}


private static bool FilterEquals(JToken token, string value)
{
if (token is JArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public override IDisplayResult Edit(ContentPartDefinition contentPartDefinition)
DisplayType = "Summary",
Description = S["Placement for a {0} part in summary views", displayName]
});

model.ContentSettingsEntries.Add(
new ContentSettingsEntry
{
ShapeType = $"{contentPartDefinition.Name}_Edit",
Description = S["Placement in admin editor for a {0} part", displayName]
});

}).Location("Shortcuts");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public override IDisplayResult Edit(ContentPartFieldDefinition contentPartFieldD
DisplayType = "Summary",
Description = S["Placement for the {0} field in a {1} in summary views", displayName, partName]
});

model.ContentSettingsEntries.Add(
new ContentSettingsEntry
{
ShapeType = $"{shapeType}_Edit",
Differentiator = differentiator,
Description = S["Placement in admin editor for the {0} field in a {1}", displayName, partName]
});

}).Location("Shortcuts");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDef
DisplayType = "Summary",
Description = S["Placement for the {0} part in a {1} type in summary views", partName, displayName]
});

model.ContentSettingsEntries.Add(
new ContentSettingsEntry
{
ShapeType = $"{partName}_Edit",
ContentType = contentType,
Description = S["Placement in admin editor for the {0} part in a {1} type", partName, displayName]
});

}).Location("Shortcuts");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace OrchardCore.Placements.ViewModels
{
public class ListShapePlacementsViewModel
{
public IEnumerable<ShapePlacementViewModel> ShapePlacements { get; set; }
public IList<ShapePlacementViewModel> ShapePlacements { get; set; }
public dynamic Pager { get; set; }
public ContentOptions Options { get; set; } = new ContentOptions();
}

public class ContentOptions
{
public string Search { get; set; }
public ContentsBulkAction BulkAction { get; set; }

#region Lists to populate

[BindNever]
public List<SelectListItem> ContentsBulkAction { get; set; }

#endregion Lists to populate
}

public enum ContentsBulkAction
{
None,
Remove
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
<a asp-route-action="Delete" asp-route-area="OrchardCore.Placements" asp-route-shapeType="@Model.ShapeType" class="btn btn-danger" role="button" itemprop="UnsafeUrl RemoveUrl">@T["Delete"]</a>
}

@if (Url.IsLocalUrl(returnUrl))
{
<a class="btn btn-secondary" href="@returnUrl">@T["Cancel"]</a>
}
<a class="btn btn-secondary cancel" role="button" href="@returnUrl">@T["Cancel"]</a>
</div>
</form>

Expand Down
Loading