<?php
/**
 * Accident Management
 *
 * @Description  This class is used to interact with the manage_permissions table using Codeignitor db core class. All the Data operations related to manage_permissions are performed here.
 *
 * @package		Accident Management
 * @subpackage  Model
 * @author		Harshada Kakade
 * @copyright	Copyright (c) 2014 - 2015
 * @since		Version 1.0
 */
 
// ------------------------------------------------------------------------

/**
 *
 * This is manage_permissions Model
 *
 * @author		Harshada Kakade
 * @package		Accident Management 
 * @subpackage	Model
 */
class Manage_permissions extends CI_Model {

    function __construct() {
        parent::__construct();
    }
		
	/**
	* auth
	*
	* fetches corresponding records
	* 
	* @author	Harshada Kakade
	* @access	public
	* @param	string $whereClause
	* @return	array
	*/
	function auth($whereClause = NULL) {
	   if(!is_null($whereClause))
		{
			$this->db->where($whereClause);
			$query = $this->db->get('manage_permissions');
            return $query->result_array();
		}
		else
			return false;
					
		
		
    }
	
	/**
	* checkIfExists
	*
	* checks if record already exists
	* 
	* @author	Harshada Kakade
	* @access	public
	* @param	string $file, string $method
	* @return	array
	*/
	function checkIfExists($file = NULL, $method = NULL) {
	
		if(!is_null($file) && !is_null($method))
		{
			$this->db->where(array('module_name' => $file, 'method_name' => $method) );
			$query = $this->db->get('manage_permissions');
			return $query->result_array();
		}
		else
			return false;
		
    }
	
	/**
	* add
	*
	* @author	Harshada Kakade
	* @access	public
	* @params	$arrdata
	* @return	boolean
	**/

	function add($arrdata)
	{
		if($this->db->insert_batch('manage_permissions', $arrdata)){
			return TRUE;
		}else{
			return FALSE;
		}	
	}	
	
	/**
	* get_modules
	*
	* This fetches all modules according to whereclause
	* 
	* @author	Harshada Kakade
	* @access	public
	* @param	string $whereClause, int $limit, int $offset
	* @return	array
	*/
	function get_modules($whereClause = NULL, $limit = 0, $offset = 0) {
	
		$this->db->select('manage_permissions.module_name, manage_permissions.user_level_id,
						   GROUP_CONCAT(manage_permissions.method_name) methods, GROUP_CONCAT(manage_permissions.permission) permissions,  						   GROUP_CONCAT(manage_permissions.id) permission_id');
						   
		if(!is_null($whereClause) && $whereClause != '')
            $whereClause .= " AND method_name NOT IN ('index', ' 	__construct')  AND method_name NOT LIKE 'fn%'";
			$this->db->where($whereClause);
		
		if($limit != 0)
			$this->db->limit($limit, $offset);
		
		$this->db->group_by('module_name, user_level_id');
		$query = $this->db->get('manage_permissions');
		//echo $this->db->last_query();
		return $query->result_array();
		
    }
	
	/**
	 * approve
	 *
	 * approves permission
	 * @author	Harshada Kakade
	 * @access	public
	 * @param array $arrData,Integer $id
	 * @return	array
	 */

	function approve($arrData, $id = 0, $arrId = array()){
			
		if($id > 0)
		{
			$this->db->where('id',$id);	
			$status = $this->db->update('manage_permissions', $arrData);
			if($status)
				return true;
		}
		else if(!empty($arrId))	
		{
			$this->db->where_in('id',$arrId);
			$status = $this->db->update('manage_permissions', $arrData);
			if($status)
				return true;
		}
		
		return false;
	}
	
	/**
	* get_permissions
	*
	* This fetches permission values according to whereclause
	* 
	* @author	Harshada Kakade
	* @access	public
	* @param	string $whereClause, int $limit, int $offset
	* @return	array
	*/

	function get_permissions($whereClause = NULL, $limit = 0, $offset = 0) {
		//print_r($arr_data);
		if(!is_null($whereClause))
			$this->db->where($whereClause);
		
		if($limit != 0)
			$this->db->limit($limit, $offset);
		$query = $this->db->get('manage_permissions');
		echo $this->db->last_query();
		return $query->result_array();
		
    }
}

/** end of relation_model * */
?>