]>
Commit | Line | Data |
---|---|---|
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 $preselected_timezone preselected timezone (optional) | |
11 | * | |
12 | * @return an 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 | // Display config form: | |
31 | $timezoneForm = ''; | |
32 | $timezoneJs = ''; | |
33 | ||
34 | // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires' | |
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 | ||
63 | $continentsHtml = ''; | |
64 | $continents = array_keys($continents); | |
65 | ||
66 | foreach ($continents as $continent) { | |
67 | $continentsHtml .= '<option value="'.$continent.'"'; | |
68 | if ($pcontinent == $continent) { | |
69 | $continentsHtml .= ' selected="selected"'; | |
70 | } | |
71 | $continentsHtml .= '>'.$continent.'</option>'; | |
72 | } | |
73 | ||
74 | // Timezone selection form | |
75 | $timezoneForm = 'Continent:'; | |
76 | $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">'; | |
77 | $timezoneForm .= $continentsHtml.'</select>'; | |
78 | $timezoneForm .= ' City:'; | |
79 | $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />'; | |
80 | ||
81 | // Javascript handler - updates the city list when the user selects a continent | |
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>'; | |
88 | ||
89 | return array($timezoneForm, $timezoneJs); | |
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 | */ | |
102 | function 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 | } |