<?php
define('TEMPLATE_EXT','.phtml');
define('CACHE_EXT',	  '.html');
/*
* Renders template
* Usage for handle templates
*/
class CtrlPHPTemplate extends CtrlComponent
{
	public $template_dir = '';
	public $cache_dir 	 = 'cached';
	public $debug = true;
	
	private $_vars = array(); // template vars
	private $_cache = false; // use cache
	
	private $_class_file;// class file name
	private $_phtml_file;// template php file name
	private $_cache_file;// cached file name
	
	/*
	* Constructor
	* Sets the phtml file name
	* @parameter template file name
	*/
	public function __construct($object)
	{
		$file = get_class($object);
		$this->_class_file = $file.CLASS_EXT;// view file
		$this->_phtml_file = $file.TEMPLATE_EXT; // template phtml file name
		$this->_cache_file = md5($file).CACHE_EXT; // cached file name
	}
	/*
	* Sets variable to the template
	* @parameter variable name
	* @parameter variable value
	*/
	public function set($var,$value)
	{
		$val = $this->Session->get($var);
		if($val!=$value)
		{
			$this->_cache = false;
			$this->Session->set($var,$value);			
		}
		$this->_vars[$var] = $value;
	}
	/*
	*
	*/
	public function useCache($cache = true)
	{
		$this->_cache = $cache;
	}
	/*
	* Do we use the cache and is the file cached
	*/
	public function isCached()
	{
		if(is_dir($this->cache_dir.DIRECTORY_SEPARATOR)==false)
		{
			#mkdir($this->cache_dir.DIRECTORY_SEPARATOR);
		}
		/*
		* Do we have the cache
		*/
		if(is_file($this->cache_dir.DIRECTORY_SEPARATOR.$this->_cache_file))
		{
			// current file modifie time
			$_modif_current_file = filemtime(__FILE__);
			
			// current template logic change time
			$_modif_class = filemtime($this->template_dir.DIRECTORY_SEPARATOR.$this->_class_file);
			
			// current template file modified time
			$_modif_file = filemtime($this->template_dir.DIRECTORY_SEPARATOR.$this->_phtml_file);
			
			// the cache file modified time
			$_modif_cache = filemtime($this->cache_dir.DIRECTORY_SEPARATOR.$this->_cache_file);

			// Cache expired?
			if($_modif_current_file > $_modif_cache OR $_modif_class > $_modif_cache OR $_modif_file > $_modif_cache) 
			{
				// remove the cache because its old
				@unlink($this->cache_dir.DIRECTORY_SEPARATOR.$this->_cache_file);
				return false;
			}
			else
			{
				$this->_cache = false;
				return true;// dispose caching. Using cache
			}
		}
		else
		{
			return false;// cant use cache file doesnt exists 
		}		
	}
    /**
	 * This function returns a cached copy of a template (if it exists),
	 * otherwise, it parses it as normal and caches the content.
	 *
	 * @param $file string the template file
	 */
    public function cache($content) 
	{
		// Write the cache
		if($fp = @fopen($this->cache_dir.DIRECTORY_SEPARATOR.$this->_cache_file, 'w')) 
		{
			fwrite($fp, $content);
			fclose($fp);
		}
		else 
		{
			trigger_error('Unable to write cache. <b>'.$this->_cache_file.'</b>', E_USER_ERROR);
		}
    }	
	/*
	* Renders the content
	* Includes the template file
	* @returns string content of the template file
	*/
	public function render()
	{
		// Start output bufferin	
		$this->Response->start();													
		// check is the template file exists
		
		if(is_file_exists($this->template_dir.DIRECTORY_SEPARATOR.$this->_phtml_file) == false)
		{
			trigger_error("Can't locate <b>$this->_phtml_file file</b>", E_USER_ERROR);
			return false;
		}
		/*
		* If the file is cache and its new then use it
		*/
		if($this->isCached()==true)
		{
			include($this->cache_dir.DIRECTORY_SEPARATOR.$this->_cache_file);
		}
		else
		{
			extract($this->_vars);	// extract vars to local namespace
			include($this->_phtml_file); // include the template file
		}
		
		$content = $this->Response->getContent();// Get the contents of the buffer
		
		$this->Response->end();				// End buffering and discard
											
		if($this->_cache == true)			// If user wants to cache
		{
			$this->cache($content);			// caches the content
		}
		
		return $content;					// return the contents		
	}
}
?>
