Adjust Preferences/GeneralForm to use handleRequest() &. Co.
refs #5525
This commit is contained in:
parent
7d212658bc
commit
fc72ddfbc8
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,171 +0,0 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Form\Preference;
|
||||
|
||||
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_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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,211 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Form;
|
||||
|
||||
use Exception;
|
||||
use DateTimeZone;
|
||||
use Icinga\Web\Form;
|
||||
use Icinga\Web\Request;
|
||||
use Icinga\Web\Session;
|
||||
use Icinga\Web\Notification;
|
||||
use Icinga\Util\Translator;
|
||||
use Icinga\User\Preferences;
|
||||
use Icinga\User\Preferences\PreferencesStore;
|
||||
|
||||
/**
|
||||
* Form class to adjust user preferences
|
||||
*/
|
||||
class PreferenceForm extends Form
|
||||
{
|
||||
/**
|
||||
* The preferences to work with
|
||||
*
|
||||
* @var Preferences
|
||||
*/
|
||||
protected $preferences;
|
||||
|
||||
/**
|
||||
* The preference store to use
|
||||
*
|
||||
* @var PreferencesStore
|
||||
*/
|
||||
protected $store;
|
||||
|
||||
/**
|
||||
* Initialize this form
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
<div class="controls">
|
||||
<?= $this->tabs->render($this); ?>
|
||||
<?= $tabs; ?>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<?= $this->form ?>
|
||||
</div>
|
||||
<?= $form; ?>
|
||||
</div>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue