Adjust general config form to suit the new form builder implementation

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-21 14:33:52 +02:00
parent a9f0b95e51
commit a73e2ee654
2 changed files with 147 additions and 175 deletions

View File

@ -66,17 +66,25 @@ class ConfigController extends BaseConfigController
{ {
$this->view->messageBox = new AlertMessageBox(true); $this->view->messageBox = new AlertMessageBox(true);
$this->view->tabs->activate('index'); $this->view->tabs->activate('index');
$form = new GeneralForm(); $form = new GeneralForm();
$form->setConfiguration(IcingaConfig::app()); $request = $this->getRequest();
$form->setRequest($this->_request); $currentConfig = IcingaConfig::app();
if ($form->isSubmittedAndValid()) { if ($request->isPost()) {
if (!$this->writeConfigFile($form->getConfig(), 'config')) { if ($form->isValid($request->getPost())) {
return; $formConfig = $form->getConfiguration();
$newConfig = new Zend_Config($currentConfig->toArray(), true);
$newConfig->global = $formConfig->global;
$newConfig->preferences = $formConfig->preferences;
if ($this->writeConfigFile($newConfig, 'config')) {
Notification::success($this->translate('New configuration has successfully been stored'));
$this->redirectNow('config');
}
} }
Notification::success('New configuration has successfully been stored'); } else {
$form->setConfiguration(IcingaConfig::app(), true); $form->setConfiguration($currentConfig);
$this->redirectNow('config/index');
} }
$this->view->form = $form; $this->view->form = $form;
} }

View File

