2022-02-17 04:48:29 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-23 17:12:09 -05:00
|
|
|
// SPDX-FileCopyrightText: 2022 Icinga GmbH <https://icinga.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-02-17 04:48:29 -05:00
|
|
|
|
|
|
|
|
namespace Icinga\Module\Icingadb\Common;
|
|
|
|
|
|
|
|
|
|
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
|
2022-10-17 10:43:57 -04:00
|
|
|
use ipl\Html\Html;
|
2022-02-17 04:48:29 -05:00
|
|
|
use ipl\Orm\Query;
|
|
|
|
|
use ipl\Web\Control\SearchBar;
|
|
|
|
|
use ipl\Web\Url;
|
|
|
|
|
use ipl\Web\Widget\ContinueWith;
|
|
|
|
|
|
|
|
|
|
trait SearchControls
|
|
|
|
|
{
|
2022-10-17 10:43:57 -04:00
|
|
|
use \ipl\Web\Compat\SearchControls {
|
|
|
|
|
\ipl\Web\Compat\SearchControls::createSearchBar as private webCreateSearchBar;
|
|
|
|
|
}
|
2022-02-17 04:48:29 -05:00
|
|
|
|
2022-06-08 09:36:01 -04:00
|
|
|
public function fetchFilterColumns(Query $query): array
|
2022-02-17 04:48:29 -05:00
|
|
|
{
|
|
|
|
|
return iterator_to_array(ObjectSuggestions::collectFilterColumns($query->getModel(), $query->getResolver()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 10:43:57 -04:00
|
|
|
/**
|
|
|
|
|
* Create and return the SearchBar
|
|
|
|
|
*
|
|
|
|
|
* @param Query $query The query being filtered
|
|
|
|
|
* @param Url $redirectUrl Url to redirect to upon success
|
|
|
|
|
* @param array $preserveParams Query params to preserve when redirecting
|
|
|
|
|
*
|
|
|
|
|
* @return SearchBar
|
|
|
|
|
*/
|
|
|
|
|
public function createSearchBar(Query $query, ...$params): SearchBar
|
|
|
|
|
{
|
|
|
|
|
$searchBar = $this->webCreateSearchBar($query, ...$params);
|
|
|
|
|
|
|
|
|
|
if (($wrapper = $searchBar->getWrapper()) && ! $wrapper->getWrapper()) {
|
|
|
|
|
// TODO: Remove this once ipl-web v0.7.0 is required
|
|
|
|
|
$searchBar->addWrapper(Html::tag('div', ['class' => 'search-controls']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $searchBar;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 04:48:29 -05:00
|
|
|
/**
|
|
|
|
|
* Create and return a ContinueWith
|
|
|
|
|
*
|
|
|
|
|
* This will automatically be appended to the SearchBar's wrapper. It's not necessary
|
|
|
|
|
* to add it separately as control or content!
|
|
|
|
|
*
|
|
|
|
|
* @param Url $detailsUrl
|
|
|
|
|
* @param SearchBar $searchBar
|
2026-01-22 08:40:57 -05:00
|
|
|
* @param bool $hasResults Whether the current query has results
|
2022-02-17 04:48:29 -05:00
|
|
|
*
|
|
|
|
|
* @return ContinueWith
|
|
|
|
|
*/
|
2026-01-22 08:40:57 -05:00
|
|
|
public function createContinueWith(Url $detailsUrl, SearchBar $searchBar, bool $hasResults = true): ContinueWith
|
2022-02-17 04:48:29 -05:00
|
|
|
{
|
2026-01-22 08:40:57 -05:00
|
|
|
if ($hasResults) {
|
|
|
|
|
$continueWith = ContinueWith::create(
|
|
|
|
|
$detailsUrl,
|
|
|
|
|
[$searchBar, 'getFilter'],
|
|
|
|
|
t('Show bulk processing actions for all filtered results'),
|
|
|
|
|
t('A filter is required to show bulk processing actions'),
|
|
|
|
|
);
|
|
|
|
|
$continueWith->setBaseTarget('_next');
|
|
|
|
|
} else {
|
|
|
|
|
$continueWith = ContinueWith::createDisabled(t('No items found'));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 04:48:29 -05:00
|
|
|
$continueWith->getAttributes()
|
|
|
|
|
->set('id', $this->getRequest()->protectId('continue-with'));
|
|
|
|
|
|
|
|
|
|
$searchBar->getWrapper()->add($continueWith);
|
|
|
|
|
|
|
|
|
|
return $continueWith;
|
|
|
|
|
}
|
|
|
|
|
}
|