<?php
class M_menu extends CI_Model {
    public $table               = "tbl_menu";
	
	// Commented by Prawira Tgl 9 September 2015
    /* public $view_authority	= array(1,7,8,9,10,11,22,23,24,25,26,27,28,29,30,31,41,42,43,44,45,46,47,48,49,50,56,57,58,59,60,62);
    public $add_authority	= array(2,7,12,13,14,15,22,23,24,25,32,33,34,35,36,37,41,42,43,44,45,46,51,52,53,54,56,57,58,59,61,62);
    public $edit_authority	= array(3,8,12,16,17,18,22,26,27,28,32,33,34,38,39,41,42,43,47,48,49,51,52,53,55,56,57,58,60,61,62);
    public $delete_authority    = array(4,9,13,16,17,18,26,27,28,32,33,34,38,39,41,42,43,47,48,50,51,52,54,55,56,57,59,60,61,62); */
	
	// function untuk memberikan semua menu untuk di display pada main page untuk level menu.
    function get_all_menu($menu_level=1)	{
        $this->db->select('tbl_menu.MenuID, tbl_menu.MenuLabel, tbl_menu.Icon, tbl_menu.MenuLink, tbl_menu.ActionType, tbl_menu.ParentID');
        $this->db->where('tbl_menu.MenuLevel',$menu_level);
		$this->db->where('tbl_menu.MenuLevel',$menu_level);
		$this->db->where('tbl_menu.ParentID !=', 101);
        $this->db->order_by('tbl_menu.ParentID, tbl_menu.MenuOrder','ASC');
        $query = $this->db->get($this->table);
		if($query->num_rows>0) {
			return $query->result_array();
		}
		else{
			return false;
		}
    }
	
	// function untuk memberikan submenu untuk di display pada main page berdasarkan menu yang di pilih user pada interface
    function get_sub_menu($parent_id) {
        $this->db->select('MenuID,MenuLabel,Icon,MenuLink,ActionType,ParentID');
        $this->db->where('ParentID',$parent_id);
        $this->db->where("ActionStatus",1);
		$this->db->where('ParentID !=', 101);
		$this->db->where_not_in('MenuID ', array(317,362, 363));
        $this->db->order_by('ParentID,MenuOrder','ASC');
        $query = $this->db->get($this->table);
		
        if($query->num_rows>0) {
            return $query->result_array();
        }
        else{
            return false;
        }
    }

	// function untuk mmendapatkan action dari user ketika memilih menu maupun sub menu.
    function get_action($ActionType,array $Parameter){
        $prm=implode("','",$Parameter);
	
        switch($ActionType){
            case 'Tab' : echo "tab_add('".$prm."')"; break;
            case 'Modal' : echo "modal_open('".$prm."')"; break;
            //default : echo "open_url('".$Parameter[1]."')"; break;
            default : echo "link_to('".$Parameter[1]."')"; break;
        }
    }
	
	// function untuk memberikan menu berdasarkan menu id yang dipilih.
    function get_by_id($id) {
        $query = $this->db->get_where($this->table, array('MenuId' => $id));
        
        return $query->result();
    }
	
	// function untuk memberikan menu link berdasarkan menu id yang terdapat pada masing2 page.
    function get_menulink($id) {
        $this->db->select('MenuLink');
        $this->db->from($this->table);
        $this->db->where('MenuID', $id); 
        $query = $this->db->get();    

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $MenuLink = $row->MenuLink;
            }    

