ac4f2a14d07c13d91fd7a52bed480b689c62b9f3
[lhc/web/wiklou.git] / Smarty-2.6.2 / libs / plugins / function.html_select_date.php
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 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
26 * (Smarty online manual)
27 * @version 1.3.1
28 * @author Andrei Zmievski
29 * @param array
30 * @param Smarty
31 * @return string
32 */
33 function smarty_function_html_select_date($params, &$smarty)
34 {
35 require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
36 require_once $smarty->_get_plugin_filepath('function','html_options');
37 /* Default values. */
38 $prefix = "Date_";
39 $start_year = strftime("%Y");
40 $end_year = $start_year;
41 $display_days = true;
42 $display_months = true;
43 $display_years = true;
44 $month_format = "%B";
45 /* Write months as numbers by default GL */
46 $month_value_format = "%m";
47 $day_format = "%02d";
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 = false;
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 */
56 $field_array = null;
57 /* <select size>'s of the different <select> tags.
58 If not set, uses default dropdown. */
59 $day_size = null;
60 $month_size = null;
61 $year_size = null;
62 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
63 An example might be in the template: all_extra ='class ="foo"'. */
64 $all_extra = null;
65 /* Separate attributes for the tags. */
66 $day_extra = null;
67 $month_extra = null;
68 $year_extra = null;
69 /* Order in which to display the fields.
70 "D" -> day, "M" -> month, "Y" -> year. */
71 $field_order = 'MDY';
72 /* String printed between the different fields. */
73 $field_separator = "\n";
74 $time = time();
75 $all_empty = null;
76 $day_empty = null;
77 $month_empty = null;
78 $year_empty = null;
79
80 foreach ($params as $_key=>$_value) {
81 switch ($_key) {
82 case 'prefix':
83 case 'time':
84 case 'start_year':
85 case 'end_year':
86 case 'month_format':
87 case 'day_format':
88 case 'day_value_format':
89 case 'field_array':
90 case 'day_size':
91 case 'month_size':
92 case 'year_size':
93 case 'all_extra':
94 case 'day_extra':
95 case 'month_extra':
96 case 'year_extra':
97 case 'field_order':
98 case 'field_separator':
99 case 'month_value_format':
100 case 'month_empty':
101 case 'day_empty':
102 case 'year_empty':
103 $$_key = (string)$_value;
104 break;
105
106 case 'all_empty':
107 $$_key = (string)$_value;
108 $day_empty = $month_empty = $year_empty = $all_empty;
109 break;
110
111 case 'display_days':
112 case 'display_months':
113 case 'display_years':
114 case 'year_as_text':
115 case 'reverse_years':
116 $$_key = (bool)$_value;
117 break;
118
119 default:
120 $smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING);
121
122 }
123 }
124
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));
131 }
132 // Now split this in pieces, which later can be used to set the select
133 $time = explode("-", $time);
134
135 // make syntax "+N" or "-N" work with start_year and end_year
136 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
137 if ($match[1] == '+') {
138 $end_year = strftime('%Y') + $match[2];
139 } else {
140 $end_year = strftime('%Y') - $match[2];
141 }
142 }
143 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
144 if ($match[1] == '+') {
145 $start_year = strftime('%Y') + $match[2];
146 } else {
147 $start_year = strftime('%Y') - $match[2];
148 }
149 }
150
151 $field_order = strtoupper($field_order);
152
153 $html_result = $month_result = $day_result = $year_result = "";
154
155 if ($display_months) {
156 $month_names = array();
157 $month_values = array();
158 if(isset($month_empty)) {
159 $month_names[''] = $month_empty;
160 $month_values[''] = '';
161 }
162 for ($i = 1; $i <= 12; $i++) {
163 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
164 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
165 }
166
167 $month_result .= '<select name=';
168 if (null !== $field_array){
169 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
170 } else {
171 $month_result .= '"' . $prefix . 'Month"';
172 }
173 if (null !== $month_size){
174 $month_result .= ' size="' . $month_size . '"';
175 }
176 if (null !== $month_extra){
177 $month_result .= ' ' . $month_extra;
178 }
179 if (null !== $all_extra){
180 $month_result .= ' ' . $all_extra;
181 }
182 $month_result .= '>'."\n";
183
184 $month_result .= smarty_function_html_options(array('output' => $month_names,
185 'values' => $month_values,
186 'selected' => $month_values[(int)$time[1]],
187 'print_result' => false),
188 $smarty);
189
190 $month_result .= '</select>';
191 }
192
193 if ($display_days) {
194 $days = array();
195 if (isset($day_empty)) {
196 $days[''] = $day_empty;
197 $day_values[''] = '';
198 }
199 for ($i = 1; $i <= 31; $i++) {
200 $days[] = sprintf($day_format, $i);
201 $day_values[] = sprintf($day_value_format, $i);
202 }
203
204 $day_result .= '<select name=';
205 if (null !== $field_array){
206 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
207 } else {
208 $day_result .= '"' . $prefix . 'Day"';
209 }
210 if (null !== $day_size){
211 $day_result .= ' size="' . $day_size . '"';
212 }
213 if (null !== $all_extra){
214 $day_result .= ' ' . $all_extra;
215 }
216 if (null !== $day_extra){
217 $day_result .= ' ' . $day_extra;
218 }
219 $day_result .= '>'."\n";
220 $day_result .= smarty_function_html_options(array('output' => $days,
221 'values' => $day_values,
222 'selected' => $time[2],
223 'print_result' => false),
224 $smarty);
225 $day_result .= '</select>';
226 }
227
228 if ($display_years) {
229 if (null !== $field_array){
230 $year_name = $field_array . '[' . $prefix . 'Year]';
231 } else {
232 $year_name = $prefix . 'Year';
233 }
234 if ($year_as_text) {
235 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
236 if (null !== $all_extra){
237 $year_result .= ' ' . $all_extra;
238 }
239 if (null !== $year_extra){
240 $year_result .= ' ' . $year_extra;
241 }
242 $year_result .= '>';
243 } else {
244 $years = range((int)$start_year, (int)$end_year);
245 if ($reverse_years) {
246 rsort($years, SORT_NUMERIC);
247 }
248 $yearvals = $years;
249 if(isset($year_empty)) {
250 array_unshift($years, $year_empty);
251 array_unshift($yearvals, '');
252 }
253 $year_result .= '<select name="' . $year_name . '"';
254 if (null !== $year_size){
255 $year_result .= ' size="' . $year_size . '"';
256 }
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 .= '>'."\n";
264 $year_result .= smarty_function_html_options(array('output' => $years,
265 'values' => $yearvals,
266 'selected' => $time[0],
267 'print_result' => false),
268 $smarty);
269 $year_result .= '</select>';
270 }
271 }
272
273 // Loop thru the field_order field
274 for ($i = 0; $i <= 2; $i++){
275 $c = substr($field_order, $i, 1);
276 switch ($c){
277 case 'D':
278 $html_result .= $day_result;
279 break;
280
281 case 'M':
282 $html_result .= $month_result;
283 break;
284
285 case 'Y':
286 $html_result .= $year_result;
287 break;
288 }
289 // Add the field seperator
290 if($i != 2) {
291 $html_result .= $field_separator;
292 }
293 }
294
295 return $html_result;
296 }
297
298 /* vim: set expandtab: */
299
300 ?>