Config: Add static method navigation()

refs #10246
This commit is contained in:
Johannes Meyer 2015-09-30 11:30:38 +02:00
parent 95d1ce371c
commit befbc6cd6a
3 changed files with 71 additions and 66 deletions

View File

@ -63,35 +63,6 @@ class NavigationController extends Controller
return $types; 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 * Show the current user a list of his/her navigation items
*/ */
@ -185,10 +156,10 @@ class NavigationController extends Controller
{ {
$form = new NavigationConfigForm(); $form = new NavigationConfigForm();
$form->setRedirectUrl('navigation'); $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->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) { $form->setOnSuccess(function (NavigationConfigForm $form) {
$data = array_filter($form->getValues()); $data = array_filter($form->getValues());
@ -234,8 +205,8 @@ class NavigationController extends Controller
$form = new NavigationConfigForm(); $form = new NavigationConfigForm();
$form->setUser($user); $form->setUser($user);
$form->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); $form->setShareConfig(Config::navigation($itemType));
$form->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $itemOwner))); $form->setUserConfig(Config::navigation($itemType, $itemOwner));
$form->setRedirectUrl($referrer === 'shared' ? 'navigation/shared' : 'navigation'); $form->setRedirectUrl($referrer === 'shared' ? 'navigation/shared' : 'navigation');
$form->setTitle(sprintf($this->translate('Edit %s %s'), $this->getItemLabel($itemType), $itemName)); $form->setTitle(sprintf($this->translate('Edit %s %s'), $this->getItemLabel($itemType), $itemName));
$form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) { $form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) {
@ -289,8 +260,8 @@ class NavigationController extends Controller
$navigationConfigForm = new NavigationConfigForm(); $navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setUser($user); $navigationConfigForm->setUser($user);
$navigationConfigForm->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); $navigationConfigForm->setShareConfig(Config::navigation($itemType));
$navigationConfigForm->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $user->getUsername()))); $navigationConfigForm->setUserConfig(Config::navigation($itemType, $user->getUsername()));
$form = new ConfirmRemovalForm(); $form = new ConfirmRemovalForm();
$form->setRedirectUrl('navigation'); $form->setRedirectUrl('navigation');
@ -336,8 +307,8 @@ class NavigationController extends Controller
$navigationConfigForm = new NavigationConfigForm(); $navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setUser($this->Auth()->getUser()); $navigationConfigForm->setUser($this->Auth()->getUser());
$navigationConfigForm->setShareConfig(Config::fromIni($this->getConfigPath($itemType))); $navigationConfigForm->setShareConfig(Config::navigation($itemType));
$navigationConfigForm->setUserConfig(Config::fromIni($this->getConfigPath($itemType, $itemOwner))); $navigationConfigForm->setUserConfig(Config::navigation($itemType, $itemOwner));
$form = new Form(array( $form = new Form(array(
'onSuccess' => function ($form) use ($navigationConfigForm) { 'onSuccess' => function ($form) use ($navigationConfigForm) {

View File

@ -134,7 +134,7 @@ class NavigationConfigForm extends ConfigForm
throw new ProgrammingError('You need to pass a type if no user configuration is set'); 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; 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'); 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; return $this->shareConfig;
@ -639,11 +639,6 @@ class NavigationConfigForm extends ConfigForm
) )
); );
} else { } else {
$multiOptions = array();
foreach ($itemTypes as $type => $options) {
$multiOptions[$type] = isset($options['label']) ? $options['label'] : $type;
}
$this->addElement( $this->addElement(
'select', 'select',
'type', 'type',
@ -652,7 +647,7 @@ class NavigationConfigForm extends ConfigForm
'autosubmit' => true, 'autosubmit' => true,
'label' => $this->translate('Type'), 'label' => $this->translate('Type'),
'description' => $this->translate('The type of this navigation item'), 'description' => $this->translate('The type of this navigation item'),
'multiOptions' => $multiOptions 'multiOptions' => $itemTypes
) )
); );
} }
@ -861,24 +856,4 @@ class NavigationConfigForm extends ConfigForm
return $form; 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);
}
} }

View File

@ -13,7 +13,9 @@ use Icinga\Data\Selectable;
use Icinga\Data\SimpleQuery; use Icinga\Data\SimpleQuery;
use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniWriter;
use Icinga\File\Ini\IniParser; use Icinga\File\Ini\IniParser;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Web\Navigation\Navigation;
/** /**
* Container for INI like configuration and global registry of application and module related configuration. * 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(); protected static $modules = array();
/**
* Navigation config instances per type
*
* @var array
*/
protected static $navigation = array();
/** /**
* The internal ConfigObject * The internal ConfigObject
* *
@ -416,6 +425,56 @@ class Config implements Countable, Iterator, Selectable
return $moduleConfigs[$configname]; 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 * Return this config rendered as a INI structured string
* *