Implement Icinga\Web\Widget\FilterEditor::setSearchColumns() for specifying custom search columns

This commit is contained in:
Alexander A. Klimov 2015-04-20 17:54:47 +02:00
parent 5f3aafd3bd
commit d6e7061de3
1 changed files with 64 additions and 24 deletions

View File

@ -6,6 +6,7 @@ namespace Icinga\Web\Widget;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterExpression; use Icinga\Data\Filter\FilterExpression;
use Icinga\Data\Filter\FilterChain; use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterOr;
use Icinga\Web\Url; use Icinga\Web\Url;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
@ -39,6 +40,8 @@ class FilterEditor extends AbstractWidget
protected $ignoreParams = array(); protected $ignoreParams = array();
protected $searchColumns = null;
/** /**
* @var string * @var string
*/ */
@ -73,6 +76,12 @@ class FilterEditor extends AbstractWidget
return $this->filter; return $this->filter;
} }
public function setSearchColumns(array $searchColumns)
{
$this->searchColumns = $searchColumns;
return $this;
}
public function setUrl($url) public function setUrl($url)
{ {
$this->url = $url; $this->url = $url;
@ -147,6 +156,26 @@ class FilterEditor extends AbstractWidget
return $filter; return $filter;
} }
protected function resetSearchColumns(Filter &$filter)
{
if ($filter->isChain()) {
$filters = &$filter->filters();
if (!($empty = empty($filters))) {
foreach ($filters as $k => &$f) {
if (false === $this->resetSearchColumns($f)) {
unset($filters[$k]);
}
}
}
return $empty || !empty($filters);
}
return $filter->isExpression() ? !(
in_array($filter->getColumn(), $this->searchColumns)
&&
$filter->getSign() === '='
) : true;
}
public function handleRequest($request) public function handleRequest($request)
{ {
$this->setUrl($request->getUrl()->without($this->ignoreParams)); $this->setUrl($request->getUrl()->without($this->ignoreParams));
@ -179,6 +208,7 @@ class FilterEditor extends AbstractWidget
$filter = $this->getFilter(); $filter = $this->getFilter();
if ($search !== null) { if ($search !== null) {
if ($this->searchColumns === null) {
if (strpos($search, '=') === false) { if (strpos($search, '=') === false) {
// TODO: Ask the view for (multiple) search columns // TODO: Ask the view for (multiple) search columns
switch($request->getActionName()) { switch($request->getActionName()) {
@ -202,11 +232,21 @@ class FilterEditor extends AbstractWidget
throw new Exception('Cannot search here'); throw new Exception('Cannot search here');
} }
$filter = $this->mergeRootExpression($filter, $searchCol, '=', "*$search*"); $filter = $this->mergeRootExpression($filter, $searchCol, '=', "*$search*");
} else { } else {
list($k, $v) = preg_split('/=/', $search); list($k, $v) = preg_split('/=/', $search);
$filter = $this->mergeRootExpression($filter, $k, '=', $v); $filter = $this->mergeRootExpression($filter, $k, '=', $v);
} }
} else {
if (false === $this->resetSearchColumns($filter)) {
$filter = Filter::matchAll();
}
$filters = array();
foreach ($this->searchColumns as $searchColumn) {
$filters[] = Filter::expression($searchColumn, '=', "*$search*");
}
$filter->andFilter(new FilterOr($filters));
}
$url = $this->url()->setQueryString( $url = $this->url()->setQueryString(
$filter->toQueryString() $filter->toQueryString()