PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/external_lib/Smarty/plugins/function.html_table.php

https://github.com/modulargaming/kittokittokitto
PHP | 176 lines | 107 code | 18 blank | 51 comment | 23 complexity | 68b04a3cf6d9fcc8f7f3d95d6417c027 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. if (!isset($params['loop'])) {
  64. throw new Exception ("html_table: missing 'loop' parameter");
  65. return;
  66. }
  67. foreach ($params as $_key => $_value) {
  68. switch ($_key) {
  69. case 'loop':
  70. $$_key = (array)$_value;
  71. break;
  72. case 'cols':
  73. if (is_array($_value) && !empty($_value)) {
  74. $cols = $_value;
  75. $cols_count = count($_value);
  76. } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
  77. $cols = explode(',', $_value);
  78. $cols_count = count($cols);
  79. } elseif (!empty($_value)) {
  80. $cols_count = (int)$_value;
  81. } else {
  82. $cols_count = $cols;
  83. }
  84. break;
  85. case 'rows':
  86. $$_key = (int)$_value;
  87. break;
  88. case 'table_attr':
  89. case 'trailpad':
  90. case 'hdir':
  91. case 'vdir':
  92. case 'inner':
  93. case 'caption':
  94. $$_key = (string)$_value;
  95. break;
  96. case 'tr_attr':
  97. case 'td_attr':
  98. case 'th_attr':
  99. $$_key = $_value;
  100. break;
  101. }
  102. }
  103. $loop_count = count($loop);
  104. if (empty($params['rows'])) {
  105. /* no rows specified */
  106. $rows = ceil($loop_count / $cols_count);
  107. } elseif (empty($params['cols'])) {
  108. if (!empty($params['rows'])) {
  109. /* no cols specified, but rows */
  110. $cols_count = ceil($loop_count / $rows);
  111. }
  112. }
  113. $output = "<table $table_attr>\n";
  114. if (!empty($caption)) {
  115. $output .= '<caption>' . $caption . "</caption>\n";
  116. }
  117. if (is_array($cols)) {
  118. $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
  119. $output .= "<thead><tr>\n";
  120. for ($r = 0; $r < $cols_count; $r++) {
  121. $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
  122. $output .= $cols[$r];
  123. $output .= "</th>\n";
  124. }
  125. $output .= "</tr></thead>\n";
  126. }
  127. $output .= "<tbody>\n";
  128. for ($r = 0; $r < $rows; $r++) {
  129. $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
  130. $rx = ($vdir == 'down') ? $r * $cols_count : ($rows-1 - $r) * $cols_count;
  131. for ($c = 0; $c < $cols_count; $c++) {
  132. $x = ($hdir == 'right') ? $rx + $c : $rx + $cols_count-1 - $c;
  133. if ($inner != 'cols') {
  134. /* shuffle x to loop over rows*/
  135. $x = floor($x / $cols_count) + ($x % $cols_count) * $rows;
  136. }
  137. if ($x < $loop_count) {
  138. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
  139. } else {
  140. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
  141. }
  142. }
  143. $output .= "</tr>\n";
  144. }
  145. $output .= "</tbody>\n";
  146. $output .= "</table>\n";
  147. return $output;
  148. }
  149. function smarty_function_html_table_cycle($name, $var, $no)
  150. {
  151. if (!is_array($var)) {
  152. $ret = $var;
  153. } else {
  154. $ret = $var[$no % count($var)];
  155. }
  156. return ($ret) ? ' ' . $ret : '';
  157. }
  158. ?>