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