diff --git a/application/controllers/PreferenceController.php b/application/controllers/PreferenceController.php index 0eb45dca0..bd76b2466 100644 --- a/application/controllers/PreferenceController.php +++ b/application/controllers/PreferenceController.php @@ -3,10 +3,12 @@ // {{{ICINGA_LICENSE_HEADER}}} use Icinga\Web\Controller\BasePreferenceController; -use Icinga\Web\Widget\Tab; use Icinga\Web\Url; -use Icinga\Form\Preference\GeneralForm; -use Icinga\Web\Notification; +use Icinga\Web\Widget\Tab; +use Icinga\Application\Config; +use Icinga\Form\PreferenceForm; +use Icinga\Exception\ConfigurationError; +use Icinga\User\Preferences\PreferencesStore; /** * Application wide preference controller for user preferences @@ -33,33 +35,22 @@ class PreferenceController extends BasePreferenceController } /** - * General settings for date and time + * Show form to adjust user preferences */ public function indexAction() { - $this->getTabs()->activate('general'); - - $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()); + $storeConfig = Config::app()->preferences; + if ($storeConfig === null) { + throw new ConfigurationError(t('You need to configure how to store preferences first.')); } + $user = $this->getRequest()->getUser(); + $form = new PreferenceForm(); + $form->setPreferences($user->getPreferences()); + $form->setStore(PreferencesStore::create($storeConfig, $user)); + $form->handleRequest(); + $this->view->form = $form; + $this->getTabs()->activate('general'); } } diff --git a/application/forms/Preference/GeneralForm.php b/application/forms/Preference/GeneralForm.php deleted file mode 100644 index b9ce5e9e2..000000000 --- a/application/forms/Preference/GeneralForm.php +++ /dev/null @@ -1,171 +0,0 @@ -setName('form_config_preferences'); - $this->setSubmitLabel(t('Save Changes')); - } - - /** - * Add a select field for setting the user's language - * - * Possible values are determined by Translator::getAvailableLocaleCodes. - * Also, a 'use browser language' checkbox is added in order to allow a user to discard his setting - * - * @param array $formData The data sent by the user - */ - protected function getLanguageElements(array $formData) - { - $languages = array(); - foreach (Translator::getAvailableLocaleCodes() as $language) { - $languages[$language] = $language; - } - - $useBrowserLanguage = isset($formData['browser_language']) ? $formData['browser_language'] == 1 : true; - $selectOptions = array( - 'label' => t('Your Current Language'), - 'required' => false === $useBrowserLanguage, - 'multiOptions' => $languages, - 'helptext' => t('Use the following language to display texts and messages'), - 'value' => substr(setlocale(LC_ALL, 0), 0, 5) - ); - if ($useBrowserLanguage) { - $selectOptions['disabled'] = 'disabled'; - } - - 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 - * - * 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 array $formData The data sent by the user - */ - protected function getTimezoneElements(array $formData) - { - $tzList = array(); - foreach (DateTimeZone::listIdentifiers() as $tz) { - $tzList[$tz] = $tz; - } - - $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' => date_default_timezone_get() - ); - if ($useLocalTimezone) { - $selectOptions['disabled'] = 'disabled'; - } - - 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) - ); - } - - /** - * @see Form::createElements() - */ - public function createElements(array $formData) - { - $elements = array_merge($this->getLanguageElements($formData), $this->getTimezoneElements($formData)); - $elements[] = $this->createElement( - 'checkbox', - 'show_benchmark', - array( - 'label' => t('Use benchmark') - ) - ); - - return $elements; - } - - /** - * Populate the form with the given preferences - * - * @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 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 - ) - ); - } -} diff --git a/application/forms/PreferenceForm.php b/application/forms/PreferenceForm.php new file mode 100644 index 000000000..c156cca12 --- /dev/null +++ b/application/forms/PreferenceForm.php @@ -0,0 +1,211 @@ +setName('form_config_preferences'); + $this->setSubmitLabel(t('Save Changes')); + } + + /** + * Set preferences to work with + * + * @param Preferences $preferences The preferences to work with + * + * @return self + */ + public function setPreferences(Preferences $preferences) + { + $this->preferences = $preferences; + return $this; + } + + /** + * Set the preference store to use + * + * @param PreferencesStore $store The preference store to use + * + * @return self + */ + public function setStore(PreferencesStore $store) + { + $this->store = $store; + } + + /** + * Persist preferences + * + * @return self + */ + public function save() + { + $this->store->load(); // Necessary for patching existing preferences + $this->store->save($this->preferences); + return $this; + } + + /** + * Adjust preferences and persist them + * + * @see Form::onSuccess() + */ + public function onSuccess(Request $request) + { + $webPreferences = $this->preferences->get('icingaweb', array()); + foreach ($this->getValues() as $key => $value) { + if ($value === null) { + if (isset($webPreferences[$key])) { + unset($webPreferences[$key]); + } + } else { + $webPreferences[$key] = $value; + } + } + $this->preferences->icingaweb = $webPreferences; + + // TODO: Is this even necessary in case the session is written on response? + $session = Session::getSession(); + $session->user->setPreferences($this->preferences); + $session->write(); + + try { + $this->save(); + Notification::success(t('Preferences successfully saved')); + } catch (Exception $e) { + Notification::error($e->getMessage()); + } + } + + /** + * Populate preferences + * + * @see Form::onRequest() + */ + public function onRequest(Request $request) + { + $values = $this->preferences->get('icingaweb', array()); + $values['browser_language'] = false === isset($values['language']); + $values['local_timezone'] = false === isset($values['timezone']); + $this->populate($values); + } + + /** + * @see Form::createElements() + */ + public function createElements(array $formData) + { + $languages = array(); + foreach (Translator::getAvailableLocaleCodes() as $language) { + $languages[$language] = $language; + } + + $tzList = array(); + foreach (DateTimeZone::listIdentifiers() as $tz) { + $tzList[$tz] = $tz; + } + + $this->addElement( + 'checkbox', + 'browser_language', + array( + 'ignore' => true, + 'required' => true, + 'autosubmit' => true, + 'value' => true, + 'label' => t('Use your browser\'s language suggestions') + ) + ); + + $useBrowserLanguage = isset($formData['browser_language']) ? $formData['browser_language'] == 1 : true; + $languageSelection = $this->createElement( + 'select', + 'language', + array( + 'required' => false === $useBrowserLanguage, + 'label' => t('Your Current Language'), + 'description' => t('Use the following language to display texts and messages'), + 'multiOptions' => $languages, + 'value' => substr(setlocale(LC_ALL, 0), 0, 5) + ) + ); + if ($useBrowserLanguage) { + $languageSelection->setAttrib('disabled', 'disabled'); + } + $this->addElement($languageSelection); + + $this->addElement( + 'checkbox', + 'local_timezone', + array( + 'ignore' => true, + 'required' => true, + 'autosubmit' => true, + 'value' => true, + 'label' => t('Use your local timezone') + ) + ); + + $useLocalTimezone = isset($formData['local_timezone']) ? $formData['local_timezone'] == 1 : true; + $timezoneSelection = $this->createElement( + 'select', + 'timezone', + array( + 'required' => false === $useLocalTimezone, + 'label' => t('Your Current Timezone'), + 'description' => t('Use the following timezone for dates and times'), + 'multiOptions' => $tzList, + 'value' => date_default_timezone_get() + ) + ); + if ($useLocalTimezone) { + $timezoneSelection->setAttrib('disabled', 'disabled'); + } + $this->addElement($timezoneSelection); + + $this->addElement( + 'checkbox', + 'show_benchmark', + array( + 'required' => true, + 'label' => t('Use benchmark') + ) + ); + + return $this; + } +} diff --git a/application/views/scripts/preference/index.phtml b/application/views/scripts/preference/index.phtml index 256f8623c..163381531 100644 --- a/application/views/scripts/preference/index.phtml +++ b/application/views/scripts/preference/index.phtml @@ -1,7 +1,7 @@
-tabs->render($this); ?> +
-form ?> -
+ + \ No newline at end of file diff --git a/library/Icinga/Web/Controller/BasePreferenceController.php b/library/Icinga/Web/Controller/BasePreferenceController.php index a83b8e6c0..f7d45aa5f 100644 --- a/library/Icinga/Web/Controller/BasePreferenceController.php +++ b/library/Icinga/Web/Controller/BasePreferenceController.php @@ -4,11 +4,6 @@ namespace Icinga\Web\Controller; -use Icinga\Application\Config as IcingaConfig; -use Icinga\Exception\ConfigurationError; -use Icinga\Web\Session; -use Icinga\User\Preferences\PreferencesStore; - /** * Base class for Preference Controllers * @@ -42,27 +37,4 @@ class BasePreferenceController extends ActionController parent::init(); $this->view->tabs = ControllerTabCollector::collectControllerTabs('PreferenceController'); } - - protected function savePreferences(array $preferences) - { - $session = Session::getSession(); - $currentPreferences = $session->user->getPreferences(); - foreach ($preferences as $key => $value) { - if ($value === null) { - $currentPreferences->remove($key); - } else { - $currentPreferences->{$key} = $value; - } - } - $session->write(); - - if (($preferencesConfig = IcingaConfig::app()->preferences) === null) { - throw new ConfigurationError( - 'Cannot save preferences changes since you\'ve not configured a preferences backend' - ); - } - $store = PreferencesStore::create($preferencesConfig, $session->user); - $store->load(); // Necessary for patching existing preferences - $store->save($currentPreferences); - } } diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 4d294dd85..8f3a47f64 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -438,8 +438,9 @@ class Form extends Zend_Form 'hidden', $this->uidElementName, array( - 'ignore' => true, - 'value' => $this->getName() + 'ignore' => true, + 'value' => $this->getName(), + 'decorators' => array('ViewHelper') ) ); }