<?php

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

	/** @CProperty */
	protected $style;
	/** @CProperty */
	protected $align;
	/** @CProperty */
	protected $valign;
	private $cells;
	
	public function __construct($align = "", $valign="", $id = "", $cssclass = "", $style = ""){
		$this->align = $align;
		$this->valign = $valign;
		$this->id = $id;
		$this->cssclazz = $cssclass;
		$this->style = $style;
		$this->cells = array();
	}
	
	/**
	 * 
	 * @return PCell
	 */
	public function addCell($cell = null, $id = "", $content = null){
		$ncell = (($cell != null) && ($cell instanceof PCell)) ? $cell : new PCell();
		if ($id != ""){
			$ncell->setId($id);
		}
		if ($this->eventsLock != null){
			$ncell->eventsLock = $this->eventsLock;
		}
		if ($content !== null){
			$ncell->setContent($content);
		}
		$this->cells[count($this->cells)] = $ncell;
		return $ncell;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function addCellString($cell){
		if ($cell != ""){
			$this->cells[count($this->cells)] = $cell;
		}
	}
	
	public function removeCell($cellnum = -1, $id = ""){
		if ($cellnum >= count($this->cells)){
			return;
		}
		
		if ($cellnum < 0){
			foreach ($this->cells as $key => $cell){
				if (($cell instanceof PCell) && ($cell->getId() == $id)){
					unset($this->cells[$key]);
					break;
				}
			}
			$this->cells = array_values($this->cells);
		}else{
			unset($this->cells[$cellnum]);
			$this->cells = array_values($this->cells);
		}
	}
	
	public function getCell($cellnum = -1, $id = ""){
		if ($cellnum >= count($this->cells)){
			return;
		}
		
		if ($cellnum < 0){
			foreach ($this->cells as $cell){
				if (($cell instanceof PCell) && ($cell->getId() == $id)){
					return $cell;
				}
			}
		}else{
			return $this->cells[$cellnum];
		}
		
		return null;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setStyle($style){
		$this->style = $style;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getStyle(){
		return $this->style;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setAlign($align){
		$this->align = $align;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getAlign(){
		return $this->align;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function setVAlign($valign){
		$this->valign = $valign;
	}
	
	/**
	 * 
	 * @deprecated 
	 */
	public function getVAlign(){
		return $this->valign;
	}
	
	public function render(){
		$res = "<tr";
		
		if ($this->id != ""){
			$res .= " id=\"".$this->id."\"";
		}
		
		if ($this->cssclazz != ""){
			$res .= " class=\"".$this->cssclazz."\"";
		}
		
		if ($this->style != ""){
			$res .= " style=\"".$this->style."\"";
		}
		
		if ($this->align != ""){
			$res .= " align=\"".$this->align."\"";
		}
		
		if ($this->valign != ""){
			$res .= " valign=\"".$this->valign."\"";
		}
		
		$res .= ">";
		
		foreach ($this->cells as $cell){
			if ($cell instanceof PCell){
				$res .= $cell->render();
			}else{
				$res .= $cell;
			}
		}
		
		$res .= "</tr>";
		
		return $res;
	}
	
}
?>