Skip to content

Commit

Permalink
Fix multi-view badge filters
Browse files Browse the repository at this point in the history
Allow providing a base URL for StateBadges to display hosts and services based on a filter.

refs #5543
  • Loading branch information
majentsch committed Sep 30, 2015
1 parent 5a6aa1c commit 7e81b00
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions library/Icinga/Web/Url.php
Expand Up @@ -207,6 +207,22 @@ public static function urlAddFilterOptional($url, $filter, $optional)
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
*
Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -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,
Expand Down
37 changes: 36 additions & 1 deletion modules/monitoring/library/Monitoring/Web/Widget/StateBadges.php
Expand Up @@ -8,6 +8,7 @@
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Url;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Data\Filter\Filter;

class StateBadges extends AbstractWidget
{
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -156,6 +164,29 @@ public function setUrl($url)
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
*
Expand Down Expand Up @@ -193,6 +224,10 @@ public function createBadge($state, Navigation $badges)
{
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,
Expand All @@ -201,7 +236,7 @@ public function createBadge($state, Navigation $badges)
mtp('monitoring', $badge->translateSingular, $badge->translatePlural, $badge->count),
$badge->translateArgs
),
'url' => clone $this->url->setParams($badge->filter)
'url' => $url
)));
}
return $this;
Expand Down

0 comments on commit 7e81b00

Please sign in to comment.