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