* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Web; use Icinga\Application\Config; use Icinga\Application\Icinga; use Icinga\Logger\Logger; use Icinga\Exception\NotReadableError; class Menu extends MenuItem { /** * Create menu from the application's menu config file plus the config files from all enabled modules * * @return Menu */ 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) { $moduleMenuConfig = Config::module($moduleName, 'menu'); if ($moduleMenuConfig) { $menuConfigs[] = $moduleMenuConfig; } } } catch (NotReadableError $e) { Logger::error($e); } return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs)); } /** * Flatten configs into a key-value array */ public function flattenConfigs(array $configs) { $flattened = array(); foreach ($configs as $menuConfig) { foreach ($menuConfig as $section => $itemConfig) { while (array_key_exists($section, $flattened)) { $section .= '_dup'; } $flattened[$section] = $itemConfig; } } ksort($flattened); return $flattened; } /** * Load menu items from a key-value array */ public function loadMenuItems(array $flattened) { foreach ($flattened as $id => $itemConfig) { $this->addChild($id, $itemConfig); } return $this; } }