From f675d539f20fb797c0abb75ff91e8f6aaa7b8cd8 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon <54990055+sukhwinder33445@users.noreply.github.com> Date: Tue, 30 May 2023 15:26:00 +0200 Subject: [PATCH] Enhance global search (#756) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Controllers: Remove not required method call `handleSearchRequest()` These controller do not support global search * Models: Add `display_name` to default search columns * Controller: Add ยด$additionaColumns` param to method handleSearchRequest() and prepareSearchFilter() * TacticalController: Remove superfluous override of method `prepareSearchFilter()` --- application/controllers/CommentsController.php | 2 -- application/controllers/DowntimesController.php | 2 -- application/controllers/HostsController.php | 2 +- application/controllers/ServicesController.php | 8 +++++--- application/controllers/TacticalController.php | 14 ++++++-------- application/controllers/UsergroupsController.php | 2 -- application/controllers/UsersController.php | 2 -- library/Icingadb/Model/Host.php | 2 +- library/Icingadb/Model/Hostgroup.php | 2 +- library/Icingadb/Model/Service.php | 2 +- library/Icingadb/Model/Servicegroup.php | 2 +- library/Icingadb/Model/User.php | 2 +- library/Icingadb/Model/Usergroup.php | 2 +- library/Icingadb/Web/Controller.php | 11 +++++++---- 14 files changed, 25 insertions(+), 30 deletions(-) diff --git a/application/controllers/CommentsController.php b/application/controllers/CommentsController.php index 298fae56..1f841971 100644 --- a/application/controllers/CommentsController.php +++ b/application/controllers/CommentsController.php @@ -36,8 +36,6 @@ class CommentsController extends Controller 'service.state' ]); - $this->handleSearchRequest($comments); - $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($comments); $sortControl = $this->createSortControl( diff --git a/application/controllers/DowntimesController.php b/application/controllers/DowntimesController.php index 89fdfc85..f2b87888 100644 --- a/application/controllers/DowntimesController.php +++ b/application/controllers/DowntimesController.php @@ -36,8 +36,6 @@ class DowntimesController extends Controller 'service.state' ]); - $this->handleSearchRequest($downtimes); - $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($downtimes); $sortControl = $this->createSortControl( diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index 3623ff7d..d67f55a6 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -42,7 +42,7 @@ class HostsController extends Controller $hosts->getWith()['host.state']->setJoinType('INNER'); $hosts->setResultSetClass(VolatileStateResults::class); - $this->handleSearchRequest($hosts); + $this->handleSearchRequest($hosts, ['address', 'address6']); $summary = null; if (! $compact) { diff --git a/application/controllers/ServicesController.php b/application/controllers/ServicesController.php index c2c5651e..40503b4a 100644 --- a/application/controllers/ServicesController.php +++ b/application/controllers/ServicesController.php @@ -411,12 +411,14 @@ class ServicesController extends Controller return new FeatureStatus('service', $summary->first()); } - protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter) + protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter, array $additionalColumns) { if ($this->params->shift('_hostFilterOnly', false)) { - $filter->add(Filter::like('host.name_ci', "*$search*")); + foreach (['host.name_ci', 'host.display_name', 'host.address', 'host.address6'] as $column) { + $filter->add(Filter::like($column, "*$search*")); + } } else { - parent::prepareSearchFilter($query, $search, $filter); + parent::prepareSearchFilter($query, $search, $filter, $additionalColumns); } } diff --git a/application/controllers/TacticalController.php b/application/controllers/TacticalController.php index 582b0463..b8d3757b 100644 --- a/application/controllers/TacticalController.php +++ b/application/controllers/TacticalController.php @@ -28,7 +28,12 @@ class TacticalController extends Controller $hoststateSummary = HoststateSummary::on($db); $servicestateSummary = ServicestateSummary::on($db); - $this->handleSearchRequest($servicestateSummary); + $this->handleSearchRequest($servicestateSummary, [ + 'host.name_ci', + 'host.display_name', + 'host.address', + 'host.address6' + ]); $searchBar = $this->createSearchBar($servicestateSummary); if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) { @@ -86,11 +91,4 @@ class TacticalController extends Controller $this->getDocument()->add($editor); $this->setTitle(t('Adjust Filter')); } - - protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter) - { - parent::prepareSearchFilter($query, $search, $filter); - - $filter->add(Filter::like('host.name_ci', "*$search*")); - } } diff --git a/application/controllers/UsergroupsController.php b/application/controllers/UsergroupsController.php index 3f0ea113..947500db 100644 --- a/application/controllers/UsergroupsController.php +++ b/application/controllers/UsergroupsController.php @@ -30,8 +30,6 @@ class UsergroupsController extends Controller $usergroups = Usergroup::on($db); - $this->handleSearchRequest($usergroups); - $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($usergroups); $sortControl = $this->createSortControl( diff --git a/application/controllers/UsersController.php b/application/controllers/UsersController.php index ef75e89c..37849941 100644 --- a/application/controllers/UsersController.php +++ b/application/controllers/UsersController.php @@ -30,8 +30,6 @@ class UsersController extends Controller $users = User::on($db); - $this->handleSearchRequest($users); - $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($users); $sortControl = $this->createSortControl( diff --git a/library/Icingadb/Model/Host.php b/library/Icingadb/Model/Host.php index a76cb365..7a14b1e6 100644 --- a/library/Icingadb/Model/Host.php +++ b/library/Icingadb/Model/Host.php @@ -121,7 +121,7 @@ class Host extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Model/Hostgroup.php b/library/Icingadb/Model/Hostgroup.php index 1dfae312..97930faa 100644 --- a/library/Icingadb/Model/Hostgroup.php +++ b/library/Icingadb/Model/Hostgroup.php @@ -50,7 +50,7 @@ class Hostgroup extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Model/Service.php b/library/Icingadb/Model/Service.php index 74eda0e0..97a110d0 100644 --- a/library/Icingadb/Model/Service.php +++ b/library/Icingadb/Model/Service.php @@ -112,7 +112,7 @@ class Service extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Model/Servicegroup.php b/library/Icingadb/Model/Servicegroup.php index 34f67bd7..0da92fb4 100644 --- a/library/Icingadb/Model/Servicegroup.php +++ b/library/Icingadb/Model/Servicegroup.php @@ -50,7 +50,7 @@ class Servicegroup extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Model/User.php b/library/Icingadb/Model/User.php index e894e0fc..91d0d717 100644 --- a/library/Icingadb/Model/User.php +++ b/library/Icingadb/Model/User.php @@ -63,7 +63,7 @@ class User extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Model/Usergroup.php b/library/Icingadb/Model/Usergroup.php index 70347708..34b0647b 100644 --- a/library/Icingadb/Model/Usergroup.php +++ b/library/Icingadb/Model/Usergroup.php @@ -50,7 +50,7 @@ class Usergroup extends Model public function getSearchColumns() { - return ['name_ci']; + return ['name_ci', 'display_name']; } public function getDefaultSort() diff --git a/library/Icingadb/Web/Controller.php b/library/Icingadb/Web/Controller.php index 08d69c98..e72b0eca 100644 --- a/library/Icingadb/Web/Controller.php +++ b/library/Icingadb/Web/Controller.php @@ -299,10 +299,11 @@ class Controller extends CompatController * Process a search request * * @param Query $query + * @param array $additionalColumns * * @return void */ - public function handleSearchRequest(Query $query) + public function handleSearchRequest(Query $query, array $additionalColumns = []) { $q = trim($this->params->shift('q', ''), ' *'); if (! $q) { @@ -310,7 +311,7 @@ class Controller extends CompatController } $filter = Filter::any(); - $this->prepareSearchFilter($query, $q, $filter); + $this->prepareSearchFilter($query, $q, $filter, $additionalColumns); $redirectUrl = Url::fromRequest(); $redirectUrl->setQueryString(QueryString::render($filter)); @@ -327,12 +328,14 @@ class Controller extends CompatController * @param Query $query * @param string $search * @param Filter\Any $filter + * @param array $additionalColumns * * @return void */ - protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter) + protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter, array $additionalColumns) { - foreach ($query->getModel()->getSearchColumns() as $column) { + $columns = array_merge($query->getModel()->getSearchColumns(), $additionalColumns); + foreach ($columns as $column) { $filter->add(Filter::like( $query->getResolver()->qualifyColumn($column, $query->getModel()->getTableName()), "*$search*"