]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/function.html_select_date.php
3a48da0bbed89e503566c0e5cd8e045760e70434
[friendica.git] / vendor / smarty / smarty / libs / 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  * Type:     function<br>
12  * Name:     html_select_date<br>
13  * Purpose:  Prints the dropdowns for date selection.
14  * ChangeLog:
15  * <pre>
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  *            - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
28  *              of 0000-00-00 dates (cybot, boots)
29  *            - 2.0 complete rewrite for performance,
30  *              added attributes month_names, *_id
31  * </pre>
32  *
33  * @link     http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
34  *           (Smarty online manual)
35  * @version  2.0
36  * @author   Andrei Zmievski
37  * @author   Monte Ohrt <monte at ohrt dot com>
38  * @author   Rodney Rehm
39  *
40  * @param array $params parameters
41  *
42  * @return string
43  */
44 function smarty_function_html_select_date($params)
45 {
46     if (!is_callable('smarty_function_escape_special_chars')) {
47         require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
48     }
49     if (!is_callable('smarty_make_timestamp')) {
50         require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
51     }
52     // generate timestamps used for month names only
53     static $_month_timestamps = null;
54     static $_current_year = null;
55     if ($_month_timestamps === null) {
56         $_current_year = date('Y');
57         $_month_timestamps = array();
58         for ($i = 1; $i <= 12; $i ++) {
59             $_month_timestamps[ $i ] = mktime(0, 0, 0, $i, 1, 2000);
60         }
61     }
62
63     /* Default values. */
64     $prefix = "Date_";
65     $start_year = null;
66     $end_year = null;
67     $display_days = true;
68     $display_months = true;
69     $display_years = true;
70     $month_format = "%B";
71     /* Write months as numbers by default  GL */
72     $month_value_format = "%m";
73     $day_format = "%02d";
74     /* Write day values using this format MB */
75     $day_value_format = "%d";
76     $year_as_text = false;
77     /* Display years in reverse order? Ie. 2000,1999,.... */
78     $reverse_years = false;
79     /* Should the select boxes be part of an array when returned from PHP?
80        e.g. setting it to "birthday", would create "birthday[Day]",
81        "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
82     $field_array = null;
83     /* <select size>'s of the different <select> tags.
84        If not set, uses default dropdown. */
85     $day_size = null;
86     $month_size = null;
87     $year_size = null;
88     /* Unparsed attributes common to *ALL* the <select>/<input> tags.
89        An example might be in the template: all_extra ='class ="foo"'. */
90     $all_extra = null;
91     /* Separate attributes for the tags. */
92     $day_extra = null;
93     $month_extra = null;
94     $year_extra = null;
95     /* Order in which to display the fields.
96        "D" -> day, "M" -> month, "Y" -> year. */
97     $field_order = 'MDY';
98     /* String printed between the different fields. */
99     $field_separator = "\n";
100     $option_separator = "\n";
101     $time = null;
102     // $all_empty = null;
103     // $day_empty = null;
104     // $month_empty = null;
105     // $year_empty = null;
106     $extra_attrs = '';
107     $all_id = null;
108     $day_id = null;
109     $month_id = null;
110     $year_id = null;
111
112     foreach ($params as $_key => $_value) {
113         switch ($_key) {
114             case 'time':
115                 if (!is_array($_value) && $_value !== null) {
116                     $time = smarty_make_timestamp($_value);
117                 }
118                 break;
119
120             case 'month_names':
121                 if (is_array($_value) && count($_value) == 12) {
122                     $$_key = $_value;
123                 } else {
124                     trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
125                 }
126                 break;
127
128             case 'prefix':
129             case 'field_array':
130             case 'start_year':
131             case 'end_year':
132             case 'day_format':
133             case 'day_value_format':
134             case 'month_format':
135             case 'month_value_format':
136             case 'day_size':
137             case 'month_size':
138             case 'year_size':
139             case 'all_extra':
140             case 'day_extra':
141             case 'month_extra':
142             case 'year_extra':
143             case 'field_order':
144             case 'field_separator':
145             case 'option_separator':
146             case 'all_empty':
147             case 'month_empty':
148             case 'day_empty':
149             case 'year_empty':
150             case 'all_id':
151             case 'month_id':
152             case 'day_id':
153             case 'year_id':
154                 $$_key = (string) $_value;
155                 break;
156
157             case 'display_days':
158             case 'display_months':
159             case 'display_years':
160             case 'year_as_text':
161             case 'reverse_years':
162                 $$_key = (bool) $_value;
163                 break;
164
165             default:
166                 if (!is_array($_value)) {
167                     $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
168                 } else {
169                     trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
170                 }
171                 break;
172         }
173     }
174
175     // Note: date() is faster than strftime()
176     // Note: explode(date()) is faster than date() date() date()
177     if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) {
178         if (isset($params[ 'time' ][ $prefix . 'Year' ])) {
179             // $_REQUEST[$field_array] given
180             foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
181                 $_variableName = '_' . strtolower($_elementName);
182                 $$_variableName =
183                     isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] :
184                         date($_elementKey);
185             }
186         } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) {
187             // $_REQUEST given
188             foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
189                 $_variableName = '_' . strtolower($_elementName);
190                 $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ?
191                     $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey);
192             }
193         } else {
194             // no date found, use NOW
195             list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
196         }
197     } elseif ($time === null) {
198         if (array_key_exists('time', $params)) {
199             $_year = $_month = $_day = $time = null;
200         } else {
201             list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
202         }
203     } else {
204         list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
205     }
206
207     // make syntax "+N" or "-N" work with $start_year and $end_year
208     // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
209     foreach (array('start', 'end') as $key) {
210         $key .= '_year';
211         $t = $$key;
212         if ($t === null) {
213             $$key = (int) $_current_year;
214         } elseif ($t[ 0 ] == '+') {
215             $$key = (int) ($_current_year + (int) trim(substr($t, 1)));
216         } elseif ($t[ 0 ] == '-') {
217             $$key = (int) ($_current_year - (int) trim(substr($t, 1)));
218         } else {
219             $$key = (int) $$key;
220         }
221     }
222
223     // flip for ascending or descending
224     if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
225         $t = $end_year;
226         $end_year = $start_year;
227         $start_year = $t;
228     }
229
230     // generate year <select> or <input>
231     if ($display_years) {
232         $_extra = '';
233         $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
234         if ($all_extra) {
235             $_extra .= ' ' . $all_extra;
236         }
237         if ($year_extra) {
238             $_extra .= ' ' . $year_extra;
239         }
240
241         if ($year_as_text) {
242             $_html_years =
243                 '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra .
244                 $extra_attrs . ' />';
245         } else {
246             $_html_years = '<select name="' . $_name . '"';
247             if ($year_id !== null || $all_id !== null) {
248                 $_html_years .= ' id="' . smarty_function_escape_special_chars($year_id !== null ?
249                                                                                    ($year_id ? $year_id : $_name) :
250                                                                                    ($all_id ? ($all_id . $_name) :
251                                                                                        $_name)) . '"';
252             }
253             if ($year_size) {
254                 $_html_years .= ' size="' . $year_size . '"';
255             }
256             $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
257
258             if (isset($year_empty) || isset($all_empty)) {
259                 $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' .
260                                 $option_separator;
261             }
262
263             $op = $start_year > $end_year ? - 1 : 1;
264             for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
265                 $_html_years .= '<option value="' . $i . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i .
266                                 '</option>' . $option_separator;
267             }
268
269             $_html_years .= '</select>';
270         }
271     }
272
273     // generate month <select> or <input>
274     if ($display_months) {
275         $_extra = '';
276         $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
277         if ($all_extra) {
278             $_extra .= ' ' . $all_extra;
279         }
280         if ($month_extra) {
281             $_extra .= ' ' . $month_extra;
282         }
283
284         $_html_months = '<select name="' . $_name . '"';
285         if ($month_id !== null || $all_id !== null) {
286             $_html_months .= ' id="' . smarty_function_escape_special_chars($month_id !== null ?
287                                                                                 ($month_id ? $month_id : $_name) :
288                                                                                 ($all_id ? ($all_id . $_name) :
289                                                                                     $_name)) . '"';
290         }
291         if ($month_size) {
292             $_html_months .= ' size="' . $month_size . '"';
293         }
294         $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
295
296         if (isset($month_empty) || isset($all_empty)) {
297             $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' .
298                              $option_separator;
299         }
300
301         for ($i = 1; $i <= 12; $i ++) {
302             $_val = sprintf('%02d', $i);
303             $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[ $i ]) :
304                 ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[ $i ]));
305             $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[ $i ]);
306             $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') .
307                              '>' . $_text . '</option>' . $option_separator;
308         }
309
310         $_html_months .= '</select>';
311     }
312
313     // generate day <select> or <input>
314     if ($display_days) {
315         $_extra = '';
316         $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
317         if ($all_extra) {
318             $_extra .= ' ' . $all_extra;
319         }
320         if ($day_extra) {
321             $_extra .= ' ' . $day_extra;
322         }
323
324         $_html_days = '<select name="' . $_name . '"';
325         if ($day_id !== null || $all_id !== null) {
326             $_html_days .= ' id="' .
327                            smarty_function_escape_special_chars($day_id !== null ? ($day_id ? $day_id : $_name) :
328                                                                     ($all_id ? ($all_id . $_name) : $_name)) . '"';
329         }
330         if ($day_size) {
331             $_html_days .= ' size="' . $day_size . '"';
332         }
333         $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
334
335         if (isset($day_empty) || isset($all_empty)) {
336             $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' .
337                            $option_separator;
338         }
339
340         for ($i = 1; $i <= 31; $i ++) {
341             $_val = sprintf('%02d', $i);
342             $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
343             $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
344             $_html_days .= '<option value="' . $_value . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' .
345                            $_text . '</option>' . $option_separator;
346         }
347
348         $_html_days .= '</select>';
349     }
350
351     // order the fields for output
352     $_html = '';
353     for ($i = 0; $i <= 2; $i ++) {
354         switch ($field_order[ $i ]) {
355             case 'Y':
356             case 'y':
357                 if (isset($_html_years)) {
358                     if ($_html) {
359                         $_html .= $field_separator;
360                     }
361                     $_html .= $_html_years;
362                 }
363                 break;
364
365             case 'm':
366             case 'M':
367                 if (isset($_html_months)) {
368                     if ($_html) {
369                         $_html .= $field_separator;
370                     }
371                     $_html .= $_html_months;
372                 }
373                 break;
374
375             case 'd':
376             case 'D':
377                 if (isset($_html_days)) {
378                     if ($_html) {
379                         $_html .= $field_separator;
380                     }
381                     $_html .= $_html_days;
382                 }
383                 break;
384         }
385     }
386
387     return $_html;
388 }