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

class Mo_role_module extends CI_Model {

	public $table = 't_role';
	
    function fnGetData()
    {
        $this->db->order_by('f_role_id', 'DESC');
        return $this->db->get($this->table)->result();		
	}

	function getDataId($id)
	{
		$sql = $this->db->get_where($this->table,array('f_role_id'=> $id))->row_array();
		return $sql;
	}

	function saveData()
	{
		$data = array(
				'f_role_id' => $this->input->post('f_role_id'),
				'f_role_code' => $this->input->post('f_role_code'),
				'f_role_name' => $this->input->post('f_role_name'),
				'f_role_desc' => $this->input->post('f_role_desc'),
				'f_role_active' => $this->input->post('f_role_active'),
		);
		$sql = $this->db->insert($this->table,$data);
		return $sql;
	}

	function updateData($id)
	{
		$data = array(
				'f_role_id' => $this->input->post('f_role_id'),
				'f_role_code' => $this->input->post('f_role_code'),
				'f_role_name' => $this->input->post('f_role_name'),
				'f_role_desc' => $this->input->post('f_role_desc'),
				'f_role_active' => $this->input->post('f_role_active'),
		);

		$this->db->where('f_role_id',$id);
		$sql = $this->db->update($this->table,$data);
		return $sql;		
	}

	function deleteData($id)
	{
		$this->db->where('f_role_id',$id);
		$sql = $this->db->delete($this->table);
		return $sql;		
	}
	
	function comboData()
	{
		$sql = $this->db->get('t_role');
		if($sql)
		{
			foreach($sql->result() as $r):
				$list[$r->f_role_id] = $r->f_role_id;
			endforeach;
		}
		return $list;
	}

	function remoteComboData()
	{
		if($this->input->get('term')) $this->db->like('f_role_code',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_role_id',$this->input->get('id')); // saat pencarian update
		
		$this->db->limit('10');
		$result = $this->db->get($this->table)->result();
		$DataJson = array();
		foreach($result as $row):
			$data['id']	= $row->f_role_id; // value
			$data['text']	= $row->f_role_code; // change display 
			array_push($DataJson,$data);
		endforeach;
		return $DataJson;
	}

	function fnroleModuleData($pRoleId) {
		$query = "select 
				    a.f_module_id,
				    a.f_module_class,
				    a.f_module_name,
				    a.f_module_icon,
					a.f_module_urut,
					a.f_module_desc,
					a.f_module_urut as p
				from
				    t_module a
				where a.f_module_parent = 0
				union
				select 
				    b.f_module_id,
				    b.f_module_class,
				    b.f_module_name,
				    b.f_module_icon,
					b.f_module_urut,
					b.f_module_desc,
					concat(	a.f_module_urut,'/0',b.f_module_urut) as p
				from
				    t_module a
				JOIN t_module b ON b.f_module_parent = a.f_module_id
				where a.f_module_parent = 0
				union
				select 
				    c.f_module_id,
				    c.f_module_class,
				    c.f_module_name,
				    c.f_module_icon,
					c.f_module_urut,
					c.f_module_desc,
					concat(	a.f_module_urut,'/0',b.f_module_urut,'/0',c.f_module_urut) as p
				from
				    t_module a
				JOIN t_module b ON b.f_module_parent = a.f_module_id
				JOIN t_module c ON c.f_module_parent = b.f_module_id
				where a.f_module_parent = 0
				order by p asc";

//		$this->db->from("t_module");
//		$this->db->order_by('f_module_id','Asc');
        
//		$vResult = $this->db->get()->result();
		$vResult = $this->db->query($query)->result();
		$vArrayTemp = array();
		$vItems = array();
        $no=$vOffset+1;        
		foreach($vResult as $vRow):
			$vArrayTemp['f_no'] = $no;		
			$vArrayTemp['f_mod_id'] = $vRow->f_module_id;		
			$vArrayTemp['f_mod_class'] = $vRow->f_module_class;		
			$vArrayTemp['f_mod_name'] = $vRow->f_module_name;		
			$vArrayTemp['f_mod_desc'] = $vRow->f_module_desc;		

            $this->db->select("f_module_id,f_add_button_status,f_edit_button_status,f_delete_button_status,f_export_button_status,f_import_button_status,f_valid_button_status");        
            $this->db->where('f_module_id',$vRow->f_module_id);
            $this->db->where('f_role_id',$pRoleId);        
            $vResultCek = $this->db->get('t_role_module');
            if($vResultCek->num_rows() > 0 ){
				$row = $vResultCek->row();
				$chk = 'checked';
				$vArrayTemp['view']		= $row->f_module_id;
				$vArrayTemp['add']		= $row->f_add_button_status;
				$vArrayTemp['edit']		= $row->f_edit_button_status;
				$vArrayTemp['delete']	= $row->f_delete_button_status;
				$vArrayTemp['export']	= $row->f_export_button_status;
				$vArrayTemp['import']	= $row->f_import_button_status;
				$vArrayTemp['valid']	= $row->f_valid_button_status;
                
            }
            else{
	            $chk = 'unchecked';
				$vArrayTemp['view']		= 0;
				$vArrayTemp['add']		= 0;
				$vArrayTemp['edit']		= 0;
				$vArrayTemp['delete']	= 0;
				$vArrayTemp['export']	= 0;
				$vArrayTemp['import']	= 0;
				$vArrayTemp['valid']	= 0;
                
            }
        
			$vArrayTemp['f_chk'] = $chk;		

        $no++;           
		array_push($vItems,$vArrayTemp);
		endforeach;
		return $vItems;
	}

	function fnroleModuleCreate($pChoosenRoleId,$pModules) {
		$this->db->where('f_role_id',$pChoosenRoleId)->delete('t_role_module');
		$vResult = $this->db->insert_batch('t_role_module',$pModules);
		if($vResult) {
			echo json_encode(array('success'=>true,'hasil'=>'ok'));
		} else {
			echo json_encode(array('hasil'=>'Some errors occured.'));
		}
	}

}

/* End of file Mo_role_module.php */
/* Location: ./application/modules/Role_module/models/Mo_role_module.php */	

	