@ -4,15 +4,11 @@
namespace Icinga\Form\Config; namespace Icinga\Form\Config;
use Icinga\Application\Config as IcingaConfig; use DateTimeZone;
use Icinga\Data\ResourceFactory; use Zend_Config;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Util\Translator; use Icinga\Util\Translator;
use Icinga\Web\Form\Validator\WritablePathValidator; use Icinga\Data\ResourceFactory;
use Icinga\Web\Form\Decorator\ConditionalHidden;
use DateTimeZone;
use Zend_Form_Element_Select;
use Zend_Config;
/** /**
* Configuration form for general, application-wide settings * Configuration form for general, application-wide settings
@ -20,74 +16,94 @@ use Zend_Config;
class GeneralForm extends Form class GeneralForm extends Form
{ {
/** /**
* The base directory of the icingaweb configuration * Initialize this configuration form
*
* @var string
*/ */
private $configDir = null; public function init()
/**
* The resources to use instead of the factory provided ones (use for testing)
*
* @var null
*/
private $resources;
/**
* Set a specific configuration directory to use for configuration specific default paths
*
* @param string $dir
*/
public function setConfigDir($dir)
{ {
$this->configDir = $dir; $this->setName('form_config_general');
} }
/** /**
* Return the config path set for this form or the application wide config path if none is set * @see Form::createElements()
*
* @return string
*
* @see IcingaConfig::configDir
*/ */
public function getConfigDir() public function createElements(array $formData)
{ {
return $this->configDir === null ? IcingaConfig::$configDir : $this->configDir; $elements = array(
$this->getLanguageSelection($formData),
$this->getTimezoneSelection($formData),
$this->getModulePathInput($formData)
);
return array_merge($elements, $this->getPreferencesElements($formData));
} }
/** /**
* Set an alternative array of resources that should be used instead of the DBFactory resource set * @see Form::addSubmitButton()
* (used for testing)
*
* @param array $resources The resources to use for populating the db selection field
*/ */
public function setResources(array $resources) public function addSubmitButton()
{ {
$this->resources = $resources; $this->addElement(
'submit',
'btn_submit',
array(
'label' => t('Save Changes')
)
);
return $this;
} }
/** /**
* Return content of the resources.ini or previously set resources for displaying in the database selection field * Populate this form with the given configuration
* *
* @return array * @param Zend_Config $config The configuration to populate this form with
*
* @return self
*/ */
public function getResources() public function setConfiguration(Zend_Config $config)
{ {
if ($this->resources === null) { $defaults = $config->get('global', new Zend_Config(array()))->toArray();
return ResourceFactory::getResourceConfigs()->toArray(); if ($config->get('preferences', new Zend_Config(array()))->type !== null) {
} else { $defaults['preferences_type'] = $config->get('preferences')->type;
return $this->resources; $defaults['preferences_resource'] = $config->get('preferences')->resource;
} }
$this->setDefaults($defaults);
return $this;
} }
/** /**
* Add a select field for setting the default language * Return the configured configuration values
*
* @return Zend_Config
*/
public function getConfiguration()
{
$values = $this->getValues();
$globalData = array(
'language' => $values['language'],
'timezone' => $values['timezone'],
'modulePath' => $values['modulePath']
);
$preferencesData = array('type' => $values['preferences_type']);
if ($values['preferences_type'] === 'db') {
$preferencesData['resource'] = $values['preferences_resource'];
}
return new Zend_Config(array('global' => $globalData, 'preferences' => $preferencesData));
}
/**
* Return a select field for setting the default language
* *
* Possible values are determined by Translator::getAvailableLocaleCodes. * Possible values are determined by Translator::getAvailableLocaleCodes.
* *
* @param Zend_Config $cfg The "global" section of the config.ini * @param array $formData The data to populate the elements with
*
* @return Zend_Form_Element
*/ */
private function addLanguageSelection(Zend_Config $cfg) protected function getLanguageSelection(array $formData)
{ {
$languages = array(); $languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) { foreach (Translator::getAvailableLocaleCodes() as $language) {
@ -95,7 +111,7 @@ class GeneralForm extends Form
} }
$languages[Translator::DEFAULT_LOCALE] = Translator::DEFAULT_LOCALE; $languages[Translator::DEFAULT_LOCALE] = Translator::DEFAULT_LOCALE;
$this->addElement( return $this->createElement(
'select', 'select',
'language', 'language',
array( array(
@ -105,167 +121,115 @@ class GeneralForm extends Form
'helptext' => t( 'helptext' => t(
'Select the language to use by default. Can be overwritten by a user in his preferences.' 'Select the language to use by default. Can be overwritten by a user in his preferences.'
), ),
'value' => $cfg->get('language', Translator::DEFAULT_LOCALE) 'value' => isset($formData['language']) ? $formData['language'] : Translator::DEFAULT_LOCALE
) )
); );
} }
/** /**
* Add a select field for setting the default timezone. * Return a select field for setting the default timezone
* *
* Possible values are determined by DateTimeZone::listIdentifiers * Possible values are determined by DateTimeZone::listIdentifiers.
* *
* @param Zend_Config $cfg The "global" section of the config.ini * @param array $formData The data to populate the elements with
*
* @return Zend_Form_Element
*/ */
private function addTimezoneSelection(Zend_Config $cfg) protected function getTimezoneSelection(array $formData)
{ {
$tzList = array(); $tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) { foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz; $tzList[$tz] = $tz;
} }
$helptext = '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 .';
$this->addElement( $this->addElement(
'select', 'select',
'timezone', 'timezone',
array( array(
'label' => 'Default Application Timezone', 'label' => t('Default Application Timezone'),
'required' => true, 'required' => true,
'multiOptions' => $tzList, 'multiOptions' => $tzList,
'helptext' => $helptext, 'helptext' => t(
'value' => $cfg->get('timezone', date_default_timezone_get()) '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' => isset($formData['timezone']) ? $formData['timezone'] : date_default_timezone_get()
) )
); );
} }
/** /**
* Add configuration settings for module paths * Return a input field for setting the module path
* *
* @param Zend_Config $cfg The "global" section of the config.ini * @param array $formData The data to populate the elements with
*/ */
private function addModuleSettings(Zend_Config $cfg) protected function getModulePathInput(array $formData)
{ {
$this->addElement( $this->addElement(
'text', 'text',
'module_path', 'modulePath',
array( array(
'label' => 'Module Path', 'label' => t('Module Path'),
'required' => true, 'required' => true,
'helptext' => 'Contains the directories that will be searched for available modules, separated by ' . 'helptext' => t(
' colons. Modules that don\'t exist in these directories can still be symlinked in the module ' . 'Contains the directories that will be searched for available modules, separated by '
' folder, but won\'t show up in the list of disabled modules.', . 'colons. Modules that don\'t exist in these directories can still be symlinked in '
'value' => $cfg->get('modulePath', realpath(ICINGAWEB_APPDIR . '/../modules')) . 'the module folder, but won\'t show up in the list of disabled modules.'
),
'value' => isset($formData['modulePath'])
? $formData['modulePath']
: realpath(ICINGAWEB_APPDIR . '/../modules')
) )
); );
} }
/** /**
* Add form elements for setting the user preference storage backend * Return form elements for setting the user preference storage backend
* *
* @param Zend_Config $cfg The Zend_config object of preference section * @param array $formData The data to populate the elements with
*/ */
public function addUserPreferencesDialog(Zend_Config $cfg) protected function getPreferencesElements(array $formData)
{ {
$backend = $cfg->get('type', 'ini'); $elements = array(
if ($this->getRequest()->get('preferences_type', null) !== null) { $this->createElement(
$backend = $this->getRequest()->get('preferences_type'); 'select',
} 'preferences_type',
$this->addElement( array(
'select', 'required' => true,
'preferences_type', 'class' => 'autosubmit',
array( 'label' => t('User Preference Storage Type'),
'label' => 'User Preference Storage Type', 'value' => isset($formData['preferences_type']) ? $formData['preferences_type'] : 'ini',
'required' => true, 'multiOptions' => array(
'value' => $backend, 'ini' => t('File System (INI Files)'),
'multiOptions' => array( 'db' => t('Database'),
'ini' => 'File System (INI Files)', 'null' => t('Don\'t Store Preferences')
'db' => 'Database', )
'null' => 'Don\'t Store Preferences'
) )
) )
); );
$backends = array(); if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') {
foreach ($this->getResources() as $name => $resource) { $backends = array();
if ($resource['type'] !== 'db') { foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
continue; if ($resource['type'] === 'db') {
$backends[$name] = $name;
}
} }
$backends[$name] = $name;
$elements[] = $this->createElement(
'select',
'preferences_resource',
array(
'required' => true,
'multiOptions' => $backends,
'label' => t('Database Connection'),
'value' => isset($formData['preferences_resource'])
? $formData['preferences_resource']
: null
)
);
} }
$txtPreferencesDbResource = new Zend_Form_Element_Select( return $elements;
array(
'name' => 'preferences_db_resource',
'label' => 'Database Connection',
'required' => $backend === 'db',
'condition' => $backend === 'db',
'value' => $cfg->get('resource'),
'multiOptions' => $backends
)
);
$validator = new WritablePathValidator();
$validator->setRequireExistence();
$this->addElement($txtPreferencesDbResource);
$txtPreferencesDbResource->addDecorator(new ConditionalHidden());
$this->enableAutoSubmit(
array(
'preferences_type'
)
);
}
/**
* Create the general form, using the provided configuration
*
* @see Form::create()
*/
public function create()
{
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$global = new Zend_Config(array());
}
$preferences = $config->preferences;
if ($preferences === null) {
$preferences = new Zend_Config(array());
}
$this->setName('form_config_general');
$this->addLanguageSelection($global);
$this->addTimezoneSelection($global);
$this->addModuleSettings($global);
$this->addUserPreferencesDialog($preferences);
$this->setSubmitLabel('Save Changes');
}
/**
* Return an Zend_Config object containing the configuration set in this form
*
* @return Zend_Config
*/
public function getConfig()
{
$config = $this->getConfiguration();
if ($config->global === null) {
$config->global = new Zend_Config(array(), true);
}
if ($config->preferences === null) {
$config->preferences = new Zend_Config(array(), true);
}
$values = $this->getValues();
$cfg = clone $config;
$cfg->global->language = $values['language'];
$cfg->global->timezone = $values['timezone'];
$cfg->global->modulePath = $values['module_path'];
$cfg->preferences->type = $values['preferences_type'];
if ($cfg->preferences->type === 'db') {
$cfg->preferences->resource = $values['preferences_db_resource'];
}
return $cfg;
} }
} }