| 1 |
joko |
1.1 |
<?php |
| 2 |
|
|
/** |
| 3 |
|
|
* Smarty plugin |
| 4 |
|
|
* @package Smarty |
| 5 |
|
|
* @subpackage plugins |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
|
| 9 |
|
|
/** |
| 10 |
|
|
* Smarty {html_table} function plugin |
| 11 |
|
|
* |
| 12 |
|
|
* Type: function<br> |
| 13 |
|
|
* Name: html_table<br> |
| 14 |
|
|
* Date: Feb 17, 2003<br> |
| 15 |
|
|
* Purpose: make an html table from an array of data<br> |
| 16 |
|
|
* Input:<br> |
| 17 |
|
|
* - loop = array to loop through |
| 18 |
|
|
* - cols = number of columns |
| 19 |
|
|
* - rows = number of rows |
| 20 |
|
|
* - table_attr = table attributes |
| 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 |
|
|
* - vdir = vertical direction (default: "down", means top-to-bottom) |
| 25 |
|
|
* - hdir = horizontal direction (default: "right", means left-to-right) |
| 26 |
|
|
* - inner = inner loop (default "cols": print $loop line by line, |
| 27 |
|
|
* $loop will be printed column by column otherwise) |
| 28 |
|
|
* |
| 29 |
|
|
* |
| 30 |
|
|
* Examples: |
| 31 |
|
|
* <pre> |
| 32 |
|
|
* {table loop=$data} |
| 33 |
|
|
* {table loop=$data cols=4 tr_attr='"bgcolor=red"'} |
| 34 |
|
|
* {table loop=$data cols=4 tr_attr=$colors} |
| 35 |
|
|
* </pre> |
| 36 |
|
|
* @author Monte Ohrt <monte@ispi.net> |
| 37 |
|
|
* @version 1.0 |
| 38 |
|
|
* @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table} |
| 39 |
|
|
* (Smarty online manual) |
| 40 |
|
|
* @param array |
| 41 |
|
|
* @param Smarty |
| 42 |
|
|
* @return string |
| 43 |
|
|
*/ |
| 44 |
|
|
function smarty_function_html_table($params, &$smarty) |
| 45 |
|
|
{ |
| 46 |
|
|
$table_attr = 'border="1"'; |
| 47 |
|
|
$tr_attr = ''; |
| 48 |
|
|
$td_attr = ''; |
| 49 |
|
|
$cols = 3; |
| 50 |
|
|
$rows = 3; |
| 51 |
|
|
$trailpad = ' '; |
| 52 |
|
|
$vdir = 'down'; |
| 53 |
|
|
$hdir = 'right'; |
| 54 |
|
|
$inner = 'cols'; |
| 55 |
|
|
|
| 56 |
|
|
if (!isset($params['loop'])) { |
| 57 |
|
|
$smarty->trigger_error("html_table: missing 'loop' parameter"); |
| 58 |
|
|
return; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
|
foreach ($params as $_key=>$_value) { |
| 62 |
|
|
switch ($_key) { |
| 63 |
|
|
case 'loop': |
| 64 |
|
|
$$_key = (array)$_value; |
| 65 |
|
|
break; |
| 66 |
|
|
|
| 67 |
|
|
case 'cols': |
| 68 |
|
|
case 'rows': |
| 69 |
|
|
$$_key = (int)$_value; |
| 70 |
|
|
break; |
| 71 |
|
|
|
| 72 |
|
|
case 'table_attr': |
| 73 |
|
|
case 'trailpad': |
| 74 |
|
|
case 'hdir': |
| 75 |
|
|
case 'vdir': |
| 76 |
|
|
$$_key = (string)$_value; |
| 77 |
|
|
break; |
| 78 |
|
|
|
| 79 |
|
|
case 'tr_attr': |
| 80 |
|
|
case 'td_attr': |
| 81 |
|
|
$$_key = $_value; |
| 82 |
|
|
break; |
| 83 |
|
|
} |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
$loop_count = count($loop); |
| 87 |
|
|
if (empty($params['rows'])) { |
| 88 |
|
|
/* no rows specified */ |
| 89 |
|
|
$rows = ceil($loop_count/$cols); |
| 90 |
|
|
} elseif (empty($params['cols'])) { |
| 91 |
|
|
if (!empty($params['rows'])) { |
| 92 |
|
|
/* no cols specified, but rows */ |
| 93 |
|
|
$cols = ceil($loop_count/$rows); |
| 94 |
|
|
} |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
$output = "<table $table_attr>\n"; |
| 98 |
|
|
|
| 99 |
|
|
for ($r=0; $r<$rows; $r++) { |
| 100 |
|
|
$output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n"; |
| 101 |
|
|
$rx = ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols; |
| 102 |
|
|
|
| 103 |
|
|
for ($c=0; $c<$cols; $c++) { |
| 104 |
|
|
$x = ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c; |
| 105 |
|
|
if ($inner!='cols') { |
| 106 |
|
|
/* shuffle x to loop over rows*/ |
| 107 |
|
|
$x = floor($x/$cols) + ($x%$cols)*$rows; |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
if ($x<$loop_count) { |
| 111 |
|
|
$output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n"; |
| 112 |
|
|
} else { |
| 113 |
|
|
$output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n"; |
| 114 |
|
|
} |
| 115 |
|
|
} |
| 116 |
|
|
$output .= "</tr>\n"; |
| 117 |
|
|
} |
| 118 |
|
|
$output .= "</table>\n"; |
| 119 |
|
|
|
| 120 |
|
|
return $output; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
function smarty_function_html_table_cycle($name, $var, $no) { |
| 124 |
|
|
if(!is_array($var)) { |
| 125 |
|
|
$ret = $var; |
| 126 |
|
|
} else { |
| 127 |
|
|
$ret = $var[$no % count($var)]; |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
return ($ret) ? ' '.$ret : ''; |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
|
|
| 134 |
|
|
/* vim: set expandtab: */ |
| 135 |
|
|
|
| 136 |
|
|
?> |