<?php
/**
 * Control
 *
 * @date 21.03.2007
 * @name CtrlControl
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2006 Margo Poolak. All Rights Reserved.
 * @version 2.0
 * @package System.Web.UI
 * @category 
 * 
 * This is UI control component
 */
class Control extends CtrlComponent
{
	private $_master_page;// master page handler
	private $_container; // container handler
	private $_visible = true;
	private $_tpl_control; // template control handler
	/*
	* 
	*/
	public function __construct()
	{
		parent::__construct();
		$this->_tpl_control = new CtrlTemplate($this);
	}
	/*
	* 
	*/
	public function setVar($var,$value)
	{	
		$this->_tpl_control->set($var,$value);
	}
	/**
	* Sets the template visible true or false
	* @param boolean visible
	*/
	public function setVisible($visible = false)
	{
		$this->_visible = $visible;
	}
	/**
	* Is the template visible
	* @return boolean is visible or not
	*/
	public function getVisible()
	{
		return $this->_visible;
	}	
	/**
	* Sets master page to the current page
	* @param string master page name
	*/
	public function setMasterPage($value)
	{
		if($value == get_class($this))
		{
			trigger_error("Not allowed to use itself as master page (Infinity loop prevented) in $value.",E_USER_ERROR);
		}
		else
		{
			$this->_master_page = new $value();
			$this->_master_page->setContainer($this);// set current page to container
		}
	}
	/**
	* Returns the master page object
	* @return object TPage
	*/
	public function getMasterPage()
	{
		return $this->_master_page;
	}
	/**
	* Sets the container to the page
	* @param object container
	*/
	public function setContainer($container)
	{
		$this->_container = &$container;
	}
	/**
	* If the page has container
	* @return boolean true|false
	*/
	public function hasContainer()
	{
		return (isset($this->_container))?true:false;
	}
	/**
	* Returns the cotainer object
	* @return object container
	*/
	public function getContainer()
	{
		return $this->_container;
	}
	/**
	* Executes all the methods in page
	*/	
	public function execute()
	{
		Ctrl::trace(get_class($this).'.OnInit()');	
		$this->onInit();
		/*
		* If set master page then end page Load and execute master page
		*/
		if(isset($this->_master_page))
		{
			$this->_master_page->onInit();// execute master page OnInit
		}
		
		if($this->_visible == true)
		{
			
			if($this->hasContainer() == true)
			{
				
				Ctrl::trace(get_class($this->Container).'.OnLoad()');
				$this->Container->onLoad();// end the container load
				
				Ctrl::trace(get_class($this).'.OnLoad()');
				$this->onLoad();// end the page load		
			}
			else
			{
				$this->onLoad();// end the page load
				Ctrl::trace(get_class($this).'.OnLoad()');	
			}
		}
	}
	/**
	*  Dummy OnInit to overload
	*/	
	public function onInit()
	{
	}
	/**
	* Dummy OnLoad to overload
	*/	
	public function onLoad()
	{
	}
	/**
	* Page render
	* @return string of page content
	*/	
	public function render()
	{
		if(!$this->_tpl_control)
		{
			exit('No template controller defined');
		}
		if($this->_visible == true)
		{
			return $this->_tpl_control->render();
		}
		return;	
	}
	/*
	* Display the page
	* @returns echo
	*/
	public function display()
	{
		if(is_object($this->_master_page))
		{
			$this->_master_page->display();
		}
		else
		{
			echo $this->render();
		}		
	}
}
?>