Source for file HTMLTable.class.php
Documentation is available at HTMLTable.class.php
* This is a very important file for the framework "GilliGan".
* With the class HTMLTable you can build tables to layout HTML-pages. It is
* very useful in close collaboration to the form-classes of the package forms.
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
* A class to format plaintext.
require_once( CLASSPATH. "core/PlainTextFormatter.class.php" );
* A pool of regular expressions.
include_once( CLASSPATH. "RegExpConstants.inc.php" );
* A very important class to generate HTML-tables. To keep a fast performance,
* this class is not purely object oriented. That means that the tabledatas are
* not objects - their information are stored in arrays.
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
* This library 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 2.1 of the License.
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* The id or name of this table.
* Stores the rows count of this table.
* Stores the cols count of this table.
* Can filled with styledefinitions that were not handled by an implemented
* Stores the content of each tabledata. This array carries two dimensions of
* string types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* Stores the width of each tabledata. This array carries two dimensions of
* integer types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* @var array $cellWidthArr
* Stores the height of each tabledata. This array carries two dimensions of
* integer types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* @var array $cellHeightArr
* Stores whether a tabledata should be formatted by the PlainTextFormatter or
* not. This array carries two dimensions of boolean types. The first
* dimension stands for the rows of the table, the second one stands for the
* @var array $no_format_arr
* Stores the colspan of each tabledata. This array carries two dimensions of
* integer types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* Stores the rowspan of each tabledata. This array carries two dimensions of
* integer types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* Stores the styleclass of each tabledata. This array carries two dimensions
* of string types. The first dimension stands for the rows of the table, the
* second one stands for the cols of the table.
* @var array $cellStyleClassArr
* Stores all customized attributes of each tabledata. This array carries two
* dimensions of string types. The first dimension stands for the rows of the
* table, the second one stands for the cols of the table.
* Stores the customized attributes of each table's row.
* @var array $rowAttributes
* Stores the value of the width-attribute of this table.
* Stores the value of the height-attribute of this table.
* Stores the value of the cellpadding-attribute of this table.
* @var integer $cellpadding
* Stores the value of the cellspacing-attribute of this table.
* @var integer $cellspacing
* Stores the value of the border-attribute of this table.
* Stores the value of the class-attribute of this table
* Stores the customized attributes of this table.
* @var string $tableAttributes
* Stores whether the table should be formatted by the PlainTextFormatter or
* @var boolean $no_format
* @author Daniel Plücken <daniel@debakel.net>
* @param array $cellpadding
* @param array $cellspacing
* @param boolean $no_format
$cols, $rows = 0, $width = "", $height = "",
$cellWidthArr = array(), $cellHeightArr = array(),
$cellpadding = "", $cellspacing = "",
$border = "", $style = "", $no_format = false
for( $i = 0; $i < $this->rows; $i++ )
for( $j = 0; $j < $this->cols; $j++ )
* Stores all used ids and makes sure that every id in the document is
* unique. It should called in every get method of classes in the packages
* @author Daniel Plücken <daniel@debakel.net>
* @param boolean $bool_store
function idExists( $str_id, $bool_store = false )
static $id_arr = array();
if ( in_array( $str_id, $id_arr ) )
if ( preg_match( "!^(.+)\_(\d+)?$!", $str_id, $match_arr ) )
$start = intval( $match_arr[2] ) + 1;
$match_arr[1] = $helper = $str_id;
for ( $i = $start; in_array( $helper, $id_arr ); $i++ )
$helper = $match_arr[1]. "_". $i;
$this->id = $id_arr[] = $str_id;
* Sets the content of the id-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
function setId( $string )
* Sets the value of the width-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the value of the height-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the number of cols this table should have. For example you want to
* set the table column by column, you simply have to increment the number of
* cols of this table by one after you set all rows of a col, and so on.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the number of rows this table should have. This makes only sense if
* you want to put out a table without any content.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the content of the cellpadding-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Set the value of style definitions that are not handled by implemented
* @author Daniel Plücken <daniel@debakel.net>
* Sets the content of the cellspacing-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the content of the border-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Sets whether the table should format in the source by "\r\n" or not.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the content of the class-attribute of the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
{ $this->style = $string; }
* Adds a custom-attribute to the given row of this table. The counting of
* @author Daniel Plücken <daniel@debakel.net>
* Adds a custom-attribute to the table-tag.
* @author Daniel Plücken <daniel@debakel.net>
* Returns a clone of this object.
* @author Daniel Plücken <daniel@debakel.net>
* Returns a clone of this object only with the specified cols.
* @author Daniel Plücken <daniel@debakel.net>
* @param array $col_arr An array containing the numbers of the cols
* which should adopt to the clone. The counting
* bigins at zero from left to right. This value
* can also be an integer to adopt only one col.
$int_cols = count( $col_arr );
$col_arr = array( $col_arr );
for( $k = 0; $k < $this->rows; $k++ )
for( $i = 0; $i < $int_cols; $i++ )
* Returns a clone of this object only with the specified rows.
* @author Daniel Plücken <daniel@debakel.net>
* @param array $col_arr An array containing the numbers of the rows
* which should adopt to the clone. The counting
* bigins at zero from top to bottom. This value
* can also be an integer to adopt only one row.
$int_rows = count( $row_arr );
$row_arr = array( $row_arr );
for( $k = 0; $k < $int_rows; $k++ )
* Sets the span of the cell at the given position.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $rowspan
* @param integer $colspan
for( $i = 1; $i < $colspan; $i++ )
for( $i = 1; $i < $rowspan; $i++ )
for( $j = 1; $j < $colspan; $j++ )
if( $this->rows < $row + $rowspan )
$this->rows = $row + $rowspan;
* Adds a cell at the next possible position to this table.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $content The content the cell should have.
* @param string $style The value of the class-attribute of the cell
* @param string $colspan The value of the colspan-attribute of the cell
* @param string $rowspan The value of the rowspan-attribute of the cell
* @param string $no_format Ascertains whether the content of the cell should
* format by newlines ("\r\n") or not.
* @return array The position where the cell was fixed.
$content = "", $style = "",
$colspan = "1", $rowspan = "1",
$content = $content == ""
$noFormat = strlen( $content ) < 80
# Search the next possible place for a cell
for( $i = 0; $i < $this->rows + 1; $i++ )
for( $j = 0; $j < $this->cols; $j++ )
&& empty( $ignoreCountCol ) )
if( !empty( $ignoreCountCol ) )
$thisRow = $i + $rowspan;
# If the colspan is greater than the given space, it will be corrected.
if( $colspan > ( $this->cols - $thisCol ) )
$colspan = $this->cols - $thisCol;
if( $this->rows < $thisRow + $rowspan )
{ $this->rows = $thisRow + $rowspan; }
if( !empty( $styleClass ) )
if( !empty( $noFormat ) )
return array( $thisRow, $thisCol );
* Adds a cell at the next possible position to this table. This method is an
* alias for the method addCol.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $content The content the cell should have.
* @param string $style The value of the class-attribute of the cell
* @param string $colspan The value of the colspan-attribute of the cell
* @param string $rowspan The value of the rowspan-attribute of the cell
* @param string $no_format Ascertains whether the content of the cell should
* format by newlines ("\r\n") or not.
* @return array The position where the cell was fixed.
$content = "", $style = "",
$colspan = "1", $rowspan = "1",
{ $this->addCol( $content, $style, $colspan, $rowspan, $noFormat ); }
* Changes the given cell.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $row The row in which the cell that should
* @param integer $col The col in which the cell that should
* @param string $styleClass The style class that should set to the
* @param integer $rowspan To manipulate the rowspan.
* @param integer $colspan To manipulate the colspan.
* @param boolean $noFormat Whether the content in the col should
* automatically get breaks or not.
* @param string $otherAttributes The attributes that should set to the
if( !empty( $styleClass ) )
if( !empty( $otherAttributes ) )
* Changes the attributes of a given cell.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $row The row in which the cell to be change is.
* @param integer $col The col in which the cell to be change is.
* @param string $attributes The attributes that should set to the
{ $this->changeCol( $row, $col, "", "", "", "", "", $attributes ); }
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $row The row in which the cell to be change is.
* @param integer $col The col in which the cell to be change is.
* @param string $content The content that should be add to the
* actual content of the cell.
* @param string $BEFORE_BEHIND Controls whether the given content should
* be add before or behind the actual content
* The possible values are:
* "BEFORE" -> The new content will be add
* before the actual content.
* "BEHIND" -> The new content will be add
* behind the actual content.
$BEFORE_BEHIND = empty( $BEFORE_BEHIND )
$row = ( empty( $row ) ) ? 0 : $row;
$col = ( empty( $col ) ) ? 0 : $col;
if ( $BEFORE_BEHIND == "BEFORE" )
* Adds a new row after the last row of this table. If there are lesser cols
* in the last row of this table than a row is able to contain, the last row
* will NOT be filled up with cols. This function ONLY adds a COMPLETE new row
* AFTER the last row of this table!
* @author Daniel Plücken <daniel@debakel.net>
* @param array $contentArr An array of strings which should contain
* the content of each col in the row. Id
* est if a row of this table is able to
* contain ten cols then the array should
* be contain ten records.
* @param array $styleClassArr An array of strings which declares the
* styleclass of the appropriate col.
* @param array $no_format_arr An array of booleans. If the the value
* is false it means that after the
* <td>-tag and after the content of
* the appropriate col there will be add a
* If the value is true no linebreaks will
* be add. You need this functionality if
* there are side by side lying pictures in
* a table which should not has any spaces
* array $otherAttributes An array of strings which can be used to
* add col-attributes to the appropriate
$styleClassArr = array(),
$no_format_arr = array(),
$otherAttributes = array()
for( $i = 0; $i < $this->cols; $i++ )
if( !empty( $contentArr[$i] ) )
$this->contentArr[$nextRow][$i] = $contentArr[$i];
if( !empty( $styleClassArr[$i] ) )
if( !empty( $no_format_arr[$i] ) )
if( !empty( $otherAttributes[$i] ) )
* Returns the rebuilded source code of this Object.
* @author Daniel Plücken <daniel@debakel.net>
$outputstr .= "\x24table = new HTMLTable(\r\n"
. " ". $this->cols. ", \x24rows, "
. "\x24width, \x24height,\r\n"
. "\x24rowsHeightArr,\r\n"
. " \x24cellpadding, \x24cellspacing,\r\n"
. " \x24border, \x24style, "
for( $i = 0; $i < $this->rows; $i++ )
for( $j = 0; $j < $this->cols; $j++ )
$outputstr .= "\x24table->addCol( \"".
if( $j + 1 == $this->cols )
* Returns a generated string based on the Attributes of this HTML-Object.
$out = "<!--\r\n\r\n TABLE\r\n "
$out .= "--><table id=\"". $this->id. "\"\r\n"
? " width=\"". $this->width. "\"\r\n"
? " height=\"". $this->height. "\"\r\n"
? " class=\"". $this->style. "\"\r\n"
. " border=\"". $this->border. "\"\r\n"
for( $i = 0; $i < $this->rows; $i++ )
for ( $j = 0; $j < $this->cols; $j++ )
$out .= "<!--\r\n --><td "
$out .= trim( $str_tmp );
$out .= "<!--\r\n --></td>";
$out .= "<!--\r\n--></tr>";
$out .= "<!--\r\n | | |\r\n "
} // END of class HTMLTable
|