]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/TimeZone.php
Minor code cleanup: PHPDoc, spelling, unused variables, etc.
[github/shaarli/Shaarli.git] / application / TimeZone.php
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
8 * list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
9 *
10 * @param string $preselectedTimezone preselected timezone (optional)
11 *
12 * @return array containing the generated HTML form and Javascript code
13 **/
14 function generateTimeZoneForm($preselectedTimezone='')
15 {
16 // Select the server timezone
17 if ($preselectedTimezone == '') {
18 $preselectedTimezone = date_default_timezone_get();
19 }
20
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 }
29
30 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
31 // We split the list in continents/cities.
32 $continents = array();
33 $cities = array();
34
35 // TODO: use a template to generate the HTML/Javascript form
36
37 foreach (timezone_identifiers_list() as $tz) {
38 if ($tz == 'UTC') {
39 $tz = 'UTC/UTC';
40 }
41 $spos = strpos($tz, '/');
42
43 if ($spos !== false) {
44 $continent = substr($tz, 0, $spos);
45 $city = substr($tz, $spos+1);
46 $continents[$continent] = 1;
47
48 if (!isset($cities[$continent])) {
49 $cities[$continent] = '';
50 }
51 $cities[$continent] .= '<option value="'.$city.'"';
52 if ($pcity == $city) {
53 $cities[$continent] .= ' selected="selected"';
54 }
55 $cities[$continent] .= '>'.$city.'</option>';
56 }
57 }
58
59 $continentsHtml = '';
60 $continents = array_keys($continents);
61
62 foreach ($continents as $continent) {
63 $continentsHtml .= '<option value="'.$continent.'"';
64 if ($pcontinent == $continent) {
65 $continentsHtml .= ' selected="selected"';
66 }
67 $continentsHtml .= '>'.$continent.'</option>';
68 }
69
70 // Timezone selection form
71 $timezoneForm = 'Continent:';
72 $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
73 $timezoneForm .= $continentsHtml.'</select>';
74 $timezoneForm .= '&nbsp;&nbsp;&nbsp;&nbsp;City:';
75 $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
76
77 // Javascript handler - updates the city list when the user selects a continent
78 $timezoneJs = '<script>';
79 $timezoneJs .= 'function onChangecontinent() {';
80 $timezoneJs .= 'document.getElementById("city").innerHTML =';
81 $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
82 $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
83 $timezoneJs .= '</script>';
84
85 return array($timezoneForm, $timezoneJs);
86 }
87
88 /**
89 * Tells if a continent/city pair form a valid timezone
90 *
91 * Note: 'UTC/UTC' is mapped to 'UTC'
92 *
93 * @param string $continent the timezone continent
94 * @param string $city the timezone city
95 *
96 * @return bool whether continent/city is a valid timezone
97 */
98 function isTimeZoneValid($continent, $city)
99 {
100 return in_array(
101 $continent.'/'.$city,
102 timezone_identifiers_list()
103 );
104 }