[ Index ]

krapohl.info

title

Body

[close]

/pnincludes/Smarty/plugins/ -> function.html_select_date.php (source)

   1  <?php
   2  /*
   3   * Smarty plugin
   4   * -------------------------------------------------------------
   5   * Type:     function
   6   * Name:     html_select_date
   7   * Version:  1.3
   8   * Purpose:  Prints the dropdowns for date selection.
   9   * Author:   Andrei Zmievski
  10   *
  11   * ChangeLog: 1.0 initial release
  12   *            1.1 added support for +/- N syntax for begin
  13   *                and end year values. (Monte)
  14   *            1.2 added support for yyyy-mm-dd syntax for
  15   *                time value. (Jan Rosier)
  16   *            1.3 added support for choosing format for 
  17   *                month values (Gary Loescher)
  18   * -------------------------------------------------------------
  19   */
  20  require_once $this->_get_plugin_filepath('shared','make_timestamp');
  21  require_once $this->_get_plugin_filepath('function','html_options');
  22  function smarty_function_html_select_date($params, &$smarty)
  23  {
  24      /* Default values. */
  25      $prefix          = "Date_";
  26      $start_year      = strftime("%Y");
  27      $end_year        = $start_year;
  28      $display_days    = true;
  29      $display_months  = true;
  30      $display_years   = true;
  31      $month_format    = "%B";
  32      /* Write months as numbers by default  GL */
  33      $month_value_format = "%m";
  34      $day_format      = "%02d";
  35      $year_as_text    = false;
  36      /* Display years in reverse order? Ie. 2000,1999,.... */
  37      $reverse_years   = false;
  38      /* Should the select boxes be part of an array when returned from PHP?
  39         e.g. setting it to "birthday", would create "birthday[Day]",
  40         "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  41      $field_array     = null;
  42      /* <select size>'s of the different <select> tags.
  43         If not set, uses default dropdown. */
  44      $day_size        = null;
  45      $month_size      = null;
  46      $year_size       = null;
  47      /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  48         An example might be in the template: all_extra ='class ="foo"'. */
  49      $all_extra       = null;
  50      /* Separate attributes for the tags. */
  51      $day_extra       = null;
  52      $month_extra     = null;
  53      $year_extra      = null;
  54      /* Order in which to display the fields.
  55         "D" -> day, "M" -> month, "Y" -> year. */
  56      $field_order      = 'MDY';
  57      /* String printed between the different fields. */
  58      $field_separator = "\n";
  59      $time = time();
  60  
  61  
  62      extract($params);
  63  
  64        // If $time is not in format yyyy-mm-dd
  65        if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time)) {
  66            // then $time is empty or unix timestamp or mysql timestamp
  67            // using smarty_make_timestamp to get an unix timestamp and
  68            // strftime to make yyyy-mm-dd
  69            $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
  70        }
  71        // Now split this in pieces, which later can be used to set the select
  72        $time = explode("-", $time);
  73    
  74        // make syntax "+N" or "-N" work with start_year and end_year
  75        if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
  76            if ($match[1] == '+') {
  77                $end_year = strftime('%Y') + $match[2];
  78            } else {
  79                $end_year = strftime('%Y') - $match[2];
  80            }
  81        }
  82        if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
  83            if ($match[1] == '+') {
  84                $start_year = strftime('%Y') + $match[2];
  85            } else {
  86                $start_year = strftime('%Y') - $match[2];
  87            }
  88        }
  89    
  90      $field_order = strtoupper($field_order);
  91  
  92      $html_result = $month_result = $day_result = $year_result = "";
  93  
  94      if ($display_months) {
  95          $month_names = array();
  96          $month_values = array();
  97  
  98          for ($i = 1; $i <= 12; $i++) {
  99              $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
 100              $month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
 101          }
 102  
 103          $month_result .= '<select name=';
 104          if (null !== $field_array){
 105              $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
 106          } else {
 107              $month_result .= '"' . $prefix . 'Month"';
 108          }
 109          if (null !== $month_size){
 110              $month_result .= ' size="' . $month_size . '"';
 111          }
 112          if (null !== $month_extra){
 113              $month_result .= ' ' . $month_extra;
 114          }
 115          if (null !== $all_extra){
 116              $month_result .= ' ' . $all_extra;
 117          }
 118          $month_result .= '>'."\n";
 119          
 120          $month_result .= smarty_function_html_options(array('output'     => $month_names,
 121                                                              'values'     => $month_values,
 122                                                              'selected'   => $month_values[$time[1]-1],
 123                                                              'print_result' => false),
 124                                                        $smarty);
 125          
 126          $month_result .= '</select>';
 127      }
 128  
 129      if ($display_days) {
 130          $days = array();
 131          for ($i = 1; $i <= 31; $i++)
 132              $days[] = sprintf($day_format, $i);
 133  
 134          $day_result .= '<select name=';
 135          if (null !== $field_array){
 136              $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
 137          } else {
 138              $day_result .= '"' . $prefix . 'Day"';
 139          }
 140          if (null !== $day_size){
 141              $day_result .= ' size="' . $day_size . '"';
 142          }
 143          if (null !== $all_extra){
 144              $day_result .= ' ' . $all_extra;
 145          }
 146          if (null !== $day_extra){
 147              $day_result .= ' ' . $day_extra;
 148          }
 149          $day_result .= '>'."\n";
 150          $day_result .= smarty_function_html_options(array('output'     => $days,
 151                                                            'values'     => range(1, 31),
 152                                                            'selected'   => $time[2],
 153                                                            'print_result' => false),
 154                                                      $smarty);
 155          $day_result .= '</select>';
 156      }
 157  
 158      if ($display_years) {
 159          if (null !== $field_array){
 160              $year_name = $field_array . '[' . $prefix . 'Year]';
 161          } else {
 162              $year_name = $prefix . 'Year';
 163          }
 164          if ($year_as_text) {
 165              $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
 166              if (null !== $all_extra){
 167                  $year_result .= ' ' . $all_extra;
 168              }
 169              if (null !== $year_extra){
 170                  $year_result .= ' ' . $year_extra;
 171              }
 172              $year_result .= '>';
 173          } else {
 174              $years = range((int)$start_year, (int)$end_year);
 175              if ($reverse_years) {
 176                  rsort($years, SORT_NUMERIC);
 177              }
 178  
 179              $year_result .= '<select name="' . $year_name . '"';
 180              if (null !== $year_size){
 181                  $year_result .= ' size="' . $year_size . '"';
 182              }
 183              if (null !== $all_extra){
 184                  $year_result .= ' ' . $all_extra;
 185              }
 186              if (null !== $year_extra){
 187                  $year_result .= ' ' . $year_extra;
 188              }
 189              $year_result .= '>'."\n";
 190              $year_result .= smarty_function_html_options(array('output' => $years,
 191                                                                 'values' => $years,
 192                                                                 'selected'   => $time[0],
 193                                                                 'print_result' => false),
 194                                                           $smarty);
 195              $year_result .= '</select>';
 196          }
 197      }
 198  
 199      // Loop thru the field_order field
 200      for ($i = 0; $i <= 2; $i++){
 201        $c = substr($field_order, $i, 1);
 202        switch ($c){
 203          case 'D':
 204              $html_result .= $day_result;
 205              break;
 206  
 207          case 'M':
 208              $html_result .= $month_result;
 209              break;
 210  
 211          case 'Y':
 212              $html_result .= $year_result;
 213              break;
 214        }
 215        // Add the field seperator
 216        if($i != 2) {
 217            $html_result .= $field_separator;
 218          }
 219      }
 220  
 221      print $html_result;
 222  }
 223  
 224  /* vim: set expandtab: */
 225  
 226  ?>


Generated: Wed Feb 16 22:40:07 2005 Cross-referenced by PHPXref 0.6