From 7f28c0a2372694b0a55df4f107dfe3933ce290b6 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Thu, 30 Apr 2015 16:06:05 +0200 Subject: [PATCH] Implement ProblemMenuItemRenderer refs # 4139 --- library/Icinga/Web/Menu.php | 23 ++++++- .../Web/Menu/ProblemMenuItemRenderer.php | 64 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 library/Icinga/Web/Menu/ProblemMenuItemRenderer.php diff --git a/library/Icinga/Web/Menu.php b/library/Icinga/Web/Menu.php index bca1e17d10..476d73879a 100644 --- a/library/Icinga/Web/Menu.php +++ b/library/Icinga/Web/Menu.php @@ -234,7 +234,8 @@ protected function addMainMenuItems() $section = $this->add(t('System'), array( 'icon' => 'wrench', - 'priority' => 200 + 'priority' => 200, + 'renderer' => 'ProblemMenuItemRenderer' )); $section->add(t('Configuration'), array( 'url' => 'config', @@ -469,6 +470,26 @@ public function getPermission() return $this->permission; } + /** + * Get parent menu + * + * @return \Icinga\Web\Menu + */ + public function getParent() + { + return $this->parent; + } + + /** + * Get submenus + * + * @return array + */ + public function getSubMenus() + { + return $this->subMenus; + } + /** * Set permission a user is required to have granted to display the menu item * diff --git a/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php b/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php new file mode 100644 index 0000000000..010bdc0346 --- /dev/null +++ b/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php @@ -0,0 +1,64 @@ +getParent() !== null && $menu->hasSubMenus()) { + /** @var $submenu Menu */ + foreach ($menu->getSubMenus() as $submenu) { + $renderer = $submenu->getRenderer(); + if (method_exists($renderer, 'getSummary')) { + if ($renderer->getSummary() !== null) { + $this->summary[] = $renderer->getSummary(); + } + } + } + } + return $this->getBadge() . $this->createLink($menu); + } + + /** + * Get the problem badge + * + * @return string + */ + protected function getBadge() + { + if (count($this->summary) > 0) { + $problems = 0; + $titles = array(); + + foreach ($this->summary as $summary) { + $problems += $summary['problems']; + $titles[] = $summary['title']; + } + + return sprintf( + '
%s
', + implode(', ', $titles), + $problems + ); + } + return ''; + } +}