Files
pmgr/site/views/elements/table.ctp
2009-08-07 22:52:01 +00:00

90 lines
2.8 KiB
PHP

<?php /* -*- mode:PHP -*- */
/* table.ctp */
/*
*
* @filesource
* @copyright Copyright 2009, Abijah Perkins
* @package pmgr
*/
// The caller may not have given us a row_class, or may have given
// it as a non-array. Ultimately, we want it to be an array with
// an entry for each row, where each entry is an array of classes.
if (!isset($row_class) || !is_array($row_class))
$row_class = array();
// Same for the column_class, except columns instead of rows.
if (!isset($column_class) || !is_array($column_class))
$column_class = array();
// Give a default if not caller supplied.
if (!isset($suppress_alternate_rows))
$suppress_alternate_rows = false;
// If we have rows, then we have a table... go to work
if (isset($rows) && is_array($rows) && count($rows)) {
// Prework to get each element of row_class as an
// array of classes, lowercased and whitespace free.
foreach ($row_class AS &$rca) {
if (!is_array($rca))
$rca = array($rca);
foreach ($rca AS &$rc)
$rc = preg_replace("/ /", "-", preg_replace('/\./', '', strtolower($rc)));
}
// Same prework for column_class
foreach ($column_class AS &$cca) {
if (!is_array($cca))
$cca = array($cca);
foreach ($cca AS &$cc)
$cc = preg_replace("/ /", "-", preg_replace('/\./', '', strtolower($cc)));
}
// Associate each cell with the appropriate class(es).
// NOTE: Very kludgy solution on the row class(es).
// I want the row_class to affix to the <tr> tag.
// tableCells, however, does not have such ability :-/
// Therefore, the row class(es) get replicated to each
// cell within the row.
foreach ($rows AS $r => &$row) {
foreach ($row AS $c => $col) {
$cell_class = implode(" ", array_merge(isset( $row_class[$r]) ? $row_class[$r] : array(),
isset($column_class[$c]) ? $column_class[$c] : array()));
if ($cell_class)
$row[$c] = array($col, array('class' => $cell_class));
}
}
// Allow user to specify a list of classes
if (isset($class) && is_array($class))
$class = implode(' ', $class);
// OK, output the table HTML
echo('<TABLE' .
(isset($id) ? ' ID="'.$id.'"' : '') .
(isset($class) ? ' CLASS="'.$class.'"' : '') .
'>' . "\n");
if (isset($caption))
echo(' <CAPTION>' . $caption . '</CAPTION>' . "\n");
if (isset($headers) && is_array($headers)) {
echo(' <THEAD>' . "\n");
echo $html->tableHeaders($headers) . "\n";
echo(' </THEAD>' . "\n");
}
echo(' <TBODY>' . "\n");
echo $html->tableCells($rows,
$suppress_alternate_rows ? null : array('class' => "oddrow"),
$suppress_alternate_rows ? null : array('class' => "evnrow"),
false, false) . "\n";
echo(' </TBODY>' . "\n");
echo('</TABLE>' . "\n");
}