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

Source for file function.bit_select_datetime.php

Documentation is available at function.bit_select_datetime.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * 
  5.  * 
  6.  * 
  7.  * @package Smarty
  8.  * @subpackage plugins
  9.  */
  10.  
  11. /**
  12.  *  smarty_function_bit_select_datetime
  13.  *
  14.  *    NOTE: This code looks good but needs intensive testing, especially with different date/time formats.
  15.  *
  16.  *    This function generates HTML code that adds a date picker to an HTML form.
  17.  *    Depending on Bitweaver settings (Administration/Themes/Theme Settings)
  18.  *    this can be the ordinary Smarty way of date/time picking (see html_select_date and html_select_time functions)
  19.  *    or a nice javascript calendar.
  20.  *
  21.  *    Parameters:
  22.  *    name    The name of the inputfield. Use this to identify the date/datetime input from other inputs. (will be used for <input name="..., defaults to 'date'.
  23.  *    showtime    defines whether you need a date or datetime picking method. Set to 'true' or 'false', defaults to 'true'.
  24.  *    format    The datetime format used to display/return the timestamp. Defaults to the user's preference on this Bitweaver system.
  25.  *    time    The time value to be displayed, in the format given in the format parameter. Defaults to the current system time.
  26.  *
  27.  *    Usage sample:
  28.  *    <form action="edit.php" method="POST">
  29.  *        {formlabel label="Test JSCalendar"}
  30.  *        {forminput}
  31.  *            {bit_select_datetime name="mydate2" time=$gContent->mInfo.start}
  32.  *        {/forminput}
  33.  *    </form>
  34.  *
  35.  *    Later, in edit.php, use this code to obtain the timestamp in UTC:
  36.  *    $timestamp = $gBitSystem->mServerTimestamp->getUTCFromDisplayDate(_REQUEST['mydate2'])
  37.  *
  38.  */
  39. function smarty_function_bit_select_datetime$pParams&$gBitSmarty {
  40.     global $gBitSystem;
  41.     global $gBitUser;
  42.  
  43.     // Default values
  44.     $name         'date';                   // ID of the input field
  45.     // unsupported as of now $format       = $gBitSystem->getConfig( 'site_short_date_format' ).' '.$gBitSystem->getConfig( 'site_short_time_format' );      // date format used
  46.     $showtime     'true';                   //true: show time; false: pick date only
  47.     $time         =  time();                   // override the currently set date
  48.  
  49.     //extract actual parameters from the params hashmap.
  50.     extract$pParams );
  51.  
  52.     //calculate a name we can use for additional (internal) fields
  53.     $nname str_replace('[''_'str_replace(']''_'$name));
  54.  
  55.     if$gBitSystem->isFeatureActive'site_use_jscalendar' ) ) {
  56.         // A readonly field will be used to display the currently selected value.
  57.         //A button besides the field will bring up the calendar (style similar to other PIM rich client applications)
  58.         //It is the readonly input field that will be evaluated back on the server
  59.  
  60.         //unsupported $format = preg_replace( "/%Z/", "", $format );  // JSCalendar does not know about time zones
  61.         $html_result "<input type=\"text\" name=\"$name\" id=\"${nname}_id\" value=\"$time\" readonly />\n";
  62.         $html_result $html_result "<button type=\"reset\" id=\"${nname}_button\">...</button>\n";
  63.         $html_result $html_result "<script type=\"text/javascript\">\n";
  64.         $html_result $html_result "    Calendar.setup({\n";
  65.         $html_result $html_result "        date        : \"$time\",\n";
  66.         $html_result $html_result "        inputField  :    \"${nname}_id\",      // id of the input field\n";
  67.         $html_result $html_result "        ifFormat    :    \"%Y-%m-%d %H:%M\",       // format of the input field\n";
  68.         $html_result $html_result "        showsTime   :    $showtime,            // will display a time selector\n";
  69.         $html_result $html_result "        button      :    \"${nname}_button\",   // trigger for the calendar (button ID)\n";
  70.         $html_result $html_result "        singleClick :    true,           // double-click mode\n";
  71.         $html_result $html_result "        step        :    1                // show all years in drop-down boxes (instead of every other year as default)\n";
  72.         $html_result $html_result "    });\n";
  73.         $html_result $html_result "</script>\n";
  74.     else {
  75.         $gBitSmarty->loadPlugin'smarty_modifier_html_select_date' );
  76.         $gBitSmarty->loadPlugin'smarty_modifier_html_select_time' );
  77.  
  78.         // we use html_select_date and html_select_time to pick a date, which generate a number of select fields.
  79.         //On every change a hidden field will be updated via javascript.
  80.         //it's the hidden field that is evaluated back on the server.
  81.  
  82.         $pDate array (
  83.             'prefix' => $nname,
  84.             'all_extra' => "onchange=\"bit_select_datetime_${nname}()\"",
  85.             'time' => $time
  86.         );
  87.  
  88.         $pTime array (
  89.             'prefix' => $nname,
  90.             'all_extra' => "onchange=\"bit_select_datetime_${nname}()\"",
  91.             'display_seconds' => false,
  92.             'time' => $time
  93.         );
  94.  
  95.         $html_result  "<input type=\"hidden\" name=\"$name\" value=\"${time}\">";
  96.         $html_result .= smarty_function_html_select_date$pDate$gBitSmarty );
  97.         if$showtime == 'true' {
  98.             $html_result .= smarty_function_html_select_time$pTime$gBitSmarty );
  99.             $html_result .= "<script type=\"text/javascript\"> \n";
  100.             $html_result .= "    function bit_select_datetime_${nname} () {\n";
  101.             $html_result .= "        var date = new Date(); \n date.setHours ( document.getElementsByName(\"${nname}Hour\")[0].value);\ndate.setMinutes( document.getElementsByName(\"${nname}Minute\")[0].value); \n date.setFullYear(document.getElementsByName(\"${nname}Year\")[0].value,document.getElementsByName(\"${nname}Month\")[0].value-1,document.getElementsByName(\"${nname}Day\")[0].value); \n ";
  102.             $html_result .= "document.getElementsByName(\"${name}\")[0].value = Math.floor(date.getTime() / 1000);";
  103.             $html_result .= "}\n";
  104.             $html_result .= "</script>\n";
  105.         else {
  106.             $html_result .= "<script type=\"text/javascript\">\n";
  107.             $html_result .= "    function bit_select_datetime_${name} () {\n";
  108.             $html_result .= "        var date = new Date(); \n date.setDate( document.getElementsByName(\"${nname}Day\")[0].value ); \n date.setMonth(document.getElementsByName(\"${nname}Month\")[0].value-1); \n date.setFullYear(document.getElementsByName(\"${nname}Year\")[0].value); \n ";
  109.             $html_result .= "        document.getElementsByName(\"${name}\")[0].value = Math.floor(date.getTime() / 1000);";
  110.             $html_result .= "}\n";
  111.             $html_result .= "</script>\n";
  112.         }
  113.     }
  114.  
  115.     return $html_result."(".$gBitUser->getPreference('site_display_utc').")\n";
  116. }
  117. ?>

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