<?php
class Line_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->tbl = 'mst_line';
		$this->tbl2 = 'mst_perusahaan';
		$this->pk = 'id_line';
		$this->pk2 = 'KdPrshn';
    }

	function getPage($page,$rows,$key=''){		
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,id_line AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.".$this->pk2."=a.kd_cmpy
			  WHERE a.nm_line LIKE '%$key%'";
			  
		if ($this->tp_prshn != 'o'){
			  $q .= " AND a.kd_cmpy = '".$this->kd_cmpy."'";
		}	 
		$q .= " ORDER BY id_line";
		$rstot=$this->db->query($q)->result();
		//echo $q;
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	
	function add($data){
		$run = $this->db->insert($this->tbl, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($data['id_line'],"Tambah Data Line ".$data['id_line']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->tbl, $data, array($this->pk => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($id,"Ubah Data Production Line ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->tbl, array($this->pk => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($id,"Hapus Data Production Line ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
	
	function getHakAkses($nm_opr){
		$q = "SELECT * FROM sys_menu
			  ORDER BY no_urut";
		$rs = $this->db->query($q)->result();
		
		$temp= array();		
		foreach($rs as $r){
			$ltuh=$this->cekAkses($nm_opr,$r->menu_id);
			
			$qSub=" SELECT * FROM sys_menugroup 
					WHERE status=1 
					AND menu_id='".$r->menu_id."'";
			$rSub = $this->db->query($qSub)->result_array();
			
			$data=array(
				'nm_opr' 	=> $nm_opr,
				'fid_ms_module' 	=> $r->menu_id,
				'nm_menu' 	=> $r->menu,
				'lihat' 	=> ($ltuh->lihat)?$ltuh->lihat:0,
				'tambah' 	=> ($ltuh->tambah)?$ltuh->tambah:0,
				'ubah' 		=> ($ltuh->ubah)?$ltuh->ubah:0,
				'hapus'		=> ($ltuh->hapus)?$ltuh->hapus:0,
				'children'	=> (count($rSub)>0)?$this->getMenuGroups($r->menu_id,$nm_opr):''
			);
			array_push($temp,$data);
		}
		
		return $temp;
	}
	
	function getMenuGroups($id,$nm_opr){ 
		$q="SELECT * FROM sys_menugroup 
			WHERE status=1 AND menu_id='$id'
			ORDER BY no_urut";		
		$rs = $this->db->query($q)->result();
		
		$submenu=array();
		foreach ($rs as $r){
			$ltuh=$this->cekAkses($nm_opr,$r->menugroup_id);
			
			$qSub=" SELECT * FROM sys_submenu 
					WHERE status=1 
					AND menugroup_id='".$r->menugroup_id."'";
			$rSub = $this->db->query($qSub)->result_array();
			
			$submenu[]=array(
				'nm_opr' 	=> $nm_opr,
				'fid_ms_module' => $r->menugroup_id,
				'nm_menu' 	=> $r->title,
				'lihat' 	=> ($ltuh->lihat)?$ltuh->lihat:0,
				'tambah' 	=> ($ltuh->tambah)?$ltuh->tambah:0,
				'ubah' 		=> ($ltuh->ubah)?$ltuh->ubah:0,
				'hapus'		=> ($ltuh->hapus)?$ltuh->hapus:0,
				'children'	=> (count($rSub)>0)?$this->getChilds($r->menugroup_id,$nm_opr):''
			);
		}
		
        return $submenu;
    }
	
	function getChilds($id,$nm_opr){ 
		$q="SELECT * FROM sys_submenu WHERE status=1 AND menugroup_id='$id'";		
		$rs = $this->db->query($q)->result();
		
		$submenu=array();
		foreach ($rs as $r){
			$ltuh=$this->cekAkses($nm_opr,$r->submenu_id);
			
			$qSub="SELECT * FROM sys_subinmenu WHERE status=1 AND submenu_id='".$r->submenu_id."'";
			$rSub = $this->db->query($qSub)->result_array();
			
			$submenu[]=array(
				'nm_opr' 	=> $nm_opr,
				'fid_ms_module' => $r->submenu_id,
				'nm_menu' 	=> $r->submenu,
				'lihat' 	=> ($ltuh->lihat)?$ltuh->lihat:0,
				'tambah' 	=> ($ltuh->tambah)?$ltuh->tambah:0,
				'ubah' 		=> ($ltuh->ubah)?$ltuh->ubah:0,
				'hapus'		=> ($ltuh->hapus)?$ltuh->hapus:0,
				'children'	=> (count($rSub)>0)?$this->getSubChilds($r->submenu_id,$nm_opr):''
			);
		}
		
        return $submenu;
    }
	
	function getSubChilds($id,$nm_opr){ 
		$qSub="SELECT * FROM sys_subinmenu WHERE status=1 AND submenu_id='$id'";		
		$rs = $this->db->query($qSub)->result();
		
		$submenu=array();
		foreach ($rs as $r){
			$ltuh=$this->cekAkses($nm_opr,$r->subinmenu_id);
			
			$submenu[]=array(
				'nm_opr' 	=> $nm_opr,
				'fid_ms_module' => $r->subinmenu_id,
				'nm_menu' 	=> $r->subinmenu,
				'lihat' 	=> ($ltuh->lihat)?$ltuh->lihat:0,
				'tambah' 	=> ($ltuh->tambah)?$ltuh->tambah:0,
				'ubah' 		=> ($ltuh->ubah)?$ltuh->ubah:0,
				'hapus'		=> ($ltuh->hapus)?$ltuh->hapus:0
			);
		}
		
        return $submenu;
    }
	
	function cekChild($id){
		$q = "SELECT * FROM sys_submenu
			  WHERE  submenu_id='$id'
			  ORDER BY no_urut";
		$rs = $this->db->query($q)->result();
		if ($rs){ return true; } else {return false;}
	}
	
	function cekAkses($nm_opr,$id){
		$q = "SELECT * FROM ms_hak_akses 
			  WHERE nm_opr='$nm_opr' AND fid_ms_module = '$id'";
		$rs = $this->db->query($q)->result();
		if ($rs){ 
			return $rs[0]; 
		} 
	}
	
	function setHakAkses($data){		
		$nm_opr=explode("`", $data['nm_opr']);
		$fid_ms_module=explode("`", $data['fid_ms_module']);
		$lihat=explode("`", $data['lihat']);
		$tambah=explode("`", $data['tambah']);		
		$ubah=explode("`", $data['ubah']);	
		$hapus=explode("`", $data['hapus']);	
		
		try { 
			$sql[] = "START TRANSACTION";
			//TAMBAH HEADER
			$sql[] = "DELETE FROM ms_hak_akses 
					WHERE nm_opr='$nm_opr[0]'";	
			//AKHIR TAMBAH HEADER
			
			//TAMBAH DETAIL		
			$jmlnodet=sizeof($nm_opr)-1;		
			for ($i=0; $i<$jmlnodet; $i++){			
			$sql[] = "INSERT INTO ms_hak_akses (
					  nm_opr,fid_ms_module,
					  lihat,tambah,
					  ubah,hapus
					  ) VALUES (
					  '$nm_opr[$i]','$fid_ms_module[$i]',
					  '$lihat[$i]','$tambah[$i]',
					  '$ubah[$i]','$hapus[$i]'
					  )";	
			}//AKHIR TAMBAH DETAIL
			$sql[] = "COMMIT";	
		
			foreach($sql as $q){
				//echo $q."\r\n";
				$this->db->query($q);
			}  	
			
			$msg="Tambah data berhasil.";
		 	return array('success'=>true,'msg'=>$msg);  
		} catch (Exception $e) {
		  	$this->db->query("ROLLBACK");					
			return array('success'=>false,'msg'=>"Tambah data gagal.".$matout_id."\r\n".$Exception->getMessage());
			exit(0);
			//alert the line then kill the process
		  	//var_dump($e->getMessage());
			
		}
				
	}
}