1 | <?php |
---|
2 | /** |
---|
3 | * Smarty plugin |
---|
4 | * |
---|
5 | * @package Smarty |
---|
6 | * @subpackage PluginsFunction |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @ignore |
---|
11 | */ |
---|
12 | require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); |
---|
13 | /** |
---|
14 | * @ignore |
---|
15 | */ |
---|
16 | require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'); |
---|
17 | |
---|
18 | /** |
---|
19 | * Smarty {html_select_date} plugin |
---|
20 | * |
---|
21 | * Type: function<br> |
---|
22 | * Name: html_select_date<br> |
---|
23 | * Purpose: Prints the dropdowns for date selection. |
---|
24 | * |
---|
25 | * ChangeLog: |
---|
26 | * <pre> |
---|
27 | * - 1.0 initial release |
---|
28 | * - 1.1 added support for +/- N syntax for begin |
---|
29 | * and end year values. (Monte) |
---|
30 | * - 1.2 added support for yyyy-mm-dd syntax for |
---|
31 | * time value. (Jan Rosier) |
---|
32 | * - 1.3 added support for choosing format for |
---|
33 | * month values (Gary Loescher) |
---|
34 | * - 1.3.1 added support for choosing format for |
---|
35 | * day values (Marcus Bointon) |
---|
36 | * - 1.3.2 support negative timestamps, force year |
---|
37 | * dropdown to include given date unless explicitly set (Monte) |
---|
38 | * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that |
---|
39 | * of 0000-00-00 dates (cybot, boots) |
---|
40 | * - 2.0 complete rewrite for performance, |
---|
41 | * added attributes month_names, *_id |
---|
42 | * </pre> |
---|
43 | * |
---|
44 | * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date} |
---|
45 | * (Smarty online manual) |
---|
46 | * @version 2.0 |
---|
47 | * @author Andrei Zmievski |
---|
48 | * @author Monte Ohrt <monte at ohrt dot com> |
---|
49 | * @author Rodney Rehm |
---|
50 | * @param array $params parameters |
---|
51 | * @param Smarty_Internal_Template $template template object |
---|
52 | * @return string |
---|
53 | */ |
---|
54 | function smarty_function_html_select_date($params, $template) |
---|
55 | { |
---|
56 | // generate timestamps used for month names only |
---|
57 | static $_month_timestamps = null; |
---|
58 | static $_current_year = null; |
---|
59 | if ($_month_timestamps === null) { |
---|
60 | $_current_year = date('Y'); |
---|
61 | $_month_timestamps = array(); |
---|
62 | for ($i = 1; $i <= 12; $i++) { |
---|
63 | $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /* Default values. */ |
---|
68 | $prefix = "Date_"; |
---|
69 | $start_year = null; |
---|
70 | $end_year = null; |
---|
71 | $display_days = true; |
---|
72 | $display_months = true; |
---|
73 | $display_years = true; |
---|
74 | $month_format = "%B"; |
---|
75 | /* Write months as numbers by default GL */ |
---|
76 | $month_value_format = "%m"; |
---|
77 | $day_format = "%02d"; |
---|
78 | /* Write day values using this format MB */ |
---|
79 | $day_value_format = "%d"; |
---|
80 | $year_as_text = false; |
---|
81 | /* Display years in reverse order? Ie. 2000,1999,.... */ |
---|
82 | $reverse_years = false; |
---|
83 | /* Should the select boxes be part of an array when returned from PHP? |
---|
84 | e.g. setting it to "birthday", would create "birthday[Day]", |
---|
85 | "birthday[Month]" & "birthday[Year]". Can be combined with prefix */ |
---|
86 | $field_array = null; |
---|
87 | /* <select size>'s of the different <select> tags. |
---|
88 | If not set, uses default dropdown. */ |
---|
89 | $day_size = null; |
---|
90 | $month_size = null; |
---|
91 | $year_size = null; |
---|
92 | /* Unparsed attributes common to *ALL* the <select>/<input> tags. |
---|
93 | An example might be in the template: all_extra ='class ="foo"'. */ |
---|
94 | $all_extra = null; |
---|
95 | /* Separate attributes for the tags. */ |
---|
96 | $day_extra = null; |
---|
97 | $month_extra = null; |
---|
98 | $year_extra = null; |
---|
99 | /* Order in which to display the fields. |
---|
100 | "D" -> day, "M" -> month, "Y" -> year. */ |
---|
101 | $field_order = 'MDY'; |
---|
102 | /* String printed between the different fields. */ |
---|
103 | $field_separator = "\n"; |
---|
104 | $option_separator = "\n"; |
---|
105 | $time = null; |
---|
106 | // $all_empty = null; |
---|
107 | // $day_empty = null; |
---|
108 | // $month_empty = null; |
---|
109 | // $year_empty = null; |
---|
110 | $extra_attrs = ''; |
---|
111 | $all_id = null; |
---|
112 | $day_id = null; |
---|
113 | $month_id = null; |
---|
114 | $year_id = null; |
---|
115 | |
---|
116 | foreach ($params as $_key => $_value) { |
---|
117 | switch ($_key) { |
---|
118 | case 'time': |
---|
119 | if (!is_array($_value) && $_value !== null) { |
---|
120 | $time = smarty_make_timestamp($_value); |
---|
121 | } |
---|
122 | break; |
---|
123 | |
---|
124 | case 'month_names': |
---|
125 | if (is_array($_value) && count($_value) == 12) { |
---|
126 | $$_key = $_value; |
---|
127 | } else { |
---|
128 | trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE); |
---|
129 | } |
---|
130 | break; |
---|
131 | |
---|
132 | case 'prefix': |
---|
133 | case 'field_array': |
---|
134 | case 'start_year': |
---|
135 | case 'end_year': |
---|
136 | case 'day_format': |
---|
137 | case 'day_value_format': |
---|
138 | case 'month_format': |
---|
139 | case 'month_value_format': |
---|
140 | case 'day_size': |
---|
141 | case 'month_size': |
---|
142 | case 'year_size': |
---|
143 | case 'all_extra': |
---|
144 | case 'day_extra': |
---|
145 | case 'month_extra': |
---|
146 | case 'year_extra': |
---|
147 | case 'field_order': |
---|
148 | case 'field_separator': |
---|
149 | case 'option_separator': |
---|
150 | case 'all_empty': |
---|
151 | case 'month_empty': |
---|
152 | case 'day_empty': |
---|
153 | case 'year_empty': |
---|
154 | case 'all_id': |
---|
155 | case 'month_id': |
---|
156 | case 'day_id': |
---|
157 | case 'year_id': |
---|
158 | $$_key = (string)$_value; |
---|
159 | break; |
---|
160 | |
---|
161 | case 'display_days': |
---|
162 | case 'display_months': |
---|
163 | case 'display_years': |
---|
164 | case 'year_as_text': |
---|
165 | case 'reverse_years': |
---|
166 | $$_key = (bool)$_value; |
---|
167 | break; |
---|
168 | |
---|
169 | default: |
---|
170 | if (!is_array($_value)) { |
---|
171 | $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"'; |
---|
172 | } else { |
---|
173 | trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
---|
174 | } |
---|
175 | break; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | // Note: date() is faster than strftime() |
---|
180 | // Note: explode(date()) is faster than date() date() date() |
---|
181 | if (isset($params['time']) && is_array($params['time'])) { |
---|
182 | if (isset($params['time'][$prefix . 'Year'])) { |
---|
183 | // $_REQUEST[$field_array] given |
---|
184 | foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) { |
---|
185 | $_variableName = '_' . strtolower($_elementName); |
---|
186 | $$_variableName = isset($params['time'][$prefix . $_elementName]) |
---|
187 | ? $params['time'][$prefix . $_elementName] |
---|
188 | : date($_elementKey); |
---|
189 | } |
---|
190 | $time = mktime(0, 0, 0, $_month, $_day, $_year); |
---|
191 | } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) { |
---|
192 | // $_REQUEST given |
---|
193 | foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) { |
---|
194 | $_variableName = '_' . strtolower($_elementName); |
---|
195 | $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName]) |
---|
196 | ? $params['time'][$field_array][$prefix . $_elementName] |
---|
197 | : date($_elementKey); |
---|
198 | } |
---|
199 | $time = mktime(0, 0, 0, $_month, $_day, $_year); |
---|
200 | } else { |
---|
201 | // no date found, use NOW |
---|
202 | list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d')); |
---|
203 | } |
---|
204 | } elseif ($time === null) { |
---|
205 | if (array_key_exists('time', $params)) { |
---|
206 | $_year = $_month = $_day = $time = null; |
---|
207 | } else { |
---|
208 | list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d')); |
---|
209 | } |
---|
210 | } else { |
---|
211 | list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time)); |
---|
212 | } |
---|
213 | |
---|
214 | // make syntax "+N" or "-N" work with $start_year and $end_year |
---|
215 | // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr |
---|
216 | foreach (array('start', 'end') as $key) { |
---|
217 | $key .= '_year'; |
---|
218 | $t = $$key; |
---|
219 | if ($t === null) { |
---|
220 | $$key = (int)$_current_year; |
---|
221 | } else if ($t[0] == '+') { |
---|
222 | $$key = (int)($_current_year + trim(substr($t, 1))); |
---|
223 | } else if ($t[0] == '-') { |
---|
224 | $$key = (int)($_current_year - trim(substr($t, 1))); |
---|
225 | } else { |
---|
226 | $$key = (int)$$key; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | // flip for ascending or descending |
---|
231 | if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) { |
---|
232 | $t = $end_year; |
---|
233 | $end_year = $start_year; |
---|
234 | $start_year = $t; |
---|
235 | } |
---|
236 | |
---|
237 | // generate year <select> or <input> |
---|
238 | if ($display_years) { |
---|
239 | $_html_years = ''; |
---|
240 | $_extra = ''; |
---|
241 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year'); |
---|
242 | if ($all_extra) { |
---|
243 | $_extra .= ' ' . $all_extra; |
---|
244 | } |
---|
245 | if ($year_extra) { |
---|
246 | $_extra .= ' ' . $year_extra; |
---|
247 | } |
---|
248 | |
---|
249 | if ($year_as_text) { |
---|
250 | $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />'; |
---|
251 | } else { |
---|
252 | $_html_years = '<select name="' . $_name . '"'; |
---|
253 | if ($year_id !== null || $all_id !== null) { |
---|
254 | $_html_years .= ' id="' . smarty_function_escape_special_chars( |
---|
255 | $year_id !== null ? ( $year_id ? $year_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name ) |
---|
256 | ) . '"'; |
---|
257 | } |
---|
258 | if ($year_size) { |
---|
259 | $_html_years .= ' size="' . $year_size . '"'; |
---|
260 | } |
---|
261 | $_html_years .= $_extra . $extra_attrs . '>' . $option_separator; |
---|
262 | |
---|
263 | if (isset($year_empty) || isset($all_empty)) { |
---|
264 | $_html_years .= '<option value="">' . ( isset($year_empty) ? $year_empty : $all_empty ) . '</option>' . $option_separator; |
---|
265 | } |
---|
266 | |
---|
267 | $op = $start_year > $end_year ? -1 : 1; |
---|
268 | for ($i=$start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) { |
---|
269 | $_html_years .= '<option value="' . $i . '"' |
---|
270 | . ($_year == $i ? ' selected="selected"' : '') |
---|
271 | . '>' . $i . '</option>' . $option_separator; |
---|
272 | } |
---|
273 | |
---|
274 | $_html_years .= '</select>'; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | // generate month <select> or <input> |
---|
279 | if ($display_months) { |
---|
280 | $_html_month = ''; |
---|
281 | $_extra = ''; |
---|
282 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month'); |
---|
283 | if ($all_extra) { |
---|
284 | $_extra .= ' ' . $all_extra; |
---|
285 | } |
---|
286 | if ($month_extra) { |
---|
287 | $_extra .= ' ' . $month_extra; |
---|
288 | } |
---|
289 | |
---|
290 | $_html_months = '<select name="' . $_name . '"'; |
---|
291 | if ($month_id !== null || $all_id !== null) { |
---|
292 | $_html_months .= ' id="' . smarty_function_escape_special_chars( |
---|
293 | $month_id !== null ? ( $month_id ? $month_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name ) |
---|
294 | ) . '"'; |
---|
295 | } |
---|
296 | if ($month_size) { |
---|
297 | $_html_months .= ' size="' . $month_size . '"'; |
---|
298 | } |
---|
299 | $_html_months .= $_extra . $extra_attrs . '>' . $option_separator; |
---|
300 | |
---|
301 | if (isset($month_empty) || isset($all_empty)) { |
---|
302 | $_html_months .= '<option value="">' . ( isset($month_empty) ? $month_empty : $all_empty ) . '</option>' . $option_separator; |
---|
303 | } |
---|
304 | |
---|
305 | for ($i = 1; $i <= 12; $i++) { |
---|
306 | $_val = sprintf('%02d', $i); |
---|
307 | $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i])); |
---|
308 | $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]); |
---|
309 | $_html_months .= '<option value="' . $_value . '"' |
---|
310 | . ($_val == $_month ? ' selected="selected"' : '') |
---|
311 | . '>' . $_text . '</option>' . $option_separator; |
---|
312 | } |
---|
313 | |
---|
314 | $_html_months .= '</select>'; |
---|
315 | } |
---|
316 | |
---|
317 | // generate day <select> or <input> |
---|
318 | if ($display_days) { |
---|
319 | $_html_day = ''; |
---|
320 | $_extra = ''; |
---|
321 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day'); |
---|
322 | if ($all_extra) { |
---|
323 | $_extra .= ' ' . $all_extra; |
---|
324 | } |
---|
325 | if ($day_extra) { |
---|
326 | $_extra .= ' ' . $day_extra; |
---|
327 | } |
---|
328 | |
---|
329 | $_html_days = '<select name="' . $_name . '"'; |
---|
330 | if ($day_id !== null || $all_id !== null) { |
---|
331 | $_html_days .= ' id="' . smarty_function_escape_special_chars( |
---|
332 | $day_id !== null ? ( $day_id ? $day_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name ) |
---|
333 | ) . '"'; |
---|
334 | } |
---|
335 | if ($day_size) { |
---|
336 | $_html_days .= ' size="' . $day_size . '"'; |
---|
337 | } |
---|
338 | $_html_days .= $_extra . $extra_attrs . '>' . $option_separator; |
---|
339 | |
---|
340 | if (isset($day_empty) || isset($all_empty)) { |
---|
341 | $_html_days .= '<option value="">' . ( isset($day_empty) ? $day_empty : $all_empty ) . '</option>' . $option_separator; |
---|
342 | } |
---|
343 | |
---|
344 | for ($i = 1; $i <= 31; $i++) { |
---|
345 | $_val = sprintf('%02d', $i); |
---|
346 | $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i); |
---|
347 | $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i); |
---|
348 | $_html_days .= '<option value="' . $_value . '"' |
---|
349 | . ($_val == $_day ? ' selected="selected"' : '') |
---|
350 | . '>' . $_text . '</option>' . $option_separator; |
---|
351 | } |
---|
352 | |
---|
353 | $_html_days .= '</select>'; |
---|
354 | } |
---|
355 | |
---|
356 | // order the fields for output |
---|
357 | $_html = ''; |
---|
358 | for ($i=0; $i <= 2; $i++) { |
---|
359 | switch ($field_order[$i]) { |
---|
360 | case 'Y': |
---|
361 | case 'y': |
---|
362 | if (isset($_html_years)) { |
---|
363 | if ($_html) { |
---|
364 | $_html .= $field_separator; |
---|
365 | } |
---|
366 | $_html .= $_html_years; |
---|
367 | } |
---|
368 | break; |
---|
369 | |
---|
370 | case 'm': |
---|
371 | case 'M': |
---|
372 | if (isset($_html_months)) { |
---|
373 | if ($_html) { |
---|
374 | $_html .= $field_separator; |
---|
375 | } |
---|
376 | $_html .= $_html_months; |
---|
377 | } |
---|
378 | break; |
---|
379 | |
---|
380 | case 'd': |
---|
381 | case 'D': |
---|
382 | if (isset($_html_days)) { |
---|
383 | if ($_html) { |
---|
384 | $_html .= $field_separator; |
---|
385 | } |
---|
386 | $_html .= $_html_days; |
---|
387 | } |
---|
388 | break; |
---|
389 | } |
---|
390 | } |
---|
391 | return $_html; |
---|
392 | } |
---|
393 | |
---|
394 | ?> |
---|