PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/smarty/plugins/function.html_table.php

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