[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/Smarty/libs/plugins/ -> function.html_table.php (source)

   1  <?php
   2  /**
   3   * Smarty plugin
   4   *
   5   * @package Smarty
   6   * @subpackage PluginsFunction
   7   */
   8  
   9  /**
  10   * Smarty {html_table} function plugin
  11   *
  12   * Type:     function<br>
  13   * Name:     html_table<br>
  14   * Date:     Feb 17, 2003<br>
  15   * Purpose:  make an html table from an array of data<br>
  16   * Params:
  17   * <pre>
  18   * - loop       - array to loop through
  19   * - cols       - number of columns, comma separated list of column names
  20   *                or array of column names
  21   * - rows       - number of rows
  22   * - table_attr - table attributes
  23   * - th_attr    - table heading attributes (arrays are cycled)
  24   * - tr_attr    - table row attributes (arrays are cycled)
  25   * - td_attr    - table cell attributes (arrays are cycled)
  26   * - trailpad   - value to pad trailing cells with
  27   * - caption    - text for caption element
  28   * - vdir       - vertical direction (default: "down", means top-to-bottom)
  29   * - hdir       - horizontal direction (default: "right", means left-to-right)
  30   * - inner      - inner loop (default "cols": print $loop line by line,
  31   *                $loop will be printed column by column otherwise)
  32   * </pre>
  33   * Examples:
  34   * <pre>
  35   * {table loop=$data}
  36   * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
  37   * {table loop=$data cols="first,second,third" tr_attr=$colors}
  38   * </pre>
  39   *
  40   * @author Monte Ohrt <monte at ohrt dot com>
  41   * @author credit to Messju Mohr <messju at lammfellpuschen dot de>
  42   * @author credit to boots <boots dot smarty at yahoo dot com>
  43   * @version 1.1
  44   * @link http://www.smarty.net/manual/en/language.function.html.table.php {html_table}
  45   *          (Smarty online manual)
  46   * @param array                    $params   parameters
  47   * @param Smarty_Internal_Template $template template object
  48   * @return string
  49   */
  50  function smarty_function_html_table($params, $template)
  51  {
  52      $table_attr = 'border="1"';
  53      $tr_attr = '';
  54      $th_attr = '';
  55      $td_attr = '';
  56      $cols = $cols_count = 3;
  57      $rows = 3;
  58      $trailpad = '&nbsp;';
  59      $vdir = 'down';
  60      $hdir = 'right';
  61      $inner = 'cols';
  62      $caption = '';
  63      $loop = null;
  64  
  65      if (!isset($params['loop'])) {
  66          trigger_error("html_table: missing 'loop' parameter",E_USER_WARNING);
  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  {
 168      if (!is_array($var)) {
 169          $ret = $var;
 170      } else {
 171          $ret = $var[$no % count($var)];
 172      }
 173  
 174      return ($ret) ? ' ' . $ret : '';
 175  }
 176  
 177  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1