1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
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');
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 }
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;
116 foreach ($params as $_key => $_value) {
117 switch ($_key) {
118 case 'time':
119 if (!is_array($_value)) {
120 $time = smarty_make_timestamp($_value);
121 }
122 break;
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;
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;
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;
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 }
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 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
206 } else {
207 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
208 }
210 // make syntax "+N" or "-N" work with $start_year and $end_year
211 // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
212 foreach (array('start', 'end') as $key) {
213 $key .= '_year';
214 $t = $$key;
215 if ($t === null) {
216 $$key = (int)$_current_year;
217 } else if ($t[0] == '+') {
218 $$key = (int)($_current_year + trim(substr($t, 1)));
219 } else if ($t[0] == '-') {
220 $$key = (int)($_current_year - trim(substr($t, 1)));
221 } else {
222 $$key = (int)$$key;
223 }
224 }
226 // flip for ascending or descending
227 if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
228 $t = $end_year;
229 $end_year = $start_year;
230 $start_year = $t;
231 }
233 // generate year <select> or <input>
234 if ($display_years) {
235 $_html_years = '';
236 $_extra = '';
237 $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
238 if ($all_extra) {
239 $_extra .= ' ' . $all_extra;
240 }
241 if ($year_extra) {
242 $_extra .= ' ' . $year_extra;
243 }
245 if ($year_as_text) {
246 $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
247 } else {
248 $_html_years = '<select name="' . $_name . '"';
249 if ($year_id !== null || $all_id !== null) {
250 $_html_years .= ' id="' . smarty_function_escape_special_chars(
251 $year_id !== null ? ( $year_id ? $year_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
252 ) . '"';
253 }
254 if ($year_size) {
255 $_html_years .= ' size="' . $year_size . '"';
256 }
257 $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
259 if (isset($year_empty) || isset($all_empty)) {
260 $_html_years .= '<option value="">' . ( isset($year_empty) ? $year_empty : $all_empty ) . '</option>' . $option_separator;
261 }
263 $op = $start_year > $end_year ? -1 : 1;
264 for ($i=$start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
265 $_html_years .= '<option value="' . $i . '"'
266 . ($_year == $i ? ' selected="selected"' : '')
267 . '>' . $i . '</option>' . $option_separator;
268 }
270 $_html_years .= '</select>';
271 }
272 }
274 // generate month <select> or <input>
275 if ($display_months) {
276 $_html_month = '';
277 $_extra = '';
278 $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
279 if ($all_extra) {
280 $_extra .= ' ' . $all_extra;
281 }
282 if ($month_extra) {
283 $_extra .= ' ' . $month_extra;
284 }
286 $_html_months = '<select name="' . $_name . '"';
287 if ($month_id !== null || $all_id !== null) {
288 $_html_months .= ' id="' . smarty_function_escape_special_chars(
289 $month_id !== null ? ( $month_id ? $month_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
290 ) . '"';
291 }
292 if ($month_size) {
293 $_html_months .= ' size="' . $month_size . '"';
294 }
295 $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
297 if (isset($month_empty) || isset($all_empty)) {
298 $_html_months .= '<option value="">' . ( isset($month_empty) ? $month_empty : $all_empty ) . '</option>' . $option_separator;
299 }
301 for ($i = 1; $i <= 12; $i++) {
302 $_val = sprintf('%02d', $i);
303 $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
304 $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
305 $_html_months .= '<option value="' . $_value . '"'
306 . ($_val == $_month ? ' selected="selected"' : '')
307 . '>' . $_text . '</option>' . $option_separator;
308 }
310 $_html_months .= '</select>';
311 }
313 // generate day <select> or <input>
314 if ($display_days) {
315 $_html_day = '';
316 $_extra = '';
317 $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
318 if ($all_extra) {
319 $_extra .= ' ' . $all_extra;
320 }
321 if ($day_extra) {
322 $_extra .= ' ' . $day_extra;
323 }
325 $_html_days = '<select name="' . $_name . '"';
326 if ($day_id !== null || $all_id !== null) {
327 $_html_days .= ' id="' . smarty_function_escape_special_chars(
328 $day_id !== null ? ( $day_id ? $day_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
329 ) . '"';
330 }
331 if ($day_size) {
332 $_html_days .= ' size="' . $day_size . '"';
333 }
334 $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
336 if (isset($day_empty) || isset($all_empty)) {
337 $_html_days .= '<option value="">' . ( isset($day_empty) ? $day_empty : $all_empty ) . '</option>' . $option_separator;
338 }
340 for ($i = 1; $i <= 31; $i++) {
341 $_val = sprintf('%02d', $i);
342 $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
343 $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
344 $_html_days .= '<option value="' . $_value . '"'
345 . ($_val == $_day ? ' selected="selected"' : '')
346 . '>' . $_text . '</option>' . $option_separator;
347 }
349 $_html_days .= '</select>';
350 }
352 // order the fields for output
353 $_html = '';
354 for ($i=0; $i <= 2; $i++) {
355 switch ($field_order[$i]) {
356 case 'Y':
357 case 'y':
358 if (isset($_html_years)) {
359 if ($_html) {
360 $_html .= $field_separator;
361 }
362 $_html .= $_html_years;
363 }
364 break;
366 case 'm':
367 case 'M':
368 if (isset($_html_months)) {
369 if ($_html) {
370 $_html .= $field_separator;
371 }
372 $_html .= $_html_months;
373 }
374 break;
376 case 'd':
377 case 'D':
378 if (isset($_html_days)) {
379 if ($_html) {
380 $_html .= $field_separator;
381 }
382 $_html .= $_html_days;
383 }
384 break;
385 }
386 }
387 return $_html;
388 }
390 ?>