/mzz/libs/smarty/plugins/function.html_table.php

https://github.com/greevex/mzz-framework-blank-application · PHP · 178 lines · 108 code · 19 blank · 51 comment · 23 complexity · 4736406de820554f1d9516bdca286f10 MD5 · raw file

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