6 * @subpackage PluginsFunction
12 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
16 require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
19 * Smarty {html_select_date} plugin
21 * Name: html_select_date<br>
22 * Purpose: Prints the dropdowns for date selection.
25 * - 1.0 initial release
26 * - 1.1 added support for +/- N syntax for begin
27 * and end year values. (Monte)
28 * - 1.2 added support for yyyy-mm-dd syntax for
29 * time value. (Jan Rosier)
30 * - 1.3 added support for choosing format for
31 * month values (Gary Loescher)
32 * - 1.3.1 added support for choosing format for
33 * day values (Marcus Bointon)
34 * - 1.3.2 support negative timestamps, force year
35 * dropdown to include given date unless explicitly set (Monte)
36 * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
37 * of 0000-00-00 dates (cybot, boots)
38 * - 2.0 complete rewrite for performance,
39 * added attributes month_names, *_id
42 * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
43 * (Smarty online manual)
45 * @author Andrei Zmievski
46 * @author Monte Ohrt <monte at ohrt dot com>
49 * @param array $params parameters
53 function smarty_function_html_select_date($params)
55 // generate timestamps used for month names only
56 static $_month_timestamps = null;
57 static $_current_year = null;
58 if ($_month_timestamps === null) {
59 $_current_year = date('Y');
60 $_month_timestamps = array();
61 for ($i = 1; $i <= 12; $i ++) {
62 $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000);
71 $display_months = true;
72 $display_years = true;
74 /* Write months as numbers by default GL */
75 $month_value_format = "%m";
77 /* Write day values using this format MB */
78 $day_value_format = "%d";
79 $year_as_text = false;
80 /* Display years in reverse order? Ie. 2000,1999,.... */
81 $reverse_years = false;
82 /* Should the select boxes be part of an array when returned from PHP?
83 e.g. setting it to "birthday", would create "birthday[Day]",
84 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
86 /* <select size>'s of the different <select> tags.
87 If not set, uses default dropdown. */
91 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
92 An example might be in the template: all_extra ='class ="foo"'. */
94 /* Separate attributes for the tags. */
98 /* Order in which to display the fields.
99 "D" -> day, "M" -> month, "Y" -> year. */
100 $field_order = 'MDY';
101 /* String printed between the different fields. */
102 $field_separator = "\n";
103 $option_separator = "\n";
105 // $all_empty = null;
106 // $day_empty = null;
107 // $month_empty = null;
108 // $year_empty = null;
115 foreach ($params as $_key => $_value) {
118 if (!is_array($_value) && $_value !== null) {
119 $time = smarty_make_timestamp($_value);
124 if (is_array($_value) && count($_value) == 12) {
127 trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
136 case 'day_value_format':
138 case 'month_value_format':
147 case 'field_separator':
148 case 'option_separator':
157 $$_key = (string) $_value;
161 case 'display_months':
162 case 'display_years':
164 case 'reverse_years':
165 $$_key = (bool) $_value;
169 if (!is_array($_value)) {
170 $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
172 trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
178 // Note: date() is faster than strftime()
179 // Note: explode(date()) is faster than date() date() date()
180 if (isset($params['time']) && is_array($params['time'])) {
181 if (isset($params['time'][$prefix . 'Year'])) {
182 // $_REQUEST[$field_array] given
183 foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
184 $_variableName = '_' . strtolower($_elementName);
185 $$_variableName = isset($params['time'][$prefix . $_elementName])
186 ? $params['time'][$prefix . $_elementName]
187 : date($_elementKey);
189 } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) {
191 foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
192 $_variableName = '_' . strtolower($_elementName);
193 $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName])
194 ? $params['time'][$field_array][$prefix . $_elementName]
195 : date($_elementKey);
198 // no date found, use NOW
199 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
201 } elseif ($time === null) {
202 if (array_key_exists('time', $params)) {
203 $_year = $_month = $_day = $time = null;
205 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
208 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
211 // make syntax "+N" or "-N" work with $start_year and $end_year
212 // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
213 foreach (array('start', 'end') as $key) {
217 $$key = (int) $_current_year;
218 } elseif ($t[0] == '+') {
219 $$key = (int) ($_current_year + (int)trim(substr($t, 1)));
220 } elseif ($t[0] == '-') {
221 $$key = (int) ($_current_year - (int)trim(substr($t, 1)));
227 // flip for ascending or descending
228 if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
230 $end_year = $start_year;
234 // generate year <select> or <input>
235 if ($display_years) {
237 $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
239 $_extra .= ' ' . $all_extra;
242 $_extra .= ' ' . $year_extra;
246 $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
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)
255 $_html_years .= ' size="' . $year_size . '"';
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;
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;
270 $_html_years .= '</select>';
274 // generate month <select> or <input>
275 if ($display_months) {
277 $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
279 $_extra .= ' ' . $all_extra;
282 $_extra .= ' ' . $month_extra;
285 $_html_months = '<select name="' . $_name . '"';
286 if ($month_id !== null || $all_id !== null) {
287 $_html_months .= ' id="' . smarty_function_escape_special_chars(
288 $month_id !== null ? ($month_id ? $month_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
292 $_html_months .= ' size="' . $month_size . '"';
294 $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
296 if (isset($month_empty) || isset($all_empty)) {
297 $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' . $option_separator;
300 for ($i = 1; $i <= 12; $i ++) {
301 $_val = sprintf('%02d', $i);
302 $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
303 $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
304 $_html_months .= '<option value="' . $_value . '"'
305 . ($_val == $_month ? ' selected="selected"' : '')
306 . '>' . $_text . '</option>' . $option_separator;
309 $_html_months .= '</select>';
312 // generate day <select> or <input>
315 $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
317 $_extra .= ' ' . $all_extra;
320 $_extra .= ' ' . $day_extra;
323 $_html_days = '<select name="' . $_name . '"';
324 if ($day_id !== null || $all_id !== null) {
325 $_html_days .= ' id="' . smarty_function_escape_special_chars(
326 $day_id !== null ? ($day_id ? $day_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
330 $_html_days .= ' size="' . $day_size . '"';
332 $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
334 if (isset($day_empty) || isset($all_empty)) {
335 $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' . $option_separator;
338 for ($i = 1; $i <= 31; $i ++) {
339 $_val = sprintf('%02d', $i);
340 $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
341 $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
342 $_html_days .= '<option value="' . $_value . '"'
343 . ($_val == $_day ? ' selected="selected"' : '')
344 . '>' . $_text . '</option>' . $option_separator;
347 $_html_days .= '</select>';
350 // order the fields for output
352 for ($i = 0; $i <= 2; $i ++) {
353 switch ($field_order[$i]) {
356 if (isset($_html_years)) {
358 $_html .= $field_separator;
360 $_html .= $_html_years;
366 if (isset($_html_months)) {
368 $_html .= $field_separator;
370 $_html .= $_html_months;
376 if (isset($_html_days)) {
378 $_html .= $field_separator;
380 $_html .= $_html_days;