5 Modification information for LGPL compliance
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
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
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
23 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
36 * Smarty {html_select_date} plugin
39 * Name: html_select_date<br>
40 * Purpose: Prints the dropdowns for date selection.
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)
57 * @author Andrei Zmievski
58 * @author Monte Ohrt <monte at ohrt dot com>
63 function smarty_function_html_select_date($params, &$smarty)
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');
70 $start_year = strftime("%Y");
71 $end_year = $start_year;
73 $display_months = true;
74 $display_years = true;
76 /* Write months as numbers by default GL */
77 $month_value_format = "%m";
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 */
88 /* <select size>'s of the different <select> tags.
89 If not set, uses default dropdown. */
93 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
94 An example might be in the template: all_extra ='class ="foo"'. */
96 /* Separate attributes for the tags. */
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";
112 foreach ($params as $_key=>$_value) {
120 case 'day_value_format':
130 case 'field_separator':
131 case 'month_value_format':
135 $$_key = (string)$_value;
139 $$_key = (string)$_value;
140 $day_empty = $month_empty = $year_empty = $all_empty;
144 case 'display_months':
145 case 'display_years':
147 case 'reverse_years':
148 $$_key = (bool)$_value;
152 if(!is_array($_value)) {
153 $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
155 $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
161 if(preg_match('!^-\d+$!',$time)) {
162 // negative timestamp, use date()
163 $time = date('Y-m-d',$time);
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));
171 // Now split this in pieces, which later can be used to set the select
172 $time = explode("-", $time);
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];
179 $end_year = strftime('%Y') - $match[2];
182 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
183 if ($match[1] == '+') {
184 $start_year = strftime('%Y') + $match[2];
186 $start_year = strftime('%Y') - $match[2];
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];
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];
200 $field_order = strtoupper($field_order);
202 $html_result = $month_result = $day_result = $year_result = "";
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[''] = '';
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));
216 $month_result .= '<select name=';
217 if (null !== $field_array){
218 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
220 $month_result .= '"' . $prefix . 'Month"';
222 if (null !== $month_size){
223 $month_result .= ' size="' . $month_size . '"';
225 if (null !== $month_extra){
226 $month_result .= ' ' . $month_extra;
228 if (null !== $all_extra){
229 $month_result .= ' ' . $all_extra;
231 $month_result .= $extra_attrs . '>'."\n";
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),
238 $month_result .= '</select>';
243 if (isset($day_empty)) {
244 $days[''] = $day_empty;
245 $day_values[''] = '';
247 for ($i = 1; $i <= 31; $i++) {
248 $days[] = sprintf($day_format, $i);
249 $day_values[] = sprintf($day_value_format, $i);
252 $day_result .= '<select name=';
253 if (null !== $field_array){
254 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
256 $day_result .= '"' . $prefix . 'Day"';
258 if (null !== $day_size){
259 $day_result .= ' size="' . $day_size . '"';
261 if (null !== $all_extra){
262 $day_result .= ' ' . $all_extra;
264 if (null !== $day_extra){
265 $day_result .= ' ' . $day_extra;
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),
273 $day_result .= '</select>';
276 if ($display_years) {
277 if (null !== $field_array){
278 $year_name = $field_array . '[' . $prefix . 'Year]';
280 $year_name = $prefix . 'Year';
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;
287 if (null !== $year_extra){
288 $year_result .= ' ' . $year_extra;
290 $year_result .= ' />';
292 $years = range((int)$start_year, (int)$end_year);
293 if ($reverse_years) {
294 rsort($years, SORT_NUMERIC);
296 sort($years, SORT_NUMERIC);
299 if(isset($year_empty)) {
300 array_unshift($years, $year_empty);
301 array_unshift($yearvals, '');
303 $year_result .= '<select name="' . $year_name . '"';
304 if (null !== $year_size){
305 $year_result .= ' size="' . $year_size . '"';
307 if (null !== $all_extra){
308 $year_result .= ' ' . $all_extra;
310 if (null !== $year_extra){
311 $year_result .= ' ' . $year_extra;
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),
319 $year_result .= '</select>';
323 // Loop thru the field_order field
324 for ($i = 0; $i <= 2; $i++){
325 $c = substr($field_order, $i, 1);
328 $html_result .= $day_result;
332 $html_result .= $month_result;
336 $html_result .= $year_result;
339 // Add the field seperator
341 $html_result .= $field_separator;
348 /* vim: set expandtab: */