Smarty
[ class tree: Smarty ] [ index: Smarty ] [ all elements ]

Source for file function.html_select_date.php

Documentation is available at function.html_select_date.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8. /**
  9.  * Smarty {html_select_date} plugin
  10.  *
  11.  * Type:     function<br>
  12.  * Name:     html_select_date<br>
  13.  * Purpose:  Prints the dropdowns for date selection.
  14.  *
  15.  * ChangeLog:<br>
  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.  * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
  26.  *       (Smarty online manual)
  27.  * @version 1.3
  28.  * @author   Andrei Zmievski
  29.  * @param array 
  30.  * @param Smarty 
  31.  * @return string 
  32.  */
  33. function smarty_function_html_select_date($params&$pSmarty)
  34. {
  35.     global $gBitSystem;
  36.     $pSmarty->loadPlugin'smarty_shared_make_timestamp' );
  37.     $pSmarty->loadPlugin'smarty_function_html_options' );
  38.     /* Default values. */
  39.     $prefix          "Date_";
  40.     $start_year      strftime("%Y");
  41.     $end_year        $start_year;
  42.     $display_days    true;
  43.     $display_months  true;
  44.     $display_years   true;
  45.     $month_format    "%B";
  46.     /* Write months as numbers by default  GL */
  47.     $month_value_format "%m";
  48.     $day_format      "%02d";
  49.     /* Write day values using this format MB */
  50.     $day_value_format "%d";
  51.     $year_as_text    false;
  52.     /* Display years in reverse order? Ie. 2000,1999,.... */
  53.     $reverse_years   false;
  54.     /* Should the select boxes be part of an array when returned from PHP?
  55.        e.g. setting it to "birthday", would create "birthday[Day]",
  56.        "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  57.     $field_array     null;
  58.     /* <select size>'s of the different <select> tags.
  59.        If not set, uses default dropdown. */
  60.     $day_size        null;
  61.     $month_size      null;
  62.     $year_size       null;
  63.     /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  64.        An example might be in the template: all_extra ='class ="foo"'. */
  65.     $all_extra       null;
  66.     /* Separate attributes for the tags. */
  67.     $day_extra       null;
  68.     $month_extra     null;
  69.     $year_extra      null;
  70.     /* Order in which to display the fields.
  71.        "D" -> day, "M" -> month, "Y" -> year. */
  72.     $field_order      'MDY';
  73.     /* String printed between the different fields. */
  74.     $field_separator "\n";
  75.     $time time();
  76.  
  77.     extract($params);
  78.     // If $time is not in format yyyy-mm-dd
  79.     if (!preg_match('/^\d{4}-\d{2}-\d{2}$/'$time)) {
  80.         // then $time is empty or unix timestamp or mysql timestamp
  81.         // strftime to make yyyy-mm-dd
  82.           // Just in case the offset moves us into another day.
  83.         $date new BitDate(0);
  84.         // sets the offset for the user - necessary because BitDate is a bitwack
  85.         $offset $date->get_display_offset();
  86.         $time $date->getDisplayDateFromUTC$time );
  87.         $time $gBitSystem->mServerTimestamp->strftime('%Y-%m-%d'$timeTRUE);
  88.     }
  89.     // Now split this in pieces, which later can be used to set the select
  90.     $time explode("-"$time);
  91.  
  92.     // make syntax "+N" or "-N" work with start_year and end_year
  93.     if (preg_match('!^(\+|\-)\s*(\d+)$!'$end_year$match)) {
  94.         if ($match[1== '+'{
  95.             $end_year strftime('%Y'$match[2];
  96.         else {
  97.             $end_year strftime('%Y'$match[2];
  98.         }
  99.     }
  100.     if (preg_match('!^(\+|\-)\s*(\d+)$!'$start_year$match)) {
  101.         if ($match[1== '+'{
  102.             $start_year strftime('%Y'$match[2];
  103.         else {
  104.             $start_year strftime('%Y'$match[2];
  105.         }
  106.     }
  107.  
  108.     $field_order strtoupper($field_order);
  109.  
  110.     $html_result $month_result $day_result $year_result "";
  111.  
  112.     if ($display_months{
  113.         $month_names array();
  114.         $month_values array();
  115.  
  116.         for ($i 1$i <= 12$i++{
  117.  
  118.             // date: 2003/03/20 22:54:34;  author: ohertel;  state: Exp;  lines: +1 -1
  119.             // added many missing translation blocks and german translations for user admin page, newsreader, notepad and bookmarks, month from html_select_date is being translated now
  120.             // -            $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
  121.             // +            $month_names[] = tra(strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)));
  122.  
  123.             $month_names[tra(strftime($month_formatmktime(000$i12000)));
  124.             $month_values[strftime($month_value_formatmktime(000$i12000));
  125.         }
  126.  
  127.         $month_result .= '<select name=';
  128.         if (null !== $field_array){
  129.             $month_result .= '"' $field_array '[' $prefix 'Month]"';
  130.         else {
  131.             $month_result .= '"' $prefix 'Month"';
  132.         }
  133.         if (null !== $month_size){
  134.             $month_result .= ' size="' $month_size '"';
  135.         }
  136.         if (null !== $month_extra){
  137.             $month_result .= ' ' $month_extra;
  138.         }
  139.         if (null !== $all_extra){
  140.             $month_result .= ' ' $all_extra;
  141.         }
  142.         $month_result .= '>'."\n";
  143.  
  144.         $month_result .= smarty_function_html_options(array('output'     => $month_names,
  145.                                                             'values'     => $month_values,
  146.                                                             'selected'   => $month_values[$time[1]-1],
  147.                                                             'print_result' => false),
  148.                                                       $pSmarty);
  149.  
  150.         $month_result .= '</select>';
  151.     }
  152.  
  153.     if ($display_days{
  154.         $days array();
  155.         for ($i 1$i <= 31$i++{
  156.             $days[sprintf($day_format$i);
  157.             $day_values[sprintf($day_value_format$i);
  158.         }
  159.  
  160.         $day_result .= '<select name=';
  161.         if (null !== $field_array){
  162.             $day_result .= '"' $field_array '[' $prefix 'Day]"';
  163.         else {
  164.             $day_result .= '"' $prefix 'Day"';
  165.         }
  166.         if (null !== $day_size){
  167.             $day_result .= ' size="' $day_size '"';
  168.         }
  169.         if (null !== $all_extra){
  170.             $day_result .= ' ' $all_extra;
  171.         }
  172.         if (null !== $day_extra){
  173.             $day_result .= ' ' $day_extra;
  174.         }
  175.         $day_result .= '>'."\n";
  176.         $day_result .= smarty_function_html_options(array('output'     => $days,
  177.                                                           'values'     => $day_values,
  178.                                                           'selected'   => $time[2],
  179.                                                           'print_result' => false),
  180.                                                     $pSmarty);
  181.         $day_result .= '</select>';
  182.     }
  183.  
  184.     if ($display_years{
  185.         if (null !== $field_array){
  186.             $year_name $field_array '[' $prefix 'Year]';
  187.         else {
  188.             $year_name $prefix 'Year';
  189.         }
  190.         if ($year_as_text{
  191.             $year_result .= '<input type="text" name="' $year_name '" value="' $time[0'" size="4" maxlength="4"';
  192.             if (null !== $all_extra){
  193.                 $year_result .= ' ' $all_extra;
  194.             }
  195.             if (null !== $year_extra){
  196.                 $year_result .= ' ' $year_extra;
  197.             }
  198.             $year_result .= '>';
  199.         else {
  200.             $years range((int)$start_year(int)$end_year);
  201.             if ($reverse_years{
  202.                 rsort($yearsSORT_NUMERIC);
  203.             }
  204.  
  205.             $year_result .= '<select name="' $year_name '"';
  206.             if (null !== $year_size){
  207.                 $year_result .= ' size="' $year_size '"';
  208.             }
  209.             if (null !== $all_extra){
  210.                 $year_result .= ' ' $all_extra;
  211.             }
  212.             if (null !== $year_extra){
  213.                 $year_result .= ' ' $year_extra;
  214.             }
  215.             $year_result .= '>'."\n";
  216.             $year_result .= smarty_function_html_options(array('output' => $years,
  217.                                                                'values' => $years,
  218.                                                                'selected'   => $time[0],
  219.                                                                'print_result' => false),
  220.                                                          $pSmarty);
  221.             $year_result .= '</select>';
  222.         }
  223.     }
  224.  
  225.     // Loop thru the field_order field
  226.     for ($i 0$i <= 2$i++){
  227.       $c substr($field_order$i1);
  228.       switch ($c){
  229.         case 'D':
  230.             $html_result .= $day_result;
  231.             break;
  232.  
  233.         case 'M':
  234.             $html_result .= $month_result;
  235.             break;
  236.  
  237.         case 'Y':
  238.             $html_result .= $year_result;
  239.             break;
  240.       }
  241.       // Add the field seperator
  242.       if($i != 2{
  243.           $html_result .= $field_separator;
  244.         }
  245.     }
  246.  
  247.     return $html_result;
  248. }
  249.  
  250. /* vim: set expandtab: */
  251.  
  252. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:38 +0000 by phpDocumentor 1.5.0-lsces