source: trunk/www.guidonia.net/wp/wp-content/plugins/odlinks/includes/Smarty/plugins/function.html_select_date.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 11.4 KB
Line 
1<?php
2/**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
7
8/**
9 * Smarty {html_select_date} plugin
10 *
11 * Type: function<br>
12 * Name: html_select_date<br>
13 * Purpose: Prints the dropdowns for date selection.
14 *
15 * ChangeLog:<br>
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 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
28 * (Smarty online manual)
29 * @version 1.3.2
30 * @author Andrei Zmievski
31 * @author Monte Ohrt <monte at ohrt dot com>
32 * @param array
33 * @param Smarty
34 * @return string
35 */
36function smarty_function_html_select_date($params, &$smarty)
37{
38 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
39 require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
40 require_once $smarty->_get_plugin_filepath('function','html_options');
41 /* Default values. */
42 $prefix = "Date_";
43 $start_year = strftime("%Y");
44 $end_year = $start_year;
45 $display_days = true;
46 $display_months = true;
47 $display_years = true;
48 $month_format = "%B";
49 /* Write months as numbers by default GL */
50 $month_value_format = "%m";
51 $day_format = "%02d";
52 /* Write day values using this format MB */
53 $day_value_format = "%d";
54 $year_as_text = false;
55 /* Display years in reverse order? Ie. 2000,1999,.... */
56 $reverse_years = false;
57 /* Should the select boxes be part of an array when returned from PHP?
58 e.g. setting it to "birthday", would create "birthday[Day]",
59 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
60 $field_array = null;
61 /* <select size>'s of the different <select> tags.
62 If not set, uses default dropdown. */
63 $day_size = null;
64 $month_size = null;
65 $year_size = null;
66 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
67 An example might be in the template: all_extra ='class ="foo"'. */
68 $all_extra = null;
69 /* Separate attributes for the tags. */
70 $day_extra = null;
71 $month_extra = null;
72 $year_extra = null;
73 /* Order in which to display the fields.
74 "D" -> day, "M" -> month, "Y" -> year. */
75 $field_order = 'MDY';
76 /* String printed between the different fields. */
77 $field_separator = "\n";
78 $time = time();
79 $all_empty = null;
80 $day_empty = null;
81 $month_empty = null;
82 $year_empty = null;
83 $extra_attrs = '';
84
85 foreach ($params as $_key=>$_value) {
86 switch ($_key) {
87 case 'prefix':
88 case 'time':
89 case 'start_year':
90 case 'end_year':
91 case 'month_format':
92 case 'day_format':
93 case 'day_value_format':
94 case 'field_array':
95 case 'day_size':
96 case 'month_size':
97 case 'year_size':
98 case 'all_extra':
99 case 'day_extra':
100 case 'month_extra':
101 case 'year_extra':
102 case 'field_order':
103 case 'field_separator':
104 case 'month_value_format':
105 case 'month_empty':
106 case 'day_empty':
107 case 'year_empty':
108 $$_key = (string)$_value;
109 break;
110
111 case 'all_empty':
112 $$_key = (string)$_value;
113 $day_empty = $month_empty = $year_empty = $all_empty;
114 break;
115
116 case 'display_days':
117 case 'display_months':
118 case 'display_years':
119 case 'year_as_text':
120 case 'reverse_years':
121 $$_key = (bool)$_value;
122 break;
123
124 default:
125 if(!is_array($_value)) {
126 $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
127 } else {
128 $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
129 }
130 break;
131 }
132 }
133
134 if(preg_match('!^-\d+$!',$time)) {
135 // negative timestamp, use date()
136 $time = date('Y-m-d',$time);
137 }
138 // If $time is not in format yyyy-mm-dd
139 if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
140 // use smarty_make_timestamp to get an unix timestamp and
141 // strftime to make yyyy-mm-dd
142 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
143 }
144 // Now split this in pieces, which later can be used to set the select
145 $time = explode("-", $time);
146
147 // make syntax "+N" or "-N" work with start_year and end_year
148 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
149 if ($match[1] == '+') {
150 $end_year = strftime('%Y') + $match[2];
151 } else {
152 $end_year = strftime('%Y') - $match[2];
153 }
154 }
155 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
156 if ($match[1] == '+') {
157 $start_year = strftime('%Y') + $match[2];
158 } else {
159 $start_year = strftime('%Y') - $match[2];
160 }
161 }
162 if (strlen($time[0]) > 0) {
163 if ($start_year > $time[0] && !isset($params['start_year'])) {
164 // force start year to include given date if not explicitly set
165 $start_year = $time[0];
166 }
167 if($end_year < $time[0] && !isset($params['end_year'])) {
168 // force end year to include given date if not explicitly set
169 $end_year = $time[0];
170 }
171 }
172
173 $field_order = strtoupper($field_order);
174
175 $html_result = $month_result = $day_result = $year_result = "";
176
177 if ($display_months) {
178 $month_names = array();
179 $month_values = array();
180 if(isset($month_empty)) {
181 $month_names[''] = $month_empty;
182 $month_values[''] = '';
183 }
184 for ($i = 1; $i <= 12; $i++) {
185 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
186 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
187 }
188
189 $month_result .= '<select name=';
190 if (null !== $field_array){
191 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
192 } else {
193 $month_result .= '"' . $prefix . 'Month"';
194 }
195 if (null !== $month_size){
196 $month_result .= ' size="' . $month_size . '"';
197 }
198 if (null !== $month_extra){
199 $month_result .= ' ' . $month_extra;
200 }
201 if (null !== $all_extra){
202 $month_result .= ' ' . $all_extra;
203 }
204 $month_result .= $extra_attrs . '>'."\n";
205
206 $month_result .= smarty_function_html_options(array('output' => $month_names,
207 'values' => $month_values,
208 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
209 'print_result' => false),
210 $smarty);
211 $month_result .= '</select>';
212 }
213
214 if ($display_days) {
215 $days = array();
216 if (isset($day_empty)) {
217 $days[''] = $day_empty;
218 $day_values[''] = '';
219 }
220 for ($i = 1; $i <= 31; $i++) {
221 $days[] = sprintf($day_format, $i);
222 $day_values[] = sprintf($day_value_format, $i);
223 }
224
225 $day_result .= '<select name=';
226 if (null !== $field_array){
227 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
228 } else {
229 $day_result .= '"' . $prefix . 'Day"';
230 }
231 if (null !== $day_size){
232 $day_result .= ' size="' . $day_size . '"';
233 }
234 if (null !== $all_extra){
235 $day_result .= ' ' . $all_extra;
236 }
237 if (null !== $day_extra){
238 $day_result .= ' ' . $day_extra;
239 }
240 $day_result .= $extra_attrs . '>'."\n";
241 $day_result .= smarty_function_html_options(array('output' => $days,
242 'values' => $day_values,
243 'selected' => $time[2],
244 'print_result' => false),
245 $smarty);
246 $day_result .= '</select>';
247 }
248
249 if ($display_years) {
250 if (null !== $field_array){
251 $year_name = $field_array . '[' . $prefix . 'Year]';
252 } else {
253 $year_name = $prefix . 'Year';
254 }
255 if ($year_as_text) {
256 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
257 if (null !== $all_extra){
258 $year_result .= ' ' . $all_extra;
259 }
260 if (null !== $year_extra){
261 $year_result .= ' ' . $year_extra;
262 }
263 $year_result .= ' />';
264 } else {
265 $years = range((int)$start_year, (int)$end_year);
266 if ($reverse_years) {
267 rsort($years, SORT_NUMERIC);
268 } else {
269 sort($years, SORT_NUMERIC);
270 }
271 $yearvals = $years;
272 if(isset($year_empty)) {
273 array_unshift($years, $year_empty);
274 array_unshift($yearvals, '');
275 }
276 $year_result .= '<select name="' . $year_name . '"';
277 if (null !== $year_size){
278 $year_result .= ' size="' . $year_size . '"';
279 }
280 if (null !== $all_extra){
281 $year_result .= ' ' . $all_extra;
282 }
283 if (null !== $year_extra){
284 $year_result .= ' ' . $year_extra;
285 }
286 $year_result .= $extra_attrs . '>'."\n";
287 $year_result .= smarty_function_html_options(array('output' => $years,
288 'values' => $yearvals,
289 'selected' => $time[0],
290 'print_result' => false),
291 $smarty);
292 $year_result .= '</select>';
293 }
294 }
295
296 // Loop thru the field_order field
297 for ($i = 0; $i <= 2; $i++){
298 $c = substr($field_order, $i, 1);
299 switch ($c){
300 case 'D':
301 $html_result .= $day_result;
302 break;
303
304 case 'M':
305 $html_result .= $month_result;
306 break;
307
308 case 'Y':
309 $html_result .= $year_result;
310 break;
311 }
312 // Add the field seperator
313 if($i != 2) {
314 $html_result .= $field_separator;
315 }
316 }
317
318 return $html_result;
319}
320
321/* vim: set expandtab: */
322
323?>
Note: See TracBrowser for help on using the repository browser.