From 4124249e03a59071ec71c267073e04913b07dba7 Mon Sep 17 00:00:00 2001 From: Oscar Zambotti Date: Tue, 20 Aug 2024 15:01:18 +0200 Subject: [PATCH] Add hook to include custom variables by default when searching under Overview entries refs #1045 --- .../Icingadb/Hook/CustomVarsRetrieverHook.php | 36 +++++++++++++++++++ .../Control/SearchBar/ObjectSuggestions.php | 5 ++- library/Icingadb/Web/Controller.php | 5 ++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 library/Icingadb/Hook/CustomVarsRetrieverHook.php diff --git a/library/Icingadb/Hook/CustomVarsRetrieverHook.php b/library/Icingadb/Hook/CustomVarsRetrieverHook.php new file mode 100644 index 000000000..e3d853c7e --- /dev/null +++ b/library/Icingadb/Hook/CustomVarsRetrieverHook.php @@ -0,0 +1,36 @@ +retrieveCustomVars($model); + if (!empty($customVars)) { + $columns = [...$columns, ...$customVars]; + } + } catch (Throwable $e) { + Logger::error( + 'Error retrieving Custom Vars for %s: %s', + $object, + $e->getMessage() + ); + } + } + + return $columns; + } +} diff --git a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php index 7630e0162..be2a37a80 100644 --- a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php +++ b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php @@ -7,6 +7,7 @@ use Generator; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Common\Database; +use Icinga\Module\Icingadb\Hook\CustomVarsRetrieverHook; use Icinga\Module\Icingadb\Model\Behavior\ReRoute; use Icinga\Module\Icingadb\Model\CustomvarFlat; use Icinga\Module\Icingadb\Model\Host; @@ -132,7 +133,9 @@ protected function createQuickSearchFilter($searchTerm) $resolver = $model::on($this->getDb())->getResolver(); $quickFilter = Filter::any(); - foreach ($model->getSearchColumns() as $column) { + $customVarColumns = CustomVarsRetrieverHook::getCustomVarColumns($model); + $columns = [...$model->getSearchColumns(), ...$customVarColumns]; + foreach ($columns as $column) { if (strpos($column, '.') === false) { $column = $resolver->qualifyColumn($column, $model->getTableName()); } diff --git a/library/Icingadb/Web/Controller.php b/library/Icingadb/Web/Controller.php index ffa711ac8..90f8d31ce 100644 --- a/library/Icingadb/Web/Controller.php +++ b/library/Icingadb/Web/Controller.php @@ -22,6 +22,7 @@ use Icinga\Module\Icingadb\Common\SearchControls; use Icinga\Module\Icingadb\Data\CsvResultSet; use Icinga\Module\Icingadb\Data\JsonResultSet; +use Icinga\Module\Icingadb\Hook\CustomVarsRetrieverHook; use Icinga\Module\Icingadb\Web\Control\GridViewModeSwitcher; use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher; use Icinga\Module\Icingadb\Widget\ItemTable\StateItemTable; @@ -305,7 +306,9 @@ public function handleSearchRequest(Query $query, array $additionalColumns = []) */ protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter, array $additionalColumns) { - $columns = array_merge($query->getModel()->getSearchColumns(), $additionalColumns); + $model = $query->getModel(); + $customVarColumns = CustomVarsRetrieverHook::getCustomVarColumns($model); + $columns = array_merge($model->getSearchColumns(), $additionalColumns, $customVarColumns); foreach ($columns as $column) { if (strpos($column, '.') === false) { $column = $query->getResolver()->qualifyColumn($column, $query->getModel()->getTableName());