Fix multi-view badge filters

Allow providing a base URL for StateBadges to display hosts and services based on a filter.

refs #5543
This commit is contained in:
Matthias Jentsch 2015-09-30 16:48:19 +02:00
parent 5a6aa1c073
commit 7e81b00a96
4 changed files with 54 additions and 1 deletions

View File

@ -207,6 +207,22 @@ class Url
return $url->setQueryString($f->toQueryString());
}
/**
* Set the new Filter of the url to be the current filter and the given filter
*
* @param Filter $and
*/
public function addFilter($and)
{
$this->setQueryString(
Filter::matchAll(
$and,
Filter::fromQueryString($this->getQueryString())
)->toQueryString()
);
return $this;
}
/**
* Overwrite the baseUrl
*

View File

@ -19,6 +19,7 @@ if (! $stats instanceof stdClass) {
<?php
$stateBadges = new StateBadges();
$stateBadges
->setBaseFilter(isset($baseFilter) ? $baseFilter : null)
->setUrl('monitoring/list/hosts')
->add(
StateBadges::STATE_UP,

View File

@ -22,6 +22,7 @@ if (! $stats instanceof stdClass) {
<?php
$stateBadges = new StateBadges();
$stateBadges
->setBaseFilter(isset($baseFilter) ? $baseFilter : null)
->setUrl('monitoring/list/services')
->add(
StateBadges::STATE_OK,

View File

@ -8,6 +8,7 @@ use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Url;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Data\Filter\Filter;
class StateBadges extends AbstractWidget
{
@ -123,6 +124,13 @@ class StateBadges extends AbstractWidget
*/
protected $priority = 1;
/**
* The base filter applied to any badge link
*
* @var Filter
*/
protected $baseFilter;
/**
* Base URL
*
@ -156,6 +164,29 @@ class StateBadges extends AbstractWidget
return $this;
}
/**
* Get the base filter
*
* @return Filter
*/
public function getBaseFilter()
{
return $this->baseFilter;
}
/**
* Set the base filter
*
* @param Filter $baseFilter
*
* @return $this
*/
public function setBaseFilter($baseFilter)
{
$this->baseFilter = $baseFilter;
return $this;
}
/**
* Add a state badge
*
@ -193,6 +224,10 @@ class StateBadges extends AbstractWidget
{
if ($this->has($state)) {
$badge = $this->get($state);
$url = clone $this->url->setParams($badge->filter);
if (isset($this->baseFilter)) {
$url->addFilter($this->baseFilter);
}
$badges->addItem(new NavigationItem($state, array(
'attributes' => array('class' => 'badge ' . $state),
'label' => $badge->count,
@ -201,7 +236,7 @@ class StateBadges extends AbstractWidget
mtp('monitoring', $badge->translateSingular, $badge->translatePlural, $badge->count),
$badge->translateArgs
),
'url' => clone $this->url->setParams($badge->filter)
'url' => $url
)));
}
return $this;