]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/TimeZone.php
3 * Generates the timezone selection form and JavaScript.
5 * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
7 * Example: preselect Europe/Paris
8 * list($htmlform, $js) = templateTZform('Europe/Paris');
10 * @param string $preselected_timezone preselected timezone (optional)
12 * @return an array containing the generated HTML form and Javascript code
14 function generateTimeZoneForm($preselected_timezone='')
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];
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);
27 // Display config form:
31 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'...
32 // We split the list in continents/cities.
33 $continents = array();
36 // TODO: use a template to generate the HTML/Javascript form
38 foreach (timezone_identifiers_list() as $tz) {
42 $spos = strpos($tz, '/');
44 if ($spos !== false) {
45 $continent = substr($tz, 0, $spos);
46 $city = substr($tz, $spos+
1);
47 $continents[$continent] = 1;
49 if (!isset($cities[$continent])) {
50 $cities[$continent] = '';
52 $cities[$continent] .= '<option value="'.$city.'"';
53 if ($pcity == $city) {
54 $cities[$continent] .= ' selected="selected"';
56 $cities[$continent] .= '>'.$city.'</option>';
60 $continents_html = '';
61 $continents = array_keys($continents);
63 foreach ($continents as $continent) {
64 $continents_html .= '<option value="'.$continent.'"';
65 if ($pcontinent == $continent) {
66 $continents_html .= ' selected="selected"';
68 $continents_html .= '>'.$continent.'</option>';
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 />';
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>';
86 return array($timezone_form, $timezone_js);
90 * Tells if a continent/city pair form a valid timezone
92 * Note: 'UTC/UTC' is mapped to 'UTC'
94 * @param string $continent the timezone continent
95 * @param string $city the timezone city
97 * @return whether continent/city is a valid timezone
99 function isTimeZoneValid($continent, $city)
101 if ($continent == 'UTC' && $city == 'UTC') {
106 $continent.'/'.$city,
107 timezone_identifiers_list()