Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {html_table} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     html_table
Chris PeBenito 696b41
 * Date:     Feb 17, 2003
Chris PeBenito 696b41
 * Purpose:  make an html table from an array of data
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *         - loop = array to loop through
Chris PeBenito 696b41
 *         - cols = number of columns
Chris PeBenito 696b41
 *         - rows = number of rows
Chris PeBenito 696b41
 *         - table_attr = table attributes
Chris PeBenito 696b41
 *         - tr_attr = table row attributes (arrays are cycled)
Chris PeBenito 696b41
 *         - td_attr = table cell attributes (arrays are cycled)
Chris PeBenito 696b41
 *         - trailpad = value to pad trailing cells with
Chris PeBenito 696b41
 *         - vdir = vertical direction (default: "down", means top-to-bottom)
Chris PeBenito 696b41
 *         - hdir = horizontal direction (default: "right", means left-to-right)
Chris PeBenito 696b41
 *         - inner = inner loop (default "cols": print $loop line by line,
Chris PeBenito 696b41
 *                   $loop will be printed column by column otherwise)
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Examples:
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * {table loop=$data}
Chris PeBenito 696b41
 * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
Chris PeBenito 696b41
 * {table loop=$data cols=4 tr_attr=$colors}
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @author   Monte Ohrt <monte at ohrt dot com>
Chris PeBenito 696b41
 * @version  1.0
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
Chris PeBenito 696b41
 *          (Smarty online manual)
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_table($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    $table_attr = 'border="1"';
Chris PeBenito 696b41
    $tr_attr = '';
Chris PeBenito 696b41
    $td_attr = '';
Chris PeBenito 696b41
    $cols = 3;
Chris PeBenito 696b41
    $rows = 3;
Chris PeBenito 696b41
    $trailpad = ' ';
Chris PeBenito 696b41
    $vdir = 'down';
Chris PeBenito 696b41
    $hdir = 'right';
Chris PeBenito 696b41
    $inner = 'cols';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!isset($params['loop'])) {
Chris PeBenito 696b41
        $smarty->trigger_error("html_table: missing 'loop' parameter");
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach ($params as $_key=>$_value) {
Chris PeBenito 696b41
        switch ($_key) {
Chris PeBenito 696b41
            case 'loop':
Chris PeBenito 696b41
                $$_key = (array)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'cols':
Chris PeBenito 696b41
            case 'rows':
Chris PeBenito 696b41
                $$_key = (int)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'table_attr':
Chris PeBenito 696b41
            case 'trailpad':
Chris PeBenito 696b41
            case 'hdir':
Chris PeBenito 696b41
            case 'vdir':
Chris PeBenito 696b41
            case 'inner':
Chris PeBenito 696b41
                $$_key = (string)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'tr_attr':
Chris PeBenito 696b41
            case 'td_attr':
Chris PeBenito 696b41
                $$_key = $_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $loop_count = count($loop);
Chris PeBenito 696b41
    if (empty($params['rows'])) {
Chris PeBenito 696b41
        /* no rows specified */
Chris PeBenito 696b41
        $rows = ceil($loop_count/$cols);
Chris PeBenito 696b41
    } elseif (empty($params['cols'])) {
Chris PeBenito 696b41
        if (!empty($params['rows'])) {
Chris PeBenito 696b41
            /* no cols specified, but rows */
Chris PeBenito 696b41
            $cols = ceil($loop_count/$rows);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $output = "\n";
Chris PeBenito 696b41
Chris PeBenito 696b41
    for ($r=0; $r<$rows; $r++) {
Chris PeBenito 696b41
        $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
Chris PeBenito 696b41
        $rx =  ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
Chris PeBenito 696b41
Chris PeBenito 696b41
        for ($c=0; $c<$cols; $c++) {
Chris PeBenito 696b41
            $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
Chris PeBenito 696b41
            if ($inner!='cols') {
Chris PeBenito 696b41
                /* shuffle x to loop over rows*/
Chris PeBenito 696b41
                $x = floor($x/$cols) + ($x%$cols)*$rows;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
            if ($x<$loop_count) {
Chris PeBenito 696b41
                $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "\n";
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad\n";
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $output .= "\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $output .= "\n";
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    return $output;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_function_html_table_cycle($name, $var, $no) {
Chris PeBenito 696b41
    if(!is_array($var)) {
Chris PeBenito 696b41
        $ret = $var;
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $ret = $var[$no % count($var)];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    return ($ret) ? ' '.$ret : '';
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>