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
22 * Name: html_select_date<br>
23 * Purpose: Prints the dropdowns for date selection.
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
44 * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
45 * (Smarty online manual)
47 * @author Andrei Zmievski
48 * @author Monte Ohrt <monte at ohrt dot com>
50 * @param array $params parameters
51 * @param Smarty_Internal_Template $template template object
54 function smarty_function_html_select_date($params, $template)
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);
72 $display_months = true;
73 $display_years = true;
75 /* Write months as numbers by default GL */
76 $month_value_format = "%m";
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 */
87 /* <select size>'s of the different <select> tags.
88 If not set, uses default dropdown. */
92 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
93 An example might be in the template: all_extra ='class ="foo"'. */
95 /* Separate attributes for the tags. */
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";
106 // $all_empty = null;
107 // $day_empty = null;
108 // $month_empty = null;
109 // $year_empty = null;
116 foreach ($params as $_key => $_value) {
119 if (!is_array($_value) && $_value !== null) {
120 $time = smarty_make_timestamp($_value);
125 if (is_array($_value) && count($_value) == 12) {
128 trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
137 case 'day_value_format':
139 case 'month_value_format':
148 case 'field_separator':
149 case 'option_separator':
158 $$_key = (string)$_value;
162 case 'display_months':
163 case 'display_years':
165 case 'reverse_years':
166 $$_key = (bool)$_value;
170 if (!is_array($_value)) {
171 $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
173 trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
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);
190 $time = mktime(0, 0, 0, $_month, $_day, $_year);
191 } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) {
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);
199 $time = mktime(0, 0, 0, $_month, $_day, $_year);
201 // no date found, use NOW
202 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
204 } elseif ($time === null) {
205 if (array_key_exists('time', $params)) {
206 $_year = $_month = $_day = $time = null;
208 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
211 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
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) {
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)));
230 // flip for ascending or descending
231 if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
233 $end_year = $start_year;
237 // generate year <select> or <input>
238 if ($display_years) {
241 $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
243 $_extra .= ' ' . $all_extra;
246 $_extra .= ' ' . $year_extra;
250 $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
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 )
259 $_html_years .= ' size="' . $year_size . '"';
261 $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
263 if (isset($year_empty) || isset($all_empty)) {
264 $_html_years .= '<option value="">' . ( isset($year_empty) ? $year_empty : $all_empty ) . '</option>' . $option_separator;
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;
274 $_html_years .= '</select>';
278 // generate month <select> or <input>
279 if ($display_months) {
282 $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
284 $_extra .= ' ' . $all_extra;
287 $_extra .= ' ' . $month_extra;
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 )
297 $_html_months .= ' size="' . $month_size . '"';
299 $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
301 if (isset($month_empty) || isset($all_empty)) {
302 $_html_months .= '<option value="">' . ( isset($month_empty) ? $month_empty : $all_empty ) . '</option>' . $option_separator;
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;
314 $_html_months .= '</select>';
317 // generate day <select> or <input>
321 $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
323 $_extra .= ' ' . $all_extra;
326 $_extra .= ' ' . $day_extra;
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 )
336 $_html_days .= ' size="' . $day_size . '"';
338 $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
340 if (isset($day_empty) || isset($all_empty)) {
341 $_html_days .= '<option value="">' . ( isset($day_empty) ? $day_empty : $all_empty ) . '</option>' . $option_separator;
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;
353 $_html_days .= '</select>';
356 // order the fields for output
358 for ($i=0; $i <= 2; $i++) {
359 switch ($field_order[$i]) {
362 if (isset($_html_years)) {
364 $_html .= $field_separator;
366 $_html .= $_html_years;
372 if (isset($_html_months)) {
374 $_html .= $field_separator;
376 $_html .= $_html_months;
382 if (isset($_html_days)) {
384 $_html .= $field_separator;
386 $_html .= $_html_days;