<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* LAYOUTS LIBRARY
* @author : Zul Zolkaffly
* Load template/layout
*/

defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/'); 
class Templates
{
	public $path = 'layouts';
	public $layout = 'default';
	public $view = null;
	public $includes = null;
	public $data = array();
	var $obj;

	function __construct() {
		$this->obj =& get_instance();
	}

	function setPath($path) {
		$this->path = $path;
		return $this;
	}
	
	function getPath() { return $this->path; }

	function setLayout($layout) {
		$this->layout = $layout;
		return $this;
	}
	
	function getLayout() { return $this->layout; }
	
	function setView($path, $data = array()) {
		$v = count($this->view);
		$this->view[$v] = $path;
		
		if($data && !empty($data)) $this->setData($data);
		
		return $this;
	}
	
	function getView($index = NULL) { return ($index === NULL) ? $this->view : ((isset($this->view[$index])) ? $this->view[$index] : NULL); }
	
	function setData($data) {
		if($data && !empty($data)) {
			foreach($data as $key=>$value) {
				$this->data[$key] = $value;
			}
		}
		return $this;
	}
	
	function getData($key = NULL) { return ($key) ? ((isset($this->data[$key])) ? $this->data[$key] : NULL) : $this->data; }
	
	public function setIncludes($path, $prepend_base_url = true){
		$inc = NULL;
		if($prepend_base_url){
			$this->CI->load->helper('url');
			$inc = base_url() . $path;
		}else{
			$inc = $path;
		}
		
		if($inc) {
			$i = count($this->includes);
			$this->includes[$v] = $inc;
		}
		
		return $this;
	}
	function getIncludes($index = NULL) { return ($index === NULL) ? $this->includes : ((isset($this->includes[$index])) ? $this->includes[$index] : NULL); }
	
	function display($data = array()) {
		if($data && !empty($data)) $this->setData($data);
		if(!empty($this->view)) {
			foreach($this->view as $key=>$value) {
				$this->obj->load->view($value, $this->data);
			}
		} else {
			$controller = $this->obj->router->fetch_class();
			$method = $this->obj->router->fetch_method();
			$view = '/'.$controller.'/'.$method.'.php';
			$this->obj->load->view($view, $this->data);
		}
		return $this;
	}
	
	function includes($inc = NULL, $prepend_base_url = true) {
		if($inc) {
			if(!is_array($inc)) {
				$this->setIncludes($inc, $prepend_base_url);
			} else {
				foreach($inc as $path) {
					$this->setIncludes($path, $prepend_base_url);
				}
			}
		}
			
		$return = NULL;
		if($this->includes) {
			foreach($this->includes as $include){
				if(preg_match('/js$/', $include)){
					$return .= '<script type="text/javascript" src="' . $include . '"></script>\n';
				}elseif(preg_match('/css$/', $include)){
					$return .= '<link type="text/css" href="' . $include . '" rel="stylesheet" />\n';
				}else{
					$return .= '<script type="text/javascript" src="' . $include . '"></script>\n';
				}
			}
		}
		
		return $return;
	}
	
	function render($view = NULL, $data = NULL, $layout = NULL) {
		if($layout)$this->setLayout($layout);
		if($data && !empty($data)) $this->setData($data);
		if($view)$this->setView($view, $data);
		
		if(strpos(strtolower($this->layout),'.php') === FALSE) $this->layout .= ".php";
		
		$path = '/'.$this->path.'/'.$this->layout;
		$path = str_replace('\\',DIR_SEPARATOR,$path);
		$this->obj->load->view($path, $this->data);
	}
}
?>