Adjust Icinga\Form\Config\General* to use handleRequest() & Co.

refs #5525
This commit is contained in:
Johannes Meyer 2014-09-01 16:16:56 +02:00
parent 0e63e36ec0
commit deebb64869
5 changed files with 321 additions and 328 deletions

View File

@ -9,7 +9,7 @@ use Icinga\Application\Modules\Module;
use Icinga\Web\Widget;
use Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig;
use Icinga\Form\Config\GeneralForm;
use Icinga\Form\Config\GeneralConfigForm;
use Icinga\Form\Config\AuthenticationBackendReorderForm;
use Icinga\Form\Config\AuthenticationBackendConfigForm;
use Icinga\Form\Config\ResourceForm;
@ -47,23 +47,12 @@ class ConfigController extends BaseConfigController
*/
public function indexAction()
{
$this->view->messageBox = new AlertMessageBox(true);
$this->view->tabs->activate('index');
$form = new GeneralForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
if ($this->writeConfigFile($form->getConfiguration(), 'config')) {
Notification::success($this->translate('New configuration has successfully been stored'));
$this->redirectNow('config');
}
}
} else {
$form->setConfiguration(IcingaConfig::app());
}
$form = new GeneralConfigForm();
$form->setConfig(IcingaConfig::app());
$form->handleRequest();
$this->view->form = $form;
$this->view->tabs->activate('index');
}
/**

View File

@ -0,0 +1,120 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\General;
use DateTimeZone;
use Icinga\Web\Form;
use Icinga\Util\Translator;
use Icinga\Data\ResourceFactory;
/**
* Form class to modify the general application configuration
*/
class ApplicationConfigForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_general_application');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array();
$languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) {
$languages[$language] = $language;
}
$elements[] = $this->createElement(
'select',
'global_language',
array(
'label' => t('Default Language'),
'required' => true,
'multiOptions' => $languages,
'helptext' => t(
'Select the language to use by default. Can be overwritten by a user in his preferences.'
)
)
);
$tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz;
}
$elements[] = $this->createElement(
'select',
'global_timezone',
array(
'label' => t('Default Application Timezone'),
'required' => true,
'multiOptions' => $tzList,
'helptext' => t(
'Select the timezone to be used as the default. User\'s can set their own timezone if'
. ' they like to, but this is the timezone to be used as the default setting .'
),
'value' => date_default_timezone_get()
)
);
$elements[] = $this->createElement(
'text',
'global_modulePath',
array(
'label' => t('Module Path'),
'required' => true,
'helptext' => t(
'Contains the directories that will be searched for available modules, separated by '
. 'colons. Modules that don\'t exist in these directories can still be symlinked in '
. 'the module folder, but won\'t show up in the list of disabled modules.'
),
'value' => realpath(ICINGAWEB_APPDIR . '/../modules')
)
);
$elements[] = $this->createElement(
'select',
'preferences_type',
array(
'required' => true,
'autosubmit' => true,
'label' => t('User Preference Storage Type'),
'multiOptions' => array(
'ini' => t('File System (INI Files)'),
'db' => t('Database'),
'null' => t('Don\'t Store Preferences')
)
)
);
if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') {
$backends = array();
foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
if ($resource['type'] === 'db') {
$backends[$name] = $name;
}
}
$elements[] = $this->createElement(
'select',
'preferences_resource',
array(
'required' => true,
'multiOptions' => $backends,
'label' => t('Database Connection')
)
);
}
return $elements;
}
}

View File

@ -0,0 +1,120 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\General;
use Icinga\Application\Icinga;
use Icinga\Web\Form;
use Icinga\Web\Form\Validator\WritablePathValidator;
class LoggingConfigForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_general_logging');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'select',
'logging_level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'multiOptions' => array(
0 => t('None'),
1 => t('Error'),
2 => t('Warning'),
3 => t('Information'),
4 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'logging_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'multiOptions' => array(
'syslog' => 'Syslog',
'file' => t('File')
)
)
);
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'logging_application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'logging_facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir('../var/log/icingaweb.log'));
}
}

View File

@ -0,0 +1,76 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config;
use Icinga\Web\Request;
use Icinga\Web\Notification;
use Icinga\Form\ConfigForm;
use Icinga\Form\Config\General\LoggingConfigForm;
use Icinga\Form\Config\General\ApplicationConfigForm;
/**
* Form class for application-wide and logging specific settings
*/
class GeneralConfigForm extends ConfigForm
{
/**
* Initialize this configuration form
*/
public function init()
{
$this->setName('form_config_general');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$appConfigForm = new ApplicationConfigForm();
$loggingConfigForm = new LoggingConfigForm();
return array_merge(
$appConfigForm->createElements($formData),
$loggingConfigForm->createElements($formData)
);
}
/**
* @see Form::onSuccess()
*/
public function onSuccess(Request $request)
{
foreach ($this->getValues() as $sectionAndPropertyName => $value) {
list($section, $property) = explode('_', $sectionAndPropertyName);
if (isset($this->config->{$section})) {
$this->config->{$section}->{$property} = $value;
} else {
$this->config->{$section} = array($property => $value);
}
}
if ($this->save()) {
Notification::success(t('New configuration has successfully been stored'));
} else {
return false;
}
}
/**
* @see Form::onShow()
*/
public function onShow(Request $request)
{
$values = array();
foreach ($this->config as $section => $properties) {
foreach ($properties as $name => $value) {
$values[$section . '_' . $name] = $value;
}
}
$this->populate($values);
}
}

