From 899141cc2c16a6557419f904bf2f07c1029f1b22 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 12:25:12 -0800 Subject: [PATCH 1/7] Add title to background tasks and improve the UI --- .../ScheduledArchivingBackgroundTask.cs | 5 ++- .../Services/AuditTrailBackgroundTask.cs | 6 ++- .../Controllers/BackgroundTaskController.cs | 37 +++++++++------- .../Views/BackgroundTask/Create.cshtml | 29 ++++--------- .../Views/BackgroundTask/Edit.cshtml | 28 +++++------- .../Views/BackgroundTask/Index.cshtml | 43 ++++++++++++------- .../Tasks/OpenIdBackgroundTask.cs | 5 ++- .../ScheduledPublishingBackgroundTask.cs | 5 ++- .../Services/IndexingBackgroundTask.cs | 5 ++- .../Cache/SitemapCacheBackgroundTask.cs | 5 ++- .../Timers/TimerBackgroundTask.cs | 5 ++- .../BackgroundTaskAttribute.cs | 1 + .../BackgroundTaskExtensions.cs | 23 +++++++--- .../BackgroundTasks/BackgroundTaskSettings.cs | 1 + .../Services/IndexingBackgroundTask.cs | 7 ++- 15 files changed, 123 insertions(+), 82 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.ArchiveLater/Services/ScheduledArchivingBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.ArchiveLater/Services/ScheduledArchivingBackgroundTask.cs index 46b86ee1bd7..8fed89fac5c 100644 --- a/src/OrchardCore.Modules/OrchardCore.ArchiveLater/Services/ScheduledArchivingBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.ArchiveLater/Services/ScheduledArchivingBackgroundTask.cs @@ -14,7 +14,10 @@ namespace OrchardCore.ArchiveLater.Services; -[BackgroundTask(Schedule = "* * * * *", Description = "Archives content items when their scheduled archive date time arrives.")] +[BackgroundTask( + Title = "Content Items Archiver", + Schedule = "* * * * *", + Description = "Archives content items when their scheduled archive date time arrives.")] public class ScheduledArchivingBackgroundTask : IBackgroundTask { private readonly ILogger _logger; diff --git a/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/AuditTrailBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/AuditTrailBackgroundTask.cs index dca7360b570..41eb2e3f8a4 100644 --- a/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/AuditTrailBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/AuditTrailBackgroundTask.cs @@ -11,8 +11,10 @@ namespace OrchardCore.AuditTrail.Services { - [BackgroundTask(Schedule = "0 0 * * *", - Description = "A background task that regularly deletes old Audit Trail Events.", + [BackgroundTask( + Title = "Audit Trail Events Purger", + Schedule = "0 0 * * *", + Description = "Regularly purges old Audit Trail events.", LockTimeout = 3_000, LockExpiration = 30_000)] public class AuditTrailBackgroundTask : IBackgroundTask diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs index 230a759da98..150705e9283 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs @@ -87,7 +87,7 @@ public async Task Create(string name) return Forbid(); } - var model = new BackgroundTaskViewModel() { Name = name }; + var model = new BackgroundTaskViewModel() { Name = name, Title = name }; var task = _backgroundTasks.GetTaskByName(name); @@ -95,6 +95,7 @@ public async Task Create(string name) { var settings = task.GetDefaultSettings(); + model.Title = settings.Title; model.Enable = settings.Enable; model.Schedule = settings.Schedule; model.DefaultSchedule = settings.Schedule; @@ -127,6 +128,7 @@ public async Task CreatePost(BackgroundTaskViewModel model) var settings = new BackgroundTaskSettings { Name = model.Name, + Title = model.Title, Enable = model.Enable, Schedule = model.Schedule?.Trim(), Description = model.Description, @@ -163,6 +165,7 @@ public async Task Edit(string name) var model = new BackgroundTaskViewModel { Name = name, + Title = settings.Title, Enable = settings.Enable, Schedule = settings.Schedule, DefaultSchedule = task?.GetDefaultSettings().Schedule, @@ -188,26 +191,28 @@ public async Task Edit(BackgroundTaskViewModel model) { ModelState.AddModelError(nameof(BackgroundTaskViewModel.Name), S["The name is mandatory."]); } - } - - if (ModelState.IsValid) - { - var settings = new BackgroundTaskSettings + else { - Name = model.Name, - Enable = model.Enable, - Schedule = model.Schedule?.Trim(), - Description = model.Description, - LockTimeout = model.LockTimeout, - LockExpiration = model.LockExpiration - }; + var document = await _backgroundTaskManager.GetDocumentAsync(); - await _backgroundTaskManager.UpdateAsync(model.Name, settings); + if (!document.Settings.ContainsKey(model.Name)) + { + return NotFound(); + } - return RedirectToAction(nameof(Index)); + var settings = document.Settings[model.Name]; + + settings.Schedule = model.Schedule?.Trim(); + settings.LockTimeout = model.LockTimeout; + settings.LockExpiration = model.LockExpiration; + + await _backgroundTaskManager.UpdateAsync(model.Name, settings); + + return RedirectToAction(nameof(Index)); + } } - // If we got this far, something failed, redisplay form + // If we got this far, something failed. Re-display form. return View(model); } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml index 3fb29098618..b89c515159b 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml @@ -16,10 +16,15 @@
-
- - - @T["The task on which this settings will apply."] + +
+ + +
+ +
+ +
@@ -28,12 +33,6 @@ @T["The task schedule as a cron expression."]
-
- - - -
-
@T["Invalid lock timeout value in the advanced tab"]
@T["Invalid lock expiration value in the advanced tab"]
@@ -61,13 +60,3 @@ @T["Cancel"]
- - diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Edit.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Edit.cshtml index 114c4aba9dd..9bb82029049 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Edit.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Edit.cshtml @@ -16,10 +16,11 @@
-
- - - @T["The task on which this settings will apply."] + +
+ + +
@@ -28,12 +29,6 @@ @T["The task schedule as a cron expression."]
-
- - - -
-
@T["Invalid lock timeout value in the advanced tab"]
@T["Invalid lock expiration value in the advanced tab"]
@@ -63,11 +58,10 @@ diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml index 2067e45f89c..3498a189a37 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml @@ -3,7 +3,8 @@

@RenderTitleSegments(T["Background Tasks"])

-
@* the form is necessary to generate an antiforgery token for the delete action *@ + + @* the form is necessary to generate an antiforgery token for the delete action *@ @if (Model.Tasks.Any()) { @@ -11,23 +12,35 @@ @foreach (var settings in Model.Tasks.Select(t => t.Settings)) {
  • -
    -
    - @if (@settings.Enable) - { - @T["Disable"] - } - else - { - @T["Enable"] - } +
    + + +
    + @if (@settings.Enable) + { + @T["Disable"] + } + else + { + @T["Enable"] + } @T["Edit"]
    - - @settings.Name - @settings.Description
    + + @if (!String.IsNullOrWhiteSpace(settings.Description)) + { +
    +
    +

    @settings.Description

    +
    +
    + } +
  • } @@ -35,7 +48,7 @@ else { }
    diff --git a/src/OrchardCore.Modules/OrchardCore.OpenId/Tasks/OpenIdBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.OpenId/Tasks/OpenIdBackgroundTask.cs index 9e39de3fa57..1d6ac0b848f 100644 --- a/src/OrchardCore.Modules/OrchardCore.OpenId/Tasks/OpenIdBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.OpenId/Tasks/OpenIdBackgroundTask.cs @@ -10,7 +10,10 @@ namespace OrchardCore.OpenId.Tasks { - [BackgroundTask(Schedule = "*/30 * * * *", Description = "Performs various cleanup operations for OpenID-related features.")] + [BackgroundTask( + Title = "OpenID Cleaner", + Schedule = "*/30 * * * *", + Description = "Performs various cleanup operations for OpenID features.")] public class OpenIdBackgroundTask : IBackgroundTask { private readonly ILogger _logger; diff --git a/src/OrchardCore.Modules/OrchardCore.PublishLater/Services/ScheduledPublishingBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.PublishLater/Services/ScheduledPublishingBackgroundTask.cs index 174bb887f12..06502c183b9 100644 --- a/src/OrchardCore.Modules/OrchardCore.PublishLater/Services/ScheduledPublishingBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.PublishLater/Services/ScheduledPublishingBackgroundTask.cs @@ -13,7 +13,10 @@ namespace OrchardCore.PublishLater.Services; -[BackgroundTask(Schedule = "* * * * *", Description = "Publishes content items when their scheduled publish date time arrives.")] +[BackgroundTask( + Title = "Scheduled Content Items Publisher", + Schedule = "* * * * *", + Description = "Publishes content items when their scheduled publish date time arrives.")] public class ScheduledPublishingBackgroundTask : IBackgroundTask { private readonly ILogger _logger; diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Services/IndexingBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Services/IndexingBackgroundTask.cs index 629148bf77f..a0c0616adc0 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Services/IndexingBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Services/IndexingBackgroundTask.cs @@ -12,7 +12,10 @@ namespace OrchardCore.Search.Lucene /// /// This services is only registered from OrchardCore.Search.Lucene.Worker feature. /// - [BackgroundTask(Schedule = "* * * * *", Description = "Update lucene indexes.")] + [BackgroundTask( + Title = "Lucene Indexes Updater", + Schedule = "* * * * *", + Description = "Updates lucene indexes.")] public class IndexingBackgroundTask : IBackgroundTask { public Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken) diff --git a/src/OrchardCore.Modules/OrchardCore.Sitemaps/Cache/SitemapCacheBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.Sitemaps/Cache/SitemapCacheBackgroundTask.cs index 712049b402f..2c9b8498415 100644 --- a/src/OrchardCore.Modules/OrchardCore.Sitemaps/Cache/SitemapCacheBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.Sitemaps/Cache/SitemapCacheBackgroundTask.cs @@ -8,7 +8,10 @@ namespace OrchardCore.Sitemaps.Cache { - [BackgroundTask(Schedule = "*/5 * * * *", Description = "Cleanup sitemap cache files.")] + [BackgroundTask( + Title = "Sitemap Cache Cleaner", + Schedule = "*/5 * * * *", + Description = "Cleans up sitemap cache files.")] public class SitemapCacheBackgroundTask : IBackgroundTask { public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken) diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/Timers/TimerBackgroundTask.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/Timers/TimerBackgroundTask.cs index 645ee42efd8..22c0c1e0cab 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/Timers/TimerBackgroundTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/Timers/TimerBackgroundTask.cs @@ -7,7 +7,10 @@ namespace OrchardCore.Workflows.Timers { - [BackgroundTask(Schedule = "* * * * *", Description = "Trigger workflow timer events.")] + [BackgroundTask( + Title = "Timed Workflow Starter", + Schedule = "* * * * *", + Description = "Triggers timed workflow events.")] public class TimerBackgroundTask : IBackgroundTask { public Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken) diff --git a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskAttribute.cs b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskAttribute.cs index 08f7cbf6bd1..0e471b0a41c 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskAttribute.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskAttribute.cs @@ -5,6 +5,7 @@ namespace OrchardCore.BackgroundTasks [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] public class BackgroundTaskAttribute : Attribute { + public string Title { get; set; } public bool Enable { get; set; } = true; public string Schedule { get; set; } = "*/5 * * * *"; public string Description { get; set; } = String.Empty; diff --git a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs index 9db626a60ff..9433e8bc32b 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -8,24 +9,36 @@ public static class BackgroundTaskExtensions { public static BackgroundTaskSettings GetDefaultSettings(this IBackgroundTask task) { - var type = task.GetType(); + var technicalName = task.GetTaskName(); - var attribute = type.GetCustomAttribute(); + var attribute = task.GetType().GetCustomAttribute(); if (attribute != null) { - return new BackgroundTaskSettings + var settings = new BackgroundTaskSettings { - Name = type.FullName, + Title = attribute.Title, + Name = technicalName, Enable = attribute.Enable, Schedule = attribute.Schedule, Description = attribute.Description, LockTimeout = attribute.LockTimeout, LockExpiration = attribute.LockExpiration }; + + if (String.IsNullOrWhiteSpace(settings.Title)) + { + settings.Title = technicalName; + } + + return settings; } - return new BackgroundTaskSettings() { Name = type.FullName }; + return new BackgroundTaskSettings() + { + Name = technicalName, + Title = technicalName + }; } public static IBackgroundTask GetTaskByName(this IEnumerable tasks, string name) diff --git a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskSettings.cs b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskSettings.cs index 7139b1f079e..2bd227bc469 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskSettings.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskSettings.cs @@ -4,6 +4,7 @@ namespace OrchardCore.BackgroundTasks { public class BackgroundTaskSettings { + public string Title { get; set; } public string Name { get; set; } = String.Empty; public bool Enable { get; set; } = true; public string Schedule { get; set; } = "* * * * *"; diff --git a/src/OrchardCore/OrchardCore.Search.Elasticsearch.Core/Services/IndexingBackgroundTask.cs b/src/OrchardCore/OrchardCore.Search.Elasticsearch.Core/Services/IndexingBackgroundTask.cs index 3338409942b..1cb0d6abfc1 100644 --- a/src/OrchardCore/OrchardCore.Search.Elasticsearch.Core/Services/IndexingBackgroundTask.cs +++ b/src/OrchardCore/OrchardCore.Search.Elasticsearch.Core/Services/IndexingBackgroundTask.cs @@ -12,7 +12,12 @@ namespace OrchardCore.Search.Elasticsearch.Core.Services /// /// This services is only registered from OrchardCore.Search.Elasticsearch.Worker feature. /// - [BackgroundTask(Schedule = "* * * * *", Description = "Update Elasticsearch indexes.", LockTimeout = 1000, LockExpiration = 300000)] + [BackgroundTask( + Title = "Elasticsearch Indexes Updater", + Schedule = "* * * * *", + Description = "Updates Elasticsearch indexes.", + LockTimeout = 1000, + LockExpiration = 300000)] public class IndexingBackgroundTask : IBackgroundTask { public Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken) From fc514689148f0d6dd442399e7d9e504b6a0e6513 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 12:53:20 -0800 Subject: [PATCH 2/7] Add search bar for tasks --- .../Views/BackgroundTask/Index.cshtml | 84 +++++++++++++++++-- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml index 3498a189a37..f0eda6ac388 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml @@ -3,15 +3,32 @@

    @RenderTitleSegments(T["Background Tasks"])

    +
    +
    +
    + +
    +
    + +
    +
    + +
    +
    +
    +
    @* the form is necessary to generate an antiforgery token for the delete action *@ - @if (Model.Tasks.Any()) + @if (Model.Tasks.Count > 0) {
      @foreach (var settings in Model.Tasks.Select(t => t.Settings)) { -
    • +
    • @@ -45,12 +62,63 @@ }
    } - else - { - - } + + +
    @await DisplayAsync(Model.Pager) + + From 5c65002a4b69ee56ac9fcc479e550e95d5ec0d08 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 14:35:30 -0800 Subject: [PATCH 3/7] Cleanup the background controller by removing the Create action --- .../Controllers/BackgroundTaskController.cs | 214 ++++++++---------- .../Models/BackgroundTaskDocument.cs | 3 +- .../OrchardCore.BackgroundTasks/Startup.cs | 9 +- .../Views/BackgroundTask/Create.cshtml | 62 ----- .../BackgroundTaskExtensions.cs | 2 +- 5 files changed, 99 insertions(+), 191 deletions(-) delete mode 100644 src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs index 150705e9283..0f0ee899b27 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs @@ -4,13 +4,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Localization; +using Microsoft.AspNetCore.Mvc.Localization; using Microsoft.Extensions.Options; using OrchardCore.Admin; +using OrchardCore.BackgroundTasks.Models; using OrchardCore.BackgroundTasks.Services; using OrchardCore.BackgroundTasks.ViewModels; using OrchardCore.DisplayManagement; -using OrchardCore.Environment.Shell; +using OrchardCore.DisplayManagement.Notify; using OrchardCore.Navigation; namespace OrchardCore.BackgroundTasks.Controllers @@ -18,31 +19,31 @@ namespace OrchardCore.BackgroundTasks.Controllers [Admin] public class BackgroundTaskController : Controller { - private readonly string _tenant; private readonly IAuthorizationService _authorizationService; private readonly IEnumerable _backgroundTasks; private readonly BackgroundTaskManager _backgroundTaskManager; private readonly PagerOptions _pagerOptions; - private readonly IStringLocalizer S; + private readonly INotifier _notifier; private readonly dynamic New; + private readonly IHtmlLocalizer H; public BackgroundTaskController( - ShellSettings shellSettings, IAuthorizationService authorizationService, IEnumerable backgroundTasks, BackgroundTaskManager backgroundTaskManager, IShapeFactory shapeFactory, IOptions pagerOptions, - IStringLocalizer stringLocalizer) + IHtmlLocalizer htmlLocalizer, + INotifier notifier) { - _tenant = shellSettings.Name; _authorizationService = authorizationService; _backgroundTasks = backgroundTasks; _backgroundTaskManager = backgroundTaskManager; _pagerOptions = pagerOptions.Value; + _notifier = notifier; New = shapeFactory; - S = stringLocalizer; + H = htmlLocalizer; } public async Task Index(PagerParameters pagerParameters) @@ -55,19 +56,11 @@ public async Task Index(PagerParameters pagerParameters) var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); var document = await _backgroundTaskManager.GetDocumentAsync(); - var taskEntries = _backgroundTasks.Select(t => - { - if (!document.Settings.TryGetValue(t.GetTaskName(), out var settings)) - { - settings = t.GetDefaultSettings(); - } - - return new BackgroundTaskEntry() { Settings = settings }; - }) - .OrderBy(entry => entry.Settings.Name) - .Skip(pager.GetStartIndex()) - .Take(pager.PageSize) - .ToList(); + var taskEntries = _backgroundTasks.Select(task => new BackgroundTaskEntry() { Settings = GetSettings(task, document) }) + .OrderBy(entry => entry.Settings.Name) + .Skip(pager.GetStartIndex()) + .Take(pager.PageSize) + .ToList(); var pagerShape = (await New.Pager(pager)).TotalItemCount(_backgroundTasks.Count()); @@ -80,87 +73,28 @@ public async Task Index(PagerParameters pagerParameters) return View(model); } - public async Task Create(string name) + public async Task Edit(string name) { - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) - { - return Forbid(); - } - - var model = new BackgroundTaskViewModel() { Name = name, Title = name }; - - var task = _backgroundTasks.GetTaskByName(name); - - if (task != null) + if (String.IsNullOrWhiteSpace(name)) { - var settings = task.GetDefaultSettings(); - - model.Title = settings.Title; - model.Enable = settings.Enable; - model.Schedule = settings.Schedule; - model.DefaultSchedule = settings.Schedule; - model.Description = settings.Description; - model.LockTimeout = settings.LockTimeout; - model.LockExpiration = settings.LockExpiration; + return NotFound(); } - return View(model); - } - - [HttpPost, ActionName("Create")] - public async Task CreatePost(BackgroundTaskViewModel model) - { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); } - if (ModelState.IsValid) - { - if (String.IsNullOrWhiteSpace(model.Name)) - { - ModelState.AddModelError(nameof(BackgroundTaskViewModel.Name), S["The name is mandatory."]); - } - } - - if (ModelState.IsValid) - { - var settings = new BackgroundTaskSettings - { - Name = model.Name, - Title = model.Title, - Enable = model.Enable, - Schedule = model.Schedule?.Trim(), - Description = model.Description, - LockTimeout = model.LockTimeout, - LockExpiration = model.LockExpiration - }; - - await _backgroundTaskManager.UpdateAsync(model.Name, settings); - - return RedirectToAction(nameof(Index)); - } - - return View(model); - } + var task = _backgroundTasks.GetTaskByName(name); - public async Task Edit(string name) - { - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) + if (task == null) { - return Forbid(); + return NotFound(); } var document = await _backgroundTaskManager.GetDocumentAsync(); - if (!document.Settings.ContainsKey(name)) - { - return RedirectToAction(nameof(Create), new { name }); - } - - var task = _backgroundTasks.GetTaskByName(name); - - var settings = document.Settings[name]; + var settings = GetSettings(task, document); var model = new BackgroundTaskViewModel { @@ -185,40 +119,46 @@ public async Task Edit(BackgroundTaskViewModel model) return Forbid(); } + if (String.IsNullOrWhiteSpace(model.Name)) + { + return NotFound(); + } + + var task = _backgroundTasks.GetTaskByName(model.Name); + + if (task == null) + { + return NotFound(); + } + if (ModelState.IsValid) { - if (String.IsNullOrWhiteSpace(model.Name)) - { - ModelState.AddModelError(nameof(BackgroundTaskViewModel.Name), S["The name is mandatory."]); - } - else - { - var document = await _backgroundTaskManager.GetDocumentAsync(); + var document = await _backgroundTaskManager.LoadDocumentAsync(); - if (!document.Settings.ContainsKey(model.Name)) - { - return NotFound(); - } + var settings = GetSettings(task, document); - var settings = document.Settings[model.Name]; + settings.Schedule = model.Schedule?.Trim(); + settings.LockTimeout = model.LockTimeout; + settings.LockExpiration = model.LockExpiration; - settings.Schedule = model.Schedule?.Trim(); - settings.LockTimeout = model.LockTimeout; - settings.LockExpiration = model.LockExpiration; + await _backgroundTaskManager.UpdateAsync(model.Name, settings); - await _backgroundTaskManager.UpdateAsync(model.Name, settings); + await _notifier.SuccessAsync(H["The task has been updated."]); - return RedirectToAction(nameof(Index)); - } + return RedirectToAction(nameof(Index)); } - // If we got this far, something failed. Re-display form. return View(model); } [HttpPost] public async Task Delete(string name) { + if (String.IsNullOrWhiteSpace(name)) + { + return NotFound(); + } + if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); @@ -233,29 +173,40 @@ public async Task Delete(string name) await _backgroundTaskManager.RemoveAsync(name); + await _notifier.SuccessAsync(H["The task has been deleted."]); + return RedirectToAction(nameof(Index)); } [HttpPost] public async Task Enable(string name) { + if (String.IsNullOrWhiteSpace(name)) + { + return NotFound(); + } + if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); } - var document = await _backgroundTaskManager.LoadDocumentAsync(); + var task = _backgroundTasks.GetTaskByName(name); - if (!document.Settings.TryGetValue(name, out var settings)) + if (task == null) { - settings = _backgroundTasks.GetTaskByName(name)?.GetDefaultSettings(); + return NotFound(); } - if (settings != null) - { - settings.Enable = true; - await _backgroundTaskManager.UpdateAsync(name, settings); - } + var document = await _backgroundTaskManager.LoadDocumentAsync(); + + var settings = GetSettings(task, document); + + settings.Enable = true; + + await _backgroundTaskManager.UpdateAsync(name, settings); + + await _notifier.SuccessAsync(H["The task has been enabled."]); return RedirectToAction(nameof(Index)); } @@ -263,25 +214,46 @@ public async Task Enable(string name) [HttpPost] public async Task Disable(string name) { + if (String.IsNullOrWhiteSpace(name)) + { + return NotFound(); + } + if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); } - var document = await _backgroundTaskManager.LoadDocumentAsync(); + var task = _backgroundTasks.GetTaskByName(name); - if (!document.Settings.TryGetValue(name, out var settings)) + if (task == null) { - settings = _backgroundTasks.GetTaskByName(name)?.GetDefaultSettings(); + return NotFound(); } - if (settings != null) + var document = await _backgroundTaskManager.LoadDocumentAsync(); + + var settings = GetSettings(task, document); + + settings.Enable = false; + + await _backgroundTaskManager.UpdateAsync(name, settings); + + await _notifier.SuccessAsync(H["The task has been disabled."]); + + return RedirectToAction(nameof(Index)); + } + + private static BackgroundTaskSettings GetSettings(IBackgroundTask task, BackgroundTaskDocument document) + { + var name = task.GetTaskName(); + + if (document.Settings.ContainsKey(name)) { - settings.Enable = false; - await _backgroundTaskManager.UpdateAsync(name, settings); + return document.Settings[name]; } - return RedirectToAction(nameof(Index)); + return task.GetDefaultSettings(); } } } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Models/BackgroundTaskDocument.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Models/BackgroundTaskDocument.cs index b1c73a4eb5e..cef98c7754c 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Models/BackgroundTaskDocument.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Models/BackgroundTaskDocument.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using OrchardCore.Data.Documents; @@ -5,6 +6,6 @@ namespace OrchardCore.BackgroundTasks.Models { public class BackgroundTaskDocument : Document { - public Dictionary Settings { get; } = new Dictionary(); + public Dictionary Settings { get; } = new(StringComparer.OrdinalIgnoreCase); } } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Startup.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Startup.cs index f2429460ff4..0ddd1b21481 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Startup.cs @@ -41,24 +41,21 @@ public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilde pattern: _adminOptions.AdminUrlPrefix + "/BackgroundTasks", defaults: new { controller = backgroundTaskControllerName, action = nameof(BackgroundTaskController.Index) } ); - routes.MapAreaControllerRoute( - name: "BackgroundTasksCreate", - areaName: "OrchardCore.BackgroundTasks", - pattern: _adminOptions.AdminUrlPrefix + "/BackgroundTasks/Create/{name}", - defaults: new { controller = backgroundTaskControllerName, action = nameof(BackgroundTaskController.Create) } - ); + routes.MapAreaControllerRoute( name: "BackgroundTasksEdit", areaName: "OrchardCore.BackgroundTasks", pattern: _adminOptions.AdminUrlPrefix + "/BackgroundTasks/Edit/{name}", defaults: new { controller = backgroundTaskControllerName, action = nameof(BackgroundTaskController.Edit) } ); + routes.MapAreaControllerRoute( name: "BackgroundTasksEnable", areaName: "OrchardCore.BackgroundTasks", pattern: _adminOptions.AdminUrlPrefix + "/BackgroundTasks/Enable/{name}", defaults: new { controller = backgroundTaskControllerName, action = nameof(BackgroundTaskController.Enable) } ); + routes.MapAreaControllerRoute( name: "BackgroundTasksDisable", areaName: "OrchardCore.BackgroundTasks", diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml deleted file mode 100644 index b89c515159b..00000000000 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Create.cshtml +++ /dev/null @@ -1,62 +0,0 @@ -@model BackgroundTaskViewModel - -

    @RenderTitleSegments(T["Edit Task Settings"])

    - -
    -
    - - - -
    -
    - -
    - - -
    - -
    - - -
    - -
    - - - @T["The task schedule as a cron expression."] -
    - -
    @T["Invalid lock timeout value in the advanced tab"]
    -
    @T["Invalid lock expiration value in the advanced tab"]
    -
    - -
    -
    - - - @T["The timeout in milliseconds to acquire a lock before executing the task atomically. Leave it to zero to not use any locking."] - @T["Ignored if there is no distributed lock feature enabled."] -
    - -
    - - - @T["The expiration in milliseconds of the lock acquired before executing the task atomically. Leave it to zero to not use any locking."] - @T["Ignored if there is no distributed lock feature enabled."] -
    -
    -
    - -
    - - - @T["Cancel"] -
    -
    diff --git a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs index 9433e8bc32b..b3104ff3692 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs @@ -43,7 +43,7 @@ public static BackgroundTaskSettings GetDefaultSettings(this IBackgroundTask tas public static IBackgroundTask GetTaskByName(this IEnumerable tasks, string name) { - return tasks.LastOrDefault(t => t.GetTaskName() == name); + return tasks.LastOrDefault(t => String.Equals(t.GetTaskName(), name, StringComparison.OrdinalIgnoreCase)); } public static string GetTaskName(this IBackgroundTask task) From 509d8844b77534db4fa5dfbd0ee40631569abf9e Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 17:28:30 -0800 Subject: [PATCH 4/7] cleanup search bar --- .../Controllers/BackgroundTaskController.cs | 39 +++++++++--- .../ViewModels/AdminIndexOptions.cs | 6 ++ .../BackgroundTaskIndexViewModel.cs | 6 ++ .../Views/BackgroundTask/Index.cshtml | 61 ++----------------- 4 files changed, 46 insertions(+), 66 deletions(-) create mode 100644 src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs index 0f0ee899b27..4df12e191ab 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Options; using OrchardCore.Admin; using OrchardCore.BackgroundTasks.Models; @@ -13,6 +14,7 @@ using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Notify; using OrchardCore.Navigation; +using OrchardCore.Routing; namespace OrchardCore.BackgroundTasks.Controllers { @@ -46,33 +48,50 @@ public BackgroundTaskController( H = htmlLocalizer; } - public async Task Index(PagerParameters pagerParameters) + public async Task Index(AdminIndexOptions options, PagerParameters pagerParameters) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); } - var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); var document = await _backgroundTaskManager.GetDocumentAsync(); - var taskEntries = _backgroundTasks.Select(task => new BackgroundTaskEntry() { Settings = GetSettings(task, document) }) - .OrderBy(entry => entry.Settings.Name) - .Skip(pager.GetStartIndex()) - .Take(pager.PageSize) - .ToList(); + var items = _backgroundTasks.Select(task => new BackgroundTaskEntry() { Settings = GetSettings(task, document) }); - var pagerShape = (await New.Pager(pager)).TotalItemCount(_backgroundTasks.Count()); + if (!String.IsNullOrWhiteSpace(options.Search)) + { + items = items.Where(x => x.Settings.Title.Contains(options.Search, StringComparison.OrdinalIgnoreCase) + || (x.Settings.Description != null && x.Settings.Description.Contains(options.Search, StringComparison.OrdinalIgnoreCase)) + ); + } + + var taskItems = items.ToList(); + var routeData = new RouteData(); + routeData.Values.Add("Options.Search", options.Search); + + var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); + var pagerShape = (await New.Pager(pager)).TotalItemCount(taskItems.Count).RouteData(routeData); var model = new BackgroundTaskIndexViewModel { - Tasks = taskEntries, - Pager = pagerShape + Tasks = taskItems.OrderBy(entry => entry.Settings.Title).Skip(pager.GetStartIndex()).Take(pager.PageSize).ToList(), + Pager = pagerShape, + Options = options, }; return View(model); } + [HttpPost, ActionName(nameof(Index))] + [FormValueRequired("submit.Filter")] + public ActionResult IndexFilterPOST(BackgroundTaskIndexViewModel model) + { + return RedirectToAction(nameof(Index), new RouteValueDictionary { + { "Options.Search", model.Options.Search }, + }); + } + public async Task Edit(string name) { if (String.IsNullOrWhiteSpace(name)) diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs new file mode 100644 index 00000000000..5780ac775c2 --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs @@ -0,0 +1,6 @@ +namespace OrchardCore.BackgroundTasks.ViewModels; + +public class AdminIndexOptions +{ + public string Search { get; set; } +} diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/BackgroundTaskIndexViewModel.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/BackgroundTaskIndexViewModel.cs index d11d640b544..0eec0564a9c 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/BackgroundTaskIndexViewModel.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/BackgroundTaskIndexViewModel.cs @@ -1,10 +1,16 @@ using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace OrchardCore.BackgroundTasks.ViewModels { public class BackgroundTaskIndexViewModel { + public AdminIndexOptions Options { get; set; } + + [BindNever] public IList Tasks { get; set; } + + [BindNever] public dynamic Pager { get; set; } } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml index f0eda6ac388..0b946fffacb 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml @@ -1,17 +1,18 @@ @model BackgroundTaskIndexViewModel -@inject OrchardCore.Modules.ILocalClock LocalClock

    @RenderTitleSegments(T["Background Tasks"])

    -
    -
    + + + +
    @@ -70,55 +71,3 @@ @await DisplayAsync(Model.Pager) - - From a5ab08cd9b38cf04878e6d7bf23e336ca806d2e4 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 17:38:54 -0800 Subject: [PATCH 5/7] minor cleanup --- .../Controllers/BackgroundTaskController.cs | 33 ++++--------------- .../BackgroundTaskExtensions.cs | 18 +++++----- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs index 4df12e191ab..5faeff22275 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Localization; using Microsoft.AspNetCore.Routing; +using Microsoft.CodeAnalysis; using Microsoft.Extensions.Options; using OrchardCore.Admin; using OrchardCore.BackgroundTasks.Models; @@ -68,7 +69,7 @@ public async Task Index(AdminIndexOptions options, PagerParameter var taskItems = items.ToList(); var routeData = new RouteData(); - routeData.Values.Add("Options.Search", options.Search); + routeData.Values.Add($"{nameof(BackgroundTaskIndexViewModel.Options)}.{nameof(options.Search)}", options.Search); var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); var pagerShape = (await New.Pager(pager)).TotalItemCount(taskItems.Count).RouteData(routeData); @@ -88,17 +89,12 @@ public async Task Index(AdminIndexOptions options, PagerParameter public ActionResult IndexFilterPOST(BackgroundTaskIndexViewModel model) { return RedirectToAction(nameof(Index), new RouteValueDictionary { - { "Options.Search", model.Options.Search }, + { $"{nameof(model.Options)}.{nameof(AdminIndexOptions.Search)}", model.Options.Search }, }); } public async Task Edit(string name) { - if (String.IsNullOrWhiteSpace(name)) - { - return NotFound(); - } - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); @@ -138,11 +134,6 @@ public async Task Edit(BackgroundTaskViewModel model) return Forbid(); } - if (String.IsNullOrWhiteSpace(model.Name)) - { - return NotFound(); - } - var task = _backgroundTasks.GetTaskByName(model.Name); if (task == null) @@ -173,14 +164,14 @@ public async Task Edit(BackgroundTaskViewModel model) [HttpPost] public async Task Delete(string name) { - if (String.IsNullOrWhiteSpace(name)) + if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { - return NotFound(); + return Forbid(); } - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) + if (String.IsNullOrWhiteSpace(name)) { - return Forbid(); + return NotFound(); } var document = await _backgroundTaskManager.LoadDocumentAsync(); @@ -200,11 +191,6 @@ public async Task Delete(string name) [HttpPost] public async Task Enable(string name) { - if (String.IsNullOrWhiteSpace(name)) - { - return NotFound(); - } - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); @@ -233,11 +219,6 @@ public async Task Enable(string name) [HttpPost] public async Task Disable(string name) { - if (String.IsNullOrWhiteSpace(name)) - { - return NotFound(); - } - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageBackgroundTasks)) { return Forbid(); diff --git a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs index b3104ff3692..39768477bd6 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/BackgroundTaskExtensions.cs @@ -15,9 +15,9 @@ public static BackgroundTaskSettings GetDefaultSettings(this IBackgroundTask tas if (attribute != null) { - var settings = new BackgroundTaskSettings + return new BackgroundTaskSettings { - Title = attribute.Title, + Title = !String.IsNullOrWhiteSpace(attribute.Title) ? attribute.Title : technicalName, Name = technicalName, Enable = attribute.Enable, Schedule = attribute.Schedule, @@ -25,13 +25,6 @@ public static BackgroundTaskSettings GetDefaultSettings(this IBackgroundTask tas LockTimeout = attribute.LockTimeout, LockExpiration = attribute.LockExpiration }; - - if (String.IsNullOrWhiteSpace(settings.Title)) - { - settings.Title = technicalName; - } - - return settings; } return new BackgroundTaskSettings() @@ -43,7 +36,12 @@ public static BackgroundTaskSettings GetDefaultSettings(this IBackgroundTask tas public static IBackgroundTask GetTaskByName(this IEnumerable tasks, string name) { - return tasks.LastOrDefault(t => String.Equals(t.GetTaskName(), name, StringComparison.OrdinalIgnoreCase)); + if (String.IsNullOrEmpty(name)) + { + return null; + } + + return tasks.LastOrDefault(task => String.Equals(task.GetTaskName(), name, StringComparison.OrdinalIgnoreCase)); } public static string GetTaskName(this IBackgroundTask task) From 1a98281ce6670678096d21f156a4aa06390881e6 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 17:46:12 -0800 Subject: [PATCH 6/7] change button order --- .../Views/BackgroundTask/Index.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml index 0b946fffacb..dbaf715a93b 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml @@ -37,6 +37,8 @@
    + @T["Edit"] + @if (@settings.Enable) { @T["Disable"] @@ -45,8 +47,6 @@ { @T["Enable"] } - - @T["Edit"]
    From e3720ee132a86381cc037211d3e80ed2a09a6fcb Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 6 Jan 2023 18:23:15 -0800 Subject: [PATCH 7/7] Ad status filter --- .../Controllers/BackgroundTaskController.cs | 24 ++++++- .../ViewModels/AdminIndexOptions.cs | 8 ++- .../Views/BackgroundTask/Index.cshtml | 66 +++++++++++++------ 3 files changed, 76 insertions(+), 22 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs index 5faeff22275..51b52b51fad 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Controllers/BackgroundTaskController.cs @@ -5,8 +5,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Routing; using Microsoft.CodeAnalysis; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; using OrchardCore.Admin; using OrchardCore.BackgroundTasks.Models; @@ -28,15 +30,17 @@ public class BackgroundTaskController : Controller private readonly PagerOptions _pagerOptions; private readonly INotifier _notifier; private readonly dynamic New; + private readonly IStringLocalizer S; private readonly IHtmlLocalizer H; public BackgroundTaskController( IAuthorizationService authorizationService, IEnumerable backgroundTasks, BackgroundTaskManager backgroundTaskManager, - IShapeFactory shapeFactory, IOptions pagerOptions, + IShapeFactory shapeFactory, IHtmlLocalizer htmlLocalizer, + IStringLocalizer stringLocalizer, INotifier notifier) { _authorizationService = authorizationService; @@ -46,6 +50,7 @@ public BackgroundTaskController( _notifier = notifier; New = shapeFactory; + S = stringLocalizer; H = htmlLocalizer; } @@ -67,9 +72,25 @@ public async Task Index(AdminIndexOptions options, PagerParameter ); } + if (String.Equals(options.Status, "enabled", StringComparison.OrdinalIgnoreCase)) + { + items = items.Where(x => x.Settings.Enable); + } + else if (String.Equals(options.Status, "disabled", StringComparison.OrdinalIgnoreCase)) + { + items = items.Where(x => !x.Settings.Enable); + } + + options.Statuses = new List() + { + new SelectListItem() { Text = S["Enabled"], Value = "enabled" }, + new SelectListItem() { Text = S["Disabled"], Value = "disabled" } + }; + var taskItems = items.ToList(); var routeData = new RouteData(); routeData.Values.Add($"{nameof(BackgroundTaskIndexViewModel.Options)}.{nameof(options.Search)}", options.Search); + routeData.Values.Add($"{nameof(BackgroundTaskIndexViewModel.Options)}.{nameof(options.Status)}", options.Status); var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); var pagerShape = (await New.Pager(pager)).TotalItemCount(taskItems.Count).RouteData(routeData); @@ -90,6 +111,7 @@ public ActionResult IndexFilterPOST(BackgroundTaskIndexViewModel model) { return RedirectToAction(nameof(Index), new RouteValueDictionary { { $"{nameof(model.Options)}.{nameof(AdminIndexOptions.Search)}", model.Options.Search }, + { $"{nameof(model.Options)}.{nameof(AdminIndexOptions.Status)}", model.Options.Status }, }); } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs index 5780ac775c2..130825caf26 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/ViewModels/AdminIndexOptions.cs @@ -1,6 +1,12 @@ -namespace OrchardCore.BackgroundTasks.ViewModels; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace OrchardCore.BackgroundTasks.ViewModels; public class AdminIndexOptions { public string Search { get; set; } + + public string Status { get; set; } + public List Statuses { get; internal set; } } diff --git a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml index dbaf715a93b..7c9e4f3e7d2 100644 --- a/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.BackgroundTasks/Views/BackgroundTask/Index.cshtml @@ -19,33 +19,39 @@
    - - -
    - @* the form is necessary to generate an antiforgery token for the delete action *@ - @if (Model.Tasks.Count > 0) - { -
    } - } - - } - - + } + else + { +
  • +
    + @T["Nothing here! Your search returned no results."] +
    +
  • + } + @await DisplayAsync(Model.Pager) + + + +