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 * - 1.3.2 suppport negative timestamps, force year
26 * dropdown to include given date unless explicitly set (Monte)
27 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
28 * (Smarty online manual)
30 * @author Andrei Zmievski
35 function smarty_function_html_select_date($params, &$smarty)
37 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
38 require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
39 require_once $smarty->_get_plugin_filepath('function','html_options');
42 $start_year = strftime("%Y");
43 $end_year = $start_year;
45 $display_months = true;
46 $display_years = true;
48 /* Write months as numbers by default GL */
49 $month_value_format = "%m";
51 /* Write day values using this format MB */
52 $day_value_format = "%d";
53 $year_as_text = false;
54 /* Display years in reverse order? Ie. 2000,1999,.... */
55 $reverse_years = false;
56 /* Should the select boxes be part of an array when returned from PHP?
57 e.g. setting it to "birthday", would create "birthday[Day]",
58 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
60 /* <select size>'s of the different <select> tags.
61 If not set, uses default dropdown. */
65 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
66 An example might be in the template: all_extra ='class ="foo"'. */
68 /* Separate attributes for the tags. */
72 /* Order in which to display the fields.
73 "D" -> day, "M" -> month, "Y" -> year. */
75 /* String printed between the different fields. */
76 $field_separator = "\n";
84 foreach ($params as $_key=>$_value) {
92 case 'day_value_format':
102 case 'field_separator':
103 case 'month_value_format':
107 $
$_key = (string)$_value;
111 $
$_key = (string)$_value;
112 $day_empty = $month_empty = $year_empty = $all_empty;
116 case 'display_months':
117 case 'display_years':
119 case 'reverse_years':
120 $
$_key = (bool)$_value;
124 if(!is_array($_value)) {
125 $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
127 $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE
);
133 if(preg_match('!^-\d+$!',$time)) {
134 // negative timestamp, use date()
135 $time = date('Y-m-d',$time);
137 // If $time is not in format yyyy-mm-dd
138 if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
139 // use smarty_make_timestamp to get an unix timestamp and
140 // strftime to make yyyy-mm-dd
141 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
143 // Now split this in pieces, which later can be used to set the select
144 $time = explode("-", $time);
146 // make syntax "+N" or "-N" work with start_year and end_year
147 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
148 if ($match[1] == '+') {
149 $end_year = strftime('%Y') +
$match[2];
151 $end_year = strftime('%Y') - $match[2];
154 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
155 if ($match[1] == '+') {
156 $start_year = strftime('%Y') +
$match[2];
158 $start_year = strftime('%Y') - $match[2];
161 if (strlen($time[0]) > 0) {
162 if ($start_year > $time[0] && !isset($params['start_year'])) {
163 // force start year to include given date if not explicitly set
164 $start_year = $time[0];
166 if($end_year < $time[0] && !isset($params['end_year'])) {
167 // force end year to include given date if not explicitly set
168 $end_year = $time[0];
172 $field_order = strtoupper($field_order);
174 $html_result = $month_result = $day_result = $year_result = "";
176 if ($display_months) {
177 $month_names = array();
178 $month_values = array();
179 if(isset($month_empty)) {
180 $month_names[''] = $month_empty;
181 $month_values[''] = '';
183 for ($i = 1; $i <= 12; $i++
) {
184 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
185 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
188 $month_result .= '<select name=';
189 if (null !== $field_array){
190 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
192 $month_result .= '"' . $prefix . 'Month"';
194 if (null !== $month_size){
195 $month_result .= ' size="' . $month_size . '"';
197 if (null !== $month_extra){
198 $month_result .= ' ' . $month_extra;
200 if (null !== $all_extra){
201 $month_result .= ' ' . $all_extra;
203 $month_result .= $extra_attrs . '>'."\n";
205 $month_result .= smarty_function_html_options(array('output' => $month_names,
206 'values' => $month_values,
207 'selected' => (int)$time[1] ?
strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
208 'print_result' => false),
210 $month_result .= '</select>';
215 if (isset($day_empty)) {
216 $days[''] = $day_empty;
217 $day_values[''] = '';
219 for ($i = 1; $i <= 31; $i++
) {
220 $days[] = sprintf($day_format, $i);
221 $day_values[] = sprintf($day_value_format, $i);
224 $day_result .= '<select name=';
225 if (null !== $field_array){
226 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
228 $day_result .= '"' . $prefix . 'Day"';
230 if (null !== $day_size){
231 $day_result .= ' size="' . $day_size . '"';
233 if (null !== $all_extra){
234 $day_result .= ' ' . $all_extra;
236 if (null !== $day_extra){
237 $day_result .= ' ' . $day_extra;
239 $day_result .= $extra_attrs . '>'."\n";
240 $day_result .= smarty_function_html_options(array('output' => $days,
241 'values' => $day_values,
242 'selected' => $time[2],
243 'print_result' => false),
245 $day_result .= '</select>';
248 if ($display_years) {
249 if (null !== $field_array){
250 $year_name = $field_array . '[' . $prefix . 'Year]';
252 $year_name = $prefix . 'Year';
255 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
256 if (null !== $all_extra){
257 $year_result .= ' ' . $all_extra;
259 if (null !== $year_extra){
260 $year_result .= ' ' . $year_extra;
262 $year_result .= ' />';
264 $years = range((int)$start_year, (int)$end_year);
265 if ($reverse_years) {
266 rsort($years, SORT_NUMERIC
);
268 sort($years, SORT_NUMERIC
);
271 if(isset($year_empty)) {
272 array_unshift($years, $year_empty);
273 array_unshift($yearvals, '');
275 $year_result .= '<select name="' . $year_name . '"';
276 if (null !== $year_size){
277 $year_result .= ' size="' . $year_size . '"';
279 if (null !== $all_extra){
280 $year_result .= ' ' . $all_extra;
282 if (null !== $year_extra){
283 $year_result .= ' ' . $year_extra;
285 $year_result .= $extra_attrs . '>'."\n";
286 $year_result .= smarty_function_html_options(array('output' => $years,
287 'values' => $yearvals,
288 'selected' => $time[0],
289 'print_result' => false),
291 $year_result .= '</select>';
295 // Loop thru the field_order field
296 for ($i = 0; $i <= 2; $i++
){
297 $c = substr($field_order, $i, 1);
300 $html_result .= $day_result;
304 $html_result .= $month_result;
308 $html_result .= $year_result;
311 // Add the field seperator
313 $html_result .= $field_separator;
320 /* vim: set expandtab: */
This page took 0.427549 seconds and 4 git commands to generate.