From a9f0b95e51814b8901b28c10de16643f869194fb Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 21 Jul 2014 10:20:04 +0200 Subject: [PATCH] Adjust preferences form to suit the new form builder implementation refs #5525 --- .../controllers/PreferenceController.php | 35 +-- application/forms/Preference/GeneralForm.php | 209 +++++++++++------- 2 files changed, 145 insertions(+), 99 deletions(-) diff --git a/application/controllers/PreferenceController.php b/application/controllers/PreferenceController.php index cc2ea4e95..0eb45dca0 100644 --- a/application/controllers/PreferenceController.php +++ b/application/controllers/PreferenceController.php @@ -4,7 +4,6 @@ use Icinga\Web\Controller\BasePreferenceController; use Icinga\Web\Widget\Tab; -use Icinga\Application\Config as IcingaConfig; use Icinga\Web\Url; use Icinga\Form\Preference\GeneralForm; use Icinga\Web\Notification; @@ -38,23 +37,29 @@ class PreferenceController extends BasePreferenceController */ public function indexAction() { - $form = new GeneralForm(); $this->getTabs()->activate('general'); - $form->setConfiguration(IcingaConfig::app()) - ->setRequest($this->getRequest()); - if ($form->isSubmittedAndValid()) { - try { - $this->savePreferences($form->getPreferences()); - Notification::success(t('Preferences updated successfully')); - // Recreate form to show new values - // TODO(el): It must sufficient to call $form->populate(...) - $form = new GeneralForm(); - $form->setConfiguration(IcingaConfig::app()); - $form->setRequest($this->getRequest()); - } catch (Exception $e) { - Notification::error(sprintf(t('Failed to persist preferences. (%s)'), $e->getMessage())); + + $form = new GeneralForm(); + $request = $this->getRequest(); + if ($request->isPost()) { + if ($form->isValid($request->getPost())) { + try { + $this->savePreferences($form->getPreferences()->toArray()); + Notification::success($this->translate('Preferences updated successfully')); + $this->redirectNow('preference'); + } catch (Exception $e) { + Notification::error( + sprintf( + $this->translate('Failed to persist preferences. (%s)'), + $e->getMessage() + ) + ); + } } + } else { + $form->setPreferences($request->getUser()->getPreferences()); } + $this->view->form = $form; } } diff --git a/application/forms/Preference/GeneralForm.php b/application/forms/Preference/GeneralForm.php index fbc8f8d74..6f79ebb34 100644 --- a/application/forms/Preference/GeneralForm.php +++ b/application/forms/Preference/GeneralForm.php @@ -4,148 +4,189 @@ namespace Icinga\Form\Preference; -use \DateTimeZone; -use \Zend_Config; -use \Zend_Form_Element_Text; -use \Zend_Form_Element_Select; -use \Zend_View_Helper_DateFormat; -use \Icinga\Web\Form; -use \Icinga\Web\Form\Validator\TimeFormatValidator; -use \Icinga\Web\Form\Validator\DateFormatValidator; -use \Icinga\Util\Translator; +use DateTimeZone; +use Icinga\Web\Form; +use Icinga\Util\Translator; +use Icinga\User\Preferences; /** * General user preferences */ class GeneralForm extends Form { + /** + * Initialize this preferences config form + */ + public function init() + { + $this->setName('form_preference_set'); + } + /** * Add a select field for setting the user's language * * Possible values are determined by Translator::getAvailableLocaleCodes. - * Also, a 'use default format' checkbox is added in order to allow a user to discard his overwritten setting + * Also, a 'use browser language' checkbox is added in order to allow a user to discard his setting * - * @param Zend_Config $cfg The "global" section of the config.ini to be used as default value + * @param array $formData The data to populate the elements with */ - private function addLanguageSelection(Zend_Config $cfg) + protected function getLanguageElements(array $formData) { $languages = array(); foreach (Translator::getAvailableLocaleCodes() as $language) { $languages[$language] = $language; } $languages[Translator::DEFAULT_LOCALE] = Translator::DEFAULT_LOCALE; - $prefs = $this->getUserPreferences(); - $useDefaultLanguage = $this->getRequest()->getParam('default_language', !$prefs->has('app.language')); - $this->addElement( - 'checkbox', - 'default_language', - array( - 'label' => t('Use Default Language'), - 'value' => $useDefaultLanguage, - 'required' => true - ) - ); + $useBrowserLanguage = isset($formData['browser_language']) ? $formData['browser_language'] == 1 : true; $selectOptions = array( 'label' => t('Your Current Language'), - 'required' => !$useDefaultLanguage, + 'required' => false === $useBrowserLanguage, 'multiOptions' => $languages, 'helptext' => t('Use the following language to display texts and messages'), - 'value' => $prefs->get('app.language', $cfg->get('language', Translator::DEFAULT_LOCALE)) + 'value' => isset($formData['language']) + ? $formData['language'] + : substr(setlocale(LC_ALL, 0), 0, 5) ); - if ($useDefaultLanguage) { + if ($useBrowserLanguage) { $selectOptions['disabled'] = 'disabled'; } - $this->addElement('select', 'language', $selectOptions); - $this->enableAutoSubmit(array('default_language')); + + return array( + $this->createElement( + 'checkbox', + 'browser_language', + array( + 'required' => true, + 'class' => 'autosubmit', + 'label' => t('Use your browser\'s language suggestions'), + 'value' => $useBrowserLanguage + ) + ), + $this->createElement('select', 'language', $selectOptions) + ); } /** - * Add a select field for setting the user's timezone. + * Add a select field for setting the user's timezone * - * Possible values are determined by DateTimeZone::listIdentifiers - * Also, a 'use default format' checkbox is added in order to allow a user to discard his overwritten setting + * Possible values are determined by DateTimeZone::listIdentifiers. + * Also, a 'use local timezone' checkbox is added in order to allow a user to discard his overwritten setting * - * @param Zend_Config $cfg The "global" section of the config.ini to be used as default value + * @param array $formData The data to populate the elements with */ - private function addTimezoneSelection(Zend_Config $cfg) + protected function getTimezoneElements(array $formData) { $tzList = array(); foreach (DateTimeZone::listIdentifiers() as $tz) { $tzList[$tz] = $tz; } - $helptext = 'Use the following timezone for dates and times'; - $prefs = $this->getUserPreferences(); - $useGlobalTimezone = $this->getRequest()->getParam('default_timezone', !$prefs->has('app.timezone')); - $selectTimezone = new Zend_Form_Element_Select( - array( - 'name' => 'timezone', - 'label' => 'Your Current Timezone', - 'required' => !$useGlobalTimezone, - 'multiOptions' => $tzList, - 'helptext' => $helptext, - 'value' => $prefs->get('app.timezone', $cfg->get('timezone', date_default_timezone_get())) - ) + $useLocalTimezone = isset($formData['local_timezone']) ? $formData['local_timezone'] == 1 : true; + $selectOptions = array( + 'label' => 'Your Current Timezone', + 'required' => false === $useLocalTimezone, + 'multiOptions' => $tzList, + 'helptext' => t('Use the following timezone for dates and times'), + 'value' => isset($formData['timezone']) + ? $formData['timezone'] + : date_default_timezone_get() ); - $this->addElement( - 'checkbox', - 'default_timezone', - array( - 'label' => 'Use Default Timezone', - 'value' => $useGlobalTimezone, - 'required' => true - ) - ); - if ($useGlobalTimezone) { - $selectTimezone->setAttrib('disabled', 1); + if ($useLocalTimezone) { + $selectOptions['disabled'] = 'disabled'; } - $this->addElement($selectTimezone); - $this->enableAutoSubmit(array('default_timezone')); + + return array( + $this->createElement( + 'checkbox', + 'local_timezone', + array( + 'required' => true, + 'class' => 'autosubmit', + 'label' => t('Use your local timezone'), + 'value' => $useLocalTimezone, + ) + ), + $this->createElement('select', 'timezone', $selectOptions) + ); } /** - * Create the general form, using the global configuration as fallback values for preferences - * - * @see Form::create() + * @see Form::createElements() */ - public function create() + public function createElements(array $formData) { - $this->setName('form_preference_set'); - - $config = $this->getConfiguration(); - $global = $config->global; - if ($global === null) { - $global = new Zend_Config(array()); - } - - $this->addLanguageSelection($global); - $this->addTimezoneSelection($global); - - $this->setSubmitLabel('Save Changes'); - - $this->addElement( + $elements = array_merge($this->getLanguageElements($formData), $this->getTimezoneElements($formData)); + $elements[] = $this->createElement( 'checkbox', 'show_benchmark', array( - 'label' => 'Use benchmark', - 'value' => $this->getUserPreferences()->get('app.show_benchmark') + 'label' => t('Use benchmark'), + 'value' => isset($formData['show_benchmark']) ? $formData['show_benchmark'] : 0 ) ); + + return $elements; } /** - * Return an array containing the preferences set in this form + * @see Form::addSubmitButton() + */ + public function addSubmitButton() + { + $this->addElement( + 'submit', + 'btn_submit', + array( + 'label' => t('Save Changes') + ) + ); + + return $this; + } + + /** + * Populate the form with the given preferences * - * @return array + * @param Preferences $preferences The preferences to populate the form with + * + * @return self + */ + public function setPreferences(Preferences $preferences) + { + $defaults = array( + 'browser_language' => $preferences->get('app.language') === null, + 'local_timezone' => $preferences->get('app.timezone') === null + ); + + if ($preferences->get('app.language') !== null) { + $defaults['language'] = $preferences->get('app.language'); + } + if ($preferences->get('app.timezone') !== null) { + $defaults['timezone'] = $preferences->get('app.timezone'); + } + if ($preferences->get('app.show_benchmark')) { + $defaults['show_benchmark'] = $preferences->get('app.show_benchmark'); + } + + $this->setDefaults($defaults); + return $this; + } + + /** + * Return the configured preferences + * + * @return Preferences */ public function getPreferences() { $values = $this->getValues(); - return array( - 'app.language' => $values['default_language'] ? null : $values['language'], - 'app.timezone' => $values['default_timezone'] ? null : $values['timezone'], - 'app.show_benchmark' => $values['show_benchmark'] === '1' ? true : false + return new Preferences( + array( + 'app.language' => $values['browser_language'] ? null : $values['language'], + 'app.timezone' => $values['local_timezone'] ? null : $values['timezone'], + 'app.show_benchmark' => $values['show_benchmark'] ? $values['show_benchmark'] : null + ) ); } }