NavigationConfigForm: Require argument $type in method getUserConfig()

refs #10246
This commit is contained in:
Johannes Meyer 2015-09-29 17:06:22 +02:00
parent 2be8835f06
commit f60a8ef60d

View File

@ -123,12 +123,18 @@ class NavigationConfigForm extends ConfigForm
/** /**
* Return the user's navigation configuration * Return the user's navigation configuration
* *
* @param string $type
*
* @return Config * @return Config
*/ */
public function getUserConfig() public function getUserConfig($type = null)
{ {
if ($this->userConfig === null) { if ($this->userConfig === null) {
$this->setUserConfig($this->getUser()->loadNavigationConfig()); if ($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()));
} }
return $this->userConfig; return $this->userConfig;
@ -205,10 +211,9 @@ class NavigationConfigForm extends ConfigForm
} }
} }
foreach ($this->getUserConfig() as $sectionName => $sectionConfig) { foreach ($this->getUserConfig($type) as $sectionName => $sectionConfig) {
if ( if (
$sectionName !== $this->itemToLoad $sectionName !== $this->itemToLoad
&& $sectionConfig->type === $type
&& !in_array($sectionName, $children, true) && !in_array($sectionName, $children, true)
) { ) {
$names[] = $sectionName; $names[] = $sectionName;
@ -271,17 +276,19 @@ class NavigationConfigForm extends ConfigForm
* *
* @return $this * @return $this
* *
* @throws InvalidArgumentException In case $data does not contain a navigation item name * @throws InvalidArgumentException In case $data does not contain a navigation item name or type
* @throws IcingaException In case a navigation item with the same name already exists * @throws IcingaException In case a navigation item with the same name already exists
*/ */
public function add(array $data) public function add(array $data)
{ {
if (! isset($data['name'])) { if (! isset($data['name'])) {
throw new InvalidArgumentException('Key \'name\' missing'); throw new InvalidArgumentException('Key \'name\' missing');
} elseif (! isset($data['type'])) {
throw new InvalidArgumentException('Key \'type\' missing');
} }
$shared = false; $shared = false;
$config = $this->getUserConfig(); $config = $this->getUserConfig($data['type']);
if ((isset($data['users']) && $data['users']) || (isset($data['groups']) && $data['groups'])) { if ((isset($data['users']) && $data['users']) || (isset($data['groups']) && $data['groups'])) {
if ($this->getUser()->can('application/share/navigation')) { if ($this->getUser()->can('application/share/navigation')) {
$data['owner'] = $this->getUser()->getUsername(); $data['owner'] = $this->getUser()->getUsername();
@ -301,7 +308,7 @@ class NavigationConfigForm extends ConfigForm
$exists = $config->hasSection($itemName); $exists = $config->hasSection($itemName);
if (! $exists) { if (! $exists) {
if ($shared) { if ($shared) {
$exists = $this->getUserConfig()->hasSection($itemName); $exists = $this->getUserConfig($data['type'])->hasSection($itemName);
} else { } else {
$exists = (bool) $this->getShareConfig() $exists = (bool) $this->getShareConfig()
->select() ->select()
@ -821,4 +828,24 @@ 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);
}
} }