Automatically exported from code.google.com/p/planningalerts
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

178 linhas
5.2 KiB

  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_table} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: html_table<br>
  12. * Date: Feb 17, 2003<br>
  13. * Purpose: make an html table from an array of data<br>
  14. * Input:<br>
  15. * - loop = array to loop through
  16. * - cols = number of columns, comma separated list of column names
  17. * or array of column names
  18. * - rows = number of rows
  19. * - table_attr = table attributes
  20. * - th_attr = table heading attributes (arrays are cycled)
  21. * - tr_attr = table row attributes (arrays are cycled)
  22. * - td_attr = table cell attributes (arrays are cycled)
  23. * - trailpad = value to pad trailing cells with
  24. * - caption = text for caption element
  25. * - vdir = vertical direction (default: "down", means top-to-bottom)
  26. * - hdir = horizontal direction (default: "right", means left-to-right)
  27. * - inner = inner loop (default "cols": print $loop line by line,
  28. * $loop will be printed column by column otherwise)
  29. *
  30. *
  31. * Examples:
  32. * <pre>
  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. * </pre>
  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://smarty.php.net/manual/en/language.function.html.table.php {html_table}
  42. * (Smarty online manual)
  43. * @param array
  44. * @param Smarty
  45. * @return string
  46. */
  47. function smarty_function_html_table($params, &$smarty)
  48. {
  49. $table_attr = 'border="1"';
  50. $tr_attr = '';
  51. $th_attr = '';
  52. $td_attr = '';
  53. $cols = $cols_count = 3;
  54. $rows = 3;
  55. $trailpad = '&nbsp;';
  56. $vdir = 'down';
  57. $hdir = 'right';
  58. $inner = 'cols';
  59. $caption = '';
  60. if (!isset($params['loop'])) {
  61. $smarty->trigger_error("html_table: missing 'loop' parameter");
  62. return;
  63. }
  64. foreach ($params as $_key=>$_value) {
  65. switch ($_key) {
  66. case 'loop':
  67. $$_key = (array)$_value;
  68. break;
  69. case 'cols':
  70. if (is_array($_value) && !empty($_value)) {
  71. $cols = $_value;
  72. $cols_count = count($_value);
  73. } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
  74. $cols = explode(',', $_value);
  75. $cols_count = count($cols);
  76. } elseif (!empty($_value)) {
  77. $cols_count = (int)$_value;
  78. } else {
  79. $cols_count = $cols;
  80. }
  81. break;
  82. case 'rows':
  83. $$_key = (int)$_value;
  84. break;
  85. case 'table_attr':
  86. case 'trailpad':
  87. case 'hdir':
  88. case 'vdir':
  89. case 'inner':
  90. case 'caption':
  91. $$_key = (string)$_value;
  92. break;
  93. case 'tr_attr':
  94. case 'td_attr':
  95. case 'th_attr':
  96. $$_key = $_value;
  97. break;
  98. }
  99. }
  100. $loop_count = count($loop);
  101. if (empty($params['rows'])) {
  102. /* no rows specified */
  103. $rows = ceil($loop_count/$cols_count);
  104. } elseif (empty($params['cols'])) {
  105. if (!empty($params['rows'])) {
  106. /* no cols specified, but rows */
  107. $cols_count = ceil($loop_count/$rows);
  108. }
  109. }
  110. $output = "<table $table_attr>\n";
  111. if (!empty($caption)) {
  112. $output .= '<caption>' . $caption . "</caption>\n";
  113. }
  114. if (is_array($cols)) {
  115. $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
  116. $output .= "<thead><tr>\n";
  117. for ($r=0; $r<$cols_count; $r++) {
  118. $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
  119. $output .= $cols[$r];
  120. $output .= "</th>\n";
  121. }
  122. $output .= "</tr></thead>\n";
  123. }
  124. $output .= "<tbody>\n";
  125. for ($r=0; $r<$rows; $r++) {
  126. $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
  127. $rx = ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count;
  128. for ($c=0; $c<$cols_count; $c++) {
  129. $x = ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c;
  130. if ($inner!='cols') {
  131. /* shuffle x to loop over rows*/
  132. $x = floor($x/$cols_count) + ($x%$cols_count)*$rows;
  133. }
  134. if ($x<$loop_count) {
  135. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
  136. } else {
  137. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
  138. }
  139. }
  140. $output .= "</tr>\n";
  141. }
  142. $output .= "</tbody>\n";
  143. $output .= "</table>\n";
  144. return $output;
  145. }
  146. function smarty_function_html_table_cycle($name, $var, $no) {
  147. if(!is_array($var)) {
  148. $ret = $var;
  149. } else {
  150. $ret = $var[$no % count($var)];
  151. }
  152. return ($ret) ? ' '.$ret : '';
  153. }
  154. /* vim: set expandtab: */
  155. ?>