library/smarty/libs/plugins/function.html_select_time.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsFunction
       
     7  */
       
     8 
       
     9 /**
       
    10  * @ignore
       
    11  */
       
    12 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
       
    13 /**
       
    14  * @ignore
       
    15  */
       
    16 require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
       
    17 
       
    18 /**
       
    19  * Smarty {html_select_time} function plugin
       
    20  * Type:     function<br>
       
    21  * Name:     html_select_time<br>
       
    22  * Purpose:  Prints the dropdowns for time selection
       
    23  *
       
    24  * @link     http://www.smarty.net/manual/en/language.function.html.select.time.php {html_select_time}
       
    25  *           (Smarty online manual)
       
    26  * @author   Roberto Berto <roberto@berto.net>
       
    27  * @author   Monte Ohrt <monte AT ohrt DOT com>
       
    28  *
       
    29  * @param array $params parameters
       
    30  *
       
    31  * @return string
       
    32  * @uses     smarty_make_timestamp()
       
    33  */
       
    34 function smarty_function_html_select_time($params) {
       
    35     $prefix = "Time_";
       
    36     $field_array = null;
       
    37     $field_separator = "\n";
       
    38     $option_separator = "\n";
       
    39     $time = null;
       
    40 
       
    41     $display_hours = true;
       
    42     $display_minutes = true;
       
    43     $display_seconds = true;
       
    44     $display_meridian = true;
       
    45 
       
    46     $hour_format = '%02d';
       
    47     $hour_value_format = '%02d';
       
    48     $minute_format = '%02d';
       
    49     $minute_value_format = '%02d';
       
    50     $second_format = '%02d';
       
    51     $second_value_format = '%02d';
       
    52 
       
    53     $hour_size = null;
       
    54     $minute_size = null;
       
    55     $second_size = null;
       
    56     $meridian_size = null;
       
    57 
       
    58     $all_empty = null;
       
    59     $hour_empty = null;
       
    60     $minute_empty = null;
       
    61     $second_empty = null;
       
    62     $meridian_empty = null;
       
    63 
       
    64     $all_id = null;
       
    65     $hour_id = null;
       
    66     $minute_id = null;
       
    67     $second_id = null;
       
    68     $meridian_id = null;
       
    69 
       
    70     $use_24_hours = true;
       
    71     $minute_interval = 1;
       
    72     $second_interval = 1;
       
    73 
       
    74     $extra_attrs = '';
       
    75     $all_extra = null;
       
    76     $hour_extra = null;
       
    77     $minute_extra = null;
       
    78     $second_extra = null;
       
    79     $meridian_extra = null;
       
    80 
       
    81     foreach ($params as $_key => $_value) {
       
    82         switch ($_key) {
       
    83             case 'time':
       
    84                 if (!is_array($_value) && $_value !== null) {
       
    85                     $time = smarty_make_timestamp($_value);
       
    86                 }
       
    87                 break;
       
    88 
       
    89             case 'prefix':
       
    90             case 'field_array':
       
    91 
       
    92             case 'field_separator':
       
    93             case 'option_separator':
       
    94 
       
    95             case 'all_extra':
       
    96             case 'hour_extra':
       
    97             case 'minute_extra':
       
    98             case 'second_extra':
       
    99             case 'meridian_extra':
       
   100 
       
   101             case 'all_empty':
       
   102             case 'hour_empty':
       
   103             case 'minute_empty':
       
   104             case 'second_empty':
       
   105             case 'meridian_empty':
       
   106 
       
   107             case 'all_id':
       
   108             case 'hour_id':
       
   109             case 'minute_id':
       
   110             case 'second_id':
       
   111             case 'meridian_id':
       
   112 
       
   113             case 'hour_format':
       
   114             case 'hour_value_format':
       
   115             case 'minute_format':
       
   116             case 'minute_value_format':
       
   117             case 'second_format':
       
   118             case 'second_value_format':
       
   119                 $$_key = (string)$_value;
       
   120                 break;
       
   121 
       
   122             case 'display_hours':
       
   123             case 'display_minutes':
       
   124             case 'display_seconds':
       
   125             case 'display_meridian':
       
   126             case 'use_24_hours':
       
   127                 $$_key = (bool)$_value;
       
   128                 break;
       
   129 
       
   130             case 'minute_interval':
       
   131             case 'second_interval':
       
   132 
       
   133             case 'hour_size':
       
   134             case 'minute_size':
       
   135             case 'second_size':
       
   136             case 'meridian_size':
       
   137                 $$_key = (int)$_value;
       
   138                 break;
       
   139 
       
   140             default:
       
   141                 if (!is_array($_value)) {
       
   142                     $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
       
   143                 } else {
       
   144                     trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
       
   145                 }
       
   146                 break;
       
   147         }
       
   148     }
       
   149 
       
   150     if (isset($params['time']) && is_array($params['time'])) {
       
   151         if (isset($params['time'][$prefix . 'Hour'])) {
       
   152             // $_REQUEST[$field_array] given
       
   153             foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
       
   154                 $_variableName = '_' . strtolower($_elementName);
       
   155                 $$_variableName = isset($params['time'][$prefix . $_elementName])
       
   156                     ? $params['time'][$prefix . $_elementName]
       
   157                     : date($_elementKey);
       
   158             }
       
   159             $_meridian = isset($params['time'][$prefix . 'Meridian'])
       
   160                 ? (' ' . $params['time'][$prefix . 'Meridian'])
       
   161                 : '';
       
   162             $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
       
   163             list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
       
   164         } elseif (isset($params['time'][$field_array][$prefix . 'Hour'])) {
       
   165             // $_REQUEST given
       
   166             foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
       
   167                 $_variableName = '_' . strtolower($_elementName);
       
   168                 $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName])
       
   169                     ? $params['time'][$field_array][$prefix . $_elementName]
       
   170                     : date($_elementKey);
       
   171             }
       
   172             $_meridian = isset($params['time'][$field_array][$prefix . 'Meridian'])
       
   173                 ? (' ' . $params['time'][$field_array][$prefix . 'Meridian'])
       
   174                 : '';
       
   175             $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
       
   176             list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
       
   177         } else {
       
   178             // no date found, use NOW
       
   179             list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
       
   180         }
       
   181     } elseif ($time === null) {
       
   182         if (array_key_exists('time', $params)) {
       
   183             $_hour = $_minute = $_second = $time = null;
       
   184         } else {
       
   185             list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s'));
       
   186         }
       
   187     } else {
       
   188         list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
       
   189     }
       
   190 
       
   191     // generate hour <select>
       
   192     if ($display_hours) {
       
   193         $_html_hours = '';
       
   194         $_extra = '';
       
   195         $_name = $field_array ? ($field_array . '[' . $prefix . 'Hour]') : ($prefix . 'Hour');
       
   196         if ($all_extra) {
       
   197             $_extra .= ' ' . $all_extra;
       
   198         }
       
   199         if ($hour_extra) {
       
   200             $_extra .= ' ' . $hour_extra;
       
   201         }
       
   202 
       
   203         $_html_hours = '<select name="' . $_name . '"';
       
   204         if ($hour_id !== null || $all_id !== null) {
       
   205             $_html_hours .= ' id="' . smarty_function_escape_special_chars(
       
   206                     $hour_id !== null ? ($hour_id ? $hour_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
       
   207                 ) . '"';
       
   208         }
       
   209         if ($hour_size) {
       
   210             $_html_hours .= ' size="' . $hour_size . '"';
       
   211         }
       
   212         $_html_hours .= $_extra . $extra_attrs . '>' . $option_separator;
       
   213 
       
   214         if (isset($hour_empty) || isset($all_empty)) {
       
   215             $_html_hours .= '<option value="">' . (isset($hour_empty) ? $hour_empty : $all_empty) . '</option>' . $option_separator;
       
   216         }
       
   217 
       
   218         $start = $use_24_hours ? 0 : 1;
       
   219         $end = $use_24_hours ? 23 : 12;
       
   220         for ($i = $start; $i <= $end; $i++) {
       
   221             $_val = sprintf('%02d', $i);
       
   222             $_text = $hour_format == '%02d' ? $_val : sprintf($hour_format, $i);
       
   223             $_value = $hour_value_format == '%02d' ? $_val : sprintf($hour_value_format, $i);
       
   224 
       
   225             if (!$use_24_hours) {
       
   226                 $_hour12 = $_hour == 0
       
   227                     ? 12
       
   228                     : ($_hour <= 12 ? $_hour : $_hour - 12);
       
   229             }
       
   230 
       
   231             $selected = $_hour !== null ? ($use_24_hours ? $_hour == $_val : $_hour12 == $_val) : null;
       
   232             $_html_hours .= '<option value="' . $_value . '"'
       
   233                 . ($selected ? ' selected="selected"' : '')
       
   234                 . '>' . $_text . '</option>' . $option_separator;
       
   235         }
       
   236 
       
   237         $_html_hours .= '</select>';
       
   238     }
       
   239 
       
   240     // generate minute <select>
       
   241     if ($display_minutes) {
       
   242         $_html_minutes = '';
       
   243         $_extra = '';
       
   244         $_name = $field_array ? ($field_array . '[' . $prefix . 'Minute]') : ($prefix . 'Minute');
       
   245         if ($all_extra) {
       
   246             $_extra .= ' ' . $all_extra;
       
   247         }
       
   248         if ($minute_extra) {
       
   249             $_extra .= ' ' . $minute_extra;
       
   250         }
       
   251 
       
   252         $_html_minutes = '<select name="' . $_name . '"';
       
   253         if ($minute_id !== null || $all_id !== null) {
       
   254             $_html_minutes .= ' id="' . smarty_function_escape_special_chars(
       
   255                     $minute_id !== null ? ($minute_id ? $minute_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
       
   256                 ) . '"';
       
   257         }
       
   258         if ($minute_size) {
       
   259             $_html_minutes .= ' size="' . $minute_size . '"';
       
   260         }
       
   261         $_html_minutes .= $_extra . $extra_attrs . '>' . $option_separator;
       
   262 
       
   263         if (isset($minute_empty) || isset($all_empty)) {
       
   264             $_html_minutes .= '<option value="">' . (isset($minute_empty) ? $minute_empty : $all_empty) . '</option>' . $option_separator;
       
   265         }
       
   266 
       
   267         $selected = $_minute !== null ? ($_minute - $_minute % $minute_interval) : null;
       
   268         for ($i = 0; $i <= 59; $i += $minute_interval) {
       
   269             $_val = sprintf('%02d', $i);
       
   270             $_text = $minute_format == '%02d' ? $_val : sprintf($minute_format, $i);
       
   271             $_value = $minute_value_format == '%02d' ? $_val : sprintf($minute_value_format, $i);
       
   272             $_html_minutes .= '<option value="' . $_value . '"'
       
   273                 . ($selected === $i ? ' selected="selected"' : '')
       
   274                 . '>' . $_text . '</option>' . $option_separator;
       
   275         }
       
   276 
       
   277         $_html_minutes .= '</select>';
       
   278     }
       
   279 
       
   280     // generate second <select>
       
   281     if ($display_seconds) {
       
   282         $_html_seconds = '';
       
   283         $_extra = '';
       
   284         $_name = $field_array ? ($field_array . '[' . $prefix . 'Second]') : ($prefix . 'Second');
       
   285         if ($all_extra) {
       
   286             $_extra .= ' ' . $all_extra;
       
   287         }
       
   288         if ($second_extra) {
       
   289             $_extra .= ' ' . $second_extra;
       
   290         }
       
   291 
       
   292         $_html_seconds = '<select name="' . $_name . '"';
       
   293         if ($second_id !== null || $all_id !== null) {
       
   294             $_html_seconds .= ' id="' . smarty_function_escape_special_chars(
       
   295                     $second_id !== null ? ($second_id ? $second_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
       
   296                 ) . '"';
       
   297         }
       
   298         if ($second_size) {
       
   299             $_html_seconds .= ' size="' . $second_size . '"';
       
   300         }
       
   301         $_html_seconds .= $_extra . $extra_attrs . '>' . $option_separator;
       
   302 
       
   303         if (isset($second_empty) || isset($all_empty)) {
       
   304             $_html_seconds .= '<option value="">' . (isset($second_empty) ? $second_empty : $all_empty) . '</option>' . $option_separator;
       
   305         }
       
   306 
       
   307         $selected = $_second !== null ? ($_second - $_second % $second_interval) : null;
       
   308         for ($i = 0; $i <= 59; $i += $second_interval) {
       
   309             $_val = sprintf('%02d', $i);
       
   310             $_text = $second_format == '%02d' ? $_val : sprintf($second_format, $i);
       
   311             $_value = $second_value_format == '%02d' ? $_val : sprintf($second_value_format, $i);
       
   312             $_html_seconds .= '<option value="' . $_value . '"'
       
   313                 . ($selected === $i ? ' selected="selected"' : '')
       
   314                 . '>' . $_text . '</option>' . $option_separator;
       
   315         }
       
   316 
       
   317         $_html_seconds .= '</select>';
       
   318     }
       
   319 
       
   320     // generate meridian <select>
       
   321     if ($display_meridian && !$use_24_hours) {
       
   322         $_html_meridian = '';
       
   323         $_extra = '';
       
   324         $_name = $field_array ? ($field_array . '[' . $prefix . 'Meridian]') : ($prefix . 'Meridian');
       
   325         if ($all_extra) {
       
   326             $_extra .= ' ' . $all_extra;
       
   327         }
       
   328         if ($meridian_extra) {
       
   329             $_extra .= ' ' . $meridian_extra;
       
   330         }
       
   331 
       
   332         $_html_meridian = '<select name="' . $_name . '"';
       
   333         if ($meridian_id !== null || $all_id !== null) {
       
   334             $_html_meridian .= ' id="' . smarty_function_escape_special_chars(
       
   335                     $meridian_id !== null ? ($meridian_id ? $meridian_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
       
   336                 ) . '"';
       
   337         }
       
   338         if ($meridian_size) {
       
   339             $_html_meridian .= ' size="' . $meridian_size . '"';
       
   340         }
       
   341         $_html_meridian .= $_extra . $extra_attrs . '>' . $option_separator;
       
   342 
       
   343         if (isset($meridian_empty) || isset($all_empty)) {
       
   344             $_html_meridian .= '<option value="">' . (isset($meridian_empty) ? $meridian_empty : $all_empty) . '</option>' . $option_separator;
       
   345         }
       
   346 
       
   347         $_html_meridian .= '<option value="am"' . ($_hour > 0 && $_hour < 12 ? ' selected="selected"' : '') . '>AM</option>' . $option_separator
       
   348             . '<option value="pm"' . ($_hour < 12 ? '' : ' selected="selected"') . '>PM</option>' . $option_separator
       
   349             . '</select>';
       
   350     }
       
   351 
       
   352     $_html = '';
       
   353     foreach (array('_html_hours', '_html_minutes', '_html_seconds', '_html_meridian') as $k) {
       
   354         if (isset($$k)) {
       
   355             if ($_html) {
       
   356                 $_html .= $field_separator;
       
   357             }
       
   358             $_html .= $$k;
       
   359         }
       
   360     }
       
   361 
       
   362     return $_html;
       
   363 }