Commit | Line | Data |
---|---|---|
e586807d H |
1 | <?php |
2 | /** | |
3 | * Smarty plugin | |
4 | * @package Smarty | |
5 | * @subpackage plugins | |
6 | */ | |
7 | ||
8 | /** | |
9 | * Smarty {html_select_date} plugin | |
10 | * | |
11 | * Type: function<br> | |
12 | * Name: html_select_date<br> | |
13 | * Purpose: Prints the dropdowns for date selection. | |
14 | * | |
15 | * ChangeLog:<br> | |
16 | * - 1.0 initial release | |
17 | * - 1.1 added support for +/- N syntax for begin | |
18 | * and end year values. (Monte) | |
19 | * - 1.2 added support for yyyy-mm-dd syntax for | |
20 | * time value. (Jan Rosier) | |
21 | * - 1.3 added support for choosing format for | |
22 | * month values (Gary Loescher) | |
23 | * - 1.3.1 added support for choosing format for | |
24 | * day values (Marcus Bointon) | |
b42b2bf9 H |
25 | * - 1.3.2 suppport negative timestamps, force year |
26 | * dropdown to include given date unless explicitly set (Monte) | |
e586807d H |
27 | * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date} |
28 | * (Smarty online manual) | |
b42b2bf9 | 29 | * @version 1.3.2 |
e586807d H |
30 | * @author Andrei Zmievski |
31 | * @param array | |
32 | * @param Smarty | |
33 | * @return string | |
34 | */ | |
35 | function smarty_function_html_select_date($params, &$smarty) | |
36 | { | |
b42b2bf9 | 37 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
e586807d H |
38 | require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); |
39 | require_once $smarty->_get_plugin_filepath('function','html_options'); | |
40 | /* Default values. */ | |
41 | $prefix = "Date_"; | |
42 | $start_year = strftime("%Y"); | |
43 | $end_year = $start_year; | |
44 | $display_days = true; | |
45 | $display_months = true; | |
46 | $display_years = true; | |
47 | $month_format = "%B"; | |
48 | /* Write months as numbers by default GL */ | |
49 | $month_value_format = "%m"; | |
50 | $day_format = "%02d"; | |
51 | /* Write day values using this format MB */ | |
52 | $day_value_format = "%d"; | |
53 | $year_as_text = false; | |
54 | /* Display years in reverse order? Ie. 2000,1999,.... */ | |
55 | $reverse_years = false; | |
56 | /* Should the select boxes be part of an array when returned from PHP? | |
57 | e.g. setting it to "birthday", would create "birthday[Day]", | |
58 | "birthday[Month]" & "birthday[Year]". Can be combined with prefix */ | |
59 | $field_array = null; | |
60 | /* <select size>'s of the different <select> tags. | |
61 | If not set, uses default dropdown. */ | |
62 | $day_size = null; | |
63 | $month_size = null; | |
64 | $year_size = null; | |
65 | /* Unparsed attributes common to *ALL* the <select>/<input> tags. | |
66 | An example might be in the template: all_extra ='class ="foo"'. */ | |
67 | $all_extra = null; | |
68 | /* Separate attributes for the tags. */ | |
69 | $day_extra = null; | |
70 | $month_extra = null; | |
71 | $year_extra = null; | |
72 | /* Order in which to display the fields. | |
73 | "D" -> day, "M" -> month, "Y" -> year. */ | |
b42b2bf9 | 74 | $field_order = 'MDY'; |
e586807d H |
75 | /* String printed between the different fields. */ |
76 | $field_separator = "\n"; | |
b42b2bf9 H |
77 | $time = time(); |
78 | $all_empty = null; | |
79 | $day_empty = null; | |
80 | $month_empty = null; | |
81 | $year_empty = null; | |
82 | $extra_attrs = ''; | |
83 | ||
84 | foreach ($params as $_key=>$_value) { | |
85 | switch ($_key) { | |
86 | case 'prefix': | |
87 | case 'time': | |
88 | case 'start_year': | |
89 | case 'end_year': | |
90 | case 'month_format': | |
91 | case 'day_format': | |
92 | case 'day_value_format': | |
93 | case 'field_array': | |
94 | case 'day_size': | |
95 | case 'month_size': | |
96 | case 'year_size': | |
97 | case 'all_extra': | |
98 | case 'day_extra': | |
99 | case 'month_extra': | |
100 | case 'year_extra': | |
101 | case 'field_order': | |
102 | case 'field_separator': | |
103 | case 'month_value_format': | |
104 | case 'month_empty': | |
105 | case 'day_empty': | |
106 | case 'year_empty': | |
107 | $$_key = (string)$_value; | |
108 | break; | |
109 | ||
110 | case 'all_empty': | |
111 | $$_key = (string)$_value; | |
112 | $day_empty = $month_empty = $year_empty = $all_empty; | |
113 | break; | |
114 | ||
115 | case 'display_days': | |
116 | case 'display_months': | |
117 | case 'display_years': | |
118 | case 'year_as_text': | |
119 | case 'reverse_years': | |
120 | $$_key = (bool)$_value; | |
121 | break; | |
122 | ||
123 | default: | |
124 | if(!is_array($_value)) { | |
125 | $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"'; | |
126 | } else { | |
127 | $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | |
128 | } | |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | if(preg_match('!^-\d+$!',$time)) { | |
134 | // negative timestamp, use date() | |
135 | $time = date('Y-m-d',$time); | |
136 | } | |
137 | // If $time is not in format yyyy-mm-dd | |
138 | if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) { | |
139 | // use smarty_make_timestamp to get an unix timestamp and | |
140 | // strftime to make yyyy-mm-dd | |
141 | $time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); | |
142 | } | |
143 | // Now split this in pieces, which later can be used to set the select | |
144 | $time = explode("-", $time); | |
145 | ||
146 | // make syntax "+N" or "-N" work with start_year and end_year | |
147 | if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { | |
148 | if ($match[1] == '+') { | |
149 | $end_year = strftime('%Y') + $match[2]; | |
150 | } else { | |
151 | $end_year = strftime('%Y') - $match[2]; | |
152 | } | |
153 | } | |
154 | if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { | |
155 | if ($match[1] == '+') { | |
156 | $start_year = strftime('%Y') + $match[2]; | |
157 | } else { | |
158 | $start_year = strftime('%Y') - $match[2]; | |
159 | } | |
160 | } | |
161 | if (strlen($time[0]) > 0) { | |
162 | if ($start_year > $time[0] && !isset($params['start_year'])) { | |
163 | // force start year to include given date if not explicitly set | |
164 | $start_year = $time[0]; | |
165 | } | |
166 | if($end_year < $time[0] && !isset($params['end_year'])) { | |
167 | // force end year to include given date if not explicitly set | |
168 | $end_year = $time[0]; | |
169 | } | |
170 | } | |
e586807d H |
171 | |
172 | $field_order = strtoupper($field_order); | |
173 | ||
174 | $html_result = $month_result = $day_result = $year_result = ""; | |
175 | ||
176 | if ($display_months) { | |
177 | $month_names = array(); | |
178 | $month_values = array(); | |
b42b2bf9 H |
179 | if(isset($month_empty)) { |
180 | $month_names[''] = $month_empty; | |
181 | $month_values[''] = ''; | |
182 | } | |
e586807d | 183 | for ($i = 1; $i <= 12; $i++) { |
b42b2bf9 H |
184 | $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); |
185 | $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); | |
e586807d H |
186 | } |
187 | ||
188 | $month_result .= '<select name='; | |
189 | if (null !== $field_array){ | |
190 | $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"'; | |
191 | } else { | |
192 | $month_result .= '"' . $prefix . 'Month"'; | |
193 | } | |
194 | if (null !== $month_size){ | |
195 | $month_result .= ' size="' . $month_size . '"'; | |
196 | } | |
197 | if (null !== $month_extra){ | |
198 | $month_result .= ' ' . $month_extra; | |
199 | } | |
200 | if (null !== $all_extra){ | |
201 | $month_result .= ' ' . $all_extra; | |
202 | } | |
b42b2bf9 | 203 | $month_result .= $extra_attrs . '>'."\n"; |
e586807d H |
204 | |
205 | $month_result .= smarty_function_html_options(array('output' => $month_names, | |
206 | 'values' => $month_values, | |
b42b2bf9 | 207 | 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '', |
e586807d H |
208 | 'print_result' => false), |
209 | $smarty); | |
e586807d H |
210 | $month_result .= '</select>'; |
211 | } | |
212 | ||
213 | if ($display_days) { | |
214 | $days = array(); | |
b42b2bf9 H |
215 | if (isset($day_empty)) { |
216 | $days[''] = $day_empty; | |
217 | $day_values[''] = ''; | |
218 | } | |
e586807d H |
219 | for ($i = 1; $i <= 31; $i++) { |
220 | $days[] = sprintf($day_format, $i); | |
221 | $day_values[] = sprintf($day_value_format, $i); | |
222 | } | |
223 | ||
224 | $day_result .= '<select name='; | |
225 | if (null !== $field_array){ | |
226 | $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"'; | |
227 | } else { | |
228 | $day_result .= '"' . $prefix . 'Day"'; | |
229 | } | |
230 | if (null !== $day_size){ | |
231 | $day_result .= ' size="' . $day_size . '"'; | |
232 | } | |
233 | if (null !== $all_extra){ | |
234 | $day_result .= ' ' . $all_extra; | |
235 | } | |
236 | if (null !== $day_extra){ | |
237 | $day_result .= ' ' . $day_extra; | |
238 | } | |
b42b2bf9 | 239 | $day_result .= $extra_attrs . '>'."\n"; |
e586807d H |
240 | $day_result .= smarty_function_html_options(array('output' => $days, |
241 | 'values' => $day_values, | |
242 | 'selected' => $time[2], | |
243 | 'print_result' => false), | |
244 | $smarty); | |
245 | $day_result .= '</select>'; | |
246 | } | |
247 | ||
248 | if ($display_years) { | |
249 | if (null !== $field_array){ | |
250 | $year_name = $field_array . '[' . $prefix . 'Year]'; | |
251 | } else { | |
252 | $year_name = $prefix . 'Year'; | |
253 | } | |
254 | if ($year_as_text) { | |
255 | $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"'; | |
256 | if (null !== $all_extra){ | |
257 | $year_result .= ' ' . $all_extra; | |
258 | } | |
259 | if (null !== $year_extra){ | |
260 | $year_result .= ' ' . $year_extra; | |
261 | } | |
b42b2bf9 | 262 | $year_result .= ' />'; |
e586807d H |
263 | } else { |
264 | $years = range((int)$start_year, (int)$end_year); | |
265 | if ($reverse_years) { | |
266 | rsort($years, SORT_NUMERIC); | |
b42b2bf9 H |
267 | } else { |
268 | sort($years, SORT_NUMERIC); | |
269 | } | |
270 | $yearvals = $years; | |
271 | if(isset($year_empty)) { | |
272 | array_unshift($years, $year_empty); | |
273 | array_unshift($yearvals, ''); | |
e586807d | 274 | } |
e586807d H |
275 | $year_result .= '<select name="' . $year_name . '"'; |
276 | if (null !== $year_size){ | |
277 | $year_result .= ' size="' . $year_size . '"'; | |
278 | } | |
279 | if (null !== $all_extra){ | |
280 | $year_result .= ' ' . $all_extra; | |
281 | } | |
282 | if (null !== $year_extra){ | |
283 | $year_result .= ' ' . $year_extra; | |
284 | } | |
b42b2bf9 | 285 | $year_result .= $extra_attrs . '>'."\n"; |
e586807d | 286 | $year_result .= smarty_function_html_options(array('output' => $years, |
b42b2bf9 | 287 | 'values' => $yearvals, |
e586807d H |
288 | 'selected' => $time[0], |
289 | 'print_result' => false), | |
290 | $smarty); | |
291 | $year_result .= '</select>'; | |
292 | } | |
293 | } | |
294 | ||
295 | // Loop thru the field_order field | |
296 | for ($i = 0; $i <= 2; $i++){ | |
b42b2bf9 H |
297 | $c = substr($field_order, $i, 1); |
298 | switch ($c){ | |
299 | case 'D': | |
300 | $html_result .= $day_result; | |
301 | break; | |
302 | ||
303 | case 'M': | |
304 | $html_result .= $month_result; | |
305 | break; | |
306 | ||
307 | case 'Y': | |
308 | $html_result .= $year_result; | |
309 | break; | |
310 | } | |
311 | // Add the field seperator | |
312 | if($i != 2) { | |
313 | $html_result .= $field_separator; | |
314 | } | |
e586807d H |
315 | } |
316 | ||
317 | return $html_result; | |
318 | } | |
319 | ||
320 | /* vim: set expandtab: */ | |
321 | ||
322 | ?> |