2014-09-05 10:21:24 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-05 10:21:24 +02:00
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms;
|
2014-09-05 10:21:24 +02:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use DateTimeZone;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2014-11-12 13:18:35 +01:00
|
|
|
use Icinga\Authentication\Manager;
|
2014-10-30 09:48:25 +01:00
|
|
|
use Icinga\User\Preferences;
|
|
|
|
use Icinga\User\Preferences\PreferencesStore;
|
|
|
|
use Icinga\Util\TimezoneDetect;
|
|
|
|
use Icinga\Util\Translator;
|
2014-09-05 10:21:24 +02:00
|
|
|
use Icinga\Web\Form;
|
2014-10-30 09:48:25 +01:00
|
|
|
use Icinga\Web\Notification;
|
2014-09-05 10:21:24 +02:00
|
|
|
use Icinga\Web\Session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->save($this->preferences);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust preferences and persist them
|
|
|
|
*
|
|
|
|
* @see Form::onSuccess()
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2014-09-05 10:21:24 +02:00
|
|
|
{
|
2015-01-23 16:19:39 +01:00
|
|
|
$this->preferences = new Preferences($this->store ? $this->store->load() : array());
|
2014-11-12 13:18:35 +01:00
|
|
|
|
2014-09-05 10:21:24 +02:00
|
|
|
$webPreferences = $this->preferences->get('icingaweb', array());
|
|
|
|
foreach ($this->getValues() as $key => $value) {
|
2014-11-12 13:18:35 +01:00
|
|
|
if ($value === null || $value === 'autodetect') {
|
2014-09-05 10:21:24 +02:00
|
|
|
if (isset($webPreferences[$key])) {
|
|
|
|
unset($webPreferences[$key]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$webPreferences[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->preferences->icingaweb = $webPreferences;
|
|
|
|
|
2014-09-17 10:42:56 +02:00
|
|
|
Session::getSession()->user->setPreferences($this->preferences);
|
2014-09-05 10:21:24 +02:00
|
|
|
|
|
|
|
try {
|
2015-01-23 16:19:39 +01:00
|
|
|
if ($this->store && $this->getElement('btn_submit_preferences')->isChecked()) {
|
2014-11-12 13:18:35 +01:00
|
|
|
$this->save();
|
2015-01-19 11:26:23 +01:00
|
|
|
Notification::success($this->translate('Preferences successfully saved'));
|
2014-11-12 13:18:35 +01:00
|
|
|
} else {
|
2015-01-19 11:26:23 +01:00
|
|
|
Notification::success($this->translate('Preferences successfully saved for the current session'));
|
2014-11-12 13:18:35 +01:00
|
|
|
}
|
2014-09-05 10:21:24 +02:00
|
|
|
} catch (Exception $e) {
|
2014-10-30 09:48:25 +01:00
|
|
|
Logger::error($e);
|
2014-09-05 10:21:24 +02:00
|
|
|
Notification::error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate preferences
|
|
|
|
*
|
|
|
|
* @see Form::onRequest()
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onRequest()
|
2014-09-05 10:21:24 +02:00
|
|
|
{
|
2014-11-12 13:18:35 +01:00
|
|
|
$auth = Manager::getInstance();
|
|
|
|
$values = $auth->getUser()->getPreferences()->get('icingaweb');
|
|
|
|
|
|
|
|
if (! isset($values['language'])) {
|
|
|
|
$values['language'] = 'autodetect';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! isset($values['timezone'])) {
|
|
|
|
$values['timezone'] = 'autodetect';
|
|
|
|
}
|
|
|
|
|
2014-09-05 10:21:24 +02:00
|
|
|
$this->populate($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
|
|
|
$languages = array();
|
2015-01-19 11:26:23 +01:00
|
|
|
$languages['autodetect'] = sprintf($this->translate('Browser (%s)', 'preferences.form'), $this->getLocale());
|
2014-09-05 10:21:24 +02:00
|
|
|
foreach (Translator::getAvailableLocaleCodes() as $language) {
|
|
|
|
$languages[$language] = $language;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tzList = array();
|
2015-01-19 11:26:23 +01:00
|
|
|
$tzList['autodetect'] = sprintf($this->translate('Browser (%s)', 'preferences.form'), $this->getDefaultTimezone());
|
2014-09-05 10:21:24 +02:00
|
|
|
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
|
|
|
$tzList[$tz] = $tz;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'language',
|
|
|
|
array(
|
2014-11-12 13:18:35 +01:00
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Your Current Language'),
|
|
|
|
'description' => $this->translate('Use the following language to display texts and messages'),
|
2014-09-05 10:21:24 +02:00
|
|
|
'multiOptions' => $languages,
|
|
|
|
'value' => substr(setlocale(LC_ALL, 0), 0, 5)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'timezone',
|
|
|
|
array(
|
2014-11-12 13:18:35 +01:00
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Your Current Timezone'),
|
|
|
|
'description' => $this->translate('Use the following timezone for dates and times'),
|
2014-09-05 10:21:24 +02:00
|
|
|
'multiOptions' => $tzList,
|
2014-09-08 08:46:53 +02:00
|
|
|
'value' => $this->getDefaultTimezone()
|
2014-09-05 10:21:24 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'show_benchmark',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Use benchmark')
|
2014-09-05 10:21:24 +02:00
|
|
|
)
|
|
|
|
);
|
2014-11-12 13:18:35 +01:00
|
|
|
|
2015-01-23 16:19:39 +01:00
|
|
|
if ($this->store) {
|
|
|
|
$this->addElement(
|
|
|
|
'submit',
|
|
|
|
'btn_submit_preferences',
|
|
|
|
array(
|
|
|
|
'ignore' => true,
|
|
|
|
'label' => $this->translate('Save to the Preferences'),
|
|
|
|
'decorators' => array(
|
|
|
|
'ViewHelper',
|
|
|
|
array('HtmlTag', array('tag' => 'div'))
|
|
|
|
)
|
2014-11-12 13:18:35 +01:00
|
|
|
)
|
2015-01-23 16:19:39 +01:00
|
|
|
);
|
|
|
|
}
|
2014-11-12 13:18:35 +01:00
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'submit',
|
|
|
|
'btn_submit_session',
|
|
|
|
array(
|
|
|
|
'ignore' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Save for the current Session'),
|
2014-11-12 13:18:35 +01:00
|
|
|
'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'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-09-05 10:21:24 +02:00
|
|
|
}
|
2014-09-08 08:46:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current default timezone
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDefaultTimezone()
|
|
|
|
{
|
|
|
|
$detect = new TimezoneDetect();
|
|
|
|
if ($detect->success()) {
|
|
|
|
return $detect->getTimezoneName();
|
|
|
|
} else {
|
2014-11-14 17:08:34 +01:00
|
|
|
return @date_default_timezone_get();
|
2014-09-08 08:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 13:18:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2014-09-05 10:21:24 +02:00
|
|
|
}
|