Skip to content

Commit

Permalink
Add hook to include custom variables by default when searching under …
Browse files Browse the repository at this point in the history
…Overview entries

refs #1045
  • Loading branch information
Oscar Zambotti authored and Oscar Zambotti committed Aug 20, 2024
1 parent 334aaf8 commit 5c2b7d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
36 changes: 36 additions & 0 deletions library/Icingadb/Hook/CustomVarsRetrieverHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Icinga\Module\Icingadb\Hook;

use Icinga\Application\Hook;
use Icinga\Application\Logger;
use ipl\Orm\Model;
use Throwable;

abstract class CustomVarsRetrieverHook
{
abstract public function retrieveCustomVars(Model $model): array;

final public static function getCustomVarColumns(Model $model): array
{
$columns = [];

$hooks = Hook::all('Icingadb/CustomVarsRetriever');
foreach ($hooks as $hook) {
try {
$customVars = $hook->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;
}
}
5 changes: 4 additions & 1 deletion library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
5 changes: 4 additions & 1 deletion library/Icingadb/Web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5c2b7d3

Please sign in to comment.