<?php
/**
* _Html_Table is a html helper class that takes data in order to generate a html table
* @copyright Copyright (c) 2012, PWF
* @license http://phpwebframework.com/License
* @version PWF_0.3.0
*/
class _Html_Table implements _Html_Interface
{
private $columns;
private $tableClass;
private $htmlAttributes;
private $cells = array();
private $cellsClasses = array();
private $cellsStyles = array();
private $cellsAtrributes = array();
private $collumnsClasses = array();
private $index = 0;
/**
* Constructs a table object
* @param $columns The number of the columns of the table (if not given then 1)
* @param $tableClass The name of the css class of the table if given
*/
public function __construct($columns = 1,$tableClass = null)
{
$this->columns = $columns;
if(!is_null($tableClass))
{
$this->tableClass = $tableClass;
}
}
/**
* Changes the number of the table columns
* @param number $columns
*/
public function resetColumns($columns)
{
$this->columns = $columns;
}
/**
* Sets the css class for a cell
* @param string $class
*/
public function setCellClass($class)
{
$this->cellsClasses[$this->index] = $class;
}
/**
* Sets any other html attributes for the cell
* @param string $attributes
*/
public function setCellAttributes($attributes)
{
$this->cellsAtrributes[$this->index] = $attributes;
}
/**
* Sets the style attribute of a cell
* @param string $style
*/
public function setCellStyle($style)
{
$this->cellsStyles[$this->index] = $style;
}
/**
* Sets a css class for all cells of a column in a table
* @param number $column
* @param string $class
*/
public function setColumnClass($column,$class)
{
$this->collumnsClasses[$column] = $class;
}
/**
* Adds a cell to the table with the content given
* @param string $cell The content of the cell
*/
public function addCell($cell = "")
{
$this->cells[$this->index] = $cell;
$this->index++;
}
/**
* Sets the Html attributes of the table
* @param string $htmlAttributes
*/
public function setHtmlAttributes($htmlAttributes)
{
$this->htmlAttributes = $htmlAttributes;
}
/**
* Sets the Html attributes of the table to be
* width='100%' cellpadding='0' cellspacing='0'
*/
public function fuulTable()
{
$this->htmlAttributes = "width='100%' cellpadding='0' cellspacing='0'";
}
/**
* Adds a blang row to the table
* @param $height If given then it will be the height in px of the row
*/
public function addRow($height = null)
{
for($i=0; $i<$this->columns; $i++)
{
if($height != null)
{
$this->setCellStyle("height:".$height."px");
}
$this->addCell("");
}
}
/* Generates and return the html code of the current instance.
* @see _Html_Interface::generate()
*/
public function generate()
{
$result = "";
if(count($this->cells)>0)
{
$currentColumn = 1;
$result = "<table";
if(isset($this->htmlAttributes))
{
$result .= " ".$this->htmlAttributes;
}
if(isset($this->tableClass))
{
$result .= " ".$this->tableClass;
}
$result .= ">\n";
for($i=0; $i<count($this->cells); $i++)
{
if($currentColumn==1)
{
$result .= "<tr>\n";
}
$result .= "<td";
if(isset($this->collumnsClasses[$currentColumn]))
{
$result .= " class='".$this->collumnsClasses[$currentColumn]."'";
}
if (isset($this->cellsClasses[$i]))
{
$result .= " class='".$this->cellsClasses[$i]."'";
}
if (isset($this->cellsStyles[$i]))
{
$result .= " style=\"".$this->cellsStyles[$i]."\"";
}
if (isset($this->cellsAtrributes[$i]))
{
$result .= " ".$this->cellsAtrributes[$i];
}
$result .= ">\n";
$result .= $this->cells[$i];
$result .= "</td>\n";
if($currentColumn==$this->columns)
{
$result .= "</tr>\n";
$currentColumn = 1;
}
else
{
$currentColumn++;
}
}
if($currentColumn!=1)
{
$currentColumn--;
$times = $this->columns - $currentColumn;
for($i=0; $i<$times; $i++)
{
$result .= "<td></td>\n";
}
$result .= "</tr>\n";
}
$result .="</table>\n";
}
return $result;
}
}
?>