Menu: Fix code compliance

refs #3759
This commit is contained in:
Eric Lippmann 2014-01-28 13:33:46 +01:00
parent c4a00b8d31
commit 48f5eebd53
2 changed files with 330 additions and 330 deletions

View File

@ -30,354 +30,53 @@
namespace Icinga\Web;
use Icinga\Application\Config;
use Icinga\Application\Modules\Manager as ModulManager;
use Icinga\Application\Icinga;
class MenuItem
{
/**
* MenuItem name
*
* @type string
*/
private $name;
/**
* MenuItem titel
*
* @type string
*/
private $title;
/**
* MenuItem priority
*
* @type int
*/
private $priority;
/**
* MenuItem url
*
* @type string
*/
private $url;
/**
* MenuItem icon path
*
* @type string
*/
private $icon;
/**
* MenuItem icon class
*
* @type string
*/
private $icon_class;
/**
* MenuItem children array
*
* @type array
*/
private $children = array();
/**
* Create a new MenuItem
*/
public function __construct($name='', $title='')
{
($name != '') ? $this->name = $name: $this->name = $title;
$this->title = $title;
$this->priority = 100;
}
/**
* Set name for MenuItems
*
* @param string name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name from MenuItem
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set title for MenuItems
*
* @param string title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title from MenuItem
*
* @return string
*/
public function getTitle()
{
return $this->title ? $this->title : $this->name;
}
/**
* Set priority for MenuItems
*
* @param int priority
*
* @return self
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority from MenuItem
*
* @return int
*/
public function getPriority()
{
return $this->priority;
}
/**
* Set url for MenuItems
*
* @param string url
*
* @return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url from MenuItem
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set icon for MenuItems
*
* @param string icon
*
* @return self
*/
public function setIcon($icon)
{
$this->icon = $icon;
return $this;
}
/**
* Get icon from MenuItem
*
* @return string
*/
public function getIcon()
{
return $this->icon;
}
/**
* Set icon class for MenuItems
*
* @param string icon class
*
* @return self
*/
public function setIconClass($iconClass)
{
$this->icon_class = $iconClass;
return $this;
}
/**
* Get icon class from MenuItem
*
* @return string
*/
public function getIconClass()
{
return $this->icon_class;
}
/**
* Add a child to MenuItem
*
* @param string identifier
* @param array props
*
* @return self
*/
public function addChild($identifier, $props)
{
if (false === ($pos = strpos($identifier, '.'))) {
$menuItem = new MenuItem($identifier);
$menuItem
->setTitle($props->title)
->setPriority($props->priority)
->setUrl($props->url)
->setIcon($props->icon)
->setIconClass($props->icon_class);
$this->children[$identifier] = $menuItem;
} else {
$parent = substr($identifier, 0, $pos);
$identifier = substr($identifier, $pos + 1);
$this->getChild($parent)->addChild($identifier, $props);
}
return $this;
}
/**
* Check if MenuItem has Childs
*
* @return bool
*/
public function hasChildren()
{
return ! empty($this->children);
}
/**
* Get children from MenuItem
*
* return all children proper sortet of the current MenuItem
*
* @return array
*/
public function getChildren()
{
usort($this->children, array($this, 'cmpChildren'));
return $this->children;
}
/**
* Get child from MenuItem
*
* @param string identifier
*
* @return MenuItem
*/
public function getChild($identifier)
{
return $this->children[$identifier];
}
/**
* Compare children
*
* compare two children against each other based on priority and name
*
* @return array
*/
protected function cmpChildren($a, $b)
{
if ($a->priority == $b->priority) {
return ($a->getTitle() > $b->getTitle()) ? +1 : -1;
}
return ($a->priority > $b->priority) ? +1 : -1;
}
}
use Icinga\Web\Menu\Item as MenuItem;
class Menu extends MenuItem
{
/**
* ZendConfig cfg
* Create menu from the application's menu config file plus the config files from all enabled modules
*
* @type Config
* @return Menu
*/
private $cfg;
/**
* Config
*
* @type config
*/
private $config;
/**
* Load MenuItems from all Configs
*
* @return Menu
*/
public static function fromConfig(){
$menu = new Menu();
$manager = Icinga::app()->getModuleManager();
$menu->cfg[] = Config::app('menu');
foreach ($manager->listEnabledModules() as $moduleName)
{
$menu->cfg[] = Config::module($moduleName, 'menu');
}
$menu->mergeConfigs();
$menu->loadMenuItems();
return $menu;
public static function fromConfig() {
$menu = new static('menu');
$manager = Icinga::app()->getModuleManager();
$menuConfigs = array(Config::app('menu'));
foreach ($manager->listEnabledModules() as $moduleName) {
$menuConfigs[] = Config::module($moduleName, 'menu');
}
return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs));
}
/**
* Merge all configs
*
* merge all configs and set suffix if duplicate entry exist
*
* Flatten configs into a key-value array
*/
private function mergeConfigs()
public function flattenConfigs(array $configs)
{
$this->config = array();
foreach ($this->cfg as $config){
foreach ($config as $identifier => $keys){
while (array_key_exists($identifier, $this->config)) {
$identifier .= '_dup';
$flattened = array();
foreach ($configs as $menuConfig) {
foreach ($menuConfig as $section => $itemConfig) {
while (array_key_exists($section, $flattened)) {
$section .= '_dup';
}
$this->config[$identifier] = $keys;
$flattened[$section] = $itemConfig;
}
}
ksort($this->config);
ksort($flattened);
return $flattened;
}
/**
* Load menu items
*
* load MenuItems based on the merged config
* Load menu items from a key-value array
*/
private function loadMenuItems()
public function loadMenuItems(array $flattened)
{
foreach ($this->config as $id => $props) {
$this->addChild($id, $props);
foreach ($flattened as $id => $itemConfig) {
$this->addChild($id, $itemConfig);
}
return $this;
}
}

View File

@ -0,0 +1,301 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
class Item
{
/**
* Item id
*
* @type string
*/
private $id;
/**
* Item title
*
* Used for sorting when priority is unset or equal to other items
*
* @type string
*/
private $title;
/**
* Item priority
*
* Used for sorting
*
* @type int
*/
private $priority = 100;
/**
* Item url
*
* @type string
*/
private $url;
/**
* Item icon path
*
* @type string
*/
private $icon;
/**
* Item icon class
*
* @type string
*/
private $iconClass;
/**
* Item's children
*
* @type array
*/
private $children = array();
/**
* Create a new MenuItem
*
* @param int $id
* @param object $config
*/
public function __construct($id, $config = null)
{
$this->id = $id;
if ($config !== null) {
$this->setConfig($config);
}
}
/**
* Setter for id
*
* @param string $id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Getter for id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Setter for title
*
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Getter for title
*
* @return string
*/
public function getTitle()
{
return $this->title ? $this->title : $this->id;
}
/**
* Setter for priority
*
* @param int $priority
*
* @return self
*/
public function setPriority($priority)
{
$this->priority = (int) $priority;
return $this;
}
/**
* Getter for priority
*
* @return int
*/
public function getPriority()
{
return $this->priority;
}
/**
* Setter for URL
*
* @param string $url
*
* @return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Getter for URL
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Setter for icon path
*
* @param string $path
*
* @return self
*/
public function setIcon($path)
{
$this->icon = $path;
return $this;
}
/**
* Getter for icon path
*
* @return string
*/
public function getIcon()
{
return $this->icon;
}
/**
* Setter for icon class
*
* @param string $iconClass
*
* @return self
*/
public function setIconClass($iconClass)
{
$this->iconClass = $iconClass;
return $this;
}
/**
* Getter for icon class
*
* @return string
*/
public function getIconClass()
{
return $this->iconClass;
}
/**
* Set the configuration for the item
*
* @param object $config
*/
public function setConfig($config)
{
foreach ($config as $key => $value) {
$method = 'set' . implode('', array_map('ucfirst', explode('_', strtolower($key))));
if (method_exists($this, $method)) {
$this->{$method}($value);
}
}
}
/**
* Add a child to MenuItem
*
* @param string $id
* @param object $itemConfig
*
* @return self
*/
public function addChild($id, $itemConfig)
{
if (false === ($pos = strpos($id, '.'))) {
$menuItem = new self($id, $itemConfig);
$this->children[$id] = $menuItem;
} else {
list($parentId, $id) = explode('.', $id, 2);
$this->getChild($parentId)->addChild($id, $itemConfig);
}
return $this;
}
/**
* Check whether the item has children
*
* @return bool
*/
public function hasChildren()
{
return !empty($this->children);
}
/**
* Get children sorted
*
* @return array
* @see cmpChildren()
*/
public function getChildren()
{
usort($this->children, array($this, 'cmpChildren'));
return $this->children;
}
/**
* Get child by its id
*
* @param string $id
*
* @return self
*/
public function getChild($id)
{
return $this->children[$id];
}
/**
* Compare children based on priority and title
*
* @param MenuItem $a
* @param MenuItem $b
*
* @return int
*/
protected function cmpChildren($a, $b)
{
if ($a->priority === $b->priority) {
return ($a->getTitle() > $b->getTitle()) ? 1 : -1;
}
return ($a->priority > $b->priority) ? 1 : -1;
}
}