Add renderer for object links
authorMarcel Werk <burntime@woltlab.com>
Tue, 10 Dec 2024 12:09:01 +0000 (13:09 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 10 Dec 2024 12:09:01 +0000 (13:09 +0100)
wcfsetup/install/files/lib/system/gridView/renderer/ObjectLinkColumnRenderer.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/ObjectLinkColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/ObjectLinkColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..06fc0b8
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use BadMethodCallException;
+use wcf\data\ILinkableObject;
+use wcf\data\ITitledObject;
+use wcf\system\cache\runtime\AbstractRuntimeCache;
+use wcf\system\request\IRouteController;
+use wcf\system\request\LinkHandler;
+use wcf\system\WCF;
+use wcf\util\StringUtil;
+
+/**
+ * Provides an abstract implementation for the option to format a column as an object link.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+abstract class ObjectLinkColumnRenderer extends AbstractColumnRenderer implements ILinkColumnRenderer
+{
+    public function __construct(
+        private readonly string $controllerClass = '',
+        private readonly array $parameters = [],
+        private readonly string $titleLanguageItem = ''
+    ) {}
+
+    protected abstract function getRuntimeCache(): AbstractRuntimeCache;
+
+    #[\Override]
+    public function render(mixed $value, mixed $context = null): string
+    {
+        if (!$value) {
+            return '';
+        }
+
+        $object = $this->getRuntimeCache()->getObject($value);
+        if ($object === null) {
+            return '';
+        }
+
+        if (!($object instanceof ITitledObject)) {
+            throw new BadMethodCallException(\get_class($object) . " does not implement ITitledObject");
+        }
+
+        if ($this->controllerClass) {
+            if (!($object instanceof IRouteController)) {
+                throw new BadMethodCallException(\get_class($object) . " does not implement IRouteController");
+            }
+
+            $href = LinkHandler::getInstance()->getControllerLink(
+                $this->controllerClass,
+                \array_merge($this->parameters, [
+                    'object' => $object,
+                ])
+            );
+        } else if ($object instanceof ILinkableObject) {
+            $href = $object->getLink();
+        } else {
+            throw new BadMethodCallException(\get_class($object) . " does not implement ILinkableObject");
+        }
+
+        return '<a href="' . StringUtil::encodeHTML($href) . '"'
+            . ($this->titleLanguageItem ? ' title="' . WCF::getLanguage()->get($this->titleLanguageItem) . '"' : '') . '>'
+            . StringUtil::encodeHTML($object->getTitle())
+            . '</a>';
+    }
+
+    #[\Override]
+    public function prepare(mixed $value, mixed $context = null): void
+    {
+        if (!$value) {
+            return;
+        }
+
+        $this->getRuntimeCache()->cacheObjectID($value);
+    }
+}
diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..4fff65b
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use wcf\system\cache\runtime\AbstractRuntimeCache;
+use wcf\system\cache\runtime\UserRuntimeCache;
+
+/**
+ * Formats the content of a column as a user link.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+class UserLinkColumnRenderer extends ObjectLinkColumnRenderer
+{
+    #[\Override]
+    protected function getRuntimeCache(): AbstractRuntimeCache
+    {
+        return UserRuntimeCache::getInstance();
+    }
+}