mirror of
https://github.com/Icinga/icingadb-web.git
synced 2026-05-19 08:41:16 -04:00
Move creation of the ColumnChooser Form to createColumnControl()
This commit is contained in:
parent
1cd03462d8
commit
cd6eb32720
4 changed files with 79 additions and 68 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(...))
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue