]>
Commit | Line | Data |
---|---|---|
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 | |
8 | * list($htmlform, $js) = templateTZform('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($preselected_timezone='') | |
15 | { | |
16 | // Select the first available timezone if no preselected value is passed | |
17 | if ($preselected_timezone == '') { | |
18 | $l = timezone_identifiers_list(); | |
19 | $preselected_timezone = $l[0]; | |
20 | } | |
21 | ||
22 | // Try to split the provided timezone | |
23 | $spos = strpos($preselected_timezone, '/'); | |
24 | $pcontinent = substr($preselected_timezone, 0, $spos); | |
25 | $pcity = substr($preselected_timezone, $spos+1); | |
26 | ||
27 | // Display config form: | |
28 | $timezone_form = ''; | |
29 | $timezone_js = ''; | |
30 | ||
31 | // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'... | |
32 | // We split the list in continents/cities. | |
33 | $continents = array(); | |
34 | $cities = array(); | |
35 | ||
36 | // TODO: use a template to generate the HTML/Javascript form | |
37 | ||
38 | foreach (timezone_identifiers_list() as $tz) { | |
39 | if ($tz == 'UTC') { | |
40 | $tz = 'UTC/UTC'; | |
41 | } | |
42 | $spos = strpos($tz, '/'); | |
43 | ||
44 | if ($spos !== false) { | |
45 | $continent = substr($tz, 0, $spos); | |
46 | $city = substr($tz, $spos+1); | |
47 | $continents[$continent] = 1; | |
48 | ||
49 | if (!isset($cities[$continent])) { | |
50 | $cities[$continent] = ''; | |
51 | } | |
52 | $cities[$continent] .= '<option value="'.$city.'"'; | |
53 | if ($pcity == $city) { | |
54 | $cities[$continent] .= ' selected="selected"'; | |
55 | } | |
56 | $cities[$continent] .= '>'.$city.'</option>'; | |
57 | } | |
58 | } | |
59 | ||
60 | $continents_html = ''; | |
61 | $continents = array_keys($continents); | |
62 | ||
63 | foreach ($continents as $continent) { | |
64 | $continents_html .= '<option value="'.$continent.'"'; | |
65 | if ($pcontinent == $continent) { | |
66 | $continents_html .= ' selected="selected"'; | |
67 | } | |
68 | $continents_html .= '>'.$continent.'</option>'; | |
69 | } | |
70 | ||
71 | // Timezone selection form | |
72 | $timezone_form = 'Continent:'; | |
73 | $timezone_form .= '<select name="continent" id="continent" onChange="onChangecontinent();">'; | |
74 | $timezone_form .= $continents_html.'</select>'; | |
75 | $timezone_form .= ' City:'; | |
76 | $timezone_form .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />'; | |
77 | ||
78 | // Javascript handler - updates the city list when the user selects a continent | |
79 | $timezone_js = '<script>'; | |
80 | $timezone_js .= 'function onChangecontinent() {'; | |
81 | $timezone_js .= 'document.getElementById("city").innerHTML ='; | |
82 | $timezone_js .= ' citiescontinent[document.getElementById("continent").value]; }'; | |
83 | $timezone_js .= 'var citiescontinent = '.json_encode($cities).';'; | |
84 | $timezone_js .= '</script>'; | |
85 | ||
86 | return array($timezone_form, $timezone_js); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Tells if a continent/city pair form a valid timezone | |
91 | * | |
92 | * Note: 'UTC/UTC' is mapped to 'UTC' | |
93 | * | |
94 | * @param string $continent the timezone continent | |
95 | * @param string $city the timezone city | |
96 | * | |
97 | * @return whether continent/city is a valid timezone | |
98 | */ | |
99 | function isTimeZoneValid($continent, $city) | |
100 | { | |
101 | if ($continent == 'UTC' && $city == 'UTC') { | |
102 | return true; | |
103 | } | |
104 | ||
105 | return in_array( | |
106 | $continent.'/'.$city, | |
107 | timezone_identifiers_list() | |
108 | ); | |
109 | } | |
110 | ?> |