From befbc6cd6acb2ea82499b8d21983a14d21b2e184 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 30 Sep 2015 11:30:38 +0200 Subject: [PATCH] Config: Add static method navigation() refs #10246 --- .../controllers/NavigationController.php | 47 +++------------ .../forms/Navigation/NavigationConfigForm.php | 31 +--------- library/Icinga/Application/Config.php | 59 +++++++++++++++++++ 3 files changed, 71 insertions(+), 66 deletions(-) diff --git a/application/controllers/NavigationController.php b/application/controllers/NavigationController.php index 2a360220c..577882f2d 100644 --- a/application/controllers/NavigationController.php +++ b/application/controllers/NavigationController.php @@ -63,35 +63,6 @@ class NavigationController extends Controller return $types; } - /** - * Return the path to the configuration file for the given navigation item type and user - * - * @param string $type - * @param string $username - * - * @return string - * - * @throws IcingaException In case the given type is unknown - */ - protected function getConfigPath($type, $username = null) - { - if (isset($this->moduleItemTypes[$type])) { - $options = $this->moduleItemTypes[$type]; - } elseif (isset($this->defaultItemTypes[$type])) { - $options = $this->defaultItemTypes[$type]; - } else { - throw new IcingaException('Invalid navigation item type %s provided', $type); - } - - if (isset($options['config'])) { - $filename = $options['config'] . '.ini'; - } else { - $filename = $type . 's.ini'; - } - - return Config::resolvePath(($username ? "preferences/$username/" : 'navigation/') . $filename); - } - /** * Show the current user a list of his/her navigation items */ @@ -185,10 +156,10 @@ class NavigationController extends Controller { $form = new NavigationConfigForm(); $form->setRedirectUrl('navigation'); - $form->setTitle($this->translate('Create New Navigation Item')); - $form->setItemTypes(array_merge($this->defaultItemTypes, $this->moduleItemTypes)); - $form->addDescription($this->translate('Create a new navigation item, such as a menu entry or dashlet.')); $form->setUser($this->Auth()->getUser()); + $form->setItemTypes($this->listItemTypes()); + $form->setTitle($this->translate('Create New Navigation Item')); + $form->addDescription($this->translate('Create a new navigation item, such as a menu entry or dashlet.')); $form->setOnSuccess(function (NavigationConfigForm $form) { $data = array_filter($form->getValues()); @@ -234,8 +205,8 @@ class NavigationController extends Controller $form = new NavigationConfigForm(); $form->setUser($user); - $form->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); - $form->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $itemOwner))); + $form->setShareConfig(Config::navigation($itemType)); + $form->setUserConfig(Config::navigation($itemType, $itemOwner)); $form->setRedirectUrl($referrer === 'shared' ? 'navigation/shared' : 'navigation'); $form->setTitle(sprintf($this->translate('Edit %s %s'), $this->getItemLabel($itemType), $itemName)); $form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) { @@ -289,8 +260,8 @@ class NavigationController extends Controller $navigationConfigForm = new NavigationConfigForm(); $navigationConfigForm->setUser($user); - $navigationConfigForm->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); - $navigationConfigForm->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $user->getUsername()))); + $navigationConfigForm->setShareConfig(Config::navigation($itemType)); + $navigationConfigForm->setUserConfig(Config::navigation($itemType, $user->getUsername())); $form = new ConfirmRemovalForm(); $form->setRedirectUrl('navigation'); @@ -336,8 +307,8 @@ class NavigationController extends Controller $navigationConfigForm = new NavigationConfigForm(); $navigationConfigForm->setUser($this->Auth()->getUser()); - $navigationConfigForm->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); - $navigationConfigForm->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $itemOwner))); + $navigationConfigForm->setShareConfig(Config::navigation($itemType)); + $navigationConfigForm->setUserConfig(Config::navigation($itemType, $itemOwner)); $form = new Form(array( 'onSuccess' => function ($form) use ($navigationConfigForm) { diff --git a/application/forms/Navigation/NavigationConfigForm.php b/application/forms/Navigation/NavigationConfigForm.php index 18ca97f47..905ef95c6 100644 --- a/application/forms/Navigation/NavigationConfigForm.php +++ b/application/forms/Navigation/NavigationConfigForm.php @@ -134,7 +134,7 @@ class NavigationConfigForm extends ConfigForm throw new ProgrammingError('You need to pass a type if no user configuration is set'); } - $this->setUserConfig($this->getItemConfig($type, $this->getUser()->getUsername())); + $this->setUserConfig(Config::navigation($type, $this->getUser()->getUsername())); } return $this->userConfig; @@ -168,7 +168,7 @@ class NavigationConfigForm extends ConfigForm throw new ProgrammingError('You need to pass a type if no share configuration is set'); } - $this->setShareConfig($this->getItemConfig($type)); + $this->setShareConfig(Config::navigation($type)); } return $this->shareConfig; @@ -639,11 +639,6 @@ class NavigationConfigForm extends ConfigForm ) ); } else { - $multiOptions = array(); - foreach ($itemTypes as $type => $options) { - $multiOptions[$type] = isset($options['label']) ? $options['label'] : $type; - } - $this->addElement( 'select', 'type', @@ -652,7 +647,7 @@ class NavigationConfigForm extends ConfigForm 'autosubmit' => true, 'label' => $this->translate('Type'), 'description' => $this->translate('The type of this navigation item'), - 'multiOptions' => $multiOptions + 'multiOptions' => $itemTypes ) ); } @@ -861,24 +856,4 @@ class NavigationConfigForm extends ConfigForm return $form; } - - /** - * Return the configuration file for the given type of navigation item - * - * @param string $type - * @param string $username - * - * @return Config - */ - protected function getItemConfig($type, $username = null) - { - $itemTypes = $this->getItemTypes(); - if (isset($itemTypes[$type]['config'])) { - $configName = $itemTypes[$type]['config']; - } else { - $configName = $type . 's'; - } - - return Config::app(($username ? "preferences/$username/" : 'navigation/') . $configName); - } } diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php index bfe2ddd60..3c491590a 100644 --- a/library/Icinga/Application/Config.php +++ b/library/Icinga/Application/Config.php @@ -13,7 +13,9 @@ use Icinga\Data\Selectable; use Icinga\Data\SimpleQuery; use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniParser; +use Icinga\Exception\IcingaException; use Icinga\Exception\NotReadableError; +use Icinga\Web\Navigation\Navigation; /** * Container for INI like configuration and global registry of application and module related configuration. @@ -41,6 +43,13 @@ class Config implements Countable, Iterator, Selectable */ protected static $modules = array(); + /** + * Navigation config instances per type + * + * @var array + */ + protected static $navigation = array(); + /** * The internal ConfigObject * @@ -416,6 +425,56 @@ class Config implements Countable, Iterator, Selectable return $moduleConfigs[$configname]; } + /** + * Retrieve a navigation config + * + * @param string $type The type identifier of the navigation item for which to return its config + * @param string $username A user's name or null if the shared config is desired + * @param bool $fromDisk If true, the configuration will be read from disk + * + * @return Config The requested configuration + */ + public static function navigation($type, $username = null, $fromDisk = false) + { + if (! isset(self::$navigation[$type])) { + self::$navigation[$type] = array(); + } + + $branch = $username ?: 'shared'; + $typeConfigs = self::$navigation[$type]; + if (! isset($typeConfigs[$branch]) || $fromDisk) { + $typeConfigs[$branch] = static::fromIni(static::getNavigationConfigPath($type, $username)); + } + + return $typeConfigs[$branch]; + } + + /** + * Return the path to the configuration file for the given navigation item type and user + * + * @param string $type + * @param string $username + * + * @return string + * + * @throws IcingaException In case the given type is unknown + */ + protected static function getNavigationConfigPath($type, $username = null) + { + $itemTypeConfig = Navigation::getItemTypeConfiguration(); + if (! isset($itemTypeConfig[$type])) { + throw new IcingaException('Invalid navigation item type %s provided', $type); + } + + if (isset($itemTypeConfig[$type]['config'])) { + $filename = $itemTypeConfig[$type]['config'] . '.ini'; + } else { + $filename = $type . 's.ini'; + } + + return static::resolvePath(($username ? "preferences/$username/" : 'navigation/') . $filename); + } + /** * Return this config rendered as a INI structured string *