commit
975985dc8c
|
@ -5,6 +5,7 @@
|
||||||
namespace Icinga\Form\Preference;
|
namespace Icinga\Form\Preference;
|
||||||
|
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
|
use Icinga\Util\TimezoneDetect;
|
||||||
use Zend_Config;
|
use Zend_Config;
|
||||||
use Zend_Form_Element_Text;
|
use Zend_Form_Element_Text;
|
||||||
use Zend_Form_Element_Select;
|
use Zend_Form_Element_Select;
|
||||||
|
@ -69,13 +70,22 @@ class GeneralForm extends Form
|
||||||
*/
|
*/
|
||||||
private function addTimezoneSelection(Zend_Config $cfg)
|
private function addTimezoneSelection(Zend_Config $cfg)
|
||||||
{
|
{
|
||||||
|
$prefs = $this->getUserPreferences();
|
||||||
|
$useGlobalTimezone = $this->getRequest()->getParam('default_timezone', !$prefs->has('app.timezone'));
|
||||||
|
$detect = new TimezoneDetect();
|
||||||
|
|
||||||
$tzList = array();
|
$tzList = array();
|
||||||
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
||||||
$tzList[$tz] = $tz;
|
$tzList[$tz] = $tz;
|
||||||
}
|
}
|
||||||
|
|
||||||
$helptext = 'Use the following timezone for dates and times';
|
$helptext = 'Use the following timezone for dates and times';
|
||||||
$prefs = $this->getUserPreferences();
|
|
||||||
$useGlobalTimezone = $this->getRequest()->getParam('default_timezone', !$prefs->has('app.timezone'));
|
if ($useGlobalTimezone && $detect->success() === true) {
|
||||||
|
$helptext .= '<br />' . t('Currently your time was detected to be')
|
||||||
|
. ': '
|
||||||
|
. '<strong>' . $detect->getTimezoneName() . '</strong>';
|
||||||
|
}
|
||||||
|
|
||||||
$selectTimezone = new Zend_Form_Element_Select(
|
$selectTimezone = new Zend_Form_Element_Select(
|
||||||
array(
|
array(
|
||||||
|
@ -87,6 +97,7 @@ class GeneralForm extends Form
|
||||||
'value' => $prefs->get('app.timezone', $cfg->get('timezone', date_default_timezone_get()))
|
'value' => $prefs->get('app.timezone', $cfg->get('timezone', date_default_timezone_get()))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->addElement(
|
$this->addElement(
|
||||||
'checkbox',
|
'checkbox',
|
||||||
'default_timezone',
|
'default_timezone',
|
||||||
|
@ -99,7 +110,9 @@ class GeneralForm extends Form
|
||||||
if ($useGlobalTimezone) {
|
if ($useGlobalTimezone) {
|
||||||
$selectTimezone->setAttrib('disabled', 1);
|
$selectTimezone->setAttrib('disabled', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addElement($selectTimezone);
|
$this->addElement($selectTimezone);
|
||||||
|
|
||||||
$this->enableAutoSubmit(array('default_timezone'));
|
$this->enableAutoSubmit(array('default_timezone'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ use Icinga\Authentication\Manager as AuthenticationManager;
|
||||||
use Icinga\Exception\ConfigurationError;
|
use Icinga\Exception\ConfigurationError;
|
||||||
use Icinga\Exception\NotReadableError;
|
use Icinga\Exception\NotReadableError;
|
||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
|
use Icinga\Util\TimezoneDetect;
|
||||||
use Icinga\Web\Request;
|
use Icinga\Web\Request;
|
||||||
use Icinga\Web\Response;
|
use Icinga\Web\Response;
|
||||||
use Icinga\Web\View;
|
use Icinga\Web\View;
|
||||||
|
@ -273,10 +274,11 @@ class Web extends ApplicationBootstrap
|
||||||
*/
|
*/
|
||||||
protected function setupTimezone()
|
protected function setupTimezone()
|
||||||
{
|
{
|
||||||
if ($this->user !== null && $this->user->getPreferences() !== null) {
|
|
||||||
$userTimezone = $this->user->getPreferences()->get('app.timezone');
|
|
||||||
} else {
|
|
||||||
$userTimezone = null;
|
$userTimezone = null;
|
||||||
|
|
||||||
|
if ($this->user !== null && $this->user->getPreferences() !== null) {
|
||||||
|
$detect = new TimezoneDetect();
|
||||||
|
$userTimezone = $this->user->getPreferences()->get('app.timezone', $detect->getTimezoneName());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Util;
|
||||||
|
|
||||||
|
use Icinga\Application\Platform;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve timezone information from cookie
|
||||||
|
*/
|
||||||
|
class TimezoneDetect
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* If detection was successful
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static $success;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timezone offset in minutes
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private static $offset = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $timezoneName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cookie name
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static $cookieName = 'icingaweb2-tzo';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timezone name
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $timezone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new object and try to identify the timezone
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
if (self::$success !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Platform::isCli() === false && array_key_exists(self::$cookieName, $_COOKIE)) {
|
||||||
|
list($offset, $dst) = explode(',', $_COOKIE[self::$cookieName]);
|
||||||
|
$timezoneName = timezone_name_from_abbr('', (int)$offset, (int)$dst);
|
||||||
|
|
||||||
|
self::$success = (bool)$timezoneName;
|
||||||
|
|
||||||
|
if (self::$success === true) {
|
||||||
|
self::$offset = $offset;
|
||||||
|
self::$timezoneName = $timezoneName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get offset
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getOffset()
|
||||||
|
{
|
||||||
|
return self::$offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get timezone name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTimezoneName()
|
||||||
|
{
|
||||||
|
return self::$timezoneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True on success
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function success()
|
||||||
|
{
|
||||||
|
return self::$success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset object
|
||||||
|
*/
|
||||||
|
public function reset()
|
||||||
|
{
|
||||||
|
self::$success = null;
|
||||||
|
self::$timezoneName = null;
|
||||||
|
self::$offset = 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ class JavaScript
|
||||||
'js/icinga/events.js',
|
'js/icinga/events.js',
|
||||||
'js/icinga/history.js',
|
'js/icinga/history.js',
|
||||||
'js/icinga/module.js',
|
'js/icinga/module.js',
|
||||||
|
'js/icinga/timezone.js',
|
||||||
);
|
);
|
||||||
|
|
||||||
protected static $vendorFiles = array(
|
protected static $vendorFiles = array(
|
||||||
|
|
|
@ -82,6 +82,7 @@
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.timezone = new Icinga.Timezone();
|
||||||
this.utils = new Icinga.Utils(this);
|
this.utils = new Icinga.Utils(this);
|
||||||
this.logger = new Icinga.Logger(this);
|
this.logger = new Icinga.Logger(this);
|
||||||
this.timer = new Icinga.Timer(this);
|
this.timer = new Icinga.Timer(this);
|
||||||
|
@ -90,6 +91,7 @@
|
||||||
this.events = new Icinga.Events(this);
|
this.events = new Icinga.Events(this);
|
||||||
this.history = new Icinga.History(this);
|
this.history = new Icinga.History(this);
|
||||||
|
|
||||||
|
this.timezone.initialize();
|
||||||
this.timer.initialize();
|
this.timer.initialize();
|
||||||
this.events.initialize();
|
this.events.initialize();
|
||||||
this.history.initialize();
|
this.history.initialize();
|
||||||
|
@ -147,6 +149,7 @@
|
||||||
module.destroy();
|
module.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.timezone.destroy();
|
||||||
this.timer.destroy();
|
this.timer.destroy();
|
||||||
this.events.destroy();
|
this.events.destroy();
|
||||||
this.loader.destroy();
|
this.loader.destroy();
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
(function(Icinga, $) {
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum timezone offset
|
||||||
|
*
|
||||||
|
* @returns {Number}
|
||||||
|
*/
|
||||||
|
Date.prototype.getStdTimezoneOffset = function() {
|
||||||
|
if (Date.maxTimezoneOffset !== undefined) {
|
||||||
|
return Date.maxTimezoneOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
var year = new Date().getYear();
|
||||||
|
var previousOffset;
|
||||||
|
|
||||||
|
for (var i=0; i<12; i++) {
|
||||||
|
var d = new Date(year, i, 1);
|
||||||
|
if (previousOffset !== undefined) {
|
||||||
|
previousOffset = Math.max(previousOffset, d.getTimezoneOffset());
|
||||||
|
} else {
|
||||||
|
previousOffset = d.getTimezoneOffset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Date.maxTimezoneOffset = previousOffset;
|
||||||
|
|
||||||
|
return Date.maxTimezoneOffset;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for daylight saving time zone
|
||||||
|
*
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
Date.prototype.isDst = function() {
|
||||||
|
return (this.getStdTimezoneOffset() === this.getTimezoneOffset()) ? false : true;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write timezone information into a cookie
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
Icinga.Timezone = function() {
|
||||||
|
this.cookieName = 'icingaweb2-tzo';
|
||||||
|
};
|
||||||
|
|
||||||
|
Icinga.Timezone.prototype = {
|
||||||
|
/**
|
||||||
|
* Initialize interface method
|
||||||
|
*/
|
||||||
|
initialize: function () {
|
||||||
|
this.writeTimezone();
|
||||||
|
},
|
||||||
|
|
||||||
|
destroy: function() {
|
||||||
|
// PASS
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write timezone information into cookie
|
||||||
|
*/
|
||||||
|
writeTimezone: function() {
|
||||||
|
var date = new Date();
|
||||||
|
var timezoneOffset = (date.getTimezoneOffset()*60) * -1;
|
||||||
|
var dst = date.isDst();
|
||||||
|
|
||||||
|
if (this.readCookie(this.cookieName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeCookie(this.cookieName, timezoneOffset + ',' + Number(dst), 1);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write cookie data
|
||||||
|
*
|
||||||
|
* @param {String} name
|
||||||
|
* @param {String} value
|
||||||
|
* @param {Number} days
|
||||||
|
*/
|
||||||
|
writeCookie: function(name, value, days) {
|
||||||
|
var expires = '';
|
||||||
|
|
||||||
|
if (days) {
|
||||||
|
var date = new Date();
|
||||||
|
date.setTime(date.getTime()+(days*24*60*60*1000));
|
||||||
|
var expires = '; expires=' + date.toGMTString();
|
||||||
|
}
|
||||||
|
document.cookie = name + '=' + value + expires + '; path=/';
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read cookie data
|
||||||
|
*
|
||||||
|
* @param {String} name
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
readCookie: function(name) {
|
||||||
|
var nameEq = name + '=';
|
||||||
|
var ca = document.cookie.split(';');
|
||||||
|
for(var i=0;i < ca.length;i++) {
|
||||||
|
var c = ca[i];
|
||||||
|
while (c.charAt(0)==' ') {
|
||||||
|
c = c.substring(1,c.length);
|
||||||
|
}
|
||||||
|
if (c.indexOf(nameEq) == 0) {
|
||||||
|
return c.substring(nameEq.length,c.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
})(Icinga, jQuery);
|
Loading…
Reference in New Issue