<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
	//Global Variables---
	var $company_name = "3A9A0A7AEC6A9A0A7AEC7AGA2AAAA99ABA6AECE9A99AEC2AA9AA";	//Encryped Name
	var $currency = "RM";
	var $record_login = TRUE;	//Records every login
	var $text_ajax_loader = "<center><br><i class='fa fa-spinner fa-pulse fa-4x'></i><br><strong>Loading...</strong></center>";
	var $text_ajax_fail = "<center><br><i class='fa fa-times-circle fa-4x'></i><br><strong>Error<br>Opps! Something Went Wrong.<br>Please refresh this page or contact the System Admin.</strong><br><br></center>";
	var $domain_id = array("1");
	var $record_footprint = FALSE;
	var $acl = TRUE;
	var $audit_trail_field_bypass = array("CREATED_BY", "DATE_CREATED", "MODIFIED_BY", "DATE_MODIFIED");
	var $user = "";
	var $pccode = "";
	var $logo = "";
	var $profile = "";
	var $setting = "";
	var $per_page_array = array(10, 50, 100, 500, 1000, 5000, 10000);
	//-------------------
	
	public function __construct(){
		parent::__construct();
		
		ini_set("memory_limit", -1);
//		ini_set("max_execution_time", 0);
		
//		$this->fm->profiler();
		
		$this->record_prev_page();
	
		if($this->acl){
			$redirect = $this->access_control();
			if($redirect != "") redirect($redirect);
		}
	}
	
	public function access_control(){
		$home_uri = array("main");
		
		$redirect = "";
		
		if($this->db->table_exists($this->menu_model->table)){
			$uri_array = isset($_SERVER['REDIRECT_QUERY_STRING'])? explode("/", $_SERVER['REDIRECT_QUERY_STRING']):array();
			foreach($uri_array as $x=>$v){
				if($v == "") unset($uri_array[$x]);
			}
			$uri_array = array_values($uri_array);
			if(count($uri_array) == 0) $uri_array = $home_uri;
			
			$condition = "";
			$url = "";
			foreach($uri_array as $v){
				$url = $this->fm->append($url, $v, "/");
				$condition = $this->fm->append($condition, "<T>.URL = '".$url."'", " OR ");
			}
			if($condition != "") $condition = "(".$condition.")";
			$menu_array = count($uri_array) > 0? $this->menu_model->get_data(array(
				'condition' => $condition,
				'additional_join' => "LEFT JOIN WEB_ACCESS ON WEB_ACCESS.MENU = <T>.PKKEY AND WEB_ACCESS.USER_GROUP = '".$this->session->userdata('group_id')."'",
				'additional_select' => "WEB_ACCESS.PKKEY AS ACCESS_PKKEY",
				'sort' => "<T>.URL DESC",
				'' => "",
			)):array();
			
			if(count($menu_array) > 0){
				$menu = $menu_array[0];
				if($menu->ACCESS_PKKEY == "" && $menu->EVERYONE_ACCESS != "1"){
					if($this->session->userdata('user_id') != ""){
						$redirect = base_url();
					}
					else{
						$redirect = base_url("logout");
					}
				}
			}
		}
		
		return $redirect;
	}
	
	public function record_prev_page(){
		$uri = $this->uri->segment(1);
		if(! in_array($uri, array("", "login", "logout"))){
			$prev_page = base_url(uri_string()."?".$_SERVER['QUERY_STRING']);
			$this->session->set_flashdata('prev_page', $prev_page);
		}
		else{
			$this->session->keep_flashdata('prev_page');
		}
	}
}