00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00017
00056 class Table
00057 {
00058 var $cells;
00059 var $rows;
00060 var $columns;
00061
00062 var $tableClass;
00063 var $defaultRowClass;
00064 var $defaultCellClass;
00065
00067 var $currentRow;
00068 var $currentCell;
00069
00079 function Table( $tableClass = "setup", $defaultRowClass = "setup", $defaultCellClass = "setup", $rows = 0, $columns = 0 )
00080 {
00081 $this->tableClass = $tableClass;
00082 $this->defaultRowClass = $defaultRowClass;
00083 $this->defaultCellClass = $defaultCellClass;
00084
00085 $this->cells = array();
00086 $this->rows = array();
00087 $this->columns = array();
00088
00089 if( $rows > 0 )
00090 for( $i = 0; $i < $rows; $i++ )
00091 $this->addRow();
00092
00093 if( $columns > 0 )
00094 for( $i = 0; $i < $columns; $i++ )
00095 $this->addColumn();
00096 }
00097
00105 function addRow( $thisRowClass = null, $headerRow = false, $where = null )
00106 {
00107 if( $where == null )
00108 {
00109 $where = count($this->rows);
00110 }
00111 else
00112 {
00114
00115 for( $i = count($this->rows) - 1; $i > $where; $i-- )
00116 {
00117 $this->rows[$i + 1] = $this->rows[$i];
00118 $this->cells[$i + 1] = $this->cells[$i];
00119 }
00120 }
00121 $this->rows[$where] = array( "styleClass" => $thisRowClass, "headerRow" => $headerRow );
00122 }
00123
00132 function addRows( $howMany, $theseRowClass = null, $headerRow = false, $where = null )
00133 {
00134 if( $where == null )
00135 {
00136 for( $i = 1; $i < $howMany; $i++ )
00137 $this->addRow( $theseRowClass, $headerRow );
00138 }
00139 else
00140 {
00141 $stopHere = $where + $howMany;
00142 for( $i = $where; $i < $stopHere; $i++ )
00143 $this->addRow( $theseRowClass, $headerRow, $i );
00144 }
00145 }
00146
00153 function setRowHeader( $row, $headerRow = true )
00154 {
00155 $this->rows[$row]["headerRow"] = $headerRow;
00156 }
00157
00164 function addColumn( $thisColumnClass = null, $where = null )
00165 {
00166 if( $where == null )
00167 $where = count($this->columns);
00168 else
00169 {
00171 for( $i = count($this->columns); $i > $where; $i-- )
00172 $this->columns[$i + 1] = $this->columns[$i];
00173
00174
00175 foreach( $this->cells as $key => $value )
00176 {
00177 for( $i = count($this->columns); $i > $where; $i-- )
00178 $this->cells[$key][$i + 1] = $this->cells[$key][$i];
00179 }
00180 }
00181 $this->columns[$where] = array( "styleClass" => $thisColumnClass );
00182 }
00183
00191 function addColumns( $howMany, $theseColumnClass = null, $where = null )
00192 {
00193 if( $where == null )
00194 {
00195 for( $i = 1; $i < $howMany; $i++ )
00196 $this->addColumn( $theseColumnClass );
00197 }
00198 else
00199 {
00200 $stopHere = $where + $howMany;
00201 for( $i = $where; $i < $stopHere; $i++ )
00202 $this->addColumn( $theseColumnClass, $i );
00203 }
00204 }
00205
00214 function setContent( $row, $column, $content, $pageData = true )
00215 {
00216 $this->cells[$row][$column]["content"] = $content;
00217 $this->cells[$row][$column]["pageData"] = $pageData;
00218 }
00219
00227 function setRowContent( $row, $pageData = true )
00228 {
00229 $contents = func_get_args();
00230 $contents = array_slice( $contents, 2 );
00231 foreach( $contents as $key => $value )
00232 $this->setContent( $row, $key, $value, $pageData );
00233 }
00234
00243 function setSpan( $row, $column, $rowSpan = null, $colSpan = null )
00244 {
00245
00246 $oldRowSpan = $this->cells[$row][$column]["rowSpan"] + $row;
00247 $oldColSpan = $this->cells[$row][$column]["colSpan"] + $column;
00248 for( $theRow = $row; $theRow < $oldRowSpan; $theRow++ )
00249 for( $theCol = $column; $theCol < $oldColSpan; $theCol++ )
00250 $this->cells[$theRow][$theCol]["inSpan"] = false;
00251
00252
00253
00254 $newRowSpan = $row + $rowSpan;
00255 $newColSpan = $column + $colSpan;
00256 if( $rowSpan == null )
00257 {
00258 $theRow = $row;
00259 for( $theCol = $column; $theCol < $newColSpan; $theCol++ )
00260 {
00261 $this->cells[$theRow][$theCol]["inSpan"] = true;
00262 $this->cells[$theRow][$theCol]["rowSpan"] = null;
00263 $this->cells[$theRow][$theCol]["colSpan"] = null;
00264 }
00265 }
00266 else if( $colSpan == null )
00267 {
00268 $theCol = $column;
00269 for( $theRow = $row; $theRow < $newRowSpan; $theRow++ )
00270 {
00271 $this->cells[$theRow][$theCol]["inSpan"] = true;
00272 $this->cells[$theRow][$theCol]["rowSpan"] = null;
00273 $this->cells[$theRow][$theCol]["colSpan"] = null;
00274 }
00275 }
00276 else
00277 {
00278 for( $theRow = $row; $theRow < $newRowSpan; $theRow++ )
00279 {
00280 for( $theCol = $column; $theCol < $newColSpan; $theCol++ )
00281 {
00282 $this->cells[$theRow][$theCol]["inSpan"] = true;
00283 $this->cells[$theRow][$theCol]["rowSpan"] = null;
00284 $this->cells[$theRow][$theCol]["colSpan"] = null;
00285 }
00286 }
00287 }
00288
00289
00290 $this->cells[$row][$column]["inSpan"] = false;
00291 $this->cells[$row][$column]["rowSpan"] = $rowSpan;
00292 $this->cells[$row][$column]["colSpan"] = $colSpan;
00293 }
00294
00302 function setRowSpan( $row, $column, $spanrange = null )
00303 {
00304 $this->setSpan( $row, $column, $spanrange, $this->cells[$row][$column]["colSpan"] );
00305 }
00306
00314 function setColSpan( $row, $column, $spanrange = null )
00315 {
00316 $this->setSpan( $row, $column, $this->cells[$row][$column]["rowSpan"], $spanrange );
00317 }
00318
00326 function setStyleClass( $row, $column, $className = null )
00327 {
00328 $this->cells[$row][$column]["styleClass"] = $className;
00329 }
00330
00337 function getContent( $row, $column )
00338 {
00339 return $this->cells[$row][$column]["content"];
00340 }
00341
00348 function newRow( $thisRowClass = null, $headerRow = false )
00349 {
00350 $this->addRow( $thisRowClass, $headerRow );
00351 $this->currentRow = endKey($this->rows);
00352 $this->currentCell = -1;
00353 }
00354
00361 function newCell( $content, $colSpan = null, $styleClass = null )
00362 {
00363 $this->currentCell++;
00364 $nextCurrent = $this->currentCell;
00365
00366
00367 if( !array_key_exists( $this->currentCell, $this->columns ) && $colSpan == null )
00368 $this->addColumn();
00369 else if( $colSpan != null )
00370 {
00371 for( $i = 0 ; $i < $colSpan ; $i++ )
00372 if( !array_key_exists( $this->currentCell + $i , $this->columns ) )
00373 $this->addColumn();
00374 $nextCurrent = endkey($this->columns) + $i;
00375 $this->setColSpan( $this->currentRow, $this->currentCell, $colSpan );
00376 }
00377
00378 $this->setContent( $this->currentRow, $this->currentCell, $content );
00379 $this->setStyleClass( $this->currentRow, $this->currentCell, $styleClass );
00380
00381
00382 $this->currentCell = $nextCurrent;
00383 }
00384
00390 function render()
00391 {
00392 $renderedContent = "<table class=\"" . $this->tableClass . "\">";
00393 foreach( $this->rows as $rowID => $row )
00394 {
00395 $styleClass = ( $this->rows[$rowID]["styleClass"] == null ) ? $this->defaultRowClass : $this->rows[$rowID]["styleClass"];
00396 $renderedContent .= "\n <tr class=\"$styleClass\">";
00397
00398 foreach( $this->columns as $columnID => $column )
00399 {
00400
00401 if( ! array_key_exists( $rowID, $this->cells ) )
00402 {
00403 if( $this->columns[$columnID]["styleClass"] == null )
00404 $cellClass = $this->defaultCellClass;
00405 else
00406 $cellClass = $this->columns[$columnID]["styleClass"];
00407
00408 $cellType = ( $row["headerRow"] ) ? "th" : "td";
00409 $rowSpan = ( $this->cells[$rowID][$columnID]["rowSpan"] > 0 ) ? " rowspan=\"" . $this->cells[$rowID][$columnID]["rowSpan"] . "\"" : "";
00410 $columnSpan = ( $this->cells[$rowID][$columnID]["colSpan"] > 0 ) ? " colspan=\"" . $this->cells[$rowID][$columnID]["colSpan"] . "\"" : "";
00411
00412 $renderedContent .= "\n <$cellType class=\"$cellClass\"></$cellType>";
00413 }
00414
00415 else if( ! $this->cells[$rowID][$columnID]["inSpan"] )
00416 {
00417 if( $this->cells[$rowID][$columnID]["styleClass"] == null )
00418 {
00419 if( $this->columns[$columnID]["styleClass"] == null )
00420 $cellClass = $this->defaultCellClass;
00421 else
00422 $cellClass = $this->columns[$columnID]["styleClass"];
00423 }
00424 else
00425 $cellClass = $this->cells[$rowID][$columnID]["styleClass"];
00426
00427 $cellType = ( $row["headerRow"] ) ? "th" : "td";
00428 $rowSpan = ( $this->cells[$rowID][$columnID]["rowSpan"] != 0 ) ? " rowspan=\"" . $this->cells[$rowID][$columnID]["rowSpan"] . "\"" : "";
00429 $columnSpan = ( $this->cells[$rowID][$columnID]["colSpan"] != 0 ) ? " colspan=\"" . $this->cells[$rowID][$columnID]["colSpan"] . "\"" : "";
00430 $cellContent = ( $this->cells[$rowID][$columnID]["pageData"] ) ? parse_page_data( $this->cells[$rowID][$columnID]["content"] ) : $this->cells[$rowID][$columnID]["content"];
00431
00432 $renderedContent .= "\n <" . $cellType . $rowSpan . $columnSpan . " class=\"$cellClass\">" . $cellContent . "</$cellType>";
00433 }
00434 }
00435
00436 $renderedContent .= "\n </tr>";
00437 }
00438 $renderedContent .= "</table>";
00439 return $renderedContent;
00440 }
00441 }
00442 ?>