• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Smarty/libs/plugins/function.html_select_date.php

00001 <?php
00035 function smarty_function_html_select_date($params, &$smarty)
00036 {
00037     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
00038     require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
00039     require_once $smarty->_get_plugin_filepath('function','html_options');
00040     /* Default values. */
00041     $prefix          = "Date_";
00042     $start_year      = strftime("%Y");
00043     $end_year        = $start_year;
00044     $display_days    = true;
00045     $display_months  = true;
00046     $display_years   = true;
00047     $month_format    = "%B";
00048     /* Write months as numbers by default  GL */
00049     $month_value_format = "%m";
00050     $day_format      = "%02d";
00051     /* Write day values using this format MB */
00052     $day_value_format = "%d";
00053     $year_as_text    = false;
00054     /* Display years in reverse order? Ie. 2000,1999,.... */
00055     $reverse_years   = false;
00056     /* Should the select boxes be part of an array when returned from PHP?
00057        e.g. setting it to "birthday", would create "birthday[Day]",
00058        "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
00059     $field_array     = null;
00060     /* <select size>'s of the different <select> tags.
00061        If not set, uses default dropdown. */
00062     $day_size        = null;
00063     $month_size      = null;
00064     $year_size       = null;
00065     /* Unparsed attributes common to *ALL* the <select>/<input> tags.
00066        An example might be in the template: all_extra ='class ="foo"'. */
00067     $all_extra       = null;
00068     /* Separate attributes for the tags. */
00069     $day_extra       = null;
00070     $month_extra     = null;
00071     $year_extra      = null;
00072     /* Order in which to display the fields.
00073        "D" -> day, "M" -> month, "Y" -> year. */
00074     $field_order     = 'MDY';
00075     /* String printed between the different fields. */
00076     $field_separator = "\n";
00077     $time = time();
00078     $all_empty       = null;
00079     $day_empty       = null;
00080     $month_empty     = null;
00081     $year_empty      = null;
00082     $extra_attrs     = '';
00083 
00084     foreach ($params as $_key=>$_value) {
00085         switch ($_key) {
00086             case 'prefix':
00087             case 'time':
00088             case 'start_year':
00089             case 'end_year':
00090             case 'month_format':
00091             case 'day_format':
00092             case 'day_value_format':
00093             case 'field_array':
00094             case 'day_size':
00095             case 'month_size':
00096             case 'year_size':
00097             case 'all_extra':
00098             case 'day_extra':
00099             case 'month_extra':
00100             case 'year_extra':
00101             case 'field_order':
00102             case 'field_separator':
00103             case 'month_value_format':
00104             case 'month_empty':
00105             case 'day_empty':
00106             case 'year_empty':
00107                 $$_key = (string)$_value;
00108                 break;
00109 
00110             case 'all_empty':
00111                 $$_key = (string)$_value;
00112                 $day_empty = $month_empty = $year_empty = $all_empty;
00113                 break;
00114 
00115             case 'display_days':
00116             case 'display_months':
00117             case 'display_years':
00118             case 'year_as_text':
00119             case 'reverse_years':
00120                 $$_key = (bool)$_value;
00121                 break;
00122 
00123             default:
00124                 if(!is_array($_value)) {
00125                     $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
00126                 } else {
00127                     $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
00128                 }
00129                 break;
00130         }
00131     }
00132 
00133     if(preg_match('!^-\d+$!',$time)) {
00134         // negative timestamp, use date()
00135         $time = date('Y-m-d',$time);
00136     }
00137     // If $time is not in format yyyy-mm-dd
00138     if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
00139         // use smarty_make_timestamp to get an unix timestamp and
00140         // strftime to make yyyy-mm-dd
00141         $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
00142     }
00143     // Now split this in pieces, which later can be used to set the select
00144     $time = explode("-", $time);
00145     
00146     // make syntax "+N" or "-N" work with start_year and end_year
00147     if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
00148         if ($match[1] == '+') {
00149             $end_year = strftime('%Y') + $match[2];
00150         } else {
00151             $end_year = strftime('%Y') - $match[2];
00152         }
00153     }
00154     if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
00155         if ($match[1] == '+') {
00156             $start_year = strftime('%Y') + $match[2];
00157         } else {
00158             $start_year = strftime('%Y') - $match[2];
00159         }
00160     }
00161     if (strlen($time[0]) > 0) { 
00162         if ($start_year > $time[0] && !isset($params['start_year'])) {
00163             // force start year to include given date if not explicitly set
00164             $start_year = $time[0];
00165         }
00166         if($end_year < $time[0] && !isset($params['end_year'])) {
00167             // force end year to include given date if not explicitly set
00168             $end_year = $time[0];
00169         }
00170     }
00171 
00172     $field_order = strtoupper($field_order);
00173 
00174     $html_result = $month_result = $day_result = $year_result = "";
00175 
00176     if ($display_months) {
00177         $month_names = array();
00178         $month_values = array();
00179         if(isset($month_empty)) {
00180             $month_names[''] = $month_empty;
00181             $month_values[''] = '';
00182         }
00183         for ($i = 1; $i <= 12; $i++) {
00184             $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
00185             $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
00186         }
00187 
00188         $month_result .= '<select name=';
00189         if (null !== $field_array){
00190             $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
00191         } else {
00192             $month_result .= '"' . $prefix . 'Month"';
00193         }
00194         if (null !== $month_size){
00195             $month_result .= ' size="' . $month_size . '"';
00196         }
00197         if (null !== $month_extra){
00198             $month_result .= ' ' . $month_extra;
00199         }
00200         if (null !== $all_extra){
00201             $month_result .= ' ' . $all_extra;
00202         }
00203         $month_result .= $extra_attrs . '>'."\n";
00204 
00205         $month_result .= smarty_function_html_options(array('output'     => $month_names,
00206                                                             'values'     => $month_values,
00207                                                             'selected'   => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
00208                                                             'print_result' => false),
00209                                                       $smarty);
00210         $month_result .= '</select>';
00211     }
00212 
00213     if ($display_days) {
00214         $days = array();
00215         if (isset($day_empty)) {
00216             $days[''] = $day_empty;
00217             $day_values[''] = '';
00218         }
00219         for ($i = 1; $i <= 31; $i++) {
00220             $days[] = sprintf($day_format, $i);
00221             $day_values[] = sprintf($day_value_format, $i);
00222         }
00223 
00224         $day_result .= '<select name=';
00225         if (null !== $field_array){
00226             $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
00227         } else {
00228             $day_result .= '"' . $prefix . 'Day"';
00229         }
00230         if (null !== $day_size){
00231             $day_result .= ' size="' . $day_size . '"';
00232         }
00233         if (null !== $all_extra){
00234             $day_result .= ' ' . $all_extra;
00235         }
00236         if (null !== $day_extra){
00237             $day_result .= ' ' . $day_extra;
00238         }
00239         $day_result .= $extra_attrs . '>'."\n";
00240         $day_result .= smarty_function_html_options(array('output'     => $days,
00241                                                           'values'     => $day_values,
00242                                                           'selected'   => $time[2],
00243                                                           'print_result' => false),
00244                                                     $smarty);
00245         $day_result .= '</select>';
00246     }
00247 
00248     if ($display_years) {
00249         if (null !== $field_array){
00250             $year_name = $field_array . '[' . $prefix . 'Year]';
00251         } else {
00252             $year_name = $prefix . 'Year';
00253         }
00254         if ($year_as_text) {
00255             $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
00256             if (null !== $all_extra){
00257                 $year_result .= ' ' . $all_extra;
00258             }
00259             if (null !== $year_extra){
00260                 $year_result .= ' ' . $year_extra;
00261             }
00262             $year_result .= ' />';
00263         } else {
00264             $years = range((int)$start_year, (int)$end_year);
00265             if ($reverse_years) {
00266                 rsort($years, SORT_NUMERIC);
00267             } else {
00268                 sort($years, SORT_NUMERIC);
00269             }
00270             $yearvals = $years;
00271             if(isset($year_empty)) {
00272                 array_unshift($years, $year_empty);
00273                 array_unshift($yearvals, '');
00274             }
00275             $year_result .= '<select name="' . $year_name . '"';
00276             if (null !== $year_size){
00277                 $year_result .= ' size="' . $year_size . '"';
00278             }
00279             if (null !== $all_extra){
00280                 $year_result .= ' ' . $all_extra;
00281             }
00282             if (null !== $year_extra){
00283                 $year_result .= ' ' . $year_extra;
00284             }
00285             $year_result .= $extra_attrs . '>'."\n";
00286             $year_result .= smarty_function_html_options(array('output' => $years,
00287                                                                'values' => $yearvals,
00288                                                                'selected'   => $time[0],
00289                                                                'print_result' => false),
00290                                                          $smarty);
00291             $year_result .= '</select>';
00292         }
00293     }
00294 
00295     // Loop thru the field_order field
00296     for ($i = 0; $i <= 2; $i++){
00297         $c = substr($field_order, $i, 1);
00298         switch ($c){
00299             case 'D':
00300                 $html_result .= $day_result;
00301                 break;
00302 
00303             case 'M':
00304                 $html_result .= $month_result;
00305                 break;
00306 
00307             case 'Y':
00308                 $html_result .= $year_result;
00309                 break;
00310         }
00311         // Add the field seperator
00312         if($i != 2) {
00313             $html_result .= $field_separator;
00314         }
00315     }
00316 
00317     return $html_result;
00318 }
00319 
00320 /* vim: set expandtab: */
00321 
00322 ?>

Generated on Thu Apr 19 2012 17:01:16 for openbiz by  doxygen 1.7.2