9 * 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 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
26 * (Smarty online manual)
28 * @author Andrei Zmievski
33 function smarty_function_html_select_date($params, &$smarty)
35 require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
36 require_once $smarty->_get_plugin_filepath('function','html_options');
39 $start_year = strftime("%Y");
40 $end_year = $start_year;
42 $display_months = true;
43 $display_years = true;
45 /* Write months as numbers by default GL */
46 $month_value_format = "%m";
48 /* Write day values using this format MB */
49 $day_value_format = "%d";
50 $year_as_text = false;
51 /* Display years in reverse order? Ie. 2000,1999,.... */
52 $reverse_years = true;
53 /* Should the select boxes be part of an array when returned from PHP?
54 e.g. setting it to "birthday", would create "birthday[Day]",
55 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
57 /* <select size>'s of the different <select> tags.
58 If not set, uses default dropdown. */
62 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
63 An example might be in the template: all_extra ='class ="foo"'. */
65 /* Separate attributes for the tags. */
69 /* Order in which to display the fields.
70 "D" -> day, "M" -> month, "Y" -> year. */
72 /* String printed between the different fields. */
73 $field_separator = "\n";
80 foreach ($params as $_key=>$_value) {
88 case 'day_value_format':
98 case 'field_separator':
99 case 'month_value_format':
103 $
$_key = (string)$_value;
107 $
$_key = (string)$_value;
108 $day_empty = $month_empty = $year_empty = $all_empty;
112 case 'display_months':
113 case 'display_years':
115 case 'reverse_years':
116 $
$_key = (bool)$_value;
120 $smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING
);
125 // If $time is not in format yyyy-mm-dd
126 if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
127 // then $time is empty or unix timestamp or mysql timestamp
128 // using smarty_make_timestamp to get an unix timestamp and
129 // strftime to make yyyy-mm-dd
130 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
132 // Now split this in pieces, which later can be used to set the select
133 $time = explode("-", $time);
134 // make syntax "+N" or "-N" work with start_year and end_year
135 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
136 if ($match[1] == '+') {
137 $end_year = strftime('%Y') +
$match[2];
139 $end_year = strftime('%Y') - $match[2];
142 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
143 if ($match[1] == '+') {
144 $start_year = strftime('%Y') +
$match[2];
146 $start_year = strftime('%Y') - $match[2];
150 $field_order = strtoupper($field_order);
152 $html_result = $month_result = $day_result = $year_result = "";
154 if ($display_months) {
155 $month_names = array();
156 $month_values = array();
157 if(isset($month_empty)) {
158 $month_names[''] = $month_empty;
159 $month_values[''] = '';
161 for ($i = 1; $i <= 12; $i++
) {
162 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
163 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
166 $month_result .= '<select name=';
167 if (null !== $field_array){
168 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
170 $month_result .= '"' . $prefix . 'm"';
172 if (null !== $month_size){
173 $month_result .= ' size="' . $month_size . '"';
175 if (null !== $month_extra){
176 $month_result .= ' ' . $month_extra;
178 if (null !== $all_extra){
179 $month_result .= ' ' . $all_extra;
181 $month_result .= '>'."\n";
183 $month_result .= smarty_function_html_options(array('output' => $month_names,
184 'values' => $month_values,
185 'selected' => $month_values[(int)$time[1]],
186 'print_result' => false),
189 $month_result .= '</select>';
194 if (isset($day_empty)) {
195 $days[''] = $day_empty;
196 $day_values[''] = '';
198 for ($i = 1; $i <= 31; $i++
) {
199 $days[] = sprintf($day_format, $i);
200 $day_values[] = sprintf($day_value_format, $i);
203 $day_result .= '<select name=';
204 if (null !== $field_array){
205 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
207 $day_result .= '"' . $prefix . 'd"';
209 if (null !== $day_size){
210 $day_result .= ' size="' . $day_size . '"';
212 if (null !== $all_extra){
213 $day_result .= ' ' . $all_extra;
215 if (null !== $day_extra){
216 $day_result .= ' ' . $day_extra;
218 $day_result .= '>'."\n";
219 $day_result .= smarty_function_html_options(array('output' => $days,
220 'values' => $day_values,
221 'selected' => $time[2],
222 'print_result' => false),
224 $day_result .= '</select>';
227 if ($display_years) {
228 if (null !== $field_array){
229 $year_name = $field_array . '[' . $prefix . 'Year]';
231 $year_name = $prefix . 'y';
234 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
235 if (null !== $all_extra){
236 $year_result .= ' ' . $all_extra;
238 if (null !== $year_extra){
239 $year_result .= ' ' . $year_extra;
243 $years = range((int)$start_year, (int)$end_year);
244 if ($reverse_years) {
245 rsort($years, SORT_NUMERIC
);
248 if(isset($year_empty)) {
249 array_unshift($years, $year_empty);
250 array_unshift($yearvals, '');
252 $year_result .= '<select name="' . $year_name . '"';
253 if (null !== $year_size){
254 $year_result .= ' size="' . $year_size . '"';
256 if (null !== $all_extra){
257 $year_result .= ' ' . $all_extra;
259 if (null !== $year_extra){
260 $year_result .= ' ' . $year_extra;
262 $year_result .= '>'."\n";
263 $year_result .= smarty_function_html_options(array('output' => $years,
264 'values' => $yearvals,
265 'selected' => $time[0],
266 'print_result' => false),
268 $year_result .= '</select>';
272 // Loop thru the field_order field
273 for ($i = 0; $i <= 2; $i++
){
274 $c = substr($field_order, $i, 1);
277 $html_result .= $day_result;
281 $html_result .= $month_result;
285 $html_result .= $year_result;
288 // Add the field seperator
290 $html_result .= $field_separator;
297 /* vim: set expandtab: */