]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/TimeZone.php
Minor code cleanup: PHPDoc, spelling, unused variables, etc.
[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 9 *
7af9a418 10 * @param string $preselectedTimezone preselected timezone (optional)
d1e2f8e5 11 *
7af9a418 12 * @return array containing the generated HTML form and Javascript code
d1e2f8e5 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 29
afd7b77b 30 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
d1e2f8e5
V
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
afd7b77b 59 $continentsHtml = '';
d1e2f8e5
V
60 $continents = array_keys($continents);
61
62 foreach ($continents as $continent) {
afd7b77b 63 $continentsHtml .= '<option value="'.$continent.'"';
d1e2f8e5 64 if ($pcontinent == $continent) {
afd7b77b 65 $continentsHtml .= ' selected="selected"';
d1e2f8e5 66 }
afd7b77b 67 $continentsHtml .= '>'.$continent.'</option>';
d1e2f8e5
V
68 }
69
70 // Timezone selection form
afd7b77b
V
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 />';
d1e2f8e5
V
76
77 // Javascript handler - updates the city list when the user selects a continent
afd7b77b
V
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>';
d1e2f8e5 84
afd7b77b 85 return array($timezoneForm, $timezoneJs);
d1e2f8e5
V
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 *
7af9a418 96 * @return bool whether continent/city is a valid timezone
d1e2f8e5
V
97 */
98function isTimeZoneValid($continent, $city)
99{
d1e2f8e5
V
100 return in_array(
101 $continent.'/'.$city,
102 timezone_identifiers_list()
103 );
104}