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

class Authorization {

	var $CI = null;

	function __construct()
	{
		$this->CI =& get_instance();
		$this->CI->load->database();
	}
	
	function do_login($login = NULL)
	{
		$result = array();

		// A few safety checks
		// Our array has to be set
		if(!isset($login))
		$result['status'] 	= 0;
		$result['error'] 	= '';
	
		//Our array has to have 2 values
		//No more, no less!
		if(count($login) != 2)
		{
			$result['status'] = 0;
			$result['error'] = 'Incorrect parameter';
		}
		
		$username = $login['username'];
		$password = $login['password'];
		$enc_pass = md5($password);
		$this->CI->db->from('msOperator');
		$this->CI->db->where('userName', $username);		
		$this->CI->db->where('passWord',$enc_pass);
		$date = date("Y").date("m").date("d");
		// $this->CI->db->where('ExpiryDate >=', $date);
		$query = $this->CI->db->get();
		// echo $this->CI->db->last_query();
		// print_r($query->result());
		if ($query->num_rows() == 1)
		{
			$newdata = $query->row_array();
			$this->CI->db->from('msOperatorType');
			$this->CI->db->where('idOperatorType', $newdata['fidOperatorType']);
			$getType = $this->CI->db->get();
			$getType = $getType->row_array();
			$newdata['isLogin'] = true;
			$newdata['oprDescription'] = $getType['description'];

			$this->CI->db->from('appConfig');
			$this->CI->db->where('fidOperator', $newdata['idOperator']);
			$getConfig = $this->CI->db->get();
			// echo $this->CI->db->last_query();
			
			$newdata['boxedlayout']		= get_myconf('boxedlayout');
			$newdata['collapse_menu']	= get_myconf('collapse_menu');
			$newdata['fixedfooter']		= get_myconf('fixedfooter');
			$newdata['fixednavbar']		= get_myconf('fixednavbar');
			$newdata['fixednavbar2']	= get_myconf('fixednavbar2');
			$newdata['fixedsidebar']	= get_myconf('fixedsidebar');
			$newdata['skinColor']		= get_myconf('skinColor');

			foreach($getConfig->result_array() as $key=>$val)
			{
				$newdata[$val['key']] = $val['value'];
			}
			// $this->CI->db->from('businessUnit');
			// $this->CI->db->where('businessUnitCode', $newdata['businessUnitCode']);
			// $getUnitBisnis = $this->CI->db->get();
			// $ub = $getUnitBisnis->row_array();
			// $newdata['businessUnitname'] = $ub['name']; 
			
			//print_r($this->CI->session->all_userdata());
			//exit;
			$result['default_menu_id'] = 0;
			$result['default_menu_url'] = '';
			$newdata['default_menu_id'] = 0;
			$newdata['default_menu_url'] = 'Main';
			$this->CI->db->from('Menu');
			$this->CI->db->where('idMenu', $newdata['fidMenu']);
			$getDefaultMenu = $this->CI->db->get();
			if($getDefaultMenu->num_rows() > 0)
			{
				$this->CI->db->from('msOperatorAccess');
				$this->CI->db->where('fidOperator', $newdata['idOperator']);
				$this->CI->db->where('fidMenu', $newdata['fidMenu']);
				$getUserAccess = $this->CI->db->get();
				if($getUserAccess->num_rows() > 0)
				{
					$row = $getDefaultMenu->row_array();
						
					$result['default_menu_id'] = $newdata['fidMenu'];
					$result['default_menu_url'] = $row['url'];
					$newdata['default_menu_id'] = $newdata['fidMenu'];
					$newdata['default_menu_url'] = $row['url'];
				}
			}
			
			$this->CI->session->set_userdata($newdata);	 
			$result['status'] = 1;
		}
		else 
		{
			// No existing user.
			$result['status'] = 0;
			$result['error'] = 'Incorrect Username or Password, please check again';
		}
		return $result;
	}
 
	 /**
         *
         * This function restricts users from certain pages.
         * use restrict(TRUE) if a user can't access a page when logged in
         *
         * @access	public
         * @param	boolean	wether the page is viewable when logged in
         * @return	void
         */	
    	function restrict($logged_out = FALSE)
    	{
		// If the user is logged in and he's trying to access a page
		// he's not allowed to see when logged in,
		// redirect him to the index!
		if ($logged_out && is_logged_in())
		{
		      redirect('');
			  exit;
			  //echo $this->CI->fungsi->warning('Maaf, sepertinya Anda sudah login...',site_url());
		      //die();
		}
		
		// If the user isn' logged in and he's trying to access a page
		// he's not allowed to see when logged out,
		// redirect him to the login page!
		if ( ! $logged_out && !is_logged_in()) 
		{
		      echo $this->CI->fungsi->warning('Anda diharuskan untuk Login bila ingin mengakses halaman ini.',site_url());
		      die();
		}
	}
	
	function logout() 
	{
		$this->CI->session->sess_destroy();	
		return TRUE;
	}
}
// End of library class
// Location: system/application/libraries/Auth.php
