<?php

/**
 *
 * @author Peter Csubak (chuby)
 * @copyright Peter Csubak (chuby)
 */
class PTable extends PComponent{

	/** @CProperty */
	protected $style;
	private $rows;
	/** @CProperty */
	protected $cellspacing;
	/** @CProperty */
	protected $cellpadding;
	/** @CProperty */
	protected $width;
	
	public static $evtOnMouseOver = 2;
	public static $evtOnMouseOut = 3;
	
	public function __construct($cellspacing = 0, $cellpadding = 0, $width = "100%", $id = "", $cssclass = "", $style = "", $evtslock = ""){
		$this->cellspacing = $cellspacing;
		$this->cellpadding = $cellpadding;
		$this->width = $width;
		$this->rows = array();
		$this->id = $id;
		$this->cssclazz = $cssclass;
		$this->style = $style;
		$this->eventsLock = $evtslock;
	}
	
	/**
	 * 
	 * @return PRow
	 */
	public function addRow($row = null, $id = ""){
		$nrow = (($row != null) && ($row instanceof PRow)) ? $row : new PRow();
		if ($id != ""){
			$nrow->setId($id);
		}
		if ($this->eventsLock != ""){
			$nrow->eventsLock = $this->eventsLock;
		}
		$this->rows[count($this->rows)] = $nrow;
		return $nrow;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function addRowString($row){
		if ($row != ""){
			$this->rows[count($this->rows)] = $row;
		}
	}
	
	public function removeRow($rownum = -1, $id = ""){
		if ($rownum >= count($this->rows)){
			return;
		}
		
		if ($rownum < 0){
			foreach ($this->rows as $key => $row){
				if (($row instanceof PRow) && ($row->getId() == $id)){
					unset($this->rows[$key]);
					break;
				}
			}
			$this->rows = array_values($this->rows);
		}else{
			unset($this->rows[$rownum]);
			$this->rows = array_values($this->rows);
		}
	}
	
	public function getRow($rownum = -1, $id = ""){
		if ($rownum >= count($this->rows)){
			return;
		}
		
		if ($rownum < 0){
			foreach ($this->rows as $row){
				if (($row instanceof PRow) && ($row->getId() == $id)){
					return $row;
				}
			}
		}else{
			return $this->rows[$rownum];
		}
		
		return null;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setStyle($style){
		$this->style = $style;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getStyle(){
		return $this->style;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setCellSpacing($cellspacing){
		$this->cellspacing = $cellspacing;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getCellSpacing(){
		return $this->cellspacing;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setCellPadding($cellpadding){
		$this->cellpadding = $cellpadding;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getCellPadding(){
		return $this->cellpadding;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setWidth($width){
		$this->width = $width;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getWidth(){
		return $this->width;
	}
	
	public function render(){
		$res = "<table";
		
		if ($this->id != ""){
			$res .= " id=\"".$this->id."\"";
		}
		
		if ($this->cssclazz != ""){
			$res .= " class=\"".$this->cssclazz."\"";
		}
		
		if ($this->style != ""){
			$res .= " style=\"".$this->style."\"";
		}
		
		if ($this->cellpadding >= 0){
			$res .= " cellpadding=\"".$this->cellpadding."\"";
		}
		
		if ($this->cellspacing >= 0){
			$res .= " cellspacing=\"".$this->cellspacing."\"";
		}
		
		if ($this->width != ""){
			$res .= " width=\"".$this->width."\"";
		}
		
		if (is_array($this->activeEvents)){
			foreach ($this->activeEvents as $evtTypes){
				if ($evtTypes == self::$evtOnMouseOut){
					$res .= " onmouseout=\"sendAjaxEvent('AjaxOnMouseOut', '".$this->id."', '".$this->bindedTo."', '".$this->eventsLock."')\"";
				}
				if ($evtTypes == self::$evtOnMouseOver){
					$res .= " onmouseover=\"sendAjaxEvent('AjaxOnMouseOver', '".$this->id."', '".$this->bindedTo."', '".$this->eventsLock."')\"";
				}
			}
		}
		
		$res .= ">";
		if (is_array($this->rows)){
			foreach ($this->rows as $row){
				if ($row instanceof PRow){
					$res .= $row->render();
				}else{
					$res .= $row;
				}
			}
		}
		$res .= "</table>";
		
		return $res;
	}
	
}
?>