            return $MenuLink;
        }
        else {
            return FALSE;
        }	
    }	
	
	// function untuk memberikan menu label berdasarkan menu id yang terdapat pada masing-masing page.
    function get_menulabel($id) {
        $this->db->select('MenuLabel');
        $this->db->from($this->table);
        $this->db->where('MenuID', $id); 
        $query = $this->db->get();   

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $MenuLabel = $row->MenuLabel;
            }
        
            return $MenuLabel;
        }
        else {
            return FALSE;
        }	
    }

    function get_menuAuthority_byID($group_id,$menu_id){
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID',$group_id);
        $this->db->where('MenuID',$menu_id);
        $this->db->where('AddPrivillage',1);

        $query = $this->db->get();
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }

    }
	
	// untuk memberikan list menu yang dapat diakses berdasarkan group user yang telah di setting di privilage.
    function get_menu_by_group($group, $id_menu) {
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $group);
        if($id_menu!=null){
		  $this->db->where('MenuID', $id_menu);
        }
		$this->db->where('ViewPrivillage', 1);
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $menu_id[] = $row->MenuID;
            }    
            
            return $menu_id;
        }
        else {
            return FALSE;
        }  	
    }
	
	// untuk memberikan list menu untuk add yang dapat diakses berdasarkan group user yang telah di setting di privilage.
	function get_menu_by_group_add($group, $id_menu) {
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $group);
		$this->db->where('MenuID', $id_menu);
		$this->db->where('AddPrivillage', 1);
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $menu_id[] = $row->MenuID;
            }    
            
            return $menu_id;
        }
        else {
            return FALSE;
        }  	
    }
	
	// untuk memberikan list menu untuk edit function yang dapat diakses berdasarkan group user yang telah di setting di privilage
	 function get_menu_by_group_edit($group, $id_menu) {
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $group);
		$this->db->where('MenuID', $id_menu);
		$this->db->where('EditPrivillage', 1);
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $menu_id[] = $row->MenuID;
            }    
            
            return $menu_id;
        }
        else {
            return FALSE;
        }  	
    } 
	
	// bole di audit. kemungkinan besar function ini sudah tidak dipakai lagi.
    /* function get_menu_by_group1($group) {
        $this->db->select('MenuID, MenuLabel');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $group); 
        $query = $this->db->get();    
    
        return $query->result();
    } */
	
	// bole di audit. kemungkinan besar function ini sudah tidak dipakai lagi.
    /* function get_menu_by_group_and_authority($GroupID, $AuthorityType){
        if($AuthorityType=='view')
            $authority = $this->view_authority;
        if($AuthorityType=='add')
            $authority = $this->add_authority;
        if($AuthorityType=='edit')
            $authority = $this->edit_authority;
        if($AuthorityType=='delete')
            $authority = $this->delete_authority;
		
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $GroupID);
        $this->db->where_in('AuthorityCode', $authority);
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $menu_id[] = $row->MenuID;
            }    

            return $menu_id;
        }
        else {
            return FALSE;
        }
    } */
	
	// function untuk mendapatkan otoritas akses dari user group.
	function group_user_autority($menu_id, $group_id) {
        $this->db->select('MenuID,GroupID, ViewPrivillage, AddPrivillage, EditPrivillage, DeletePrivillage, SavePrivillage, PrintPrivillage, JournalPrivillage, ReversePrivillage, ExcelPrivillage');
		$this->db->from('tbl_groupauthority');
        $this->db->where('MenuID',$menu_id);
		$this->db->where('GroupID',$group_id);
        $query = $this->db->get();
		
        if($query->num_rows>0) {
            return $query->row();
        }
        else{
            return false;
        }
    }
	
	// function untuk mendapatkan menu yang perlu di approve
	// 121 = purchase, 115 = journal voucher, 242 = expense cashbon, 122 = item, 124 = inventories, 125 = fixed asset, 83 = JobCosting, Roll Over, COA, 116 = Cash & Bank, 118 = Sales
	function get_menu_by_menu_order($group_menu) {
        $this->db->select('tbl_menu.MenuID, tbl_menu.MenuLabel,  tbl_menu.MenuLink, tbl_menu.MenuName');
        $this->db->from('tbl_menu');
		$this->db->join('tbl_groupauthority', 'tbl_groupauthority.MenuID = tbl_menu.MenuID', 'INNER');
		$this->db->where('tbl_groupauthority.GroupID', $group_menu);
		$this->db->where('tbl_groupauthority.ViewPrivillage', 1);
		$this->db->where_in('tbl_menu.ParentID', array('121','115','242','122','124','125','83', '116', '118'));
		$this->db->order_by('GroupAuthorityID','ASC');
        $query = $this->db->get();    
    
        return $query->result();
    }
	
	//menu report only
	function get_menu_report($group_menu) {
        $this->db->select('tbl_menu.MenuID, tbl_menu.MenuLabel,  tbl_menu.MenuLink, tbl_menu.MenuName');
        $this->db->from('tbl_menu');
		$this->db->join('tbl_groupauthority', 'tbl_groupauthority.MenuID = tbl_menu.MenuID', 'INNER');
		$this->db->where('tbl_menu.ParentID', 101);
		$this->db->where('tbl_groupauthority.GroupID', $group_menu);
		$this->db->where('tbl_groupauthority.ViewPrivillage', 1);
		$this->db->order_by('MenuOrder','ASC');
        $query = $this->db->get();    
    
        return $query->result();
    }
	//end
	
	//menu sub report only
	function get_menu_sub_report($menu_id, $group_menu) {
        $this->db->select('tbl_menu.MenuID, tbl_menu.MenuLabel,  tbl_menu.MenuLink, tbl_menu.MenuName, tbl_menu.Icon');
        $this->db->from('tbl_menu');
		$this->db->join('tbl_groupauthority', 'tbl_groupauthority.MenuID = tbl_menu.MenuID', 'INNER');
		$this->db->where('tbl_menu.ParentID', $menu_id);
		$this->db->where('tbl_groupauthority.GroupID', $group_menu);
		$this->db->where('tbl_groupauthority.ViewPrivillage', 1);
		$this->db->order_by('MenuOrder','ASC');
        $query = $this->db->get();    
    
        return $query->result();
    }
	//end
}	
	