NavigationConfigForm: Write both configurations in a single "operation"

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-16 10:58:57 +02:00
parent 50cdd0ad15
commit ac3ef393b5
3 changed files with 49 additions and 12 deletions

View File

@ -211,23 +211,27 @@ class NavigationController extends Controller
$form = new Form(array(
'onSuccess' => function ($form) use ($navigationConfigForm) {
try {
if ($navigationConfigForm->unshare($form->getValue('name'))) {
Notification::success(sprintf(
t('Navigation item "%s" has been unshared'),
$form->getValue('name')
));
} else {
Notification::error(sprintf(
t('Failed to unshare navigation item "%s"'),
$form->getValue('name')
));
}
$navigationConfigForm->unshare($form->getValue('name'));
} catch (NotFoundError $e) {
throw $e;
} catch (Exception $e) {
Notification::error($e->getMessage());
}
if ($navigationConfigForm->save()) {
Notification::success(sprintf(
t('Navigation item "%s" has been unshared'),
$form->getValue('name')
));
} else {
// TODO: It failed obviously to write one of the configs, so we're leaving the user in
// a inconsistent state. Luckily, it's nothing lost but possibly duplicated...
Notification::error(sprintf(
t('Failed to unshare navigation item "%s"'),
$form->getValue('name')
));
}
$redirect = $form->getValue('redirect');
if (! empty($redirect)) {
$form->setRedirectUrl(htmlspecialchars_decode($redirect));

View File

@ -43,7 +43,7 @@ class ConfigForm extends Form
public function save()
{
try {
$this->config->saveIni();
$this->writeConfig($this->config);
} catch (Exception $e) {
$this->addDecorator('ViewScript', array(
'viewModule' => 'default',
@ -58,4 +58,14 @@ class ConfigForm extends Form
return true;
}
/**
* Write the configuration to disk
*
* @param Config $config
*/
protected function writeConfig(Config $config)
{
$config->saveIni();
}
}

View File

@ -27,6 +27,16 @@ class NavigationConfigForm extends ConfigForm
'dashlet'
);
/**
* The secondary configuration to write
*
* This is always the reduced configuration and is only written to
* disk once the main configuration has been successfully written.
*
* @var Config
*/
protected $secondaryConfig;
/**
* The navigation item to load when displaying the form for the first time
*
@ -319,6 +329,19 @@ class NavigationConfigForm extends ConfigForm
return $values;
}
/**
* {@inheritdoc}
*/
protected function writeConfig(Config $config)
{
parent::writeConfig($config);
if ($this->secondaryConfig !== null) {
$this->config = $this->secondaryConfig; // Causes the config being displayed to the user in case of an error
parent::writeConfig($this->secondaryConfig);
}
}
/**
* Return a list of available item types
*