Add renderer / filter for users
authorMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:16:57 +0000 (16:16 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:16:57 +0000 (16:16 +0100)
wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php
wcfsetup/install/files/lib/system/gridView/filter/UserFilter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/gridView/renderer/AbstractColumnRenderer.class.php
wcfsetup/install/files/lib/system/gridView/renderer/IColumnRenderer.class.php
wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php [new file with mode: 0644]

index 40e6d561172073528a3a4809aed4d0bd00b7f7df..930a092e1c3ae1e6d50b3f9bb7a479943e7d12ab 100644 (file)
@@ -228,6 +228,8 @@ abstract class AbstractGridView
      */
     public function renderRows(): string
     {
+        $this->prepareRenderers();
+
         return WCF::getTPL()->fetch('shared_gridViewRows', 'wcf', ['view' => $this], true);
     }
 
@@ -498,6 +500,20 @@ abstract class AbstractGridView
         return null;
     }
 
+    /**
+     * Prepares the column renderers.
+     */
+    protected function prepareRenderers(): void
+    {
+        foreach ($this->getVisibleColumns() as $column) {
+            foreach ($column->getRenderers() as $renderer) {
+                foreach ($this->getRows() as $row) {
+                    $renderer->prepare($this->getData($row, $column->getID()), $row);
+                }
+            }
+        }
+    }
+
     /**
      * Returns the rows for the active page.
      */
diff --git a/wcfsetup/install/files/lib/system/gridView/filter/UserFilter.class.php b/wcfsetup/install/files/lib/system/gridView/filter/UserFilter.class.php
new file mode 100644 (file)
index 0000000..48d972f
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace wcf\system\gridView\filter;
+
+use wcf\data\DatabaseObjectList;
+use wcf\system\cache\runtime\UserRuntimeCache;
+use wcf\system\form\builder\field\AbstractFormField;
+use wcf\system\form\builder\field\user\UserFormField;
+
+/**
+ * Filter for columns that contain user ids.
+ *
+ * @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 UserFilter implements IGridViewFilter
+{
+    #[\Override]
+    public function getFormField(string $id, string $label): AbstractFormField
+    {
+        return UserFormField::create($id)
+            ->label($label)
+            ->nullable();
+    }
+
+    #[\Override]
+    public function applyFilter(DatabaseObjectList $list, string $id, string $value): void
+    {
+        $list->getConditionBuilder()->add("{$id} = ?", [$value]);
+    }
+
+    #[\Override]
+    public function matches(string $filterValue, string $rowValue): bool
+    {
+        return $rowValue == $filterValue;
+    }
+
+    #[\Override]
+    public function renderValue(string $value): string
+    {
+        $user = UserRuntimeCache::getInstance()->getObject($value);
+
+        return $user ? $user->username : '';
+    }
+}
index 5400204e345ebff9eaa5bf9858f8170370dce4e0..73085b4d4f5da6cbf90337479eadd5be35a3db8c 100644 (file)
@@ -17,4 +17,7 @@ abstract class AbstractColumnRenderer implements IColumnRenderer
     {
         return '';
     }
+
+    #[\Override]
+    public function prepare(mixed $value, mixed $context = null): void {}
 }
index eda6c61af6b09497498c7832f8fee2f78b93bd18..259bc7de9e04ba2b068ea6677076836101ecc2b1 100644 (file)
@@ -21,4 +21,9 @@ interface IColumnRenderer
      * Returns the css classes of a column.
      */
     public function getClasses(): string;
+
+    /**
+     * Is called after the grid view data has been loaded and allows additional data to be loaded or cached.
+     */
+    public function prepare(mixed $value, mixed $context = null): void;
 }
diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..d4c2bd3
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use wcf\system\cache\runtime\UserRuntimeCache;
+use wcf\util\StringUtil;
+
+/**
+ * Formats the content of a column as a user.
+ *
+ * @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 UserColumnRenderer extends DefaultColumnRenderer
+{
+    #[\Override]
+    public function render(mixed $value, mixed $context = null): string
+    {
+        if (!$value) {
+            return '';
+        }
+
+        $user = UserRuntimeCache::getInstance()->getObject($value);
+        if ($user === null) {
+            return '';
+        }
+
+        return StringUtil::encodeHTML($user->username);
+    }
+
+    #[\Override]
+    public function prepare(mixed $value, mixed $context = null): void
+    {
+        if (!$value) {
+            return;
+        }
+
+        UserRuntimeCache::getInstance()->cacheObjectID($value);
+    }
+}