Add action for DBO actions
authorMarcel Werk <burntime@woltlab.com>
Wed, 18 Dec 2024 15:27:36 +0000 (16:27 +0100)
committerMarcel Werk <burntime@woltlab.com>
Wed, 18 Dec 2024 15:27:36 +0000 (16:27 +0100)
ts/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.ts [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.js [new file with mode: 0644]
wcfsetup/install/files/lib/system/gridView/action/LegacyDboAction.class.php [new file with mode: 0644]

diff --git a/ts/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.ts b/ts/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.ts
new file mode 100644 (file)
index 0000000..a242bf7
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * Handles execution of DBO actions within grid views.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @deprecated 6.2 DBO actions are considered outdated and should be migrated to RPC endpoints.
+ */
+
+import { dboAction } from "WoltLabSuite/Core/Ajax";
+import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification";
+import { ConfirmationType, handleConfirmation } from "./Confirmation";
+
+async function handleDboAction(
+  row: HTMLTableRowElement,
+  objectName: string,
+  className: string,
+  actionName: string,
+  confirmationType: ConfirmationType,
+  customConfirmationMessage: string = "",
+): Promise<void> {
+  const confirmationResult = await handleConfirmation(objectName, confirmationType, customConfirmationMessage);
+  if (!confirmationResult.result) {
+    return;
+  }
+
+  await dboAction(actionName, className)
+    .objectIds([parseInt(row.dataset.objectId!)])
+    .payload(confirmationResult.reason ? { reason: confirmationResult.reason } : {})
+    .dispatch();
+
+  if (confirmationType == ConfirmationType.Delete) {
+    row.remove();
+  } else {
+    row.dispatchEvent(
+      new CustomEvent("refresh", {
+        bubbles: true,
+      }),
+    );
+
+    // TODO: This shows a generic success message and should be replaced with a more specific message.
+    showNotification();
+  }
+}
+
+export function setup(table: HTMLTableElement): void {
+  table.addEventListener("action", (event: CustomEvent) => {
+    if (event.detail.action === "legacy-dbo-action") {
+      void handleDboAction(
+        event.target as HTMLTableRowElement,
+        event.detail.objectName,
+        event.detail.className,
+        event.detail.actionName,
+        event.detail.confirmationType,
+        event.detail.confirmationMessage,
+      );
+    }
+  });
+}
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction.js
new file mode 100644 (file)
index 0000000..1a917fe
--- /dev/null
@@ -0,0 +1,41 @@
+/**
+ * Handles execution of DBO actions within grid views.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @deprecated 6.2 DBO actions are considered outdated and should be migrated to RPC endpoints.
+ */
+define(["require", "exports", "WoltLabSuite/Core/Ajax", "WoltLabSuite/Core/Ui/Notification", "./Confirmation"], function (require, exports, Ajax_1, Notification_1, Confirmation_1) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.setup = setup;
+    async function handleDboAction(row, objectName, className, actionName, confirmationType, customConfirmationMessage = "") {
+        const confirmationResult = await (0, Confirmation_1.handleConfirmation)(objectName, confirmationType, customConfirmationMessage);
+        if (!confirmationResult.result) {
+            return;
+        }
+        await (0, Ajax_1.dboAction)(actionName, className)
+            .objectIds([parseInt(row.dataset.objectId)])
+            .payload(confirmationResult.reason ? { reason: confirmationResult.reason } : {})
+            .dispatch();
+        if (confirmationType == Confirmation_1.ConfirmationType.Delete) {
+            row.remove();
+        }
+        else {
+            row.dispatchEvent(new CustomEvent("refresh", {
+                bubbles: true,
+            }));
+            // TODO: This shows a generic success message and should be replaced with a more specific message.
+            (0, Notification_1.show)();
+        }
+    }
+    function setup(table) {
+        table.addEventListener("action", (event) => {
+            if (event.detail.action === "legacy-dbo-action") {
+                void handleDboAction(event.target, event.detail.objectName, event.detail.className, event.detail.actionName, event.detail.confirmationType, event.detail.confirmationMessage);
+            }
+        });
+    }
+});
diff --git a/wcfsetup/install/files/lib/system/gridView/action/LegacyDboAction.class.php b/wcfsetup/install/files/lib/system/gridView/action/LegacyDboAction.class.php
new file mode 100644 (file)
index 0000000..288f155
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+
+namespace wcf\system\gridView\action;
+
+use Closure;
+use wcf\data\DatabaseObject;
+use wcf\data\ITitledObject;
+use wcf\system\gridView\AbstractGridView;
+use wcf\system\WCF;
+use wcf\util\StringUtil;
+
+/**
+ * Represents an action that executes a dbo action.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ * @deprecated  6.2 DBO actions are considered outdated and should be migrated to RPC endpoints.
+ */
+class LegacyDboAction extends AbstractAction
+{
+    public function __construct(
+        protected readonly string $className,
+        protected readonly string $actionName,
+        protected readonly string|Closure $languageItem,
+        protected readonly ActionConfirmationType $confirmationType = ActionConfirmationType::None,
+        protected readonly string|Closure $confirmationMessage = '',
+        ?Closure $isAvailableCallback = null
+    ) {
+        parent::__construct($isAvailableCallback);
+    }
+
+    #[\Override]
+    public function render(mixed $row): string
+    {
+        \assert($row instanceof DatabaseObject);
+
+        if (\is_string($this->languageItem)) {
+            $label = WCF::getLanguage()->get($this->languageItem);
+        } else {
+            $label = ($this->languageItem)($row);
+        }
+
+        if (\is_string($this->confirmationMessage)) {
+            $confirmationMessage = WCF::getLanguage()->get($this->confirmationMessage);
+        } else {
+            $confirmationMessage = ($this->confirmationMessage)($row);
+        }
+
+        if ($row instanceof ITitledObject) {
+            $objectName = StringUtil::encodeHTML($row->getTitle());
+        } else {
+            $objectName = '';
+        }
+
+        $className = StringUtil::encodeHTML($this->className);
+        $actionName = StringUtil::encodeHTML($this->actionName);
+
+        return <<<HTML
+            <button
+                type="button"
+                data-action="legacy-dbo-action"
+                data-object-name="{$objectName}"
+                data-class-name="{$className}"
+                data-action-name="{$actionName}"
+                data-confirmation-type="{$this->confirmationType->toString()}"
+                data-confirmation-message="{$confirmationMessage}"
+            >
+                {$label}
+            </button>
+            HTML;
+    }
+
+    #[\Override]
+    public function renderInitialization(AbstractGridView $gridView): ?string
+    {
+        $id = StringUtil::encodeJS($gridView->getID());
+
+        return <<<HTML
+            <script data-relocate="true">
+                require(['WoltLabSuite/Core/Component/GridView/Action/LegacyDboAction'], ({ setup }) => {
+                    setup(document.getElementById('{$id}_table'));
+                });
+            </script>
+            HTML;
+    }
+}