All pastes #824632 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-12-20 02:26 UTC
rendered paste body
<?PHP
/**
 *	clsTable
 * 
 *	Create dynamic tables 
 * 
 *	@author Timothy G Majerus 
 *	@package greelcore 
 *	@filesource 
 */
class clsTable {

/****************************************************************
 *	Public Variables
 ****************************************************************/	

	/**
	 * Must be a multi-dimensional / associative
	 * array, ie: a recordset from a database return
	 * Example of Array:
	 *	Array
	 *		(
	 *	    [0] => Array
	 *	        (
	 *	            [id] => 1
	 *	            [fname] => John
	 *	            [lname] => Doe
	 *	        )
	 *	
	 *	    [1] => Array
	 *	        (
	 *	            [id] => 2
	 *	            [fname] => Jane
	 *	            [lname] => Doe
	 *	        )
	 *		)
	 * @var Array Associative array of column data
	 * @access Public
	 */
	public $columns			= array();
	
	/**
	 * The number of columns in the dataset
	 * 
	 * @var Integer Column count
	 * @access Public
	 */	
	public $colCount		= 0;
	
	/**
	 * The tables cell spacing
	 * 
	 * @var String Cell spacing
	 * @access Public
	 */
	public $cellSpacing		= '';
	
	/**
	 * The tables cell padding
	 * 
	 * @var String Cell padding
	 * @access Public
	 */	
	public $cellPadding		= '';
	
	/**
	 * The tables CSS class name
	 * 
	 * @var String Table CSS class name
	 * @access Public
	 */		
	public $tableClassName	= '';
	
	/**
	 * The TD CSS class name
	 * 
	 * @var String TD CSS class name
	 * @access Public
	 */		
	public $tdClassName		= '';
	
	/**
	 * The TR CSS class name
	 * 
	 * @var String TR CSS class name
	 * @access Public
	 */		
	public $trClassName		= '';
	
	/**
	 * The tables width
	 * 
	 * @var Integer The tables width
	 * @access Public
	 */		
	public $tableWidth		= 0;
	
	/**
	 * The tables height
	 * 
	 * @var Integer The tables height
	 * @access Public
	 */		
	public $tableHeight		= 0;
	
	/**
	 * The tables border
	 * 
	 * @var Integer The tables border
	 * @access Public
	 */		
	public $tableBorder		= 0;
	
	/**
	 * The tables background color
	 * 
	 * @var String The tables background color
	 * @access Public
	 */		
	public $tableBGColor	= '';
	
	/**
	 * The TD background color
	 * 
	 * @var String The TD background color
	 * @access Public
	 */	
	public $tdBGColor		= '';

	/**
	 * The TR width
	 * 
	 * @var String The TR width
	 * @access Public
	 */	
	public $trWidth			= '';
	
	/**
	 * The TD height
	 * 
	 * @var String The TD height
	 * @access Public
	 */		
	public $tdHeight		= '';
	
	/**
	 * The TR height
	 * 
	 * @var String The TR height
	 * @access Public
	 */		
	public $trHeight		= '';
	
/****************************************************************
 *	Private Variables
 ****************************************************************/

	/**
	 * The number of rows in the dataset
	 * We get this information from $columns
	 * 
	 * @var Integer Row count
	 * @access Private
	 */		
	private $rowCount		= 0;
	
	/**
	 * The table
	 * 
	 * @var String The table
	 * @access Private
	 */		
	private $_table			= '';
	
	/**
	 * The table head tag
	 * 
	 * @var String The table head tag
	 * @access Private
	 */		
	private $_tableHead		= '';
	
	/**
	 * The table foot tag
	 * 
	 * @var String The table foot tag
	 * @access Private
	 */		
	private $_tableFoot		= '';
	
	/**
	 * The table row head tag
	 * 
	 * @var String The table row head tag
	 * @access Private
	 */		
	private $_trHead		= '';
	
	/**
	 * The table row foot tag
	 * 
	 * @var String The table row foot tag
	 * @access Private
	 */		
	private $_trFoot		= '';
	
	/**
	 * The measurement type
	 * ie: percentage (%) or pixels (px)
	 * 
	 * @var String The type of measure
	 * @access Private
	 */
	private $_measureType	= '';
	
	/**
	 * The table data
	 * We do alternating colors for rows
	 * and also print the column headers in bold
	 * 
	 * @var String The table data (TD)
	 * @access Private
	 */	
	private $_columnData	= '';
	
	/**
	 * The TD width
	 * This var is private because it
	 * is determined by the width of
	 * the column data
	 * 
	 * @var String The TD width
	 * @access Private
	 */		
	private $_tdWidth		= '';
		
	/**
	 *	Class Constructor
	 *
	 *	Sets default table element settings
	 * 
	 * @param Array Column data
	 */
	public function __construct($columns) {
		$this->setColumns($columns);
		$this->setColCount(count($columns[0]));
		$this->setRowCount(count($columns));
		$this->setCellSpacing(0);
		$this->setCellPadding(40);
		$this->setTableClass('dpTable');
		$this->setTrClass('borderTable');		
		$this->setTDClass('');	
		$this->setTableWidth(100);
		$this->setTableHeight(2);
		$this->setTableBGColor('#000000');
		$this->setTableBorder(1);
		$this->setMeasureType('%');
	}
	
/****************************************************************
 *	Accessors
 ****************************************************************/
		
	/**
	 *	setColumns()
	 *
	 *	Sets the column data array
	 * 
	 * @param Array Column data
	 */
	public function setColumns($val) {
		$this->columns = $val;
	}

	public function setCellSpacing($val) {
		$this->cellSpacing = $val;
	}

