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

class Useraccess extends CI_Model{
	
	public function useraccessA($id){
		$query = $this->db->get_where('user_module_access', array('userID'=>$id));		
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function useraccess_id($id){
		$this->db->select('user_module_access.*');
		$this->db->select('module.description');
		$this->db->where('userID', $id);
		$this->db->from('user_module_access');
		$this->db->join('module', 'module.ID = user_module_access.moduleID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	public function useraccess_company_id(){
		$this->db->select('user_module_access.*');
		$this->db->select('module.description');
		$this->db->where('userID', $this->input->post('fldid'));
		$this->db->where('companyID', $this->input->post('companyID'));
		$this->db->from('user_module_access');
		$this->db->join('module', 'module.ID = user_module_access.moduleID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function useraccess_update($id, $data){
		$this->db->where('ID', $id);
		$query = $this->db->update('user_module_access', $data); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	function useraccess_update2(){
		$this->db->select('a.*');
		$this->db->where('a.companyID', $this->input->post('companyID'));
		$this->db->where('a.userID', $this->input->post('fldid'));
		$this->db->where('a.moduleID', $this->input->post('module'));
		$this->db->from('user_module_access a');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$data = $query->result();
			$dataupdate = array (
				'transaction' => $this->input->post('transaction'),
				'reports' => $this->input->post('reports'),
				'setup' => $this->input->post('setup'),
				'updatedBy' => element('role', $this->session->userdata('logged_in'))
			);
			$this->db->flush_cache();
			$this->db->where('ID', $data[0]->ID);
			$query1 = $this->db->update('user_module_access', $dataupdate); 
			if($query1 === TRUE){
				return TRUE;
			} else {
				return FALSE;
			}
		}else{
			$dataupdate = array (
				'companyID' => $this->input->post('companyID'),
				'userID' => $this->input->post('fldid'),
				'moduleID' => $this->input->post('module'),
				'transaction' => $this->input->post('transaction'),
				'reports' => $this->input->post('reports'),
				'setup' => $this->input->post('setup'),
				'createdBy' => element('role', $this->session->userdata('logged_in'))
			);
			$query1 = $this->db->insert('user_module_access', $dataupdate); 
			if($query1 === TRUE){
				$module = $this->input->post('module');
				$this->db->select('*');
				$this->db->where('ID', $this->input->post('module'));
				$this->db->from('module');
				$query = $this->db->get();
				$menuCode = $query->result();
				$this->db->flush_cache();
				if($this->input->post('transaction')){
					if($module == 9 OR $module == 10){
						$code = $menuCode[0]->moduleName;
					}else{
						$code = $menuCode[0]->moduleName . 'TRANS';
					}
					$this->db->select('a.code as acode');
					$this->db->select('b.*');
					$this->db->select('c.accessRight');
					$this->db->where('a.code', $code);
					$this->db->from('_sys_menu a');
					$this->db->join('_sys_menu b', 'b.parents = a.fldid', 'left outer');
					$this->db->join('user_module_access_details c', 'c.menuCode = b.code AND c.userID = ' . $this->input->post('fldid') . ' AND c.companyID = ' . $this->input->post('companyID'), 'left outer');
					$query2 = $this->db->get();
					$accessDetail = $query2->result();
					foreach($accessDetail as $row){
						if($row->accessRight){
						}else{
							$dataupdate = array (
								'companyID' => $this->input->post('companyID'),
								'userID' => $this->input->post('fldid'),
								'menuCode' => $row->code,
								'accessRight' => 1,
								'createdBy' => element('role', $this->session->userdata('logged_in'))
							);
							$query3 = $this->db->insert('user_module_access_details', $dataupdate); 
						}
					}
				}
				if($this->input->post('reports')){
					if($module == 9 OR $module == 10){
						$code = $menuCode[0]->moduleName;
					}else{
						$code = $menuCode[0]->moduleName . 'RPT';
					}
					$this->db->select('a.code as acode');
					$this->db->select('b.*');
					$this->db->select('c.accessRight');
					$this->db->where('a.code', $code);
					$this->db->from('_sys_menu a');
					$this->db->join('_sys_menu b', 'b.parents = a.fldid', 'left outer');
					$this->db->join('user_module_access_details c', 'c.menuCode = b.code AND c.userID = ' . $this->input->post('fldid') . ' AND c.companyID = ' . $this->input->post('companyID'), 'left outer');
					$query2 = $this->db->get();
					$accessDetail = $query2->result();
					foreach($accessDetail as $row){
						if($row->accessRight){
						}else{
							$dataupdate = array (
								'companyID' => $this->input->post('companyID'),
								'userID' => $this->input->post('fldid'),
								'menuCode' => $row->code,
								'accessRight' => 1,
								'createdBy' => element('role', $this->session->userdata('logged_in'))
							);
							$query3 = $this->db->insert('user_module_access_details', $dataupdate); 
						}
					}
				}
				if($this->input->post('setup')){
					if($module == 9 OR $module == 10){
						$code = $menuCode[0]->moduleName;
					}else{
						$code = $menuCode[0]->moduleName . 'SETUP';
					}
					$this->db->select('a.code as acode');
					$this->db->select('b.*');
					$this->db->select('c.accessRight');
					$this->db->where('a.code', $code);
					$this->db->from('_sys_menu a');
					$this->db->join('_sys_menu b', 'b.parents = a.fldid', 'left outer');
					$this->db->join('user_module_access_details c', 'c.menuCode = b.code AND c.userID = ' . $this->input->post('fldid') . ' AND c.companyID = ' . $this->input->post('companyID'), 'left outer');
					$query2 = $this->db->get();
					$accessDetail = $query2->result();
					foreach($accessDetail as $row){
						if($row->accessRight){
						}else{
							$dataupdate = array (
								'companyID' => $this->input->post('companyID'),
								'userID' => $this->input->post('fldid'),
								'menuCode' => $row->code,
								'accessRight' => 1,
								'createdBy' => element('role', $this->session->userdata('logged_in'))
							);
							$query3 = $this->db->insert('user_module_access_details', $dataupdate); 
						}
					}
				}
				return TRUE;
			} else {
				return FALSE;
			}
		}
	}

/*	public function menu_access_all(){
		$this->lang->load("menu",$this->session->userdata('language'));
		$this->db->select('*');
		$this->db->where('ID', $this->input->post('module'));
		$this->db->from('module');
		$query = $this->db->get();
		$menuCode = $query->result();
		$this->db->flush_cache();
		switch ($this->input->post('detailsAcccess')){
			case '1':
				$code = $menuCode[0]->moduleName . 'TRANS';
			break;
			case '2':
				$code = $menuCode[0]->moduleName . 'RPT';
			break;
			case '3':
				$code = $menuCode[0]->moduleName . 'SETUP';
			break;
		}
		$this->db->select('a.code as acode');
		$this->db->select('b.*');
		$this->db->select('c.accessRight');
		$this->db->where('a.code', $code);
		$this->db->from('_sys_menu a');
		$this->db->join('_sys_menu b', 'b.parents = a.fldid', 'left outer');
		$this->db->join('user_module_access_details c', 'c.menuCode = b.code AND c.userID = ' . $this->input->post('fldid') . ' AND c.companyID = ' . $this->input->post('companyID'), 'left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$test = $query->result();
			$resultDetails = array();
			foreach($test as $row){
				$rowDet = array();
				$rowDet['fldid'] = $row->fldid;
				$rowDet['code'] = $row->code;
				$rowDet['name'] = $this->lang->line($row->name);
				$rowDet['accessRight'] = $row->accessRight;
				$rowDet['type'] = $this->input->post('detailsAcccess');
				$resultDetails[] = $rowDet;
			}
			return $resultDetails;
		}else{
			return FALSE;
		}
	}
*/
	function useraccess_update3(){
		$this->db->select('a.*');
		$this->db->where('a.companyID', $this->input->post('companyID'));
		$this->db->where('a.userID', $this->input->post('fldid'));
		$this->db->where('a.moduleID', $this->input->post('module'));
		$this->db->from('user_module_access a');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$data = $query->result();
			$dataupdate = array (
				$this->input->post('col') => 1,
				'updatedBy' => element('role', $this->session->userdata('logged_in'))
			);
			$this->db->flush_cache();
			$this->db->where('ID', $data[0]->ID);
			$query1 = $this->db->update('user_module_access', $dataupdate); 
			if($query1 === TRUE){
				return TRUE;
			} else {
				return FALSE;
			}
		}else{
			$dataupdate = array (
				'companyID' => $this->input->post('companyID'),
				'userID' => $this->input->post('fldid'),
				'moduleID' => $this->input->post('module'),
				$this->input->post('col') => 1,
				'createdBy' => element('role', $this->session->userdata('logged_in'))
			);
			$query1 = $this->db->insert('user_module_access', $dataupdate); 
			if($query1 === TRUE){
				return TRUE;
			} else {
				return FALSE;
			}
		}
	}
	
	public function useraccess_id_module($module){
		$where = '(transaction = "1" OR reports = "1" OR setup = "1")';
		$id = element('role', $this->session->userdata('logged_in'));
		$this->db->where('userID', $id);
		$this->db->where('moduleID', $module);
		$this->db->where($where);
		$query = $this->db->get('user_module_access');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function useraccess_id_module2($module, $fields){
		$id = element('role', $this->session->userdata('logged_in'));
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('userID', $id);
		$this->db->where($fields, '1');
		$this->db->where('moduleID', $module);
		$query = $this->db->get('user_module_access');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
}