2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // | Bertrand Mansion <bmansion@mamasam.com> |
18 // +----------------------------------------------------------------------+
22 require_once "PEAR.php";
23 require_once "HTML/Common.php";
26 * Builds an HTML table
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
32 class HTML_Table extends HTML_Common {
35 * Automatically adds a new row or column if a given row or column index does not exist
39 var $_autoGrow = true;
42 * Value to insert into empty cells
46 var $_autoFill = " ";
49 * Array containing the table structure
53 var $_structure = array();
56 * Number of rows composing in the table
63 * Number of column composing the table
70 * Tracks the level of nested tables
78 * @param array $attributes Associative array of table tag attributes
79 * @param int $tabOffset
82 function HTML_Table($attributes = null, $tabOffset = 0)
85 if (HTML_Common::apiVersion() < $commonVersion) {
86 return PEAR::raiseError("HTML_Table version " . $this->apiVersion() . " requires " .
87 "HTML_Common version $commonVersion or greater.", 0, PEAR_ERROR_TRIGGER);
89 HTML_Common::HTML_Common($attributes, $tabOffset);
93 * Returns the API version
100 } // end func apiVersion
103 * Sets the table caption
104 * @param string $caption
105 * @param mixed $attributes Associative array or string of table row attributes
108 function setCaption($caption, $attributes = null)
110 $attributes = $this->_parseAttributes($attributes);
111 $this->_structure["caption"] = array("attr" => $attributes, "contents" => $caption);
112 } // end func setCaption
115 * Sets the autoFill value
119 function setAutoFill($fill)
121 $this->_autoFill = $fill;
122 } // end func setAutoFill
125 * Returns the autoFill value
129 function getAutoFill()
131 return $this->_autoFill;
132 } // end func getAutoFill
135 * Sets the autoGrow value
139 function setAutoGrow($grow)
141 $this->_autoGrow = $grow;
142 } // end func setAutoGrow
145 * Returns the autoGrow value
149 function getAutoGrow()
151 return $this->_autoGrow;
152 } // end func getAutoGrow
155 * Sets the number of rows in the table
159 function setRowCount($rows)
161 $this->_rows = $rows;
162 } // end func setRowCount
165 * Sets the number of columns in the table
169 function setColCount($cols)
171 $this->_cols = $cols;
172 } // end func setColCount
175 * Returns the number of rows in the table
179 function getRowCount()
182 } // end func getRowCount
185 * Sets the number of columns in the table
189 function getColCount()
192 } // end func getColCount
195 * Sets a rows type 'TH' or 'TD'
196 * @param int $row Row index
197 * @param string $type 'TH' or 'TD'
201 function setRowType($row, $type)
203 for ($counter = 0; $counter < $this->_cols; $counter++) {
204 $this->_structure[$row][$counter]["type"] = $type;
206 } // end func setRowType
209 * Sets a columns type 'TH' or 'TD'
210 * @param int $col Column index
211 * @param string $type 'TH' or 'TD'
214 function setColType($col, $type)
216 for ($counter = 0; $counter < $this->_rows; $counter++) {
217 $this->_structure[$counter][$col]["type"] = $type;
219 } // end func setColType
222 * Sets the cell attributes for an existing cell.
224 * If the given indices do not exist and autoGrow is true then the given
225 * row and/or col is automatically added. If autoGrow is false then an
227 * @param int $row Row index
228 * @param int $col Column index
229 * @param mixed $attributes Associative array or string of table row attributes
233 function setCellAttributes($row, $col, $attributes)
235 if (isset($this->_structure[$row][$col]) && $this->_structure[$row][$col] == "__SPANNED__") return;
236 $attributes = $this->_parseAttributes($attributes);
237 $err = $this->_adjustEnds($row, $col, 'setCellAttributes', $attributes);
238 if (PEAR::isError($err)) {
241 $this->_structure[$row][$col]["attr"] = $attributes;
242 $this->_updateSpanGrid($row, $col);
243 } // end func setCellAttributes
246 * Updates the cell attributes passed but leaves other existing attributes in tact
247 * @param int $row Row index
248 * @param int $col Column index
249 * @param mixed $attributes Associative array or string of table row attributes
252 function updateCellAttributes($row, $col, $attributes)
254 if (isset($this->_structure[$row][$col]) && $this->_structure[$row][$col] == "__SPANNED__") return;
255 $attributes = $this->_parseAttributes($attributes);
256 $err = $this->_adjustEnds($row, $col, 'updateCellAttributes', $attributes);
257 if (PEAR::isError($err)) {
260 $this->_updateAttrArray($this->_structure[$row][$col]["attr"], $attributes);
261 $this->_updateSpanGrid($row, $col);
262 } // end func updateCellAttributes
265 * Returns the attributes for a given cell
266 * @param int $row Row index
267 * @param int $col Column index
271 function getCellAttributes($row, $col)
273 if (isset($this->_structure[$row][$col]) && $this->_structure[$row][$col] != '__SPANNED__') {
274 return $this->_structure[$row][$col]['attr'];
275 } elseif (!isset($this->_structure[$row][$col])) {
276 return PEAR::raiseError('Invalid table cell reference[' .
277 $row . '][' . $col . '] in HTML_Table::getCellAttributes');
280 } // end func getCellAttributes
283 * Sets the cell contents for an existing cell
285 * If the given indices do not exist and autoGrow is true then the given
286 * row and/or col is automatically added. If autoGrow is false then an
288 * @param int $row Row index
289 * @param int $col Column index
290 * @param mixed $contents May contain html or any object with a toHTML method
291 * @param string $type (optional) Cell type either 'TH' or 'TD'
295 function setCellContents($row, $col, $contents, $type = 'TD')
297 if(isset($this->_structure[$row][$col]) && $this->_structure[$row][$col] == "__SPANNED__") return;
298 $err = $this->_adjustEnds($row, $col, 'setCellContents');
299 if (PEAR::isError($err)) {
302 $this->_structure[$row][$col]['contents'] = $contents;
303 $this->_structure[$row][$col]['type'] = $type;
304 } // end func setCellContents
307 * Returns the cell contents for an existing cell
308 * @param int $row Row index
309 * @param int $col Column index
313 function getCellContents($row, $col)
315 if (isset($this->_structure[$row][$col]) && $this->_structure[$row][$col] == "__SPANNED__") return;
316 return $this->_structure[$row][$col]["contents"];
317 } // end func getCellContents
320 * Sets the contents of a header cell
323 * @param mixed $contents
326 function setHeaderContents($row, $col, $contents)
328 $this->setCellContents($row, $col, $contents, 'TH');
329 } // end func setHeaderContents
332 * Adds a table row and returns the row identifier
333 * @param array $contents (optional) Must be a indexed array of valid cell contents
334 * @param mixed $attributes (optional) Associative array or string of table row attributes
335 * This can also be an array of attributes, in which case the attributes
336 * will be repeated in a loop.
337 * @param string $type (optional) Cell type either 'TH' or 'TD'
338 * @param bool $inTR false if attributes are to be applied in TD tags
339 * true if attributes are to be applied in TR tag
343 function addRow($contents = null, $attributes = null, $type = 'TD', $inTR = false)
345 if (isset($contents) && !is_array($contents)) {
346 return PEAR::raiseError("First parameter to HTML_Table::addRow must be an array");
348 $row = $this->_rows++;
349 for ($counter = 0; $counter < count($contents); $counter++) {
351 $this->setCellContents($row, $counter, $contents[$counter]);
352 } elseif ($type == 'TH') {
353 $this->setHeaderContents($row, $counter, $contents[$counter]);
356 $this->setRowAttributes($row, $attributes, $inTR);
361 * Sets the row attributes for an existing row
362 * @param int $row Row index
363 * @param mixed $attributes Associative array or string of table row attributes
364 * This can also be an array of attributes, in which case the attributes
365 * will be repeated in a loop.
366 * @param bool $inTR false if attributes are to be applied in TD tags
367 * true if attributes are to be applied in TR tag
371 function setRowAttributes($row, $attributes, $inTR = false)
373 $multiAttr = $this->_isAttributesArray($attributes);
375 for ($i = 0; $i < $this->_cols; $i++) {
377 $this->setCellAttributes($row, $i,
378 $attributes[$i - ((ceil(($i+1) / count($attributes)))-1) * count($attributes)]);
380 $this->setCellAttributes($row, $i, $attributes);
384 $attributes = $this->_parseAttributes($attributes);
385 $err = $this->_adjustEnds($row, 1, 'setRowAttributes', $attributes);
386 if (PEAR::isError($err)) {
389 $this->_structure[$row]['attr'] = $attributes;
391 } // end func setRowAttributes
394 * Updates the row attributes for an existing row
395 * @param int $row Row index
396 * @param mixed $attributes Associative array or string of table row attributes
397 * @param bool $inTR false if attributes are to be applied in TD tags
398 * true if attributes are to be applied in TR tag
402 function updateRowAttributes($row, $attributes = null, $inTR = false)
404 $multiAttr = $this->_isAttributesArray($attributes);
406 for ($i = 0; $i < $this->_cols; $i++) {
408 $this->updateCellAttributes($row, $i,
409 $attributes[$i - ((ceil(($i+1) / count($attributes)))-1) * count($attributes)]);
411 $this->updateCellAttributes($row, $i, $attributes);
415 $attributes = $this->_parseAttributes($attributes);
416 $err = $this->_adjustEnds($row, 1, 'updateRowAttributes', $attributes);
417 if (PEAR::isError($err)) {
420 $this->_updateAttrArray($this->_structure[$row]['attr'], $attributes);
422 } // end func updateRowAttributes
425 * Returns the attributes for a given row as contained in the TR tag
426 * @param int $row Row index
430 function getRowAttributes($row)
432 if (isset($this->_structure[$row]['attr'])) {
433 return $this->_structure[$row]['attr'];
436 } // end func getRowAttributes
439 * Alternates the row attributes starting at $start
440 * @param int $start Row index of row in which alternating begins
441 * @param mixed $attributes1 Associative array or string of table row attributes
442 * @param mixed $attributes2 Associative array or string of table row attributes
443 * @param bool $inTR false if attributes are to be applied in TD tags
444 * true if attributes are to be applied in TR tag
447 function altRowAttributes($start, $attributes1, $attributes2, $inTR = false)
449 for ($row = $start ; $row < $this->_rows ; $row++) {
450 $attributes = ( ($row+$start)%2 == 0 ) ? $attributes1 : $attributes2;
451 $this->updateRowAttributes($row, $attributes, $inTR);
453 } // end func altRowAttributes
456 * Adds a table column and returns the column identifier
457 * @param array $contents (optional) Must be a indexed array of valid cell contents
458 * @param mixed $attributes (optional) Associative array or string of table row attributes
459 * @param string $type (optional) Cell type either 'TH' or 'TD'
463 function addCol($contents = null, $attributes = null, $type = 'TD')
465 if (isset($contents) && !is_array($contents)) {
466 return PEAR::raiseError("First parameter to HTML_Table::addCol must be an array");
468 $col = $this->_cols++;
469 for ($counter = 0; $counter < count($contents); $counter++) {
470 $this->setCellContents($counter, $col, $contents[$counter], $type);
472 $this->setColAttributes($col, $attributes);
477 * Sets the column attributes for an existing column
478 * @param int $col Column index
479 * @param mixed $attributes (optional) Associative array or string of table row attributes
482 function setColAttributes($col, $attributes = null)
484 $multiAttr = $this->_isAttributesArray($attributes);
485 for ($i = 0; $i < $this->_rows; $i++) {
487 $this->setCellAttributes($i, $col,
488 $attributes[$i - ((ceil(($i+1) / count($attributes)))-1) * count($attributes)]);
490 $this->setCellAttributes($i, $col, $attributes);
493 } // end func setColAttributes
496 * Updates the column attributes for an existing column
497 * @param int $col Column index
498 * @param mixed $attributes (optional) Associative array or string of table row attributes
501 function updateColAttributes($col, $attributes = null)
503 $multiAttr = $this->_isAttributesArray($attributes);
504 for ($i = 0; $i < $this->_rows; $i++) {
506 $this->updateCellAttributes($i, $col,
507 $attributes[$i - ((ceil(($i+1) / count($attributes)))-1) * count($attributes)]);
509 $this->updateCellAttributes($i, $col, $attributes);
512 } // end func updateColAttributes
515 * Sets the attributes for all cells
516 * @param mixed $attributes (optional) Associative array or string of table row attributes
519 function setAllAttributes($attributes = null)
521 for ($i = 0; $i < $this->_rows; $i++) {
522 $this->setRowAttributes($i, $attributes);
524 } // end func setAllAttributes
527 * Updates the attributes for all cells
528 * @param mixed $attributes (optional) Associative array or string of table row attributes
531 function updateAllAttributes($attributes = null)
533 for ($i = 0; $i < $this->_rows; $i++) {
534 $this->updateRowAttributes($i, $attributes);
536 } // end func updateAllAttributes
539 * Returns the table structure as HTML
546 $tabs = $this->_getTabs();
547 $tab = $this->_getTab();
548 $lnEnd = $this->_getLineEnd();
549 if ($this->_comment) {
550 $strHtml .= $tabs . "<!-- $this->_comment -->" . $lnEnd;
553 $tabs . "<table" . $this->_getAttrString($this->_attributes) . ">" . $lnEnd;
554 if (!empty($this->_structure["caption"])) {
555 $attr = $this->_structure["caption"]["attr"];
556 $contents = $this->_structure["caption"]["contents"];
557 $strHtml .= $tabs . $tab . "<caption" . $this->_getAttrString($attr) . ">";
558 if (is_array($contents)) $contents = implode(", ", $contents);
559 $strHtml .= $contents;
560 $strHtml .= "</caption>" . $lnEnd;
562 for ($i = 0 ; $i < $this->_rows ; $i++) {
563 if (isset($this->_structure[$i]['attr'])) {
564 $strHtml .= $tabs . $tab . "<tr".$this->_getAttrString($this->_structure[$i]['attr']).">" . $lnEnd;
566 $strHtml .= $tabs .$tab . "<tr>" . $lnEnd;
568 for ($j = 0 ; $j < $this->_cols ; $j++) {
569 if (isset($this -> _structure[$i][$j]) && $this->_structure[$i][$j] == "__SPANNED__") {
570 $strHtml .= $tabs . $tab . $tab ."<!-- span -->" . $lnEnd;
573 if (isset($this->_structure[$i][$j]["type"])) {
574 $type = (strtoupper($this->_structure[$i][$j]["type"]) == "TH" ? "th" : "td");
578 if (isset($this->_structure[$i][$j]["attr"])) {
579 $attr = $this->_structure[$i][$j]["attr"];
583 if (isset($this->_structure[$i][$j]["contents"])) {
584 $contents = $this->_structure[$i][$j]["contents"];
588 $strHtml .= $tabs . $tab . $tab . "<$type" . $this->_getAttrString($attr) . ">";
589 if (is_object($contents)) {
590 // changes indent and line end settings on nested tables
591 if (is_subclass_of($contents, "html_common")) {
592 $contents->setTab($tab);
593 $contents->setTabOffset($this->_tabOffset + 3);
594 $contents->_nestLevel = $this->_nestLevel + 1;
595 $contents->setLineEnd($this->_getLineEnd());
597 if (method_exists($contents, "toHtml")) {
598 $contents = $contents->toHtml();
599 } elseif (method_exists($contents, "toString")) {
600 $contents = $contents->toString();
603 if (is_array($contents)) {
604 $contents = implode(", ",$contents);
606 if (isset($this->_autoFill) && $contents == "") {
607 $contents = $this->_autoFill;
609 $strHtml .= $contents;
610 $strHtml .= "</$type>" . $lnEnd;
612 $strHtml .= $tabs . $tab . "</tr>" . $lnEnd;
614 $strHtml .= $tabs . "</table>" . $lnEnd;
619 * Checks if rows or columns are spanned
620 * @param int $row Row index
621 * @param int $col Column index
624 function _updateSpanGrid($row, $col)
626 if (isset($this->_structure[$row][$col]["attr"]["colspan"])) {
627 $colspan = $this->_structure[$row][$col]["attr"]["colspan"];
629 if (isset($this->_structure[$row][$col]["attr"]["rowspan"])) {
630 $rowspan = $this->_structure[$row][$col]["attr"]["rowspan"];
632 if (isset($colspan)) {
633 for ($j = $col+1; (($j < $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
634 $this->_structure[$row][$j] = "__SPANNED__";
637 if (isset($rowspan)) {
638 for ($i = $row+1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
639 $this->_structure[$i][$col] = "__SPANNED__";
642 if (isset($colspan) && isset($rowspan)) {
643 for ($i = $row+1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
644 for ($j = $col+1; (($j <= $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
645 $this->_structure[$i][$j] = "__SPANNED__";
649 } // end func _updateSpanGrid
652 * Adjusts ends (total number of rows and columns)
653 * @param int $row Row index
654 * @param int $col Column index
655 * @param string $method Method name of caller
656 * Used to populate PEAR_Error if thrown.
657 * @param array $attributes Assoc array of attributes
658 * Default is an empty array.
662 function _adjustEnds($row, $col, $method, $attributes = array())
664 $colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1;
665 $rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1;
666 if (($row + $rowspan - 1) >= $this->_rows) {
667 if ($this->_autoGrow) {
668 $this->_rows = $row + $rowspan;
670 return PEAR::raiseError('Invalid table row reference[' .
671 $row . '] in HTML_Table::' . $method);
674 if (($col + $colspan - 1) >= $this->_cols) {
675 if ($this->_autoGrow) {
676 $this->_cols = $col + $colspan;
678 return PEAR::raiseError('Invalid table column reference[' .
679 $col . '] in HTML_Table::' . $method);
682 } // end func _adjustEnds
685 * Tells if the parameter is an array of attribute arrays/strings
686 * @param mixed $attributes Variable to test
690 function _isAttributesArray($attributes)
692 if (is_array($attributes) && isset($attributes[0])) {
693 if (is_array($attributes[0]) || (is_string($attributes[0]) && count($attributes) > 1)) {
698 } // end func _isAttributesArray
699 } // end class HTML_Table