From 2e5cf464aeb6ebde94a07d1a421ad12700aee7c5 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 6 Nov 2024 13:00:51 +0100 Subject: [PATCH] Add default filtering for non DBO grid views --- .../system/view/grid/ArrayGridView.class.php | 17 +++++++++++++++++ .../grid/DatabaseObjectListGridView.class.php | 2 +- .../view/grid/ExceptionLogGridView.class.php | 7 +++++++ .../view/grid/filter/IGridViewFilter.class.php | 2 ++ .../view/grid/filter/SelectFilter.class.php | 6 ++++++ .../view/grid/filter/TextFilter.class.php | 6 ++++++ 6 files changed, 39 insertions(+), 1 deletion(-) 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 b3c648069e..2c588b40e0 100644 --- a/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/ArrayGridView.class.php @@ -2,6 +2,8 @@ namespace wcf\system\view\grid; +use LogicException; + abstract class ArrayGridView extends AbstractGridView { protected array $dataArray; @@ -40,10 +42,25 @@ abstract class ArrayGridView extends AbstractGridView { if (!isset($this->dataArray)) { $this->dataArray = $this->loadDataArray(); + $this->applyFilters(); } return $this->dataArray; } + protected function applyFilters(): void + { + foreach ($this->getActiveFilters() as $key => $value) { + $column = $this->getColumn($key); + if (!$column) { + throw new LogicException("Unknown column '" . $key . "'"); + } + + $this->dataArray = \array_filter($this->dataArray, function (array $row) use ($column, $value) { + return $column->getFilter()->matches($value, $row[$column->getID()]); + }); + } + } + protected abstract function loadDataArray(): array; } 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 486477c18d..f12ed786f0 100644 --- a/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php @@ -59,7 +59,7 @@ abstract class DatabaseObjectListGridView extends AbstractGridView return $this->objectList; } - private function applyFilters(): void + protected function applyFilters(): void { foreach ($this->getActiveFilters() as $key => $value) { $column = $this->getColumn($key); 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 10b041d4c5..4929d185bf 100644 --- a/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/ExceptionLogGridView.class.php @@ -65,6 +65,7 @@ final class ExceptionLogGridView extends ArrayGridView return $row['exceptionID']; } + #[\Override] protected function loadDataArray(): array { if (!empty($this->getActiveFilters()['exceptionID'])) { @@ -125,6 +126,12 @@ final class ExceptionLogGridView extends ArrayGridView return []; } + #[\Override] + protected function applyFilters(): void + { + // Overwrite the default filtering, as this is already applied when the data is loaded. + } + private function getAvailableLogFiles(): array { if (!isset($this->availableLogFiles)) { diff --git a/wcfsetup/install/files/lib/system/view/grid/filter/IGridViewFilter.class.php b/wcfsetup/install/files/lib/system/view/grid/filter/IGridViewFilter.class.php index aecf4cd9cc..4862acbb52 100644 --- a/wcfsetup/install/files/lib/system/view/grid/filter/IGridViewFilter.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/filter/IGridViewFilter.class.php @@ -11,5 +11,7 @@ interface IGridViewFilter public function applyFilter(DatabaseObjectList $list, string $id, string $value): void; + public function matches(string $filterValue, string $rowValue): bool; + public function renderValue(string $value): string; } diff --git a/wcfsetup/install/files/lib/system/view/grid/filter/SelectFilter.class.php b/wcfsetup/install/files/lib/system/view/grid/filter/SelectFilter.class.php index 643ab8c9c7..f2000710f6 100644 --- a/wcfsetup/install/files/lib/system/view/grid/filter/SelectFilter.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/filter/SelectFilter.class.php @@ -25,6 +25,12 @@ class SelectFilter implements IGridViewFilter $list->getConditionBuilder()->add("$id = ?", [$value]); } + #[\Override] + public function matches(string $filterValue, string $rowValue): bool + { + return $filterValue === $rowValue; + } + #[\Override] public function renderValue(string $value): string { diff --git a/wcfsetup/install/files/lib/system/view/grid/filter/TextFilter.class.php b/wcfsetup/install/files/lib/system/view/grid/filter/TextFilter.class.php index 0f4b5aed69..52605e7945 100644 --- a/wcfsetup/install/files/lib/system/view/grid/filter/TextFilter.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/filter/TextFilter.class.php @@ -21,6 +21,12 @@ class TextFilter implements IGridViewFilter $list->getConditionBuilder()->add("$id LIKE ?", ['%' . $value . '%']); } + #[\Override] + public function matches(string $filterValue, string $rowValue): bool + { + return \str_contains(\mb_strtolower($rowValue), \mb_strtolower($filterValue)); + } + #[\Override] public function renderValue(string $value): string { -- 2.20.1