icingaweb2/library/Icinga/Web/Menu.php

97 lines
2.9 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Logger\Logger;
use Icinga\Exception\NotReadableError;
2014-01-28 13:33:46 +01:00
class Menu extends MenuItem
{
/**
2014-01-28 13:33:46 +01:00
* Create menu from the application's menu config file plus the config files from all enabled modules
*
2014-01-28 13:33:46 +01:00
* @return Menu
*/
2014-02-21 14:07:32 +01:00
public static function fromConfig()
{
2014-01-28 13:33:46 +01:00
$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);
}
2014-01-28 13:33:46 +01:00
return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs));
}
/**
2014-01-28 13:33:46 +01:00
* Flatten configs into a key-value array
*/
2014-01-28 13:33:46 +01:00
public function flattenConfigs(array $configs)
{
2014-01-28 13:33:46 +01:00
$flattened = array();
foreach ($configs as $menuConfig) {
foreach ($menuConfig as $section => $itemConfig) {
while (array_key_exists($section, $flattened)) {
$section .= '_dup';
}
2014-01-28 13:33:46 +01:00
$flattened[$section] = $itemConfig;
}
}
2014-01-28 13:33:46 +01:00
ksort($flattened);
return $flattened;
}
2014-01-28 13:33:46 +01:00
/**
2014-01-28 13:33:46 +01:00
* Load menu items from a key-value array
*/
2014-01-28 13:33:46 +01:00
public function loadMenuItems(array $flattened)
{
2014-01-28 13:33:46 +01:00
foreach ($flattened as $id => $itemConfig) {
$this->addChild($id, $itemConfig);
}
2014-01-28 13:33:46 +01:00
return $this;
}
}