| 
<?php
 /*
 * Copyright 2008 Sam Barnett-Cormack
 *
 * This file is part of PHPTables.
 *
 * PHPTables is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Foobar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 class Table {
 
 private $num_columns;
 private $rows = array();
 private $headings;
 public $style_class = NULL;
 
 private function update_state() {
 if (isset($this->headings)) {
 $this->num_columns = $this->headings->get_width();
 } else {
 $this->num_columns = 0;
 }
 }
 
 function __construct(Row $headings = NULL) {
 if (isset($headings)) {
 $this->headings = $headings;
 } else {
 $this->headings = new Row();
 }
 $this->update_state();
 }
 
 function empty_headings() {
 $this->headings->empty_row();
 }
 
 function add_heading(Cell $heading) {
 $this->headings->add_cell($heading);
 $this->update_state();
 }
 
 function set_headings(Row $headings) {
 $this->headings = $headings;
 $this->update_state();
 }
 
 function num_rows() {
 return count($this->rows);
 }
 
 function num_cols() {
 return $this->num_columns;
 }
 
 function clear_rows() {
 $this->rows = array();
 }
 
 function add_row(Row $row, $index = NULL) {
 if ($row->get_width() != $this->num_columns) {
 throw new Exception("Row does not have correct number of columns (want " . $this->num_columns . ", row has " . $row->get_width() . ")");
 }
 if (isset($index)) {
 if (!is_int($index)) {
 throw new InvalidArgumentException("Index must be an integer");
 }
 if (isset($this->rows[$index])) {
 $rkeys = rsort(array_keys($this->rows));
 for ($i = $rkeys[0]; i >= $index; $i++) {
 $this->rows[$i+1] = $this->rows[$i];
 }
 }
 $this->rows[$index] = &$row;
 $this->condense_rows();
 } else {
 $this->rows[] = &$row;
 }
 }
 
 function set_row(Row $row, $index) {
 if ($row->get_width() != $this->num_columns) {
 throw new Exception("Row does not have correct number of columns");
 }
 if (!is_int($index)) {
 throw new InvalidArgumentException("Index must be an integer");
 }
 $this->rows[$index] = $row;
 $this->condense_rows();
 }
 
 private function condense_rows() {
 $newrows = array();
 foreach ($this->rows as $row) {
 $newrows[] = $row;
 }
 $this->rows = $newrows;
 }
 
 function get_cell_at($rowindex,$columnindex) {
 if (!is_int($rowindex)) {
 throw new InvalidArgumentException("Row index must be an integer");
 }
 if (!is_int($columnindex)) {
 throw new InvalidArgumentException("Column index must be an integer");
 }
 if ($columnindex >= $this->num_columns) {
 throw new InvalidArgumentException("Column index out of bounds");
 }
 if (isset($this->rows[$rowindex])) {
 return $this->rows[$rowindex]->get_cell_at_column($columnindex);
 } else {
 throw new InvalidArgumentException("Row index out of bounds");
 }
 }
 
 function get_html() {
 $output = "<!-- PHPTables generated table follows: -->\n";
 $output .= "<table";
 if (isset($this->style_class)) {
 $output .= " class=\"" . $this->style_class . "\" ";
 }
 $output .= ">\n";
 // fill in headings
 if (isset($this->headings->style_class)) {
 $output .= "\t<tr class=\"" . $this->headings->style_class . "\" >\n";
 } else {
 $output .= "\t<tr>\n";
 }
 foreach ($this->headings as $head) {
 if (isset($head->style_class)) {
 $output .= "\t\t<th class=\"" . $head->style_class . "\" >\n";
 } else {
 $output .= "\t\t<th>\n";
 }
 $output .= "\t\t\t" . $head->get_content() . "\n";
 $output .= "\t\t</th>\n";
 }
 $output .= "\t</tr>\n";
 // fill in everything else
 foreach ($this->rows as $row) {
 if (isset($row->style_class)) {
 $output .= "\t<tr class=\"" . $row->style_class . "\" >\n";
 } else {
 $output .= "\t<tr>\n";
 }
 foreach ($row as $cell) {
 if (isset($cell->style_class)) {
 $output .= "\t\t<td class=\"" . $cell->style_class . "\" >\n";
 } else {
 $output .= "\t\t<td>\n";
 }
 $output .= "\t\t\t" . $cell->get_content() . "\n";
 $output .= "\t\t</td>\n";
 }
 $output .= "\t</tr>\n";
 }
 // and finish
 $output .= "</table>";
 return $output;
 }
 }
 ?>
 
 |