Adjust LoggingForm to suit the "final" form builder implementation

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-18 10:29:23 +02:00
parent 5da14d3fc5
commit 7157b11f97
2 changed files with 89 additions and 80 deletions

View File

@ -89,15 +89,22 @@ class ConfigController extends BaseConfigController
$this->view->tabs->activate('logging'); $this->view->tabs->activate('logging');
$form = new LoggingForm(); $form = new LoggingForm();
if ($form->isSubmittedAndValid($this->_request->getParams())) { $request = $this->getRequest();
$appConfig = IcingaConfig::app(); if ($request->isPost()) {
$appConfig['logging'] = $form->getValues(); if ($form->isValid($request->getPost())) {
if (false === $this->writeConfigFile($appConfig, 'config')) { $appConfig = IcingaConfig::app();
return; $appConfig->logging = new Zend_Config($form->getValues());
if ($this->writeConfigFile($appConfig, 'config')) {
$this->addSuccessMessage($this->translate('Logging configuration has sucessfully been stored'));
$this->redirectNow('config/logging');
}
} }
} else {
Notification::success('New configuration has sucessfully been stored'); $loggingConfig = Icinga::app()->getConfig()->logging;
$this->redirectNow('config/logging'); if ($loggingConfig === null) {
$loggingConfig = new Zend_Config(array());
}
$form->populate($loggingConfig->toArray());
} }
$this->view->form = $form; $this->view->form = $form;

View File

@ -4,7 +4,6 @@
namespace Icinga\Form\Config; namespace Icinga\Form\Config;
use Zend_Config;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Web\Form\Validator\WritablePathValidator; use Icinga\Web\Form\Validator\WritablePathValidator;
@ -16,17 +15,18 @@ class LoggingForm extends Form
{ {
/** /**
* Initialize this logging configuration form * Initialize this logging configuration form
*
* Sets actually only the name.
*/ */
public function init() public function init()
{ {
$this->setName('form_config_logging'); $this->setName('form_config_logging');
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
} }
/** /**
* @see Form::createElements() * @see Form::createElements()
*/ */
public function createElements() public function createElements(array $formData)
{ {
$elements = array(); $elements = array();
@ -37,7 +37,7 @@ class LoggingForm extends Form
'required' => true, 'required' => true,
'label' => t('Logging Enabled'), 'label' => t('Logging Enabled'),
'helptext' => t('Check this to enable logging.'), 'helptext' => t('Check this to enable logging.'),
'value' => 0 'value' => isset($formData['enable']) ? $formData['enable'] : 0
) )
); );
$elements[] = $this->createElement( $elements[] = $this->createElement(
@ -47,7 +47,7 @@ class LoggingForm extends Form
'required' => true, 'required' => true,
'label' => t('Logging Level'), 'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'), 'helptext' => t('The maximum loglevel to emit.'),
'value' => 0, 'value' => isset($formData['level']) ? $formData['level'] : 0,
'multiOptions' => array( 'multiOptions' => array(
0 => t('Error'), 0 => t('Error'),
1 => t('Warning'), 1 => t('Warning'),
@ -64,94 +64,96 @@ class LoggingForm extends Form
'class' => 'autosubmit', 'class' => 'autosubmit',
'label' => t('Logging Type'), 'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'), 'helptext' => t('The type of logging to utilize.'),
'value' => 'file', 'value' => isset($formData['type']) ? $formData['type'] : 'syslog',
'multiOptions' => array( 'multiOptions' => array(
'file' => t('File'), 'file' => t('File'),
'syslog' => 'Syslog' 'syslog' => 'Syslog'
) )
) )
); );
$elements[] = $this->createElement(
'text', if (false === isset($formData['type']) || $formData['type'] === 'syslog') {
'application', $elements[] = $this->createElement(
array( 'text',
'depends' => 'type', 'application',
'requires' => 'syslog', array(
'required' => true, 'required' => true,
'label' => t('Application Prefix'), 'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'), 'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb', 'value' => isset($formData['application']) ? $formData['application'] : 'icingaweb',
'validators' => array( 'validators' => array(
array(
'Regex',
false,
array( array(
'pattern' => '/^[^\W]+$/', 'Regex',
'messages' => array( false,
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.' array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
) )
) )
) )
) )
) );
); $elements[] = $this->createElement(
$elements[] = $this->createElement( 'select',
'select', 'facility',
'facility', array(
array( 'required' => true,
'depends' => 'type', 'label' => t('Facility'),
'requires' => 'syslog', 'helptext' => t('The Syslog facility to utilize.'),
'required' => true, 'value' => isset($formData['facility']) ? $formData['facility'] : 'LOG_USER',
'label' => t('Facility'), 'multiOptions' => array(
'helptext' => t('The Syslog facility to utilize.'), 'LOG_USER' => 'LOG_USER'
'value' => 'LOG_USER', )
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
) )
) );
); } elseif ($formData['type'] === 'file') {
$elements[] = $this->createElement( $elements[] = $this->createElement(
'text', 'text',
'target', 'target',
array( array(
'depends' => 'type', 'required' => true,
'requires' => 'file', 'label' => t('Filepath'),
'required' => true, 'helptext' => t('The logfile to write messages to.'),
'label' => t('Filepath'), 'value' => isset($formData['target']) ? $formData['target'] : $this->getDefaultLogDir(),
'helptext' => t('The logfile to write messages to.'), 'validators' => array(new WritablePathValidator())
'value' => $this->getDefaultLogDir(), )
'validators' => array(new WritablePathValidator()) );
) }
);
return $elements; return $elements;
} }
/** /**
* Return the current logging configuration * @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'label' => t('Save')
)
);
return $this;
}
/**
* Retrieve all form element values
*
* Returns all configuration relevant element values.
* *
* @return array * @return array
*/ */
public function getConfiguration() public function getValues()
{ {
$loggingConfig = Icinga::app()->getConfig()->logging; $values = parent::getValues();
if ($loggingConfig === null) { unset($values['btn_submit']);
$loggingConfig = new Zend_Config(array()); unset($values[$this->getTokenElementName()]);
} return $values;
$config = array();
$config['enable'] = $loggingConfig->enable ? 1 : 0;
$config['level'] = $loggingConfig->level;
$config['type'] = $loggingConfig->type;
if ($loggingConfig->type === 'file') {
$config['target'] = $loggingConfig->target;
} elseif ($loggingConfig->type === 'syslog') {
$config['application'] = $loggingConfig->application;
$config['facility'] = $loggingConfig->facility;
}
return $config;
} }
/** /**