From: Marcel Werk Date: Tue, 19 Nov 2024 15:16:57 +0000 (+0100) Subject: Add renderer / filter for users X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=16d5ef6820e3289ae7db05a79be2593f69ac7803;p=GitHub%2FWoltLab%2FWCF.git Add renderer / filter for users --- diff --git a/wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php b/wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php index 40e6d56117..930a092e1c 100644 --- a/wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php +++ b/wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php @@ -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 index 0000000000..48d972f729 --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/filter/UserFilter.class.php @@ -0,0 +1,47 @@ + + * @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 : ''; + } +} diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/AbstractColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/AbstractColumnRenderer.class.php index 5400204e34..73085b4d4f 100644 --- a/wcfsetup/install/files/lib/system/gridView/renderer/AbstractColumnRenderer.class.php +++ b/wcfsetup/install/files/lib/system/gridView/renderer/AbstractColumnRenderer.class.php @@ -17,4 +17,7 @@ abstract class AbstractColumnRenderer implements IColumnRenderer { return ''; } + + #[\Override] + public function prepare(mixed $value, mixed $context = null): void {} } diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/IColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/IColumnRenderer.class.php index eda6c61af6..259bc7de9e 100644 --- a/wcfsetup/install/files/lib/system/gridView/renderer/IColumnRenderer.class.php +++ b/wcfsetup/install/files/lib/system/gridView/renderer/IColumnRenderer.class.php @@ -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 index 0000000000..d4c2bd3953 --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php @@ -0,0 +1,42 @@ + + * @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); + } +}