Code

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