<?php

class Layouts {
	// Hold CI Instance
	private $CI;

	// Layout title
	private $layout_title = null;

	// Layout description
	private $layout_description = null;


	// hold includes  like css and js
	private $includes = array();

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


	public function set_title($title){
		$this->layout_title = $title;
	}

	public function set_description($description){
		$this->layout_description = $description;
	}

	public function add_includes($path, $prepend_base_url = true){
		if($prepend_base_url){
			$this->CI->load->helper('url');
			$this->includes[] = base_url() . $path;
		}else{
			$this->includes[] = $path;
		}
		//print_r($this->includes);
		return $this;
	}

	public function print_includes(){
		$final_includes = '';

		foreach($this->includes as $include){
			if(preg_match('/js$/', $include)){
				$final_includes .= '<script type="text/javascript" src="' . $include . '"></script>';
			}elseif(preg_match('/css$/', $include)){
				$final_includes .= '<link type="text/css" href="' . $include . '" rel="stylesheet" />';
			}else{
				$final_includes .= '<script type="text/javascript" src="' . $include . '"></script>';
			}
		}
		return $final_includes;
	}

	public function view($view_name, $layouts = array(), $params = array(), $default = true, $userGuide = false){
		if ($default) {
			$header_params['layout_title'] = 'MiTi';
			$header_params['layout_description'] = $this->layout_description;

			if ($params) {
				foreach($params as $key=>$value) {
					$header_params[$key] = $value;
				}
			}

			$this->CI->load->view('layouts/header', $header_params);

            if ($userGuide) {
                $this->CI->load->view('layouts/sidebar_ug', $header_params);
            } else {
                $this->CI->load->view('layouts/sidebar', $header_params);
            }

			if (is_array($layouts) && count($layouts) > 0) {
				foreach ($layouts as $layout) {
					$this->CI->load->view($layout, $params);
				}
			}

			$this->CI->load->view($view_name, $params);
			$this->CI->load->view('layouts/footer');
		} else {
			$params['layout_title'] = 'Salihin Premier Solution';
			$params['layout_description'] = $this->layout_description;
			$this->CI->load->view($view_name, $params);
		}
	}
}
?>
