mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-21 21:04:25 +02:00
Change PreferenceForm and add the ability to store preferences in the current session
This commit is contained in:
parent
4f84979c89
commit
2bae33d6ad
@ -7,10 +7,12 @@ namespace Icinga\Form;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use Icinga\Application\Logger;
|
use Icinga\Application\Logger;
|
||||||
|
use Icinga\Authentication\Manager;
|
||||||
use Icinga\User\Preferences;
|
use Icinga\User\Preferences;
|
||||||
use Icinga\User\Preferences\PreferencesStore;
|
use Icinga\User\Preferences\PreferencesStore;
|
||||||
use Icinga\Util\TimezoneDetect;
|
use Icinga\Util\TimezoneDetect;
|
||||||
use Icinga\Util\Translator;
|
use Icinga\Util\Translator;
|
||||||
|
use Icinga\Web\Controller\ControllerTabCollector;
|
||||||
use Icinga\Web\Form;
|
use Icinga\Web\Form;
|
||||||
use Icinga\Web\Notification;
|
use Icinga\Web\Notification;
|
||||||
use Icinga\Web\Request;
|
use Icinga\Web\Request;
|
||||||
@ -41,7 +43,6 @@ class PreferenceForm extends Form
|
|||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$this->setName('form_config_preferences');
|
$this->setName('form_config_preferences');
|
||||||
$this->setSubmitLabel(t('Save Changes'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,7 +77,6 @@ class PreferenceForm extends Form
|
|||||||
*/
|
*/
|
||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
$this->store->load(); // Necessary for patching existing preferences
|
|
||||||
$this->store->save($this->preferences);
|
$this->store->save($this->preferences);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -88,9 +88,11 @@ class PreferenceForm extends Form
|
|||||||
*/
|
*/
|
||||||
public function onSuccess(Request $request)
|
public function onSuccess(Request $request)
|
||||||
{
|
{
|
||||||
|
$this->preferences = new Preferences($this->store->load());
|
||||||
|
|
||||||
$webPreferences = $this->preferences->get('icingaweb', array());
|
$webPreferences = $this->preferences->get('icingaweb', array());
|
||||||
foreach ($this->getValues() as $key => $value) {
|
foreach ($this->getValues() as $key => $value) {
|
||||||
if ($value === null) {
|
if ($value === null || $value === 'autodetect') {
|
||||||
if (isset($webPreferences[$key])) {
|
if (isset($webPreferences[$key])) {
|
||||||
unset($webPreferences[$key]);
|
unset($webPreferences[$key]);
|
||||||
}
|
}
|
||||||
@ -106,8 +108,12 @@ class PreferenceForm extends Form
|
|||||||
$session->write();
|
$session->write();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->save();
|
if ($this->getElement('btn_submit_preferences')->isChecked()) {
|
||||||
Notification::success(t('Preferences successfully saved'));
|
$this->save();
|
||||||
|
Notification::success(t('Preferences successfully saved'));
|
||||||
|
} else {
|
||||||
|
Notification::success(t('Preferences successfully saved for the current session'));
|
||||||
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Logger::error($e);
|
Logger::error($e);
|
||||||
Notification::error($e->getMessage());
|
Notification::error($e->getMessage());
|
||||||
@ -121,9 +127,17 @@ class PreferenceForm extends Form
|
|||||||
*/
|
*/
|
||||||
public function onRequest(Request $request)
|
public function onRequest(Request $request)
|
||||||
{
|
{
|
||||||
$values = $this->preferences->get('icingaweb', array());
|
$auth = Manager::getInstance();
|
||||||
$values['browser_language'] = false === isset($values['language']);
|
$values = $auth->getUser()->getPreferences()->get('icingaweb');
|
||||||
$values['local_timezone'] = false === isset($values['timezone']);
|
|
||||||
|
if (! isset($values['language'])) {
|
||||||
|
$values['language'] = 'autodetect';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! isset($values['timezone'])) {
|
||||||
|
$values['timezone'] = 'autodetect';
|
||||||
|
}
|
||||||
|
|
||||||
$this->populate($values);
|
$this->populate($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,72 +147,40 @@ class PreferenceForm extends Form
|
|||||||
public function createElements(array $formData)
|
public function createElements(array $formData)
|
||||||
{
|
{
|
||||||
$languages = array();
|
$languages = array();
|
||||||
|
$languages['autodetect'] = sprintf(t('Browser (%s)', 'preferences.form'), $this->getLocale());
|
||||||
foreach (Translator::getAvailableLocaleCodes() as $language) {
|
foreach (Translator::getAvailableLocaleCodes() as $language) {
|
||||||
$languages[$language] = $language;
|
$languages[$language] = $language;
|
||||||
}
|
}
|
||||||
|
|
||||||
$tzList = array();
|
$tzList = array();
|
||||||
|
$tzList['autodetect'] = sprintf(t('Browser (%s)', 'preferences.form'), $this->getDefaultTimezone());
|
||||||
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
||||||
$tzList[$tz] = $tz;
|
$tzList[$tz] = $tz;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addElement(
|
$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',
|
'select',
|
||||||
'language',
|
'language',
|
||||||
array(
|
array(
|
||||||
'required' => false === $useBrowserLanguage,
|
'required' => true,
|
||||||
'label' => t('Your Current Language'),
|
'label' => t('Your Current Language'),
|
||||||
'description' => t('Use the following language to display texts and messages'),
|
'description' => t('Use the following language to display texts and messages'),
|
||||||
'multiOptions' => $languages,
|
'multiOptions' => $languages,
|
||||||
'value' => substr(setlocale(LC_ALL, 0), 0, 5)
|
'value' => substr(setlocale(LC_ALL, 0), 0, 5)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if ($useBrowserLanguage) {
|
|
||||||
$languageSelection->setAttrib('disabled', 'disabled');
|
|
||||||
}
|
|
||||||
$this->addElement($languageSelection);
|
|
||||||
|
|
||||||
$this->addElement(
|
$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',
|
'select',
|
||||||
'timezone',
|
'timezone',
|
||||||
array(
|
array(
|
||||||
'required' => false === $useLocalTimezone,
|
'required' => true,
|
||||||
'label' => t('Your Current Timezone'),
|
'label' => t('Your Current Timezone'),
|
||||||
'description' => t('Use the following timezone for dates and times'),
|
'description' => t('Use the following timezone for dates and times'),
|
||||||
'multiOptions' => $tzList,
|
'multiOptions' => $tzList,
|
||||||
'value' => $this->getDefaultTimezone()
|
'value' => $this->getDefaultTimezone()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if ($useLocalTimezone) {
|
|
||||||
$timezoneSelection->setAttrib('disabled', 'disabled');
|
|
||||||
}
|
|
||||||
$this->addElement($timezoneSelection);
|
|
||||||
|
|
||||||
$this->addElement(
|
$this->addElement(
|
||||||
'checkbox',
|
'checkbox',
|
||||||
@ -208,6 +190,43 @@ class PreferenceForm extends Form
|
|||||||
'label' => t('Use benchmark')
|
'label' => t('Use benchmark')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->addElement(
|
||||||
|
'submit',
|
||||||
|
'btn_submit_preferences',
|
||||||
|
array(
|
||||||
|
'ignore' => true,
|
||||||
|
'label' => t('Save to the Preferences'),
|
||||||
|
'decorators' => array(
|
||||||
|
'ViewHelper',
|
||||||
|
array('HtmlTag', array('tag' => 'div'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->addElement(
|
||||||
|
'submit',
|
||||||
|
'btn_submit_session',
|
||||||
|
array(
|
||||||
|
'ignore' => true,
|
||||||
|
'label' => t('Save for the current Session'),
|
||||||
|
'decorators' => array(
|
||||||
|
'ViewHelper',
|
||||||
|
array('HtmlTag', array('tag' => 'div'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->addDisplayGroup(
|
||||||
|
array('btn_submit_preferences', 'btn_submit_session'),
|
||||||
|
'submit_buttons',
|
||||||
|
array(
|
||||||
|
'decorators' => array(
|
||||||
|
'FormElements',
|
||||||
|
array('HtmlTag', array('tag' => 'div', 'class' => 'control-group'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -224,4 +243,15 @@ class PreferenceForm extends Form
|
|||||||
return date_default_timezone_get();
|
return date_default_timezone_get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the preferred locale based on the given HTTP header and the available translations
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getLocale()
|
||||||
|
{
|
||||||
|
$locale = Translator::getPreferredLocaleCode($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
||||||
|
return $locale;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user