Let the SummaryNavigationItemRenderer show the worst state

fixes #11185
This commit is contained in:
Eric Lippmann 2016-12-06 14:47:03 +01:00
parent 4f77cce7a9
commit b3bc1b6f81
1 changed files with 47 additions and 21 deletions

View File

@ -3,44 +3,70 @@
namespace Icinga\Web\Navigation\Renderer; namespace Icinga\Web\Navigation\Renderer;
use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;
/** /**
* Summary badge adding up all badges in the navigation's children that have the same state * Badge renderer summing up the worst state of its children
*/ */
class SummaryNavigationItemRenderer extends BadgeNavigationItemRenderer class SummaryNavigationItemRenderer extends BadgeNavigationItemRenderer
{ {
/** /**
* The title of each summarized child * Cached count
*
* @var int
*/
protected $count;
/**
* State to severity map
* *
* @var array * @var array
*/ */
protected $titles; protected static $stateSeverityMap = array(
self::STATE_OK => 0,
self::STATE_PENDING => 1,
self::STATE_UNKNOWN => 2,
self::STATE_WARNING => 3,
self::STATE_CRITICAL => 4,
);
/**
* Severity to state map
*
* @var array
*/
protected static $severityStateMap = array(
self::STATE_OK,
self::STATE_PENDING,
self::STATE_UNKNOWN,
self::STATE_WARNING,
self::STATE_CRITICAL
);
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCount() public function getCount()
{ {
$count = 0; if ($this->count === null) {
$countMap = array_fill(0, 5, 0);
$maxSeverity = 0;
$titles = array();
foreach ($this->getItem()->getChildren() as $child) { foreach ($this->getItem()->getChildren() as $child) {
$renderer = $child->getRenderer(); $renderer = $child->getRenderer();
if ($renderer instanceof BadgeNavigationItemRenderer) { if ($renderer instanceof BadgeNavigationItemRenderer) {
if ($renderer->getState() === $this->getState()) { $count = $renderer->getCount();
$this->titles[] = $renderer->getTitle(); if ($count) {
$count += $renderer->getCount(); $severity = static::$stateSeverityMap[$renderer->getState()];
$countMap[$severity] += $count;
$titles[] = $renderer->getTitle();
$maxSeverity = max($maxSeverity, $severity);
} }
} }
} }
$this->count = $countMap[$maxSeverity];
$this->state = static::$severityStateMap[$maxSeverity];
$this->title = implode('. ', $titles);
}
return $count; return $this->count;
}
/**
* {@inheritdoc}
*/
public function getTitle()
{
return ! empty($this->titles) ? join(', ', $this->titles) : '';
} }
} }