FilterEditor: Support queries implementing the FilterColumns interface

refs #9029
This commit is contained in:
Johannes Meyer 2015-08-13 15:58:04 +02:00
parent 40dd094af0
commit 41b80efa0e

View File

@ -4,6 +4,7 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use Icinga\Data\Filterable; use Icinga\Data\Filterable;
use Icinga\Data\FilterColumns;
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;
@ -232,7 +233,12 @@ class FilterEditor extends AbstractWidget
if (strpos($search, '=') !== false) { if (strpos($search, '=') !== false) {
list($k, $v) = preg_split('/=/', $search); list($k, $v) = preg_split('/=/', $search);
$filter = $this->mergeRootExpression($filter, trim($k), '=', ltrim($v)); $filter = $this->mergeRootExpression($filter, trim($k), '=', ltrim($v));
} elseif (! empty($this->searchColumns)) { } else {
if ($this->searchColumns === null && $this->query instanceof FilterColumns) {
$this->searchColumns = $this->query->getSearchColumns();
}
if (! empty($this->searchColumns)) {
if (! $this->resetSearchColumns($filter)) { if (! $this->resetSearchColumns($filter)) {
$filter = Filter::matchAll(); $filter = Filter::matchAll();
} }
@ -246,6 +252,7 @@ class FilterEditor extends AbstractWidget
Notification::error(mt('monitoring', 'Cannot search here')); Notification::error(mt('monitoring', 'Cannot search here'));
return $this; return $this;
} }
}
$url = $this->url()->setQueryString( $url = $this->url()->setQueryString(
$filter->toQueryString() $filter->toQueryString()
@ -500,12 +507,14 @@ class FilterEditor extends AbstractWidget
); );
} }
protected function arrayForSelect($array) protected function arrayForSelect($array, $flip = false)
{ {
$res = array(); $res = array();
foreach ($array as $k => $v) { foreach ($array as $k => $v) {
if (is_int($k)) { if (is_int($k)) {
$res[$v] = $v; $res[str_replace('_', ' ', ucfirst($v))] = $v;
} elseif ($flip) {
$res[$v] = $k;
} else { } else {
$res[$k] = $v; $res[$k] = $v;
} }
@ -576,20 +585,15 @@ class FilterEditor extends AbstractWidget
); );
} }
if ($this->cachedColumnSelect === null) { if ($this->cachedColumnSelect === null && $this->query instanceof FilterColumns) {
$this->cachedColumnSelect = $this->arrayForSelect($this->query->getColumns()); $this->cachedColumnSelect = $this->arrayForSelect($this->query->getFilterColumns(), true);
asort($this->cachedColumnSelect); asort($this->cachedColumnSelect);
} } elseif ($this->cachedColumnSelect === null) {
$cols = $this->cachedColumnSelect; throw new ProgrammingError('No columns set nor does the query provide any');
$seen = false;
foreach ($cols as $k => & $v) {
$v = str_replace('_', ' ', ucfirst($v));
if ($k === $active) {
$seen = true;
}
} }
if (!$seen) { $cols = $this->cachedColumnSelect;
if (! isset($cols[$active])) {
$cols[$active] = str_replace('_', ' ', ucfirst(ltrim($active, '_'))); $cols[$active] = str_replace('_', ' ', ucfirst(ltrim($active, '_')));
} }