Add events and an option to add columns before / after existing columns
authorMarcel Werk <burntime@woltlab.com>
Wed, 6 Nov 2024 14:39:32 +0000 (15:39 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 12 Nov 2024 11:53:43 +0000 (12:53 +0100)
wcfsetup/install/files/lib/event/gridView/CronjobLogGridViewInitialized.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/event/gridView/ExceptionLogGridViewInitialized.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/event/gridView/UserRankGridViewInitialized.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php
wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php
wcfsetup/install/files/lib/system/view/grid/CronjobLogGridView.class.php
wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php
wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php
wcfsetup/install/files/lib/system/view/grid/UserRankGridView.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 (file)
index 0000000..07012b2
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace wcf\event\gridView;
+
+use wcf\event\IPsr14Event;
+use wcf\system\view\grid\CronjobLogGridView;
+
+/**
+ * Indicates that the cronjob log grid view has been initialized.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..5e10db0
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace wcf\event\gridView;
+
+use wcf\event\IPsr14Event;
+use wcf\system\view\grid\ExceptionLogGridView;
+
+/**
+ * Indicates that the exception log grid view has been initialized.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..4eb06b4
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace wcf\event\gridView;
+
+use wcf\event\IPsr14Event;
+use wcf\system\view\grid\UserRankGridView;
+
+/**
+ * Indicates that the user rank grid view has been initialized.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+final class UserRankGridViewInitialized implements IPsr14Event
+{
+    public function __construct(public readonly UserRankGridView $gridView) {}
+}
index d1f5c2d9a357b7cca6224b1f2cf56f07cab236cf..c46053b607ec612b0646fe7726cbc5a23249fd3c 100644 (file)
@@ -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;
+    }
 }
index 2c588b40e028fde4edac583d572ecbd48e9a6706..4bb726d64733d1ab0332d8b86003291d020acc3d 100644 (file)
@@ -43,6 +43,7 @@ abstract class ArrayGridView extends AbstractGridView
         if (!isset($this->dataArray)) {
             $this->dataArray = $this->loadDataArray();
             $this->applyFilters();
+            $this->fireInitializedEvent();
         }
 
         return $this->dataArray;
index fe86d58888c4329ff2a2e7cb6a0c5b6c8084471f..3c32738712214e3f11ec4084f38fe52d07407757 100644 (file)
@@ -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();
index f12ed786f051f990fa92fa4ca1b0e4dfb617968d..7116ac2eb47aee586cede39b86ae139d8fd35dbc 100644 (file)
@@ -48,6 +48,7 @@ abstract class DatabaseObjectListGridView extends AbstractGridView
             }
         }
         $this->applyFilters();
+        $this->fireInitializedEvent();
     }
 
     public function getObjectList(): DatabaseObjectList
index ae6ab51c17d3c2c8621c93d6380ee6f8343d5bc9..a8da2dd2f97565eb6d0cf78f56e98f252a470788 100644 (file)
@@ -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);
+    }
 }
index 2a97dc1ad248a930993d9f23203bbc90fd4cc82c..5d1a12d05d7279350dbde6d0e5ad8b2050ede3d3 100644 (file)
@@ -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);
+    }
 }