2014-08-29 12:25:25 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms;
|
2014-08-29 12:25:25 +02:00
|
|
|
|
|
|
|
use Exception;
|
2014-11-04 14:48:24 +01:00
|
|
|
use Zend_Form_Decorator_Abstract;
|
2014-08-29 12:25:25 +02:00
|
|
|
use Icinga\Web\Form;
|
|
|
|
use Icinga\Application\Config;
|
2014-10-31 10:54:53 +01:00
|
|
|
use Icinga\File\Ini\IniWriter;
|
2014-08-29 12:25:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-09-08 13:31:25 +02:00
|
|
|
public function setIniConfig(Config $config)
|
2014-08-29 12:25:25 +02:00
|
|
|
{
|
|
|
|
$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()
|
|
|
|
{
|
2014-10-31 10:54:53 +01:00
|
|
|
$writer = new IniWriter(
|
2014-08-29 12:25:25 +02:00
|
|
|
array(
|
|
|
|
'config' => $this->config,
|
|
|
|
'filename' => $this->config->getConfigFile()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$writer->write();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->addDecorator('ViewScript', array(
|
2014-09-09 13:22:51 +02:00
|
|
|
'viewModule' => 'default',
|
2014-08-29 12:25:25 +02:00
|
|
|
'viewScript' => 'showConfiguration.phtml',
|
|
|
|
'errorMessage' => $e->getMessage(),
|
|
|
|
'configString' => $writer->render(),
|
2014-11-04 14:48:24 +01:00
|
|
|
'filePath' => $this->config->getConfigFile(),
|
|
|
|
'placement' => Zend_Form_Decorator_Abstract::PREPEND
|
2014-08-29 12:25:25 +02:00
|
|
|
));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|