<?php
/**
 * CtrlApplication file
 * @date 9.03.2007
 * @name CtrlApplication
 *
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2007 Margo Poolak. All Rights Reserved.
 * @package System
 * 
 * Application file
 */ 
if(!defined('CTRL'))
{
	exit();
}
/**
 * CtrlApplication class

 * @name CtrlApplication
 *
 * @version 2.0
 * @internal This class is extended to CtrlComponent to get use of all the methods in init
 * 
 * Runs the system by running module and page using the request
 */ 
class CtrlApplication extends CtrlComponent
{
	/**
	* @var object request handler
	* @access private
	*/
	private $_request;
	/**
	* @var object response handler
	* @access private
	*/
	private $_response;
	/**
	* @var object session handler
	* @access private
	*/
	private $_session;
	/**
	* @var object cookie handler
	* @access private
	*/	
	private $_cookie;
	/**
	* @var array property handler
	* @access private
	*/	
	private $_propertys = array();
	/**
	* Sets application to Ctrl singleton
	*/
	public function __construct()
	{
		Ctrl::setApplication($this);
		$this->getSession();
	}
	/**
	* Sets new property to application
	* Overrides the parameter set before
	* @param string property name
	* @param mixed property value
	*/
	public function setProperty($name,$value)
	{
		$this->_propertys[$name] = $value;
		return true;
	}
	/**
	* Gets property if it set
	* If property is not set then trys to import class object
	* @param string variable name
	* @return mixed if failed then false else the object or value
	*/
	public function getProperty($var)
	{
		if(isset($this->_propertys[$var]))
		{
			return $this->_propertys[$var];
		}
		else
		{
			/**
			* Lets try to import a class object
			*/
			if(import($var) == true)
			{
				$object = new $var();
				$this->SetProperty($var,$object);
				return $object;
			}
			else
			{
				// didnt make it
				trigger_error("Unable to load $var", E_USER_ERROR);
			}
		}
		return false;
	}	
	/**
	* Sets Request object
	* @param CtrlHttpRequest object
	*/
	public function setRequest(CtrlHttpRequest $request)
	{
		$this->_request = $request;
		return true;
	}
	/**
	* GetsRequest object
	* @return CtrlHttpRequest object
	*/
	public function getRequest()
	{
		if(!$this->_request)
		{
			$this->_request = new CtrlHttpRequest();
		}
		return $this->_request;
	}
	/**
	* Sets Response object
	* @param CtrlHttpResponse object
	*/
	public function setResponse(CtrlHttpResponse $response)
	{
		$this->_response = $response;
		return true;
	}	
	/**
	* GetsResponse object
	* @return CtrlHttpResponse object
	*/	
	public function getResponse()
	{
		if(!$this->_response)
		{
			$this->_response = new CtrlHttpResponse();
		}
		return $this->_response;
	}
	/**
	* GetSession object
	* @return CtrlHttpSession
	*/
	public function getSession()
	{
		if(!$this->_session)
		{
			$this->_session = new CtrlHttpSession();
		}
		return $this->_session;
	}
	/**
	* SetSession object
	* @param CtrlHttpSession object
	* @return true
	*/
	public function setSession(CtrlHttpSession $session)
	{
		$this->_session = $session;
		return true;
	}
	/**
	* GetCookie object
	* @return CtrlHttpCookie
	*/
	public function getCookie()
	{
		if(!$this->_cookie)
		{
			$this->_cookie = new CtrlHttpCookie();
		}
		return $this->_cookie;
	}
	/**
	* SetCookie object
	* @param CtrlHttpCookie
	*/
	public function setCookie(CtrlHttpCookie $cookie)
	{
		$this->_cookie = $cookie;
		return true;
	}	
	/**
	* Starts running application
	*/
	public function run()
	{
		$page = $this->getRequest()->getRequestedPage();
		$method = $this->getRequest()->getRequestedMethod();
		/**
		* If the $mode has set get or post
		*/
		if (!!($page))
		{
			if ($this->getRequest()->isPostBack() == true) 
			{
				$args = $this->getRequest()->get("rsargs",array());
			}
			else 
			{
				// Bust cache in the head
				header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
				header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
				// always modified
				header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
				header ("Pragma: no-cache");                          // HTTP/1.0
				$args = $this->getRequest()->get("rsargs",array());				
			}
		}
		/**
		* Import page
		*/
		if(import($page)==false)
		{
			trigger_error("Could'nt find requested <b>".$page."</b> page",E_USER_ERROR);
		}
		/**
		* Generate page object
		*/
		$object = new $page();
		if(!$object instanceof Page)
		{
			trigger_error("$page is not instance of Page", E_USER_ERROR);
		}
		/**
		* Call page method
		*/				
		if(is_callable(array($object,$method)) == false)
		{
			trigger_error( "Method: <b>$method</b> is not callable in ".$page, E_USER_NOTICE);
		}
		/**
		* Call page object method
		*/
		if(!!($args))
		{
			$result = call_user_func_array(array($object,$method), $args);
		}
		else
		{
			$result = $object->$method();
		}
		exit($result);		
	}
	/**
	* Ends the application
	*/
	public function __destruct()
	{
		//echo $this->getResponse()->getTimeLeft().' sec';
		unset($this);
	}	
}
?>