From cc02853f54f62127ef3e08ba58d034d459decd5e Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:27:27 +0200 Subject: [PATCH] Use `GridViewModeSwitcher` for host and service groups host and service groups need `GridViewModeSwitcher` to toggle between list and grid view. --- .../controllers/HostgroupsController.php | 83 ++++++++----------- .../controllers/ServicegroupsController.php | 58 +++++-------- library/Icingadb/Web/Controller.php | 32 +++---- 3 files changed, 72 insertions(+), 101 deletions(-) diff --git a/application/controllers/HostgroupsController.php b/application/controllers/HostgroupsController.php index 2fe09dca..86e1ca8c 100644 --- a/application/controllers/HostgroupsController.php +++ b/application/controllers/HostgroupsController.php @@ -29,67 +29,32 @@ class HostgroupsController extends Controller public function indexAction() { $this->addTitleTab(t('Host Groups')); + $compact = $this->view->compact; $db = $this->getDb(); $hostgroups = Hostgroupsummary::on($db); - yield from $this->renderHostGroups($hostgroups); - } - - public function completeAction() - { - $suggestions = new ObjectSuggestions(); - $suggestions->setModel(Hostgroup::class); - $suggestions->forRequest(ServerRequest::fromGlobals()); - $this->getDocument()->add($suggestions); - } - - public function searchEditorAction() - { - $editor = $this->createSearchEditor(Hostgroupsummary::on($this->getDb()), [ - LimitControl::DEFAULT_LIMIT_PARAM, - SortControl::DEFAULT_SORT_PARAM, - ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM - ]); - - $this->getDocument()->add($editor); - $this->setTitle(t('Adjust Filter')); - } - - public function gridAction() - { - $this->addTitleTab(t('Host Group Grid')); - - $db = $this->getDb(); - - $hostgroups = Hostgroupsummary::on($db)->without([ - 'services_critical_handled', - 'services_critical_unhandled', - 'services_ok', - 'services_pending', - 'services_total', - 'services_unknown_handled', - 'services_unknown_unhandled', - 'services_warning_handled', - 'services_warning_unhandled', - ]); - - yield from $this->renderHostGroups($hostgroups); - } - - protected function renderHostGroups(Query $hostgroups) - { - $compact = $this->view->compact; $this->handleSearchRequest($hostgroups); $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($hostgroups); - $viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl); $defaultSort = null; - if ($viewModeSwitcher->getViewMode() === 'minimal') { + if ($viewModeSwitcher->getViewMode() === 'grid') { + $hostgroups->without([ + 'services_critical_handled', + 'services_critical_unhandled', + 'services_ok', + 'services_pending', + 'services_total', + 'services_unknown_handled', + 'services_unknown_unhandled', + 'services_warning_handled', + 'services_warning_unhandled', + ]); + $defaultSort = ['hosts_severity DESC', 'display_name']; } @@ -156,4 +121,24 @@ class HostgroupsController extends Controller $this->setAutorefreshInterval(30); } + + public function completeAction() + { + $suggestions = new ObjectSuggestions(); + $suggestions->setModel(Hostgroup::class); + $suggestions->forRequest(ServerRequest::fromGlobals()); + $this->getDocument()->add($suggestions); + } + + public function searchEditorAction() + { + $editor = $this->createSearchEditor(Hostgroupsummary::on($this->getDb()), [ + LimitControl::DEFAULT_LIMIT_PARAM, + SortControl::DEFAULT_SORT_PARAM, + ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM + ]); + + $this->getDocument()->add($editor); + $this->setTitle(t('Adjust Filter')); + } } diff --git a/application/controllers/ServicegroupsController.php b/application/controllers/ServicegroupsController.php index c60bdf96..8fd45ba4 100644 --- a/application/controllers/ServicegroupsController.php +++ b/application/controllers/ServicegroupsController.php @@ -28,39 +28,6 @@ class ServicegroupsController extends Controller public function indexAction() { $this->addTitleTab(t('Service Groups')); - - yield from $this->renderServiceGroups(); - } - - public function completeAction() - { - $suggestions = new ObjectSuggestions(); - $suggestions->setModel(Servicegroup::class); - $suggestions->forRequest(ServerRequest::fromGlobals()); - $this->getDocument()->add($suggestions); - } - - public function searchEditorAction() - { - $editor = $this->createSearchEditor(ServicegroupSummary::on($this->getDb()), [ - LimitControl::DEFAULT_LIMIT_PARAM, - SortControl::DEFAULT_SORT_PARAM, - ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM - ]); - - $this->getDocument()->add($editor); - $this->setTitle(t('Adjust Filter')); - } - - public function gridAction() - { - $this->addTitleTab(t('Service Group Grid')); - - yield from $this->renderServiceGroups(); - } - - protected function renderServiceGroups() - { $compact = $this->view->compact; $db = $this->getDb(); @@ -71,11 +38,10 @@ class ServicegroupsController extends Controller $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($servicegroups); - $viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl); $defaultSort = null; - if ($viewModeSwitcher->getViewMode() === 'minimal') { + if ($viewModeSwitcher->getViewMode() === 'grid') { $defaultSort = ['services_severity DESC', 'display_name']; } @@ -89,8 +55,6 @@ class ServicegroupsController extends Controller $defaultSort ); - $this->params->shift($sortControl->getSortParam()); - $searchBar = $this->createSearchBar($servicegroups, [ $limitControl->getLimitParam(), $sortControl->getSortParam(), @@ -144,4 +108,24 @@ class ServicegroupsController extends Controller $this->setAutorefreshInterval(30); } + + public function completeAction() + { + $suggestions = new ObjectSuggestions(); + $suggestions->setModel(Servicegroup::class); + $suggestions->forRequest(ServerRequest::fromGlobals()); + $this->getDocument()->add($suggestions); + } + + public function searchEditorAction() + { + $editor = $this->createSearchEditor(ServicegroupSummary::on($this->getDb()), [ + LimitControl::DEFAULT_LIMIT_PARAM, + SortControl::DEFAULT_SORT_PARAM, + ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM + ]); + + $this->getDocument()->add($editor); + $this->setTitle(t('Adjust Filter')); + } } diff --git a/library/Icingadb/Web/Controller.php b/library/Icingadb/Web/Controller.php index c15baedb..dc8408e1 100644 --- a/library/Icingadb/Web/Controller.php +++ b/library/Icingadb/Web/Controller.php @@ -22,6 +22,7 @@ use Icinga\Module\Icingadb\Common\BaseItemList; use Icinga\Module\Icingadb\Common\SearchControls; use Icinga\Module\Icingadb\Data\CsvResultSet; use Icinga\Module\Icingadb\Data\JsonResultSet; +use Icinga\Module\Icingadb\Web\Control\GridViewModeSwitcher; use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher; use Icinga\Module\Icingadb\Widget\ItemTable\StateItemTable; use Icinga\Module\Pdfexport\PrintableHtmlDocument; @@ -106,14 +107,21 @@ class Controller extends CompatController * @param LimitControl $limitControl * @param bool $verticalPagination * - * @return ViewModeSwitcher + * @return ViewModeSwitcher|GridViewModeSwitcher */ public function createViewModeSwitcher( PaginationControl $paginationControl, LimitControl $limitControl, bool $verticalPagination = false ): ViewModeSwitcher { - $viewModeSwitcher = new ViewModeSwitcher(); + $controllerName = $this->getRequest()->getControllerName(); + + if ($controllerName === 'hostgroups' || $controllerName === 'servicegroups') { + $viewModeSwitcher = new GridViewModeSwitcher(); + } else { + $viewModeSwitcher = new ViewModeSwitcher(); + } + $viewModeSwitcher->setIdProtector([$this->getRequest(), 'protectId']); $user = $this->Auth()->getUser(); @@ -156,16 +164,6 @@ class Controller extends CompatController $viewMode = $viewModeSwitcher->getValue($viewModeSwitcher->getViewModeParam()); $requestUrl = Url::fromRequest(); - if ($viewMode === 'minimal' && $requestUrl->getPath() === 'icingadb/hostgroups') { - $requestUrl = Url::fromPath('icingadb/hostgroups/grid')->setParams($requestUrl->getParams()); - } elseif ($viewMode !== 'minimal' && $requestUrl->getPath() === 'icingadb/hostgroups/grid') { - $requestUrl = Url::fromPath('icingadb/hostgroups')->setParams($requestUrl->getParams()); - } elseif ($viewMode === 'minimal' && $requestUrl->getPath() === 'icingadb/servicegroups') { - $requestUrl = Url::fromPath('icingadb/servicegroups/grid')->setParams($requestUrl->getParams()); - } elseif ($viewMode !== 'minimal' && $requestUrl->getPath() === 'icingadb/servicegroups/grid') { - $requestUrl = Url::fromPath('icingadb/servicegroups')->setParams($requestUrl->getParams()); - } - $preferredModes[$requestUrl->getPath()] = $viewMode; $user->setAdditional('icingadb.view_modes', $preferredModes); @@ -191,7 +189,7 @@ class Controller extends CompatController $requestUrl->setParam($viewModeSwitcher->getViewModeParam(), $viewMode); if (! $requestUrl->hasParam($limitParam)) { - if ($viewMode === 'minimal') { + if ($viewMode === 'minimal' || $viewMode === 'grid') { $session->set('previous_page', $currentPage); $session->set('request_path', $requestUrl->getPath()); @@ -204,7 +202,10 @@ class Controller extends CompatController } $session->set('current_page', $currentPage); - } elseif ($viewModeSwitcher->getDefaultViewMode() === 'minimal') { + } elseif ( + $viewModeSwitcher->getDefaultViewMode() === 'minimal' + || $viewModeSwitcher->getDefaultViewMode() === 'grid' + ) { $limit = $paginationControl->getLimit(); if ($currentPage === $session->get('current_page')) { // No other page numbers have been selected, i.e the user only @@ -230,7 +231,8 @@ class Controller extends CompatController } )->handleRequest(ServerRequest::fromGlobals()); - if ($viewModeSwitcher->getViewMode() === 'minimal') { + $viewMode = $viewModeSwitcher->getViewMode(); + if ($viewMode === 'minimal' || $viewMode === 'grid') { $hasLimitParam = Url::fromRequest()->hasParam($limitControl->getLimitParam()); if ($paginationControl->getDefaultPageSize() <= LimitControl::DEFAULT_LIMIT && ! $hasLimitParam) {