PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/bedita-app/vendors/_smartyPlugins/function.html_table_x.php

http://bedita.googlecode.com/
PHP | 180 lines | 100 code | 32 blank | 48 comment | 19 complexity | 8a8b8d223b986bba9ba529f7fac070d6 MD5 | raw file
Possible License(s): AGPL-1.0, AGPL-3.0, LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. #### modified by xho to support TH (with attr and colspan)
  8. /**
  9. * Smarty {html_table} function plugin
  10. *
  11. * Type: function<br>
  12. * Name: html_table<br>
  13. * Date: Mar 17, 2004<br>
  14. * Purpose: make an html table from an array of data<br>
  15. * Input:<br>
  16. * - loop = array to loop through
  17. * - cols = number of columns
  18. * - rows = number of rows
  19. * - table_attr = table attributes
  20. * - tr_attr = table row attributes (arrays are cycled)
  21. * - td_attr = table cell attributes (arrays are cycled)
  22. * - trailpad = value to pad trailing cells with
  23. * - vdir = vertical direction (default: "down", means top-to-bottom)
  24. * - hdir = horizontal direction (default: "right", means left-to-right)
  25. * - inner = inner loop (default "cols": print $loop line by line,
  26. * $loop will be printed column by column otherwise)
  27. *
  28. *
  29. * Examples:
  30. * <pre>
  31. * {table loop=$data}
  32. * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
  33. * {table loop=$data cols=4 tr_attr=$colors}
  34. * </pre>
  35. * @author Monte Ohrt <monte@ispi.net>
  36. * @version 1.0
  37. * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
  38. * (Smarty online manual)
  39. * @param array
  40. * @param Smarty
  41. * @return string
  42. */
  43. function smarty_function_html_table_x($params, &$smarty)
  44. {
  45. $table_attr = 'border="1"';
  46. $tr_attr = '';
  47. $td_attr = '';
  48. $cols = 3;
  49. $rows = 3;
  50. $trailpad = '&nbsp;';
  51. $vdir = 'down';
  52. $hdir = 'right';
  53. $inner = 'cols';
  54. // xho
  55. $th = array();
  56. $th_attr = '';
  57. $th_span = array();
  58. $tr_spec = array();
  59. $tr_spec_attr = '';
  60. if (!isset($params['loop'])) {
  61. throw new SmartyException("html_table: missing 'loop' parameter");
  62. }
  63. foreach ($params as $_key=>$_value) {
  64. switch ($_key) {
  65. case 'loop':
  66. $$_key = (array)$_value;
  67. break;
  68. case 'cols':
  69. case 'rows':
  70. $$_key = (int)$_value;
  71. break;
  72. case 'table_attr':
  73. case 'trailpad':
  74. case 'hdir':
  75. case 'vdir':
  76. $$_key = (string)$_value;
  77. break;
  78. case 'tr_attr':
  79. case 'td_attr':
  80. $$_key = $_value;
  81. break;
  82. case 'th':
  83. $$_key = $_value;
  84. case 'th_span':
  85. $$_key = $_value;
  86. case 'th_attr':
  87. $$_key = $_value;
  88. case 'td_spec':
  89. $$_key = $_value;
  90. case 'td_spec_attr':
  91. $$_key = $_value;
  92. }
  93. }
  94. $loop_count = count($loop);
  95. if (empty($params['rows'])) {
  96. /* no rows specified */
  97. $rows = ceil($loop_count/$cols);
  98. } elseif (empty($params['cols'])) {
  99. if (!empty($params['rows'])) {
  100. /* no cols specified, but rows */
  101. $cols = ceil($loop_count/$rows);
  102. }
  103. }
  104. $output = "<table $table_attr>\n";
  105. // xho
  106. if (count($th)) {
  107. if (is_array($th)) $th = array_slice($th, 0, $cols);
  108. $output .= "<tr>";
  109. foreach ($th as $key => $colTitle) {
  110. $output .= "<th " . $th_attr;
  111. if (!empty($th_span[$key])) $output .= "colspan='" . $th_span[$key] . "'";
  112. $output .= ">" . $colTitle . "</th>";
  113. }
  114. $output .= "</tr>\n";
  115. }
  116. for ($r=0; $r<$rows; $r++) {
  117. $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
  118. $rx = ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
  119. for ($c=0; $c<$cols; $c++) {
  120. $x = ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
  121. if ($inner!='cols') {
  122. /* shuffle x to loop over rows*/
  123. $x = floor($x/$cols) + ($x%$cols)*$rows;
  124. }
  125. // xho
  126. if (count($td_spec) && $td_spec[$x]) $td_adds = $td_spec_attr;
  127. else $td_adds = $td_attr;
  128. if ($x<$loop_count) {
  129. $output .= "<td" . smarty_function_html_table_cycle('td', $td_adds, $c) . ">" . $loop[$x] . "</td>\n";
  130. } else {
  131. $output .= "<td" . smarty_function_html_table_cycle('td', $td_adds, $c) . ">$trailpad</td>\n";
  132. }
  133. }
  134. $output .= "</tr>\n";
  135. }
  136. $output .= "</table>\n";
  137. print $output;
  138. }
  139. function smarty_function_html_table_cycle($name, $var, $no) {
  140. if(!is_array($var)) {
  141. $ret = $var;
  142. } else {
  143. $ret = $var[$no % count($var)];
  144. }
  145. return ($ret) ? ' '.$ret : '';
  146. }
  147. /* vim: set expandtab: */
  148. ?>