Adjust Icinga\Form\Config\LoggingForm to suit the new form builder

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-10 11:16:31 +02:00
parent 630b36e706
commit 6690410c42
2 changed files with 102 additions and 138 deletions

View File

@ -113,16 +113,17 @@ class ConfigController extends BaseConfigController
$this->view->tabs->activate('logging'); $this->view->tabs->activate('logging');
$form = new LoggingForm(); $form = new LoggingForm();
$form->setConfiguration(IcingaConfig::app()); if ($form->isSubmittedAndValid($this->_request->getParams())) {
$form->setRequest($this->_request); $appConfig = IcingaConfig::app();
if ($form->isSubmittedAndValid()) { $appConfig['logging'] = $form->getValues();
if (!$this->writeConfigFile($form->getConfig(), 'config')) { if (false === $this->writeConfigFile($appConfig, 'config')) {
return; return;
} }
Notification::success('New configuration has sucessfully been stored'); Notification::success('New configuration has sucessfully been stored');
$form->setConfiguration(IcingaConfig::app(), true);
$this->redirectNow('config/logging'); $this->redirectNow('config/logging');
} }
$this->view->form = $form; $this->view->form = $form;
} }

View File

@ -1,35 +1,10 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config; namespace Icinga\Form\Config;
use \Zend_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;
@ -40,47 +15,39 @@ use Icinga\Web\Form\Validator\WritablePathValidator;
class LoggingForm extends Form class LoggingForm extends Form
{ {
/** /**
* Return the default logging directory for type "file" * Initialize this logging configuration form
*
* @return string
*/ */
protected function getDefaultLogDir() public function init()
{ {
return realpath(Icinga::app()->getApplicationDir() . '/../var/log/icingaweb.log'); $this->setName('form_config_logging');
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
} }
/** /**
* Create this logging configuration form * @see Form::createElements()
*
* @see Form::create()
*/ */
public function create() public function createElements()
{ {
$this->setName('form_config_logging'); $elements = array();
$config = $this->getConfiguration(); $elements[] = $this->createElement(
if (($loggingConfig = $config->logging) === null) {
$loggingConfig = new Zend_Config(array());
}
$this->addElement(
'checkbox', 'checkbox',
'logging_enable', 'enable',
array( array(
'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' => $loggingConfig->enable ? 1 : 0 'value' => 0
) )
); );
$this->addElement( $elements[] = $this->createElement(
'select', 'select',
'logging_level', 'level',
array( array(
'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' => intval($loggingConfig->get('level', 0)), 'value' => 0,
'multiOptions' => array( 'multiOptions' => array(
0 => t('Error'), 0 => t('Error'),
1 => t('Warning'), 1 => t('Warning'),
@ -89,33 +56,31 @@ class LoggingForm extends Form
) )
) )
); );
$this->addElement( $elements[] = $this->createElement(
'select', 'select',
'logging_type', 'type',
array( array(
'required' => true, 'required' => true,
'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' => $loggingConfig->get('type', 'file'), 'value' => 'file',
'multiOptions' => array( 'multiOptions' => array(
'file' => t('File'), 'file' => t('File'),
'syslog' => 'Syslog' 'syslog' => 'Syslog'
) )
) )
); );
$this->enableAutoSubmit(array('logging_type')); $elements[] = $this->createElement(
switch ($this->getRequest()->getParam('logging_type', $loggingConfig->get('type', 'file')))
{
case 'syslog':
$this->addElement(
'text', 'text',
'logging_application', 'application',
array( array(
'depends' => 'type',
'requires' => 'syslog',
'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' => $loggingConfig->get('application', 'icingaweb'), 'value' => 'icingaweb',
'validators' => array( 'validators' => array(
array( array(
'Regex', 'Regex',
@ -130,74 +95,72 @@ class LoggingForm extends Form
) )
) )
); );
$this->addElement( $elements[] = $this->createElement(
'select', 'select',
'logging_facility', 'facility',
array( array(
'depends' => 'type',
'requires' => 'syslog',
'required' => true, 'required' => true,
'label' => t('Facility'), 'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'), 'helptext' => t('The Syslog facility to utilize.'),
'value' => $loggingConfig->get('facility', 'LOG_USER'), 'value' => 'LOG_USER',
'multiOptions' => array( 'multiOptions' => array(
'LOG_USER' => 'LOG_USER' 'LOG_USER' => 'LOG_USER'
) )
) )
); );
break; $elements[] = $this->createElement(
case 'file':
default:
$this->addElement(
'text', 'text',
'logging_target', 'target',
array( array(
'depends' => 'type',
'requires' => 'file',
'required' => true, 'required' => true,
'label' => t('Filepath'), 'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'), 'helptext' => t('The logfile to write messages to.'),
'value' => $loggingConfig->target ? $loggingConfig->target : $this->getDefaultLogDir(), 'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator()) 'validators' => array(new WritablePathValidator())
) )
); );
}
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes'); return $elements;
}
public function isValid($data) {
foreach ($this->getElements() as $key => $element) {
// Initialize all empty elements with their default values.
if (!isset($data[$key])) {
$data[$key] = $element->getValue();
}
}
return parent::isValid($data);
} }
/** /**
* Return a Zend_Config object containing the state defined in this form * Return the current logging configuration
* *
* @return Zend_Config The config defined in this form * @return array
*/ */
public function getConfig() public function getConfiguration()
{ {
$values = $this->getValues(); $loggingConfig = Icinga::app()->getConfig()->logging;
$cfg = $this->getConfiguration()->toArray(); if ($loggingConfig === null) {
$loggingConfig = new Zend_Config(array());
}
$cfg['logging']['enable'] = $values['logging_enable'] == 1; $config = array();
$cfg['logging']['level'] = $values['logging_level']; $config['enable'] = $loggingConfig->enable ? 1 : 0;
$config['level'] = $loggingConfig->level;
$config['type'] = $loggingConfig->type;
switch ($values['logging_type']) if ($loggingConfig->type === 'file') {
$config['target'] = $loggingConfig->target;
} elseif ($loggingConfig->type === 'syslog') {
$config['application'] = $loggingConfig->application;
$config['facility'] = $loggingConfig->facility;
}
return $config;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{ {
case 'file': return realpath(Icinga::app()->getApplicationDir() . '/../var/log/icingaweb.log');
$cfg['logging']['type'] = 'file';
$cfg['logging']['target'] = $values['logging_target'];
break;
case 'syslog':
$cfg['logging']['type'] = 'syslog';
$cfg['logging']['application'] = $values['logging_application'];
$cfg['logging']['facility'] = $values['logging_facility'];
break;
}
return new Zend_Config($cfg);
} }
} }