]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/TimeZone.php
Fixes #325 - Shaarli does not recognize saved links
[github/shaarli/Shaarli.git] / application / TimeZone.php
CommitLineData
d1e2f8e5
V
1<?php
2/**
3 * Generates the timezone selection form and JavaScript.
4 *
5 * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
6 *
7 * Example: preselect Europe/Paris
afd7b77b 8 * list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
d1e2f8e5
V
9 *
10 * @param string $preselected_timezone preselected timezone (optional)
11 *
12 * @return an array containing the generated HTML form and Javascript code
13 **/
afd7b77b 14function generateTimeZoneForm($preselectedTimezone='')
d1e2f8e5 15{
afd7b77b
V
16 // Select the server timezone
17 if ($preselectedTimezone == '') {
18 $preselectedTimezone = date_default_timezone_get();
d1e2f8e5
V
19 }
20
afd7b77b
V
21 if ($preselectedTimezone == 'UTC') {
22 $pcity = $pcontinent = 'UTC';
23 } else {
24 // Try to split the provided timezone
25 $spos = strpos($preselectedTimezone, '/');
26 $pcontinent = substr($preselectedTimezone, 0, $spos);
27 $pcity = substr($preselectedTimezone, $spos+1);
28 }
d1e2f8e5
V
29
30 // Display config form:
afd7b77b
V
31 $timezoneForm = '';
32 $timezoneJs = '';
d1e2f8e5 33
afd7b77b 34 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
d1e2f8e5
V
35 // We split the list in continents/cities.
36 $continents = array();
37 $cities = array();
38
39 // TODO: use a template to generate the HTML/Javascript form
40
41 foreach (timezone_identifiers_list() as $tz) {
42 if ($tz == 'UTC') {
43 $tz = 'UTC/UTC';
44 }
45 $spos = strpos($tz, '/');
46
47 if ($spos !== false) {
48 $continent = substr($tz, 0, $spos);
49 $city = substr($tz, $spos+1);
50 $continents[$continent] = 1;
51
52 if (!isset($cities[$continent])) {
53 $cities[$continent] = '';
54 }
55 $cities[$continent] .= '<option value="'.$city.'"';
56 if ($pcity == $city) {
57 $cities[$continent] .= ' selected="selected"';
58 }
59 $cities[$continent] .= '>'.$city.'</option>';
60 }
61 }
62
afd7b77b 63 $continentsHtml = '';
d1e2f8e5
V
64 $continents = array_keys($continents);
65
66 foreach ($continents as $continent) {
afd7b77b 67 $continentsHtml .= '<option value="'.$continent.'"';
d1e2f8e5 68 if ($pcontinent == $continent) {
afd7b77b 69 $continentsHtml .= ' selected="selected"';
d1e2f8e5 70 }
afd7b77b 71 $continentsHtml .= '>'.$continent.'</option>';
d1e2f8e5
V
72 }
73
74 // Timezone selection form
afd7b77b
V
75 $timezoneForm = 'Continent:';
76 $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
77 $timezoneForm .= $continentsHtml.'</select>';
78 $timezoneForm .= '&nbsp;&nbsp;&nbsp;&nbsp;City:';
79 $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
d1e2f8e5
V
80
81 // Javascript handler - updates the city list when the user selects a continent
afd7b77b
V
82 $timezoneJs = '<script>';
83 $timezoneJs .= 'function onChangecontinent() {';
84 $timezoneJs .= 'document.getElementById("city").innerHTML =';
85 $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
86 $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
87 $timezoneJs .= '</script>';
d1e2f8e5 88
afd7b77b 89 return array($timezoneForm, $timezoneJs);
d1e2f8e5
V
90}
91
92/**
93 * Tells if a continent/city pair form a valid timezone
94 *
95 * Note: 'UTC/UTC' is mapped to 'UTC'
96 *
97 * @param string $continent the timezone continent
98 * @param string $city the timezone city
99 *
100 * @return whether continent/city is a valid timezone
101 */
102function isTimeZoneValid($continent, $city)
103{
104 if ($continent == 'UTC' && $city == 'UTC') {
105 return true;
106 }
107
108 return in_array(
109 $continent.'/'.$city,
110 timezone_identifiers_list()
111 );
112}