Skip to content

Commit

Permalink
Config: Add static method navigation()
Browse files Browse the repository at this point in the history
refs #10246
  • Loading branch information
Johannes Meyer committed Sep 30, 2015
1 parent 95d1ce3 commit befbc6c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
45 changes: 8 additions & 37 deletions application/controllers/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,6 @@ protected function listItemTypes()
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
*/
Expand Down Expand Up @@ -185,10 +156,10 @@ public function addAction()
{
$form = new NavigationConfigForm();
$form->setRedirectUrl('navigation');
$form->setUser($this->Auth()->getUser());
$form->setItemTypes($this->listItemTypes());
$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->setOnSuccess(function (NavigationConfigForm $form) {
$data = array_filter($form->getValues());

Expand Down Expand Up @@ -234,8 +205,8 @@ public function editAction()

$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) {
Expand Down Expand Up @@ -289,8 +260,8 @@ public function removeAction()

$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');
Expand Down Expand Up @@ -336,8 +307,8 @@ public function unshareAction()

$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) {
Expand Down
31 changes: 3 additions & 28 deletions application/forms/Navigation/NavigationConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getUserConfig($type = null)
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;
Expand Down Expand Up @@ -168,7 +168,7 @@ public function getShareConfig($type = null)
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;
Expand Down Expand Up @@ -639,11 +639,6 @@ public function createElements(array $formData)
)
);
} else {
$multiOptions = array();
foreach ($itemTypes as $type => $options) {
$multiOptions[$type] = isset($options['label']) ? $options['label'] : $type;
}

$this->addElement(
'select',
'type',
Expand All @@ -652,7 +647,7 @@ public function createElements(array $formData)
'autosubmit' => true,
'label' => $this->translate('Type'),
'description' => $this->translate('The type of this navigation item'),
'multiOptions' => $multiOptions
'multiOptions' => $itemTypes
)
);
}
Expand Down Expand Up @@ -861,24 +856,4 @@ protected function getItemForm($type)

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);
}
}
59 changes: 59 additions & 0 deletions library/Icinga/Application/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
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.
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -416,6 +425,56 @@ public static function module($modulename, $configname = 'config', $fromDisk = f
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
*
Expand Down

0 comments on commit befbc6c

Please sign in to comment.