6 * @subpackage PluginsFunction
10 * Smarty {html_select_date} plugin
12 * Name: html_select_date<br>
13 * Purpose: Prints the dropdowns for date selection.
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)
25 * - 1.3.2 support negative timestamps, force year
26 * dropdown to include given date unless explicitly set (Monte)
27 * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
28 * of 0000-00-00 dates (cybot, boots)
29 * - 2.0 complete rewrite for performance,
30 * added attributes month_names, *_id
33 * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
34 * (Smarty online manual)
36 * @author Andrei Zmievski
37 * @author Monte Ohrt <monte at ohrt dot com>
40 * @param array $params parameters
44 function smarty_function_html_select_date($params)
46 if (!is_callable('smarty_function_escape_special_chars')) {
47 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
49 if (!is_callable('smarty_make_timestamp')) {
50 require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
52 // generate timestamps used for month names only
53 static $_month_timestamps = null;
54 static $_current_year = null;
55 if ($_month_timestamps === null) {
56 $_current_year = date('Y');
57 $_month_timestamps = array();
58 for ($i = 1; $i <= 12; $i ++) {
59 $_month_timestamps[ $i ] = mktime(0, 0, 0, $i, 1, 2000);
68 $display_months = true;
69 $display_years = true;
71 /* Write months as numbers by default GL */
72 $month_value_format = "%m";
74 /* Write day values using this format MB */
75 $day_value_format = "%d";
76 $year_as_text = false;
77 /* Display years in reverse order? Ie. 2000,1999,.... */
78 $reverse_years = false;
79 /* Should the select boxes be part of an array when returned from PHP?
80 e.g. setting it to "birthday", would create "birthday[Day]",
81 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
83 /* <select size>'s of the different <select> tags.
84 If not set, uses default dropdown. */
88 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
89 An example might be in the template: all_extra ='class ="foo"'. */
91 /* Separate attributes for the tags. */
95 /* Order in which to display the fields.
96 "D" -> day, "M" -> month, "Y" -> year. */
98 /* String printed between the different fields. */
99 $field_separator = "\n";
100 $option_separator = "\n";
102 // $all_empty = null;
103 // $day_empty = null;
104 // $month_empty = null;
105 // $year_empty = null;
112 foreach ($params as $_key => $_value) {
115 if (!is_array($_value) && $_value !== null) {
116 $time = smarty_make_timestamp($_value);
121 if (is_array($_value) && count($_value) == 12) {
124 trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
133 case 'day_value_format':
135 case 'month_value_format':
144 case 'field_separator':
145 case 'option_separator':
154 $$_key = (string) $_value;
158 case 'display_months':
159 case 'display_years':
161 case 'reverse_years':
162 $$_key = (bool) $_value;
166 if (!is_array($_value)) {
167 $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
169 trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
175 // Note: date() is faster than strftime()
176 // Note: explode(date()) is faster than date() date() date()
177 if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) {
178 if (isset($params[ 'time' ][ $prefix . 'Year' ])) {
179 // $_REQUEST[$field_array] given
180 foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
181 $_variableName = '_' . strtolower($_elementName);
183 isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] :
186 } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) {
188 foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
189 $_variableName = '_' . strtolower($_elementName);
190 $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ?
191 $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey);
194 // no date found, use NOW
195 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
197 } elseif ($time === null) {
198 if (array_key_exists('time', $params)) {
199 $_year = $_month = $_day = $time = null;
201 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
204 list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
207 // make syntax "+N" or "-N" work with $start_year and $end_year
208 // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
209 foreach (array('start', 'end') as $key) {
213 $$key = (int) $_current_year;
214 } elseif ($t[ 0 ] == '+') {
215 $$key = (int) ($_current_year + (int) trim(substr($t, 1)));
216 } elseif ($t[ 0 ] == '-') {
217 $$key = (int) ($_current_year - (int) trim(substr($t, 1)));
223 // flip for ascending or descending
224 if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
226 $end_year = $start_year;
230 // generate year <select> or <input>
231 if ($display_years) {
233 $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
235 $_extra .= ' ' . $all_extra;
238 $_extra .= ' ' . $year_extra;
243 '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra .
244 $extra_attrs . ' />';
246 $_html_years = '<select name="' . $_name . '"';
247 if ($year_id !== null || $all_id !== null) {
248 $_html_years .= ' id="' . smarty_function_escape_special_chars($year_id !== null ?
249 ($year_id ? $year_id : $_name) :
250 ($all_id ? ($all_id . $_name) :
254 $_html_years .= ' size="' . $year_size . '"';
256 $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
258 if (isset($year_empty) || isset($all_empty)) {
259 $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' .
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 . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i .
266 '</option>' . $option_separator;
269 $_html_years .= '</select>';
273 // generate month <select> or <input>
274 if ($display_months) {
276 $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
278 $_extra .= ' ' . $all_extra;
281 $_extra .= ' ' . $month_extra;
284 $_html_months = '<select name="' . $_name . '"';
285 if ($month_id !== null || $all_id !== null) {
286 $_html_months .= ' id="' . smarty_function_escape_special_chars($month_id !== null ?
287 ($month_id ? $month_id : $_name) :
288 ($all_id ? ($all_id . $_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>' .
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 ]) :
304 ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[ $i ]));
305 $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[ $i ]);
306 $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') .
307 '>' . $_text . '</option>' . $option_separator;
310 $_html_months .= '</select>';
313 // generate day <select> or <input>
316 $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
318 $_extra .= ' ' . $all_extra;
321 $_extra .= ' ' . $day_extra;
324 $_html_days = '<select name="' . $_name . '"';
325 if ($day_id !== null || $all_id !== null) {
326 $_html_days .= ' id="' .
327 smarty_function_escape_special_chars($day_id !== null ? ($day_id ? $day_id : $_name) :
328 ($all_id ? ($all_id . $_name) : $_name)) . '"';
331 $_html_days .= ' size="' . $day_size . '"';
333 $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
335 if (isset($day_empty) || isset($all_empty)) {
336 $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' .
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 . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' .
345 $_text . '</option>' . $option_separator;
348 $_html_days .= '</select>';
351 // order the fields for output
353 for ($i = 0; $i <= 2; $i ++) {
354 switch ($field_order[ $i ]) {
357 if (isset($_html_years)) {
359 $_html .= $field_separator;
361 $_html .= $_html_years;
367 if (isset($_html_months)) {
369 $_html .= $field_separator;
371 $_html .= $_html_months;
377 if (isset($_html_days)) {
379 $_html .= $field_separator;
381 $_html .= $_html_days;