Migrate cronjob log clear to modern code
authorMarcel Werk <burntime@woltlab.com>
Fri, 15 Nov 2024 15:24:05 +0000 (16:24 +0100)
committerMarcel Werk <burntime@woltlab.com>
Fri, 15 Nov 2024 15:24:05 +0000 (16:24 +0100)
ts/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.ts [new file with mode: 0644]
wcfsetup/install/files/acp/js/WCF.ACP.js
wcfsetup/install/files/acp/templates/cronjobLogList.tpl
wcfsetup/install/files/js/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.js [new file with mode: 0644]
wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php
wcfsetup/install/files/lib/data/cronjob/log/CronjobLogAction.class.php
wcfsetup/install/files/lib/system/endpoint/controller/core/cronjobs/logs/ClearLogs.class.php [new file with mode: 0644]

diff --git a/ts/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.ts b/ts/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.ts
new file mode 100644 (file)
index 0000000..e6396c6
--- /dev/null
@@ -0,0 +1,22 @@
+/**
+ * Deletes all entries of the cronjob log.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @woltlabExcludeBundle tiny
+ */
+
+import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
+import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";
+
+export async function clearLogs(): Promise<ApiResult<[]>> {
+  try {
+    await prepareRequest(`${window.WSC_RPC_API_URL}core/cronjobs/logs`).delete().fetchAsJson();
+  } catch (e) {
+    return apiResultFromError(e);
+  }
+
+  return apiResultFromValue([]);
+}
index d559e5869dacf9b9236b9ea770e35f4f5668adbb..782d9cdb8c26676c3e2ed839695c21201d13db50 100644 (file)
@@ -92,63 +92,6 @@ WCF.ACP.Cronjob.ExecutionHandler = Class.extend({
        }
 });
 
-/**
- * Handles the cronjob log list.
- */
-WCF.ACP.Cronjob.LogList = Class.extend({
-       /**
-        * error message dialog
-        * @var jQuery
-        */
-       _dialog: null,
-       
-       /**
-        * Initializes WCF.ACP.Cronjob.LogList object.
-        */
-       init: function() {
-               // bind event listener to delete cronjob log button
-               $('.jsCronjobLogDelete').click(function() {
-                       WCF.System.Confirmation.show(WCF.Language.get('wcf.acp.cronjob.log.clear.confirm'), function(action) {
-                               if (action == 'confirm') {
-                                       new WCF.Action.Proxy({
-                                               autoSend: true,
-                                               data: {
-                                                       actionName: 'clearAll',
-                                                       className: 'wcf\\data\\cronjob\\log\\CronjobLogAction'
-                                               },
-                                               success: function() {
-                                                       window.location.reload();
-                                               }
-                                       });
-                               }
-                       });
-               });
-               
-               // bind event listeners to error badges
-               $('.jsCronjobError').click($.proxy(this._showError, this));
-       },
-       
-       /**
-        * Shows certain error message
-        * 
-        * @param       object          event
-        */
-       _showError: function(event) {
-               var $errorBadge = $(event.currentTarget);
-               
-               if (this._dialog === null) {
-                       this._dialog = $('<div style="overflow: auto"><pre>' + $errorBadge.next().html() + '</pre></div>').hide().appendTo(document.body);
-                       this._dialog.wcfDialog({
-                               title: WCF.Language.get('wcf.acp.cronjob.log.error.details')
-                       });
-               }
-               else {
-                       this._dialog.html('<pre>' + $errorBadge.next().html() + '</pre>');
-                       this._dialog.wcfDialog('open');
-               }
-       }
-});
-
 /**
  * Namespace for ACP package management.
  */
index af482fc14c24a1f67e4b3baf2c9cd50774158a77..1140d117a29611af258a81a34952bdb3eaab3d95 100644 (file)
@@ -1,13 +1,23 @@
 {include file='header' pageTitle='wcf.acp.cronjob.log'}
 
 <script data-relocate="true">