	public function setCellPadding($val) {
		$this->cellPadding = $val;
	}

	public function setTDClass($val) {
		$this->trClassName = $val;
	}

	public function setTableClass($val) {
		$this->tableClassName = $val;
	}
	
	public function setTRClass($val) {
		$this->tdClassName = $val;
	}
	
	private function _setTDWidth($val) {
		$this->_tdWidth = $val;
	}

	public function setTDHeight($val) {
		$this->height = $val;
	}

	public function setTableWidth($val) {
		$this->width = $val;
	}

	public function setTableHeight($val) {
		$this->height = $val;
	}
	
	public function setColCount($val) {
		$this->colCount = $val;
	}

	public function setRowCount($val) {
		$this->rowCount = $val;
	}

	public function setTRBGColor($val) {
		$this->trBgColor = $val;
	}
	public function setTDBGColor($val) {
		$this->bgColor = $val;
	}
	public function setTableBGColor($val) {
		$this->tableBGColor = $val;
	}
		
	public function setTableBorder($val) {
		$this->tableBorder = $val;
	}
	public function setMeasureType($val) {

		$this->_measureType = $val;
	}
	
/****************************************************************
 *	Modifiers
 ****************************************************************/

	public function getColumns() {
		return $this->columns;
	}

	public function getRowCount() {

		return $this->rowCount;
	}

	public function getCellSpacing() {
		return $this->cellSpacing;
	}

	public function getCellPadding() {
		return $this->cellPadding;
	}

	public function getTableClass() {
		return $this->tableClassName;
	}
	
	public function getTDClass() {
		return $this->tdClassName;
	}

	public function getTRClass() {
		return $this->trClassName;
	}
	
	public function getTableWidth($type='') {
		return $this->width.$type;
	}

	public function getTableHeight($type='') {
		return $this->height.$type;
	}

	public function getTDWidth($type='') {
		return $this->_tdWidth.$type;
	}

	public function getTDHeight($type='') {
		return $this->tdHeight.$type;
	}
	
	public function getColCount() {
		return $this->colCount;
	}

	public function getTDBGColor() {
		return $this->bgColor;
	}

	public function getTableBorder() {
		return $this->border;
	}

	public function getTRBGColor() {
		return $this->trBgColor;
	}	
/****************************************************************
 *	Public Methods
 ****************************************************************/

	/**
	 * Build the god damned table for christs sake
	 */
	public function buildTable() {
		$this->_table .= $this->_createTableHead();
		$this->_table .= $this->_createColumns();
		$this->_table .= $this->_getTableFoot();
	
	 	return $this->_table;
	 }

/****************************************************************
 *	Private Methods
 ****************************************************************/

	/**
	 * Create the table head
	 */
	private function _createTableHead() {
	 	$this->_tableHead = "<TABLE BORDER='".$this->getTableBorder()."'WIDTH='".
	 		$this->getTableWidth($this->_getMeasureType())."'HEIGHT='".$this->getTableHeight($this->_getMeasureType())."' CELLSPACING='".
	 		$this->getCellSpacing()."' CELLPADDING='".$this->getCellPadding()."'CLASS='".
	 		$this->getTableClass()."'>\r\n";

		return $this->_tableHead;
	}

	/**
	 * Create the TR head
	 */
	private function _getTRHead() {
	 	$this->_trHead = "<TR BGCOLOR='".$this->getTRBGColor()."' CLASS='".$this->getTRClass()."'>\r\n";

		return $this->_trHead;
	}

	/**
	 * Create the TR foot
	 */
	private function _getTRFoot() {
	 	$this->_trFoot = "</TR>\r\n";

		return $this->_trFoot;
	}
	
	/**
	 * Create the TD data set
	 * @todo create setters and getters for alternating row colors
	 * 
	 */
	private function _createColumns() {
		$tmp = $this->getColumns();
		$x = 0;
		foreach($tmp as $key=>$val){
			if ($this->getTRBGColor() == '#BDEDFF') {
				$this->setTRBGColor('#ffffff');
			}else{
				$this->setTRBGColor('#BDEDFF');
			}
			if($x < count($val)) {		
				$this->_columnData .= $this->_getTRHead();
				foreach($val as $key2=>$val2){
					$this->_setTDWidth(strlen($val2));
					$this->_columnData .= "<TD WIDTH='".$this->getTDWidth()."' HEIGHT='".$this->getTDHeight()."' BGCOLOR='".
					$this->getTDBGColor()."' CLASS='".
					$this->getTDClass()."'><B>".ucfirst($key2)."</B></TD>\r\n";				
					$x++;
				}
				$this->_columnData .= $this->_getTRFoot();
			}
			$this->_columnData .= $this->_getTRHead();			
			foreach($val as $key2=>$val2){
				if(empty($val2)){
					$val2 = '&nbsp;';
				}
				$this->_setTDWidth(strlen($val2));
				$this->_columnData .= "<TD WIDTH='".$this->getTDWidth()."' HEIGHT='".$this->getTDHeight()."' BGCOLOR='".
				$this->getTDBGColor()."' CLASS='".
				$this->getTDClass()."'>".$val2."</TD>\r\n";	
			}
			$this->_columnData .= $this->_getTRFoot();			

		}
		
		return $this->_columnData;
	}

	/**
	 * Create the table foot
	 */
	private function _getTableFoot() {
	 	$this->_tableFoot = "</TABLE>\r\n";

		return $this->_tableFoot;
	}

	/**
	 * Gets the measure type
	 * for heights and widths ( % or px )
	 */
	private function _getMeasureType() {

		return $this->_measureType;
	}
}
?>