From 63858c1b5083d3de48b2ba193c10b4aea1c3e39d Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 6 Nov 2024 15:39:32 +0100 Subject: [PATCH] Add events and an option to add columns before / after existing columns --- .../CronjobLogGridViewInitialized.class.php | 19 +++++ .../ExceptionLogGridViewInitialized.class.php | 19 +++++ .../UserRankGridViewInitialized.class.php | 19 +++++ .../view/grid/AbstractGridView.class.php | 72 +++++++++++++++++++ .../system/view/grid/ArrayGridView.class.php | 1 + .../view/grid/CronjobLogGridView.class.php | 8 +++ .../grid/DatabaseObjectListGridView.class.php | 1 + .../view/grid/ExceptionLogGridView.class.php | 8 +++ .../view/grid/UserRankGridView.class.php | 8 +++ 9 files changed, 155 insertions(+) create mode 100644 wcfsetup/install/files/lib/event/gridView/CronjobLogGridViewInitialized.class.php create mode 100644 wcfsetup/install/files/lib/event/gridView/ExceptionLogGridViewInitialized.class.php create mode 100644 wcfsetup/install/files/lib/event/gridView/UserRankGridViewInitialized.class.php diff --git a/wcfsetup/install/files/lib/event/gridView/CronjobLogGridViewInitialized.class.php b/wcfsetup/install/files/lib/event/gridView/CronjobLogGridViewInitialized.class.php new file mode 100644 index 0000000000..07012b2413 --- /dev/null +++ b/wcfsetup/install/files/lib/event/gridView/CronjobLogGridViewInitialized.class.php @@ -0,0 +1,19 @@ + + * @since 6.2 + */ +final class CronjobLogGridViewInitialized implements IPsr14Event +{ + public function __construct(public readonly CronjobLogGridView $gridView) {} +} diff --git a/wcfsetup/install/files/lib/event/gridView/ExceptionLogGridViewInitialized.class.php b/wcfsetup/install/files/lib/event/gridView/ExceptionLogGridViewInitialized.class.php new file mode 100644 index 0000000000..5e10db0a7b --- /dev/null +++ b/wcfsetup/install/files/lib/event/gridView/ExceptionLogGridViewInitialized.class.php @@ -0,0 +1,19 @@ + + * @since 6.2 + */ +final class ExceptionLogGridViewInitialized implements IPsr14Event +{ + public function __construct(public readonly ExceptionLogGridView $gridView) {} +} diff --git a/wcfsetup/install/files/lib/event/gridView/UserRankGridViewInitialized.class.php b/wcfsetup/install/files/lib/event/gridView/UserRankGridViewInitialized.class.php new file mode 100644 index 0000000000..4eb06b4247 --- /dev/null +++ b/wcfsetup/install/files/lib/event/gridView/UserRankGridViewInitialized.class.php @@ -0,0 +1,19 @@ + + * @since 6.2 + */ +final class UserRankGridViewInitialized implements IPsr14Event +{ + public function __construct(public readonly UserRankGridView $gridView) {} +} diff --git a/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php index d1f5c2d9a3..c46053b607 100644 --- a/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php @@ -4,6 +4,8 @@ namespace wcf\system\view\grid; use LogicException; use wcf\action\GridViewFilterAction; +use wcf\event\IPsr14Event; +use wcf\system\event\EventHandler; use wcf\system\request\LinkHandler; use wcf\system\view\grid\action\IGridViewAction; use wcf\system\WCF; @@ -28,12 +30,62 @@ abstract class AbstractGridView private int $pageNo = 1; private array $activeFilters = []; + /** + * Adds a new column to the grid view. + */ public function addColumn(GridViewColumn $column): void { $this->columns[] = $column; } /** + * Adds a new column to the grid view at the position before the specific id. + */ + public function addColumnBefore(GridViewColumn $column, string $beforeID): void + { + $position = -1; + + foreach ($this->getColumns() as $key => $existingColumn) { + if ($existingColumn->getID() === $beforeID) { + $position = $key; + break; + } + } + + if ($position === -1) { + throw new \InvalidArgumentException("Invalid column id '{$beforeID}' given."); + } + + array_splice($this->columns, $position, 0, [ + $column, + ]); + } + + /** + * Adds a new column to the grid view at the position after the specific id. + */ + public function addColumnAfter(GridViewColumn $column, string $afterID): void + { + $position = -1; + + foreach ($this->getColumns() as $key => $existingColumn) { + if ($existingColumn->getID() === $afterID) { + $position = $key; + break; + } + } + + if ($position === -1) { + throw new \InvalidArgumentException("Invalid column id '{$afterID}' given."); + } + + array_splice($this->columns, $position + 1, 0, [ + $column, + ]); + } + + /** + * Adds multiple new columns to the grid view. * @param GridViewColumn[] $columns */ public function addColumns(array $columns): void @@ -44,6 +96,7 @@ abstract class AbstractGridView } /** + * Returns all columns of the grid view. * @return GridViewColumn[] */ public function getColumns(): array @@ -52,6 +105,7 @@ abstract class AbstractGridView } /** + * Returns all visible (non-hidden) columns of the grid view. * @return GridViewColumn[] */ public function getVisibleColumns(): array @@ -59,6 +113,9 @@ abstract class AbstractGridView return \array_filter($this->getColumns(), fn($column) => !$column->isHidden()); } + /** + * Returns the column with the given id or null if no such column exists. + */ public function getColumn(string $id): ?GridViewColumn { foreach ($this->getColumns() as $column) { @@ -295,4 +352,19 @@ abstract class AbstractGridView { return ''; } + + protected function fireInitializedEvent(): void + { + $event = $this->getInitializedEvent(); + if ($event === null) { + return; + } + + EventHandler::getInstance()->fire($event); + } + + protected function getInitializedEvent(): ?IPsr14Event + { + return null; + } } diff --git a/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php index 2c588b40e0..4bb726d647 100644 --- a/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php @@ -43,6 +43,7 @@ abstract class ArrayGridView extends AbstractGridView if (!isset($this->dataArray)) { $this->dataArray = $this->loadDataArray(); $this->applyFilters(); + $this->fireInitializedEvent(); } return $this->dataArray; diff --git a/wcfsetup/install/files/lib/system/view/grid/CronjobLogGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/CronjobLogGridView.class.php index fe86d58888..3c32738712 100644 --- a/wcfsetup/install/files/lib/system/view/grid/CronjobLogGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/CronjobLogGridView.class.php @@ -7,6 +7,8 @@ use wcf\data\cronjob\I18nCronjobList; use wcf\data\cronjob\log\CronjobLog; use wcf\data\cronjob\log\CronjobLogList; use wcf\data\DatabaseObjectList; +use wcf\event\gridView\CronjobLogGridViewInitialized; +use wcf\event\IPsr14Event; use wcf\system\view\grid\filter\SelectFilter; use wcf\system\view\grid\renderer\DefaultColumnRenderer; use wcf\system\view\grid\renderer\NumberColumnRenderer; @@ -106,6 +108,12 @@ final class CronjobLogGridView extends DatabaseObjectListGridView return new CronjobLogList(); } + #[\Override] + protected function getInitializedEvent(): ?IPsr14Event + { + return new CronjobLogGridViewInitialized($this); + } + private function getAvailableCronjobs(): array { $list = new I18nCronjobList(); diff --git a/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php index f12ed786f0..7116ac2eb4 100644 --- a/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php @@ -48,6 +48,7 @@ abstract class DatabaseObjectListGridView extends AbstractGridView } } $this->applyFilters(); + $this->fireInitializedEvent(); } public function getObjectList(): DatabaseObjectList diff --git a/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php index ae6ab51c17..a8da2dd2f9 100644 --- a/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php @@ -2,6 +2,8 @@ namespace wcf\system\view\grid; +use wcf\event\gridView\ExceptionLogGridViewInitialized; +use wcf\event\IPsr14Event; use wcf\system\Regex; use wcf\system\view\grid\filter\SelectFilter; use wcf\system\view\grid\filter\TextFilter; @@ -144,4 +146,10 @@ final class ExceptionLogGridView extends ArrayGridView { return \array_key_first($this->getAvailableLogFiles()); } + + #[\Override] + protected function getInitializedEvent(): ?IPsr14Event + { + return new ExceptionLogGridViewInitialized($this); + } } diff --git a/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php index 2a97dc1ad2..5d1a12d05d 100644 --- a/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php @@ -7,6 +7,8 @@ use wcf\data\DatabaseObjectList; use wcf\data\user\group\UserGroup; use wcf\data\user\rank\I18nUserRankList; use wcf\data\user\rank\UserRank; +use wcf\event\gridView\UserRankGridViewInitialized; +use wcf\event\IPsr14Event; use wcf\system\view\grid\action\DeleteAction; use wcf\system\view\grid\action\EditAction; use wcf\system\view\grid\renderer\DefaultColumnRenderer; @@ -108,4 +110,10 @@ final class UserRankGridView extends DatabaseObjectListGridView { return new I18nUserRankList(); } + + #[\Override] + protected function getInitializedEvent(): ?IPsr14Event + { + return new UserRankGridViewInitialized($this); + } } -- 2.20.1