-       $(function() {
-               WCF.Language.addObject({
-                       'wcf.acp.cronjob.log.clear.confirm': '{jslang}wcf.acp.cronjob.log.clear.confirm{/jslang}',
-                       'wcf.acp.cronjob.log.error.details': '{jslang}wcf.acp.cronjob.log.error.details{/jslang}'
+       require(['WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs', 'Ui/Notification', 'WoltLabSuite/Core/Component/Confirmation'], ({ clearLogs }, UiNotification, { confirmationFactory }) => {
+               document.querySelectorAll('.jsCronjobLogDelete').forEach((button) => {
+                       button.addEventListener('click', async () => {
+                               const result = await confirmationFactory()
+                                       .custom('{jslang}wcf.acp.cronjob.log.clear.confirm{/jslang}')
+                                       .withoutMessage();
+                                       
+                               if (result) {
+                                       const response = await clearLogs();
+                                       if (response.ok) {
+                                               UiNotification.show(undefined, () => {
+                                                       window.location.reload();
+                                               });
+                                       }
+                               }
+                       });
                });
-               
-               new WCF.ACP.Cronjob.LogList();
        });
 </script>
 
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Cronjobs/Logs/ClearLogs.js
new file mode 100644 (file)
index 0000000..e0fa3cd
--- /dev/null
@@ -0,0 +1,23 @@
+/**
+ * Deletes all entries of the cronjob log.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @woltlabExcludeBundle tiny
+ */
+define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend", "../../Result"], function (require, exports, Backend_1, Result_1) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.clearLogs = clearLogs;
+    async function clearLogs() {
+        try {
+            await (0, Backend_1.prepareRequest)(`${window.WSC_RPC_API_URL}core/cronjobs/logs`).delete().fetchAsJson();
+        }
+        catch (e) {
+            return (0, Result_1.apiResultFromError)(e);
+        }
+        return (0, Result_1.apiResultFromValue)([]);
+    }
+});
index f3554ed8ecdefd4341285e59c3ece40924f94469..7a17748c74cabf9ac7ebf3116369eaebde15c3be 100644 (file)
@@ -134,6 +134,7 @@ return static function (): void {
             $event->register(new \wcf\system\endpoint\controller\core\comments\responses\RenderResponse);
             $event->register(new \wcf\system\endpoint\controller\core\comments\responses\RenderResponses);
             $event->register(new \wcf\system\endpoint\controller\core\comments\responses\UpdateResponse);
+            $event->register(new \wcf\system\endpoint\controller\core\cronjobs\logs\ClearLogs);
             $event->register(new \wcf\system\endpoint\controller\core\messages\GetMentionSuggestions);
             $event->register(new \wcf\system\endpoint\controller\core\sessions\DeleteSession);
         }
index bd295ba1f51c5727a15abfb301b029d0c4bf909e..b3da9bfe79f11e6f4c261ff458af464e04ba6ee0 100644 (file)
@@ -3,7 +3,6 @@
 namespace wcf\data\cronjob\log;
 
 use wcf\data\AbstractDatabaseObjectAction;
-use wcf\system\WCF;
 
 /**
  * Executes cronjob log-related actions.
@@ -22,25 +21,4 @@ class CronjobLogAction extends AbstractDatabaseObjectAction
      * @inheritDoc
      */
     protected $className = CronjobLogEditor::class;
-
-    /**
-     * @inheritDoc
-     */
-    protected $requireACP = ['clearAll'];
-
-    /**
-     * Validates the clear all action.
-     */
-    public function validateClearAll()
-    {
-        WCF::getSession()->checkPermissions(['admin.management.canManageCronjob']);
-    }
-
-    /**
-     * Deletes the entire cronjob log.
-     */
-    public function clearAll()
-    {
-        CronjobLogEditor::clearLogs();
-    }
 }
diff --git a/wcfsetup/install/files/lib/system/endpoint/controller/core/cronjobs/logs/ClearLogs.class.php b/wcfsetup/install/files/lib/system/endpoint/controller/core/cronjobs/logs/ClearLogs.class.php
new file mode 100644 (file)
index 0000000..e17abca
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace wcf\system\endpoint\controller\core\cronjobs\logs;
+
+use Laminas\Diactoros\Response\JsonResponse;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use wcf\data\cronjob\log\CronjobLogEditor;
+use wcf\system\endpoint\DeleteRequest;
+use wcf\system\endpoint\IController;
+use wcf\system\WCF;
+
+/**
+ * API endpoint for the deletion of all entries of the cronjob log.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+#[DeleteRequest('/core/cronjobs/logs')]
+final class ClearLogs implements IController
+{
+    #[\Override]
+    public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
+    {
+        WCF::getSession()->checkPermissions(['admin.management.canManageCronjob']);
+
+        CronjobLogEditor::clearLogs();
+
+        return new JsonResponse([]);
+    }
+}