Implement ProblemMenuItemRenderer

refs # 4139
This commit is contained in:
Alexander Fuhr 2015-04-30 16:06:05 +02:00
parent 7c0be30def
commit 7f28c0a237
2 changed files with 86 additions and 1 deletions

View File

@ -234,7 +234,8 @@ class Menu implements RecursiveIterator
$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 @@ class Menu implements RecursiveIterator
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
*

View File

@ -0,0 +1,64 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Menu;
use Icinga\Web\Menu;
class ProblemMenuItemRenderer extends MenuItemRenderer
{
/**
* Set of summarized problems from submenus
*
* @var array
*/
protected $summary = array();
/**
* Renders the html content of a single menu item and summarizes submenu problems
*
* @param Menu $menu
*
* @return string
*/
public function render(Menu $menu)
{
if ($menu->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(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
implode(', ', $titles),
$problems
);
}
return '';
}
}