Add base form class for configuration forms

refs #5525
This commit is contained in:
Johannes Meyer 2014-08-29 12:25:25 +02:00
parent e020dd3541
commit 364c7c0858
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form;
use Exception;
use Icinga\Web\Form;
use Icinga\Application\Config;
use Icinga\Config\PreservingIniWriter;
/**
* Form base-class providing standard functionality for configuration forms
*/
class ConfigForm extends Form
{
/**
* The configuration to work with
*
* @var Config
*/
protected $config;
/**
* Set the configuration to use when populating the form or when saving the user's input
*
* @param Config $config The configuration to use
*
* @return self
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
/**
* Persist the current configuration to disk
*
* If an error occurs the user is shown a view describing the issue and displaying the raw INI configuration.
*
* @return bool Whether the configuration could be persisted
*/
public function save()
{
$writer = new PreservingIniWriter(
array(
'config' => $this->config,
'filename' => $this->config->getConfigFile()
)
);
try {
$writer->write();
} catch (Exception $e) {
$this->addDecorator('ViewScript', array(
'viewScript' => 'showConfiguration.phtml',
'errorMessage' => $e->getMessage(),
'configString' => $writer->render(),
'filePath' => $this->config->getConfigFile()
));
return false;
}
return true;
}
}

View File

@ -0,0 +1,28 @@
<div>
<h4><?= $this->translate('Saving Configuration Failed'); ?></h4>
<br>
<p>
<?= sprintf(
$this->translate('The file %s couldn\'t be stored. (Error: "%s")'),
$this->escape($filePath),
$this->escape($errorMessage)
); ?>
<br>
<?= $this->translate('This could have one or more of the following reasons:'); ?>
</p>
<ul>
<li><?= $this->translate('You don\'t have file-system permissions to write to the file'); ?></li>
<li><?= $this->translate('Something went wrong while writing the file'); ?></li>
<li><?= $this->translate('There\'s an application error preventing you from persisting the configuration'); ?></li>
</ul>
</div>
<p>
<?= $this->translate('Details can be found in the application log. (If you don\'t have access to this log, call your administrator in this case)'); ?>
<br>
<?= $this->translate('In case you can access the file by yourself, you can open it and insert the config manually:'); ?>
</p>
<p>
<pre>
<code><?= $this->escape($configString); ?></code>
</pre>
</p>