From cd6eb32720070705e920ed2df2eb04d52fd2e899 Mon Sep 17 00:00:00 2001 From: Bastian Lederer Date: Mon, 9 Mar 2026 14:21:15 +0100 Subject: [PATCH] Move creation of the `ColumnChooser` Form to `createColumnControl()` --- application/controllers/HostsController.php | 40 ++++++++--------- .../controllers/ServicesController.php | 43 +++++++++---------- .../Icingadb/Web/Control/ColumnChooser.php | 29 +++++-------- library/Icingadb/Web/Controller.php | 35 ++++++++++++--- 4 files changed, 79 insertions(+), 68 deletions(-) diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index 0a6bae37..622a9b39 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -72,7 +72,14 @@ class HostsController extends Controller $limitControl, viewModeSwitcherClass: TabularViewModeSwitcher::class ); - $columns = $this->createColumnControl($hosts, $viewModeSwitcher, ['host.name', 'host.state.output']); + $columns = $this->createColumnControl( + $hosts, + $viewModeSwitcher, + Url::fromPath('icingadb/hosts/suggestColumns'), + Host::on($this->getDb())->getResolver(), + ['host.name', 'host.state.output'] + ) + ->getColumns(); $searchBar = $this->createSearchBar($hosts, [ $limitControl->getLimitParam(), @@ -230,25 +237,18 @@ class HostsController extends Controller public function columnControlAction() { $this->addTitleTab($this->translate('Select Columns')); - $this->addContent( - (new ColumnChooser(Url::fromPath('icingadb/hosts/suggestColumns'), Host::on($this->getDb())->getResolver())) - ->setAction((string) Url::fromRequest()) - ->on(ColumnChooser::ON_SENT, function (ColumnChooser $form) { - if ($form->hasBeenSubmitted()) { - $url = Url::fromPath('icingadb/hosts'); - $url->setParam('columns', $form->getValue('columns', '')); - $this->redirectNow($url); - } else { - foreach ($form->getPartUpdates() as $update) { - if (! is_array($update)) { - $update = [$update]; - } - - $this->addPart(...$update); - } - } - })->handleRequest($this->getServerRequest()) - ); + $columnChooser = $this->createColumnControl( + Host::on($this->getDb()), + $this->createViewModeSwitcher( + $this->createPaginationControl(Host::on($this->getDb())), + $this->createLimitControl(), + viewModeSwitcherClass: TabularViewModeSwitcher::class + ), + Url::fromPath('icingadb/hosts/suggestColumns'), + Host::on($this->getDb())->getResolver(), + ['host.name', 'host.state.output'] + )->handleRequest($this->getServerRequest()); + $this->addContent($columnChooser); } protected function fetchCommandTargets(): Query diff --git a/application/controllers/ServicesController.php b/application/controllers/ServicesController.php index 27a74523..d439a464 100644 --- a/application/controllers/ServicesController.php +++ b/application/controllers/ServicesController.php @@ -84,7 +84,14 @@ class ServicesController extends Controller $limitControl, viewModeSwitcherClass: TabularViewModeSwitcher::class ); - $columns = $this->createColumnControl($services, $viewModeSwitcher, ['service.name', 'service.state.output']); + $columns = $this->createColumnControl( + $services, + $viewModeSwitcher, + Url::fromPath('icingadb/services/suggestColumns'), + Service::on($this->getDb())->getResolver(), + ['service.name', 'service.state.output'] + ) + ->getColumns(); $searchBar = $this->createSearchBar($services, [ $limitControl->getLimitParam(), @@ -396,28 +403,18 @@ class ServicesController extends Controller public function columnControlAction() { $this->addTitleTab($this->translate('Select Columns')); - $this->addContent( - (new ColumnChooser( - Url::fromPath('icingadb/services/suggestColumns'), - Service::on($this->getDb())->getResolver() - )) - ->setAction((string) Url::fromRequest()) - ->on(ColumnChooser::ON_SENT, function (ColumnChooser $form) { - if ($form->hasBeenSubmitted()) { - $url = Url::fromPath('icingadb/services'); - $url->setParam('columns', $form->getValue('columns', '')); - $this->redirectNow($url); - } else { - foreach ($form->getPartUpdates() as $update) { - if (! is_array($update)) { - $update = [$update]; - } - - $this->addPart(...$update); - } - } - })->handleRequest($this->getServerRequest()) - ); + $columnChooser = $this->createColumnControl( + Service::on($this->getDb()), + $this->createViewModeSwitcher( + $this->createPaginationControl(Service::on($this->getDb())), + $this->createLimitControl(), + viewModeSwitcherClass: TabularViewModeSwitcher::class + ), + Url::fromPath('icingadb/services/suggestColumns'), + Service::on($this->getDb())->getResolver(), + ['service.name', 'service.state.output'] + )->handleRequest($this->getServerRequest()); + $this->addContent($columnChooser); } protected function fetchCommandTargets(): Query diff --git a/library/Icingadb/Web/Control/ColumnChooser.php b/library/Icingadb/Web/Control/ColumnChooser.php index bae6b1a2..2ccf1bdd 100644 --- a/library/Icingadb/Web/Control/ColumnChooser.php +++ b/library/Icingadb/Web/Control/ColumnChooser.php @@ -4,27 +4,33 @@ namespace Icinga\Module\Icingadb\Web\Control; -use ipl\Html\Attributes; -use ipl\Html\HtmlElement; use ipl\Orm\Resolver; use ipl\Web\Compat\CompatForm; use ipl\Web\FormElement\TermInput; use ipl\Web\FormElement\TermInput\Term; use ipl\Web\Url; -use Psr\Http\Message\ServerRequestInterface; class ColumnChooser extends CompatForm { + /** @var string[] The columns already present in the url */ + protected array $columns = []; + /** @var Url The suggestionUrl for the TermInput {@see TermInput::$suggestionUrl} */ protected Url $suggestionUrl; /** @var Resolver The resolver used to validate column names and get their labels */ protected Resolver $resolver; - public function __construct(Url $suggestionUrl, Resolver $resolver) + public function __construct(Url $suggestionUrl, Resolver $resolver, array $columns = []) { $this->suggestionUrl = $suggestionUrl; $this->resolver = $resolver; + $this->columns = $columns; + } + + public function getColumns(): array + { + return $this->columns; } public function getPartUpdates(): array @@ -69,19 +75,6 @@ class ColumnChooser extends CompatForm } } - /** - * Get the columns from the request - * - * @param ServerRequestInterface $request - * - * @return string - */ - protected function getColumns(ServerRequestInterface $request): string - { - $columns = $request->getQueryParams()['columns']; - - return $columns; - } protected function assemble() { $termInput = (new TermInput( @@ -96,7 +89,7 @@ class ColumnChooser extends CompatForm ->setReadOnly() ->setOrdered() ->setSuggestionUrl($this->suggestionUrl) - ->setValue($this->getColumns($this->getRequest())) + ->setValue(implode(',', $this->columns)) ->on(TermInput::ON_ENRICH, $this->validateTermsAndSetLabels(...)) ->on(TermInput::ON_ADD, $this->validateTermsAndSetLabels(...)) ->on(TermInput::ON_SAVE, $this->validateTermsAndSetLabels(...)) diff --git a/library/Icingadb/Web/Controller.php b/library/Icingadb/Web/Controller.php index 8a55c07e..5b9aa06d 100644 --- a/library/Icingadb/Web/Controller.php +++ b/library/Icingadb/Web/Controller.php @@ -24,6 +24,7 @@ use Icinga\Module\Icingadb\Common\Model; use Icinga\Module\Icingadb\Common\SearchControls; use Icinga\Module\Icingadb\Data\CsvResultSet; use Icinga\Module\Icingadb\Data\JsonResultSet; +use Icinga\Module\Icingadb\Web\Control\ColumnChooser; use Icinga\Module\Icingadb\Web\Control\GridViewModeSwitcher; use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions; use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher; @@ -38,6 +39,7 @@ use Icinga\Util\Json; use ipl\Html\Html; use ipl\Html\ValidHtml; use ipl\Orm\Query; +use ipl\Orm\Resolver; use ipl\Orm\UnionQuery; use ipl\Stdlib\Filter; use ipl\Web\Compat\CompatController; @@ -84,10 +86,15 @@ class Controller extends CompatController * @param ViewModeSwitcher $viewModeSwitcher * @param array $defaultColumns * - * @return array provided columns + * @return ColumnChooser provided columns */ - public function createColumnControl(Query $query, ViewModeSwitcher $viewModeSwitcher, array $defaultColumns): array - { + public function createColumnControl( + Query $query, + ViewModeSwitcher $viewModeSwitcher, + Url $suggestionUrl, + Resolver $resolver, + array $defaultColumns + ): ColumnChooser { // All of that is essentially what `ColumnControl::apply()` should do $viewMode = $viewModeSwitcher->getViewMode(); $columnsDef = $this->params->shift('columns'); @@ -95,7 +102,7 @@ class Controller extends CompatController if ($viewMode === 'tabular') { $columns = $defaultColumns; } else { - return []; + return new ColumnChooser($suggestionUrl, $resolver); } } else { $columns = []; @@ -117,9 +124,23 @@ class Controller extends CompatController $viewModeSwitcher->setViewMode('tabular'); } - // For now this also returns the columns, but they should be accessible - // by calling `ColumnControl::getColumns()` in the future - return $columns; + return (new ColumnChooser($suggestionUrl, $resolver, $columns)) + ->setAction((string) Url::fromRequest()) + ->on(ColumnChooser::ON_SENT, function (ColumnChooser $form) { + if ($form->hasBeenSubmitted()) { + $url = Url::fromPath('icingadb/services'); + $url->setParam('columns', $form->getValue('columns', '')); + $this->redirectNow($url); + } else { + foreach ($form->getPartUpdates() as $update) { + if (! is_array($update)) { + $update = [$update]; + } + + $this->addPart(...$update); + } + } + }); } /**