4 * -------------------------------------------------------------
6 * Name: html_select_date
8 * Purpose: Prints the dropdowns for date selection.
9 * Author: Andrei Zmievski
11 * ChangeLog: 1.0 initial release
12 * 1.1 added support for +/- N syntax for begin
13 * and end year values. (Monte)
14 * 1.2 added support for yyyy-mm-dd syntax for
15 * time value. (Jan Rosier)
16 * 1.3 added support for choosing format for
17 * month values (Gary Loescher)
18 * 1.3.1 added support for choosing format for
19 * day values (Marcus Bointon)
20 * Taken from the original Smarty
21 * http://smarty.php.net
22 * -------------------------------------------------------------
24 function tpl_function_html_select_date($params, &$template_object)
26 require_once("shared.make_timestamp.php");
27 require_once("function.html_options.php");
31 $start_year = strftime("%Y");
32 $end_year = $start_year;
34 $display_months = true;
35 $display_years = true;
37 /* Write months as numbers by default GL */
38 $month_value_format = "%m";
40 /* Write day values using this format MB */
41 $day_value_format = "%d";
42 $year_as_text = false;
43 /* Display years in reverse order? Ie. 2000,1999,.... */
44 $reverse_years = false;
45 /* Should the select boxes be part of an array when returned from PHP?
46 e.g. setting it to "birthday", would create "birthday[Day]",
47 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
49 /* <select size>'s of the different <select> tags.
50 If not set, uses default dropdown. */
54 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
55 An example might be in the template: all_extra ='class ="foo"'. */
57 /* Separate attributes for the tags. */
61 /* Order in which to display the fields.
62 "D" -> day, "M" -> month, "Y" -> year. */
64 /* String printed between the different fields. */
65 $field_separator = "\n";
70 // If $time is not in format yyyy-mm-dd
71 if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time))
73 // then $time is empty or unix timestamp or mysql timestamp
74 // using smarty_make_timestamp to get an unix timestamp and
75 // strftime to make yyyy-mm-dd
76 $time = strftime('%Y-%m-%d', tpl_make_timestamp($time));
78 // Now split this in pieces, which later can be used to set the select
79 $time = explode("-", $time);
81 // make syntax "+N" or "-N" work with start_year and end_year
82 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match))
86 $end_year = strftime('%Y') +
$match[2];
90 $end_year = strftime('%Y') - $match[2];
93 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match))
97 $start_year = strftime('%Y') +
$match[2];
101 $start_year = strftime('%Y') - $match[2];
105 $field_order = strtoupper($field_order);
106 $html_result = $month_result = $day_result = $year_result = "";
110 $month_names = array();
111 $month_values = array();
113 for ($i = 1; $i <= 12; $i++
)
115 $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
116 $month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
119 $month_result .= '<select name=';
120 if (null !== $field_array)
122 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
126 $month_result .= '"' . $prefix . 'Month"';
128 if (null !== $month_size)
130 $month_result .= ' size="' . $month_size . '"';
132 if (null !== $month_extra)
134 $month_result .= ' ' . $month_extra;
136 if (null !== $all_extra)
138 $month_result .= ' ' . $all_extra;
140 $month_result .= '>'."\n";
141 $month_result .= tpl_function_html_options(array('output' => $month_names,
142 'values' => $month_values,
143 'selected' => $month_values[$time[1]-1],
144 'print_result' => false),
146 $month_result .= '</select>';
152 for ($i = 1; $i <= 31; $i++
)
154 $days[] = sprintf($day_format, $i);
155 $day_values[] = sprintf($day_value_format, $i);
158 $day_result .= '<select name=';
159 if (null !== $field_array)
161 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
165 $day_result .= '"' . $prefix . 'Day"';
167 if (null !== $day_size)
169 $day_result .= ' size="' . $day_size . '"';
171 if (null !== $all_extra)
173 $day_result .= ' ' . $all_extra;
175 if (null !== $day_extra)
177 $day_result .= ' ' . $day_extra;
179 $day_result .= '>'."\n";
180 $day_result .= tpl_function_html_options(array('output' => $days,
181 'values' => $day_values,
182 'selected' => $time[2],
183 'print_result' => false),
185 $day_result .= '</select>';
190 if (null !== $field_array)
192 $year_name = $field_array . '[' . $prefix . 'Year]';
196 $year_name = $prefix . 'Year';
200 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
201 if (null !== $all_extra)
203 $year_result .= ' ' . $all_extra;
205 if (null !== $year_extra)
207 $year_result .= ' ' . $year_extra;
213 $years = range((int)$start_year, (int)$end_year);
216 rsort($years, SORT_NUMERIC
);
219 $year_result .= '<select name="' . $year_name . '"';
220 if (null !== $year_size)
222 $year_result .= ' size="' . $year_size . '"';
224 if (null !== $all_extra)
226 $year_result .= ' ' . $all_extra;
228 if (null !== $year_extra)
230 $year_result .= ' ' . $year_extra;
232 $year_result .= '>'."\n";
233 $year_result .= tpl_function_html_options(array('output' => $years,
235 'selected' => $time[0],
236 'print_result' => false),
238 $year_result .= '</select>';
242 // Loop thru the field_order field
243 for ($i = 0; $i <= 2; $i++
)
245 $c = substr($field_order, $i, 1);
249 $html_result .= $day_result;
253 $html_result .= $month_result;
257 $html_result .= $year_result;
260 // Add the field seperator
263 $html_result .= $field_separator;