library/smarty/libs/plugins/function.html_table.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsFunction
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty {html_table} function plugin
       
    11  * Type:     function<br>
       
    12  * Name:     html_table<br>
       
    13  * Date:     Feb 17, 2003<br>
       
    14  * Purpose:  make an html table from an array of data<br>
       
    15  * Params:
       
    16  * <pre>
       
    17  * - loop       - array to loop through
       
    18  * - cols       - number of columns, comma separated list of column names
       
    19  *                or array of column names
       
    20  * - rows       - number of rows
       
    21  * - table_attr - table attributes
       
    22  * - th_attr    - table heading attributes (arrays are cycled)
       
    23  * - tr_attr    - table row attributes (arrays are cycled)
       
    24  * - td_attr    - table cell attributes (arrays are cycled)
       
    25  * - trailpad   - value to pad trailing cells with
       
    26  * - caption    - text for caption element
       
    27  * - vdir       - vertical direction (default: "down", means top-to-bottom)
       
    28  * - hdir       - horizontal direction (default: "right", means left-to-right)
       
    29  * - inner      - inner loop (default "cols": print $loop line by line,
       
    30  *                $loop will be printed column by column otherwise)
       
    31  * </pre>
       
    32  * Examples:
       
    33  * <pre>
       
    34  * {table loop=$data}
       
    35  * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
       
    36  * {table loop=$data cols="first,second,third" tr_attr=$colors}
       
    37  * </pre>
       
    38  *
       
    39  * @author   Monte Ohrt <monte at ohrt dot com>
       
    40  * @author   credit to Messju Mohr <messju at lammfellpuschen dot de>
       
    41  * @author   credit to boots <boots dot smarty at yahoo dot com>
       
    42  * @version  1.1
       
    43  * @link     http://www.smarty.net/manual/en/language.function.html.table.php {html_table}
       
    44  *           (Smarty online manual)
       
    45  *
       
    46  * @param array $params parameters
       
    47  *
       
    48  * @return string
       
    49  */
       
    50 function smarty_function_html_table($params) {
       
    51     $table_attr = 'border="1"';
       
    52     $tr_attr = '';
       
    53     $th_attr = '';
       
    54     $td_attr = '';
       
    55     $cols = $cols_count = 3;
       
    56     $rows = 3;
       
    57     $trailpad = '&nbsp;';
       
    58     $vdir = 'down';
       
    59     $hdir = 'right';
       
    60     $inner = 'cols';
       
    61     $caption = '';
       
    62     $loop = null;
       
    63 
       
    64     if (!isset($params['loop'])) {
       
    65         trigger_error("html_table: missing 'loop' parameter", E_USER_WARNING);
       
    66 
       
    67         return;
       
    68     }
       
    69 
       
    70     foreach ($params as $_key => $_value) {
       
    71         switch ($_key) {
       
    72             case 'loop':
       
    73                 $$_key = (array)$_value;
       
    74                 break;
       
    75 
       
    76             case 'cols':
       
    77                 if (is_array($_value) && !empty($_value)) {
       
    78                     $cols = $_value;
       
    79                     $cols_count = count($_value);
       
    80                 } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
       
    81                     $cols = explode(',', $_value);
       
    82                     $cols_count = count($cols);
       
    83                 } elseif (!empty($_value)) {
       
    84                     $cols_count = (int)$_value;
       
    85                 } else {
       
    86                     $cols_count = $cols;
       
    87                 }
       
    88                 break;
       
    89 
       
    90             case 'rows':
       
    91                 $$_key = (int)$_value;
       
    92                 break;
       
    93 
       
    94             case 'table_attr':
       
    95             case 'trailpad':
       
    96             case 'hdir':
       
    97             case 'vdir':
       
    98             case 'inner':
       
    99             case 'caption':
       
   100                 $$_key = (string)$_value;
       
   101                 break;
       
   102 
       
   103             case 'tr_attr':
       
   104             case 'td_attr':
       
   105             case 'th_attr':
       
   106                 $$_key = $_value;
       
   107                 break;
       
   108         }
       
   109     }
       
   110 
       
   111     $loop_count = count($loop);
       
   112     if (empty($params['rows'])) {
       
   113         /* no rows specified */
       
   114         $rows = ceil($loop_count / $cols_count);
       
   115     } elseif (empty($params['cols'])) {
       
   116         if (!empty($params['rows'])) {
       
   117             /* no cols specified, but rows */
       
   118             $cols_count = ceil($loop_count / $rows);
       
   119         }
       
   120     }
       
   121 
       
   122     $output = "<table $table_attr>\n";
       
   123 
       
   124     if (!empty($caption)) {
       
   125         $output .= '<caption>' . $caption . "</caption>\n";
       
   126     }
       
   127 
       
   128     if (is_array($cols)) {
       
   129         $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
       
   130         $output .= "<thead><tr>\n";
       
   131 
       
   132         for ($r = 0; $r < $cols_count; $r++) {
       
   133             $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
       
   134             $output .= $cols[$r];
       
   135             $output .= "</th>\n";
       
   136         }
       
   137         $output .= "</tr></thead>\n";
       
   138     }
       
   139 
       
   140     $output .= "<tbody>\n";
       
   141     for ($r = 0; $r < $rows; $r++) {
       
   142         $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
       
   143         $rx = ($vdir == 'down') ? $r * $cols_count : ($rows - 1 - $r) * $cols_count;
       
   144 
       
   145         for ($c = 0; $c < $cols_count; $c++) {
       
   146             $x = ($hdir == 'right') ? $rx + $c : $rx + $cols_count - 1 - $c;
       
   147             if ($inner != 'cols') {
       
   148                 /* shuffle x to loop over rows*/
       
   149                 $x = floor($x / $cols_count) + ($x % $cols_count) * $rows;
       
   150             }
       
   151 
       
   152             if ($x < $loop_count) {
       
   153                 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
       
   154             } else {
       
   155                 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
       
   156             }
       
   157         }
       
   158         $output .= "</tr>\n";
       
   159     }
       
   160     $output .= "</tbody>\n";
       
   161     $output .= "</table>\n";
       
   162 
       
   163     return $output;
       
   164 }
       
   165 
       
   166 function smarty_function_html_table_cycle($name, $var, $no) {
       
   167     if (!is_array($var)) {
       
   168         $ret = $var;
       
   169     } else {
       
   170         $ret = $var[$no % count($var)];
       
   171     }
       
   172 
       
   173     return ($ret) ? ' ' . $ret : '';
       
   174 }