aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-08-04 18:31:16 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-08-04 23:54:03 +0200
commitafd7b77b4c79a0450a6ef0489ca383c156111173 (patch)
tree1892b2fdcdaa65448b653b7ee57fd96dae376fd9
parent27cf2e671d1f35bd8c9383d008cd3733fc1c6e0d (diff)
downloadShaarli-afd7b77b4c79a0450a6ef0489ca383c156111173.tar.gz
Shaarli-afd7b77b4c79a0450a6ef0489ca383c156111173.tar.zst
Shaarli-afd7b77b4c79a0450a6ef0489ca383c156111173.zip
Installation: default to the server's timezone
Modifications - attempt to use the server's timezone - if none is set, use UTC - TimeZone: apply coding conventions - variable naming - no closing PHP tag Relates to #274 Signed-off-by: VirtualTam <virtualtam@flibidi.net>
-rw-r--r--application/TimeZone.php62
-rw-r--r--index.php10
-rw-r--r--tests/TimeZoneTest.php1
3 files changed, 38 insertions, 35 deletions
diff --git a/application/TimeZone.php b/application/TimeZone.php
index ccbef918..e363d90a 100644
--- a/application/TimeZone.php
+++ b/application/TimeZone.php
@@ -5,30 +5,33 @@
5 * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option 5 * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
6 * 6 *
7 * Example: preselect Europe/Paris 7 * Example: preselect Europe/Paris
8 * list($htmlform, $js) = templateTZform('Europe/Paris'); 8 * list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
9 * 9 *
10 * @param string $preselected_timezone preselected timezone (optional) 10 * @param string $preselected_timezone preselected timezone (optional)
11 * 11 *
12 * @return an array containing the generated HTML form and Javascript code 12 * @return an array containing the generated HTML form and Javascript code
13 **/ 13 **/
14function generateTimeZoneForm($preselected_timezone='') 14function generateTimeZoneForm($preselectedTimezone='')
15{ 15{
16 // Select the first available timezone if no preselected value is passed 16 // Select the server timezone
17 if ($preselected_timezone == '') { 17 if ($preselectedTimezone == '') {
18 $l = timezone_identifiers_list(); 18 $preselectedTimezone = date_default_timezone_get();
19 $preselected_timezone = $l[0];
20 } 19 }
21 20
22 // Try to split the provided timezone 21 if ($preselectedTimezone == 'UTC') {
23 $spos = strpos($preselected_timezone, '/'); 22 $pcity = $pcontinent = 'UTC';
24 $pcontinent = substr($preselected_timezone, 0, $spos); 23 } else {
25 $pcity = substr($preselected_timezone, $spos+1); 24 // Try to split the provided timezone
25 $spos = strpos($preselectedTimezone, '/');
26 $pcontinent = substr($preselectedTimezone, 0, $spos);
27 $pcity = substr($preselectedTimezone, $spos+1);
28 }
26 29
27 // Display config form: 30 // Display config form:
28 $timezone_form = ''; 31 $timezoneForm = '';
29 $timezone_js = ''; 32 $timezoneJs = '';
30 33
31 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'... 34 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
32 // We split the list in continents/cities. 35 // We split the list in continents/cities.
33 $continents = array(); 36 $continents = array();
34 $cities = array(); 37 $cities = array();
@@ -57,33 +60,33 @@ function generateTimeZoneForm($preselected_timezone='')
57 } 60 }
58 } 61 }
59 62
60 $continents_html = ''; 63 $continentsHtml = '';
61 $continents = array_keys($continents); 64 $continents = array_keys($continents);
62 65
63 foreach ($continents as $continent) { 66 foreach ($continents as $continent) {
64 $continents_html .= '<option value="'.$continent.'"'; 67 $continentsHtml .= '<option value="'.$continent.'"';
65 if ($pcontinent == $continent) { 68 if ($pcontinent == $continent) {
66 $continents_html .= ' selected="selected"'; 69 $continentsHtml .= ' selected="selected"';
67 } 70 }
68 $continents_html .= '>'.$continent.'</option>'; 71 $continentsHtml .= '>'.$continent.'</option>';
69 } 72 }
70 73
71 // Timezone selection form 74 // Timezone selection form
72 $timezone_form = 'Continent:'; 75 $timezoneForm = 'Continent:';
73 $timezone_form .= '<select name="continent" id="continent" onChange="onChangecontinent();">'; 76 $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
74 $timezone_form .= $continents_html.'</select>'; 77 $timezoneForm .= $continentsHtml.'</select>';
75 $timezone_form .= '&nbsp;&nbsp;&nbsp;&nbsp;City:'; 78 $timezoneForm .= '&nbsp;&nbsp;&nbsp;&nbsp;City:';
76 $timezone_form .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />'; 79 $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
77 80
78 // Javascript handler - updates the city list when the user selects a continent 81 // Javascript handler - updates the city list when the user selects a continent
79 $timezone_js = '<script>'; 82 $timezoneJs = '<script>';
80 $timezone_js .= 'function onChangecontinent() {'; 83 $timezoneJs .= 'function onChangecontinent() {';
81 $timezone_js .= 'document.getElementById("city").innerHTML ='; 84 $timezoneJs .= 'document.getElementById("city").innerHTML =';
82 $timezone_js .= ' citiescontinent[document.getElementById("continent").value]; }'; 85 $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
83 $timezone_js .= 'var citiescontinent = '.json_encode($cities).';'; 86 $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
84 $timezone_js .= '</script>'; 87 $timezoneJs .= '</script>';
85 88
86 return array($timezone_form, $timezone_js); 89 return array($timezoneForm, $timezoneJs);
87} 90}
88 91
89/** 92/**
@@ -107,4 +110,3 @@ function isTimeZoneValid($continent, $city)
107 timezone_identifiers_list() 110 timezone_identifiers_list()
108 ); 111 );
109} 112}
110?>
diff --git a/index.php b/index.php
index 1439ec2f..e3b612c8 100644
--- a/index.php
+++ b/index.php
@@ -5,10 +5,12 @@
5// Licence: http://www.opensource.org/licenses/zlib-license.php 5// Licence: http://www.opensource.org/licenses/zlib-license.php
6// Requires: PHP 5.3.x 6// Requires: PHP 5.3.x
7// ----------------------------------------------------------------------------------------------- 7// -----------------------------------------------------------------------------------------------
8// NEVER TRUST IN PHP.INI 8
9// Some hosts do not define a default timezone in php.ini, 9// Set 'UTC' as the default timezone if it is not defined in php.ini
10// so we have to do this for avoid the strict standard error. 10// See http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
11date_default_timezone_set('UTC'); 11if (date_default_timezone_get() == '') {
12 date_default_timezone_set('UTC');
13}
12 14
13// ----------------------------------------------------------------------------------------------- 15// -----------------------------------------------------------------------------------------------
14// Hardcoded parameter (These parameters can be overwritten by editing the file /data/config.php) 16// Hardcoded parameter (These parameters can be overwritten by editing the file /data/config.php)
diff --git a/tests/TimeZoneTest.php b/tests/TimeZoneTest.php
index f3de3913..b219030a 100644
--- a/tests/TimeZoneTest.php
+++ b/tests/TimeZoneTest.php
@@ -80,4 +80,3 @@ class TimeZoneTest extends PHPUnit_Framework_TestCase
80 $this->assertFalse(isTimeZoneValid('Middle_Earth', 'Moria')); 80 $this->assertFalse(isTimeZoneValid('Middle_Earth', 'Moria'));
81 } 81 }
82} 82}
83?>