<?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){
				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;
		}
	}
	
}