Skip to content

Commit

Permalink
Implement ProblemMenuItemRenderer
Browse files Browse the repository at this point in the history
refs # 4139
  • Loading branch information
Alexander Fuhr committed Apr 30, 2015
1 parent 7c0be30 commit 7f28c0a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
23 changes: 22 additions & 1 deletion library/Icinga/Web/Menu.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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
*
Expand Down
64 changes: 64 additions & 0 deletions library/Icinga/Web/Menu/ProblemMenuItemRenderer.php
@@ -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 '';
}
}

0 comments on commit 7f28c0a

Please sign in to comment.