View File

@ -1,312 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config;
use DateTimeZone;
use Zend_Config;
use Icinga\Web\Form;
use Icinga\Util\Translator;
use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
use Icinga\Web\Form\Validator\WritablePathValidator;
/**
* Configuration form for general, application-wide settings
*/
class GeneralForm extends Form
{
/**
* Initialize this configuration form
*/
public function init()
{
$this->setName('form_config_general');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array(
$this->getLanguageSelection(),
$this->getTimezoneSelection(),
$this->getModulePathInput()
);
return array_merge(
$elements,
$this->getPreferencesElements($formData),
$this->getLoggingElements($formData)
);
}
/**
* Populate this form with the given configuration
*
* @param Zend_Config $config The configuration to populate this form with
*
* @return self
*/
public function setConfiguration(Zend_Config $config)
{
$defaults = array();
foreach ($config as $section => $properties) {
foreach ($properties as $name => $value) {
$defaults[$section . '_' . $name] = $value;
}
}
$this->populate($defaults);
return $this;
}
/**
* Return the configured configuration values
*
* @return Zend_Config
*/
public function getConfiguration()
{
$config = array();
$values = $this->getValues();
foreach ($values as $sectionAndPropertyName => $value) {
list($section, $property) = explode('_', $sectionAndPropertyName);
$config[$section][$property] = $value;
}
return new Zend_Config($config);
}
/**
* Return a select field for setting the default language
*
* Possible values are determined by Translator::getAvailableLocaleCodes.
*
* @return Zend_Form_Element
*/
protected function getLanguageSelection()
{
$languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) {
$languages[$language] = $language;
}
return $this->createElement(
'select',
'global_language',
array(
'label' => t('Default Language'),
'required' => true,
'multiOptions' => $languages,
'helptext' => t(
'Select the language to use by default. Can be overwritten by a user in his preferences.'
)
)
);
}
/**
* Return a select field for setting the default timezone
*
* Possible values are determined by DateTimeZone::listIdentifiers.
*
* @return Zend_Form_Element
*/
protected function getTimezoneSelection()
{
$tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz;
}
$this->addElement(
'select',
'global_timezone',
array(
'label' => t('Default Application Timezone'),
'required' => true,
'multiOptions' => $tzList,
'helptext' => t(
'Select the timezone to be used as the default. User\'s can set their own timezone if'
. ' they like to, but this is the timezone to be used as the default setting .'
),
'value' => date_default_timezone_get()
)
);
}
/**
* Return a input field for setting the module path
*/
protected function getModulePathInput()
{
$this->addElement(
'text',
'global_modulePath',
array(
'label' => t('Module Path'),
'required' => true,
'helptext' => t(
'Contains the directories that will be searched for available modules, separated by '
. 'colons. Modules that don\'t exist in these directories can still be symlinked in '
. 'the module folder, but won\'t show up in the list of disabled modules.'
),
'value' => realpath(ICINGAWEB_APPDIR . '/../modules')
)
);
}
/**
* Return form elements for setting the user preference storage backend
*
* @param array $formData The data sent by the user
*/
protected function getPreferencesElements(array $formData)
{
$elements = array(
$this->createElement(
'select',
'preferences_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('User Preference Storage Type'),
'multiOptions' => array(
'ini' => t('File System (INI Files)'),
'db' => t('Database'),
'null' => t('Don\'t Store Preferences')
)
)
)
);
if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') {
$backends = array();
foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
if ($resource['type'] === 'db') {
$backends[$name] = $name;
}
}
$elements[] = $this->createElement(
'select',
'preferences_resource',
array(
'required' => true,
'multiOptions' => $backends,
'label' => t('Database Connection')
)
);
}
return $elements;
}
/**
* Return form elements to setup the application's logging
*
* @param array $formData The data sent by the user
*
* @return array
*/
protected function getLoggingElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'select',
'logging_level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'multiOptions' => array(
0 => t('None'),
1 => t('Error'),
2 => t('Warning'),
3 => t('Information'),
4 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'logging_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'multiOptions' => array(
'syslog' => 'Syslog',
'file' => t('File')
)
)
);
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'logging_application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'logging_facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir('../var/log/icingaweb.log'));
}
}