From b04a0fc0a50ef58cf9fd5fe437a117b60dd69243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Sun, 25 Jun 2023 20:40:19 +0200 Subject: [PATCH 01/22] Revert "revert last 3 commits" This reverts commit c19ff30c31f79ed8a46c40aa3f476ca901422786. --- .../Controllers/AdminController.cs | 79 +++++++++++++++++++ Lombiq.BaseTheme/Lombiq.BaseTheme.csproj | 1 + Lombiq.BaseTheme/Manifest.cs | 6 +- Lombiq.BaseTheme/Models/BaseThemeSettings.cs | 7 ++ .../Navigation/BaseThemeSettingsAdminMenu.cs | 27 +++++++ .../BaseThemeSettingsPermissions.cs | 13 +++ .../Services/MainMenuWidgetFilter.cs | 10 +-- Lombiq.BaseTheme/Startup.cs | 6 ++ .../ViewModels/BaseThemeSettingsViewModel.cs | 13 +++ Lombiq.BaseTheme/Views/Admin/Index.cshtml | 9 +++ Lombiq.BaseTheme/Views/_ViewImports.cshtml | 1 + 11 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 Lombiq.BaseTheme/Controllers/AdminController.cs create mode 100644 Lombiq.BaseTheme/Models/BaseThemeSettings.cs create mode 100644 Lombiq.BaseTheme/Navigation/BaseThemeSettingsAdminMenu.cs create mode 100644 Lombiq.BaseTheme/Permissions/BaseThemeSettingsPermissions.cs create mode 100644 Lombiq.BaseTheme/ViewModels/BaseThemeSettingsViewModel.cs create mode 100644 Lombiq.BaseTheme/Views/Admin/Index.cshtml diff --git a/Lombiq.BaseTheme/Controllers/AdminController.cs b/Lombiq.BaseTheme/Controllers/AdminController.cs new file mode 100644 index 0000000..ab9b9de --- /dev/null +++ b/Lombiq.BaseTheme/Controllers/AdminController.cs @@ -0,0 +1,79 @@ +using Lombiq.BaseTheme.Models; +using Lombiq.BaseTheme.ViewModels; +using Lombiq.HelpfulExtensions.Extensions.ContentTypes; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OrchardCore.ContentManagement; +using OrchardCore.ContentManagement.Metadata.Models; +using OrchardCore.DisplayManagement; +using OrchardCore.Entities; +using OrchardCore.Media.Fields; +using OrchardCore.Media.Settings; +using OrchardCore.Media.ViewModels; +using OrchardCore.Settings; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Lombiq.BaseTheme.Controllers; + +public class AdminController : Controller +{ + private readonly ISiteService _siteService; + private readonly IShapeFactory _shapeFactory; + public AdminController(ISiteService siteService, IShapeFactory shapeFactory) + { + _siteService = siteService; + _shapeFactory = shapeFactory; + } + + public async Task Index() + { + var section = (await _siteService.GetSiteSettingsAsync()).As(); + + return View(new BaseThemeSettingsViewModel + { + ShowMenu = section.ShowMenu, + Icon = section.Icon, + Editor = await _shapeFactory.CreateAsync("MediaField_Edit", editor => + { + var part = CreatePart(section); + var icon = part.Icon; + + editor.Paths = JsonConvert.SerializeObject(new[] { section.Icon }); + editor.Field = icon; + editor.Part = part; + editor.PartFieldDefinition = new ContentPartFieldDefinition( + new ContentFieldDefinition(nameof(BaseThemeSettingsPart.Icon)), + nameof(BaseThemeSettingsPart.Icon), + JObject.FromObject(new Dictionary { [nameof(MediaFieldSettings)] = new MediaFieldSettings() })) + { + PartDefinition = new ContentPartDefinition(nameof(BaseThemeSettingsPart)), + }; + }), + }); + } + + private static BaseThemeSettingsPart CreatePart(BaseThemeSettings section) + { + var content = new ContentItem { ContentType = ContentTypes.Empty }; + + content.Weld(new BaseThemeSettingsPart + { + ContentItem = content, + Icon = new MediaField + { + ContentItem = content, + MediaTexts = new[] { section.Icon }, + Paths = new[] { section.Icon }, + }, + }); + + return content.As(); + } + + public class BaseThemeSettingsPart : ContentPart + { + public MediaField Icon { get; set; } = new(); + } +} diff --git a/Lombiq.BaseTheme/Lombiq.BaseTheme.csproj b/Lombiq.BaseTheme/Lombiq.BaseTheme.csproj index 73e7284..12fac32 100644 --- a/Lombiq.BaseTheme/Lombiq.BaseTheme.csproj +++ b/Lombiq.BaseTheme/Lombiq.BaseTheme.csproj @@ -34,6 +34,7 @@ + diff --git a/Lombiq.BaseTheme/Manifest.cs b/Lombiq.BaseTheme/Manifest.cs index 3d95563..0aedbb2 100644 --- a/Lombiq.BaseTheme/Manifest.cs +++ b/Lombiq.BaseTheme/Manifest.cs @@ -8,5 +8,9 @@ Description = "The base frontend theme for shared code that is not specific to a specific project's theme." + "Warning: themes using this as the base remove the stock Bootstrap resource. If you switch to a different " + "theme, please reload the tenant from Configuration → Tenants in the admin menu.", - Dependencies = new[] { Lombiq.HelpfulExtensions.FeatureIds.Widgets } + Dependencies = new[] + { + Lombiq.HelpfulExtensions.FeatureIds.ContentTypes, + Lombiq.HelpfulExtensions.FeatureIds.Widgets, + } )] diff --git a/Lombiq.BaseTheme/Models/BaseThemeSettings.cs b/Lombiq.BaseTheme/Models/BaseThemeSettings.cs new file mode 100644 index 0000000..a619263 --- /dev/null +++ b/Lombiq.BaseTheme/Models/BaseThemeSettings.cs @@ -0,0 +1,7 @@ +namespace Lombiq.BaseTheme.Models; + +public class BaseThemeSettings +{ + public string Icon { get; set; } + public bool ShowMenu { get; set; } +} diff --git a/Lombiq.BaseTheme/Navigation/BaseThemeSettingsAdminMenu.cs b/Lombiq.BaseTheme/Navigation/BaseThemeSettingsAdminMenu.cs new file mode 100644 index 0000000..cb49508 --- /dev/null +++ b/Lombiq.BaseTheme/Navigation/BaseThemeSettingsAdminMenu.cs @@ -0,0 +1,27 @@ +using Lombiq.BaseTheme.Controllers; +using Lombiq.BaseTheme.Permissions; +using Lombiq.HelpfulLibraries.OrchardCore.Navigation; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Localization; +using OrchardCore.Navigation; + +namespace Lombiq.BaseTheme.Navigation; + +public class BaseThemeSettingsAdminMenu : AdminMenuNavigationProviderBase +{ + public BaseThemeSettingsAdminMenu( + IHttpContextAccessor hca, + IStringLocalizer stringLocalizer) + : base(hca, stringLocalizer) + { + } + + protected override void Build(NavigationBuilder builder) => + builder.Add(T["Configuration"], configuration => configuration + .Add(T["Settings"], settings => settings + .Add(T["Base Theme"], T["Base Theme"], baseTheme => baseTheme + .ActionTask(_hca.HttpContext, controller => controller.Index()) + .Permission(BaseThemeSettingsPermissions.ManageBaseThemeSettings) + .LocalNav() + ))); +} diff --git a/Lombiq.BaseTheme/Permissions/BaseThemeSettingsPermissions.cs b/Lombiq.BaseTheme/Permissions/BaseThemeSettingsPermissions.cs new file mode 100644 index 0000000..4512f0a --- /dev/null +++ b/Lombiq.BaseTheme/Permissions/BaseThemeSettingsPermissions.cs @@ -0,0 +1,13 @@ +using Lombiq.HelpfulLibraries.OrchardCore.Users; +using OrchardCore.Security.Permissions; +using System.Collections.Generic; + +namespace Lombiq.BaseTheme.Permissions; + +public class BaseThemeSettingsPermissions : AdminPermissionBase +{ + public static readonly Permission ManageBaseThemeSettings = + new(nameof(ManageBaseThemeSettings), "Manage Lombiq.BaseTheme Settings."); + + protected override IEnumerable AdminPermissions => new[] { ManageBaseThemeSettings }; +} diff --git a/Lombiq.BaseTheme/Services/MainMenuWidgetFilter.cs b/Lombiq.BaseTheme/Services/MainMenuWidgetFilter.cs index 6dc4a01..8572659 100644 --- a/Lombiq.BaseTheme/Services/MainMenuWidgetFilter.cs +++ b/Lombiq.BaseTheme/Services/MainMenuWidgetFilter.cs @@ -1,4 +1,5 @@ -using Lombiq.HelpfulExtensions.Extensions.Widgets; +using Lombiq.BaseTheme.Constants; +using Lombiq.HelpfulExtensions.Extensions.Widgets; using Lombiq.HelpfulExtensions.Extensions.Widgets.ViewModels; using Lombiq.HelpfulLibraries.OrchardCore.Mvc; using Lombiq.HelpfulLibraries.OrchardCore.Navigation; @@ -8,7 +9,6 @@ using OrchardCore.DisplayManagement.Layout; using OrchardCore.Navigation; using System.Threading.Tasks; -using static Lombiq.BaseTheme.Constants.ZoneNames; namespace Lombiq.BaseTheme.Services; @@ -18,7 +18,7 @@ public class MainMenuWidgetFilter : WidgetFilterBase private readonly IActionContextAccessor _actionContextAccessor; private readonly ICssClassHolder _cssClassHolder; - protected override string ZoneName => Navigation; + protected override string ZoneName => ZoneNames.Navigation; protected override string ViewName => WidgetTypes.MenuWidget; protected override bool FrontEndOnly => true; @@ -39,8 +39,8 @@ public MainMenuWidgetFilter( protected override async Task GetViewModelAsync() { // Add the