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,33 +208,44 @@ class FilterEditor extends AbstractWidget
$filter = $this->getFilter(); $filter = $this->getFilter();
if ($search !== null) { if ($search !== null) {
if (strpos($search, '=') === false) { if ($this->searchColumns === null) {
// TODO: Ask the view for (multiple) search columns if (strpos($search, '=') === false) {
switch($request->getActionName()) { // TODO: Ask the view for (multiple) search columns
case 'services': switch($request->getActionName()) {
$searchCol = 'service'; case 'services':
break; $searchCol = 'service';
case 'hosts': break;
$searchCol = 'host'; case 'hosts':
break; $searchCol = 'host';
case 'hostgroups': break;
$searchCol = 'hostgroup'; case 'hostgroups':
break; $searchCol = 'hostgroup';
case 'servicegroups': break;
$searchCol = 'servicegroup'; case 'servicegroups':
break; $searchCol = 'servicegroup';
default: break;
$searchCol = null; default:
} $searchCol = null;
}
if ($searchCol === null) { if ($searchCol === null) {
throw new Exception('Cannot search here'); throw new Exception('Cannot search here');
}
$filter = $this->mergeRootExpression($filter, $searchCol, '=', "*$search*");
} else {
list($k, $v) = preg_split('/=/', $search);
$filter = $this->mergeRootExpression($filter, $k, '=', $v);
} }
$filter = $this->mergeRootExpression($filter, $searchCol, '=', "*$search*");
} else { } else {
list($k, $v) = preg_split('/=/', $search); if (false === $this->resetSearchColumns($filter)) {
$filter = $this->mergeRootExpression($filter, $k, '=', $v); $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(