From 74cf625cbd24838f137e4f0bd6bc635d2575756a Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 10 Dec 2024 13:09:01 +0100 Subject: [PATCH] Add renderer for object links --- .../ObjectLinkColumnRenderer.class.php | 80 +++++++++++++++++++ .../renderer/UserLinkColumnRenderer.class.php | 23 ++++++ 2 files changed, 103 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/gridView/renderer/ObjectLinkColumnRenderer.class.php create mode 100644 wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php 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 index 0000000000..06fc0b8026 --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/renderer/ObjectLinkColumnRenderer.class.php @@ -0,0 +1,80 @@ + + * @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 'titleLanguageItem ? ' title="' . WCF::getLanguage()->get($this->titleLanguageItem) . '"' : '') . '>' + . StringUtil::encodeHTML($object->getTitle()) + . ''; + } + + #[\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 index 0000000000..4fff65ba2c --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php @@ -0,0 +1,23 @@ + + * @since 6.2 + */ +class UserLinkColumnRenderer extends ObjectLinkColumnRenderer +{ + #[\Override] + protected function getRuntimeCache(): AbstractRuntimeCache + { + return UserRuntimeCache::getInstance(); + } +} -- 2.20.1