diff --git a/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php b/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php index 1c6d230ed..2916f4eea 100644 --- a/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php +++ b/library/Icinga/Web/Navigation/Renderer/SummaryNavigationItemRenderer.php @@ -3,44 +3,70 @@ 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 { /** - * The title of each summarized child + * Cached count + * + * @var int + */ + protected $count; + + /** + * State to severity map * * @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} */ public function getCount() { - $count = 0; - foreach ($this->getItem()->getChildren() as $child) { - $renderer = $child->getRenderer(); - if ($renderer instanceof BadgeNavigationItemRenderer) { - if ($renderer->getState() === $this->getState()) { - $this->titles[] = $renderer->getTitle(); - $count += $renderer->getCount(); + if ($this->count === null) { + $countMap = array_fill(0, 5, 0); + $maxSeverity = 0; + $titles = array(); + foreach ($this->getItem()->getChildren() as $child) { + $renderer = $child->getRenderer(); + if ($renderer instanceof BadgeNavigationItemRenderer) { + $count = $renderer->getCount(); + if ($count) { + $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; - } - - /** - * {@inheritdoc} - */ - public function getTitle() - { - return ! empty($this->titles) ? join(', ', $this->titles) : ''; + return $this->count; } }