diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-07-11 01:29:12 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-07-13 13:06:06 +0200 |
commit | d1e2f8e52c931f84c11d4f54f32959710d528182 (patch) | |
tree | be5ad2fcfeb31136e7afca0603a3cd3da3d76b57 /application/TimeZone.php | |
parent | 5b0ebbc5de06b8a0e9679b78b45d0dc755db7986 (diff) | |
download | Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.tar.gz Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.tar.zst Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.zip |
PHP: ensure 5.3 compatibility, refactor timezone utilities
Relates to #250
Modifications
- supported version
- bump required version from 5.1.0 to 5.3.x
- update README
- add PHP 5.3 to Travis environments
- rewrite array declarations: explicitely use array() instead of []
- move checkPHPVersion to application/Utils.php
- move timezone functions to application/TimeZone.php
- cleanup code
- improve test coverage
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application/TimeZone.php')
-rw-r--r-- | application/TimeZone.php | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/application/TimeZone.php b/application/TimeZone.php new file mode 100644 index 00000000..ccbef918 --- /dev/null +++ b/application/TimeZone.php | |||
@@ -0,0 +1,110 @@ | |||
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 | ?> | ||