parent
290fe9eeb5
commit
c5c375e72d
|
@ -29,9 +29,9 @@
|
|||
|
||||
namespace Icinga\Web;
|
||||
|
||||
use Icinga\Logger\Logger;
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Logger\Logger;
|
||||
use Icinga\Exception\NotReadableError;
|
||||
|
||||
class Menu extends MenuItem
|
||||
|
@ -39,34 +39,49 @@ class Menu extends MenuItem
|
|||
/**
|
||||
* Create menu from the application's menu config file plus the config files from all enabled modules
|
||||
*
|
||||
* @return Menu
|
||||
* @return self
|
||||
*/
|
||||
public static function fromConfig()
|
||||
{
|
||||
$menu = new static('menu');
|
||||
$manager = Icinga::app()->getModuleManager();
|
||||
|
||||
try {
|
||||
$menuConfigs = array(Config::app('menu'));
|
||||
} catch (NotReadableError $e) {
|
||||
Logger::error($e);
|
||||
$menuConfigs = array();
|
||||
}
|
||||
try {
|
||||
|
||||
foreach ($manager->listEnabledModules() as $moduleName) {
|
||||
try {
|
||||
$modules = $manager->listEnabledModules();
|
||||
} catch (NotReadableError $e) {
|
||||
Logger::error($e);
|
||||
$modules = array();
|
||||
}
|
||||
|
||||
foreach ($modules as $moduleName) {
|
||||
try {
|
||||
$moduleMenuConfig = Config::module($moduleName, 'menu');
|
||||
if ($moduleMenuConfig) {
|
||||
} catch (NotReadableError $e) {
|
||||
Logger::error($e);
|
||||
$moduleMenuConfig = array();
|
||||
}
|
||||
|
||||
if (!empty($moduleMenuConfig)) {
|
||||
$menuConfigs[] = $moduleMenuConfig;
|
||||
}
|
||||
}
|
||||
} catch (NotReadableError $e) {
|
||||
Logger::error($e);
|
||||
}
|
||||
|
||||
return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten configs into a key-value array
|
||||
* Flatten configs
|
||||
*
|
||||
* @param array $configs An two dimensional array of menu configurations
|
||||
*
|
||||
* @return array The flattened config, as key-value array
|
||||
*/
|
||||
public function flattenConfigs(array $configs)
|
||||
{
|
||||
|
@ -79,18 +94,23 @@ class Menu extends MenuItem
|
|||
$flattened[$section] = $itemConfig;
|
||||
}
|
||||
}
|
||||
ksort($flattened);
|
||||
|
||||
return $flattened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load menu items from a key-value array
|
||||
* Load menu items
|
||||
*
|
||||
* @param array $items The items to load, as key-value array
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function loadMenuItems(array $flattened)
|
||||
public function loadMenuItems(array $items)
|
||||
{
|
||||
foreach ($flattened as $id => $itemConfig) {
|
||||
foreach ($items as $id => $itemConfig) {
|
||||
$this->addChild($id, $itemConfig);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ class MenuItem
|
|||
*
|
||||
* @type string
|
||||
*/
|
||||
private $id;
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Item title
|
||||
|
@ -22,7 +22,7 @@ class MenuItem
|
|||
*
|
||||
* @type string
|
||||
*/
|
||||
private $title;
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* Item priority
|
||||
|
@ -31,38 +31,42 @@ class MenuItem
|
|||
*
|
||||
* @type int
|
||||
*/
|
||||
private $priority = 100;
|
||||
protected $priority = 100;
|
||||
|
||||
/**
|
||||
* Item url
|
||||
*
|
||||
* @type string
|
||||
*/
|
||||
private $url;
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Item icon path
|
||||
*
|
||||
* @type string
|
||||
*/
|
||||
private $icon;
|
||||
protected $icon;
|
||||
|
||||
/**
|
||||
* Item icon class
|
||||
*
|
||||
* @type string
|
||||
*/
|
||||
private $iconClass;
|
||||
protected $iconClass;
|
||||
|
||||
/**
|
||||
* Item's children
|
||||
*
|
||||
* @type array
|
||||
*/
|
||||
private $children = array();
|
||||
|
||||
private $attribs = array();
|
||||
protected $children = array();
|
||||
|
||||
/**
|
||||
* HTML anchor tag attributes
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attribs = array();
|
||||
|
||||
/**
|
||||
* Create a new MenuItem
|
||||
|
@ -114,7 +118,6 @@ class MenuItem
|
|||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Getter for title
|
||||
*
|
||||
|
@ -220,7 +223,7 @@ class MenuItem
|
|||
/**
|
||||
* Set the configuration for the item
|
||||
*
|
||||
* @param object $config
|
||||
* @param Zend_Config $config
|
||||
*/
|
||||
public function setConfig($config)
|
||||
{
|
||||
|
@ -249,18 +252,21 @@ class MenuItem
|
|||
} else {
|
||||
// Submenu item
|
||||
list($parentId, $id) = explode('.', $id, 2);
|
||||
|
||||
if ($this->hasChild($parentId)) {
|
||||
$parent = $this->getChild($parentId);
|
||||
} else {
|
||||
$parent = $this->addChild($parentId);
|
||||
}
|
||||
|
||||
$menuItem = $parent->addChild($id, $itemConfig);
|
||||
}
|
||||
|
||||
return $menuItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the item has children
|
||||
* Check whether the item has any children
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
@ -270,10 +276,11 @@ class MenuItem
|
|||
}
|
||||
|
||||
/**
|
||||
* Get children sorted
|
||||
* Get sorted children
|
||||
*
|
||||
* @return array
|
||||
* @see cmpChildren()
|
||||
*
|
||||
* @see MenuItem::cmpChildren()
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
|
@ -282,11 +289,11 @@ class MenuItem
|
|||
}
|
||||
|
||||
/**
|
||||
* Whether a given child id exists
|
||||
* Return whether a given child id exists
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return self|$default
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChild($id)
|
||||
{
|
||||
|
@ -300,18 +307,20 @@ class MenuItem
|
|||
* @param mixed $default
|
||||
*
|
||||
* @return MenuItem
|
||||
*
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public function getChild($id)
|
||||
{
|
||||
if ($this->hasChild($id)) {
|
||||
return $this->children[$id];
|
||||
}
|
||||
if (!$this->hasChild($id)) {
|
||||
throw new ProgrammingError(sprintf('Trying to get invalid Menu child "%s"', $id));
|
||||
}
|
||||
|
||||
return $this->children[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set HTML a tag attributes
|
||||
* Set HTML anchor tag attributes
|
||||
*
|
||||
* @param array $attribs
|
||||
*
|
||||
|
@ -324,7 +333,7 @@ class MenuItem
|
|||
}
|
||||
|
||||
/**
|
||||
* Get HTML a tag attributes
|
||||
* Get HTML anchor tag attributes
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
@ -346,6 +355,7 @@ class MenuItem
|
|||
if ($a->priority === $b->priority) {
|
||||
return ($a->getTitle() > $b->getTitle()) ? 1 : -1;
|
||||
}
|
||||
|
||||
return ($a->priority > $b->priority) ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Tests\Icinga\Web;
|
||||
|
||||
use Zend_Config;
|
||||
use Icinga\Web\MenuItem;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
|
||||
class MenuItemTest extends BaseTestCase
|
||||
{
|
||||
public function testWhetherMenuItemsAreNaturallySorted()
|
||||
{
|
||||
$item = new MenuItem('test');
|
||||
$item->addChild(5, new Zend_Config(array('title' => 'ccc5')));
|
||||
$item->addChild(0, new Zend_Config(array('title' => 'aaa')));
|
||||
$item->addChild(3, new Zend_Config(array('title' => 'ccc')));
|
||||
$item->addChild(2, new Zend_Config(array('title' => 'bbb')));
|
||||
$item->addChild(4, new Zend_Config(array('title' => 'ccc2')));
|
||||
$item->addChild(1, new Zend_Config(array('title' => 'bb')));
|
||||
|
||||
$this->assertEquals(
|
||||
array('aaa', 'bb', 'bbb', 'ccc', 'ccc2', 'ccc5'),
|
||||
array_map(function ($it) { return $it->getTitle(); }, $item->getChildren()),
|
||||
'MenuItem::getChildren does not return its elements in natural order'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue