<?php
(defined('BASEPATH')) or exit('No direct script access allowed');

/* load the HMVC_Loader class */
require APPPATH . 'third_party/HMVC/Loader.php';
#[AllowDynamicProperties]
class MY_Loader extends HMVC_Loader {

	public function __construct()
	{
		parent::__construct();
	}

	public function page($view, $vars = array(), $return = FALSE)
	{
		parent::view('header', $vars, $return);
		$ret = parent::view($view, $vars, $return);
		parent::view('footer', $vars, $return);

		return $ret;
	}

	// Reponse loader
	public function response()
	{
		// Grab the super object
		$CI =& get_instance();

		// Initialize the response variable.
		$this->library("response");

		// check ajax request
		$is_ajax_req = $CI->input->is_ajax_request();
		$is_ajax_req = true;

		if ($is_ajax_req)
		{
			// if request is ajax then load ajax response class
			$CI->response = new ResponseAjax();
		}
	}

	public function subview($view, $vars = array(), $return = FALSE)
	{
		$ret = null;

		if (!empty($vars))
		{
			$cachedVars = $this->_ci_cached_vars;
			$this->_ci_cached_vars = array();

			$ret = $this->view($view, $vars, $return);

			$this->_ci_cached_vars = $cachedVars;
		}
		else
		{
			$ret = $this->view($view, $vars, $return);
		}

		return $ret;
	}

	public function database($params = '', $return = FALSE, $query_builder = NULL)
	{
		parent::database($params, $return, $query_builder);

		$CI =& get_instance();

		if (!$CI->db->initialize())
		{
			$error = $CI->db->error();
			if ($error["code"] == 1049)
			{
				// $error["message"];
				header('Content-type: application/json');
				echo json_encode(array("error" => "Invalid Request !"));
				exit();
			}
		}

		$res = $CI->db->query('SHOW VARIABLES WHERE variable_name="wait_timeout";')->row_array();
		if (empty($res) || empty($res["Value"]) || $res["Value"] < 3600)
		{
			$CI->db->query("SET SESSION wait_timeout = 3600;");
		}

		$CI->db->query("SET SESSION sql_mode = '';");
		$CI->db->query('SET SESSION group_concat_max_len=1048576'); // increase group concat text length

		return $this;
	}

	public function unload_model($model)
    {
        if ($this->is_model_loaded($model))
        {
            $this->_ci_models = array_diff($this->_ci_models, array($model));
        }
    }

	public function planlib($file="")
	{
		$licenseData = get_user_data("licenseData");

		$plan = $licenseData["planTxt"];
		$plan = ucfirst(strtolower($plan));
		$file = ucfirst(strtolower($file));

		$libFile = $plan . $file;
		$phpFile = APPPATH . "libraries/plans/" . strtolower($file) . "/" . $libFile . ".php";

		if (is_file($phpFile))
		{
			if (!class_exists($libFile, FALSE))
			{
				$this->file($phpFile);
				$this->_ci_classes[$libFile] = new $libFile();
			}

			return $this->_ci_classes[$libFile];
		}

		return false;
	}
/*
	$CI =& get_instance();
	if (isset($CI->$name))
	{
		throw new RuntimeException('The model name you are loading is the name of a resource that is already being used: '.$name);
	}
*/

	public function classes($cls="")
	{
		if (!class_exists("ClsBase"))
		{
			require_once(APPPATH . "classes/ClsBase.php");
		}

		$cls = strtolower($cls);
		$clsName = ucfirst($cls);

		$className = $clsName . "Factory";

		$clsFile = APPPATH . "classes/" . $cls . "/" . $className . ".php";

		if (file_exists($clsFile))
		{
			require_once($clsFile);

			$className = $clsName . "\\" . $className;

			return $className::getLib();
		}

		return null;
	}
/*
	public function view($view, $vars = array(), $return = FALSE, $path="")
	{
		$arr = array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return);
		if (!empty($path))
		{
			$arr["_ci_path"] = $path;
		}

		return $this->_ci_load($arr);
	}
*/

	public function handler($cls="")
	{
		$CI =& get_instance();

		$folder = '';
		if($pos = strpos($cls, '_')) 
		{
			$folder = strtolower(substr($cls, 0, $pos)).DS;
		}

		$cls = strtolower($cls);
		$clsName = ucfirst($cls);
		$handlerFile = APPPATH.'classes'.DS.'handlers'.DS.$folder. "/" . $clsName . ".php";

		if (is_file($handlerFile))
		{
			if (!class_exists($clsName, FALSE))
			{
				$this->file($handlerFile);
				$this->_ci_classes[$clsName] = new $clsName();
			}
			$CI->$clsName = new $clsName();
			return;
		}

		return null;
	}
}
