<?php
class Dbset_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->tbl = 'user';
		$this->tbl2 = 'mst_perusahaan';
		$this->tbl3 = 'user_group';
		$this->pk = 'nama_user';
		$this->pk2 = 'KdPrshn';
		$this->pk3 = 'code_group_user';
    }

	function getPage($page,$rows,$key=''){		
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,nama_user AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.".$this->pk2."=a.kd_cmpy
			  LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=a.grup
			  WHERE a.nama_user LIKE '%$key%'";
		if ($this->username !=  'admin'&& $this->username !='doit'){
			  $q .= " AND a.nama_user = '".$username."'";
		}
		$q .= " ORDER BY nama_user";
		$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['nama_user'],"Tambah Data User ".$data['nama_user']);	
		} 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 Operator ".$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 Operator ".$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 getHakAkses2($group_name) {
		$q = "SELECT id AS menu_id, menu_name, parent_id
			  FROM tb_menu
			  
			  WHERE parent_id = '0'
			  ORDER BY ordering ASC";
		$rs = $this->db->query($q)->result();
		$temp = array();
		foreach ($rs as $r) {
			$ltuh = $this->cekAkses($group_name, $r->menu_id);
			$lihat = 0;
			$tambah = 0;
			$ubah = 0;
			$hapus = 0;
			if ($ltuh != "") {
				$ltuh2 = json_encode($ltuh);
				// print_r($ltuh2->lihat);die;
				$lihat = $ltuh->lihat;
				$tambah = $ltuh->tambah;
				$ubah = $ltuh->ubah;
				$hapus = $ltuh->hapus;
			}
			$qSub = " SELECT id AS menu_id, menu_name, parent_id
					  FROM tb_menu
			  		-- WHERE parent_id = '1'
					  -- ORDER BY ordering ASC
					  -- 
					  WHERE parent_id = '" . $r->menu_id . "'";
			$rSub = $this->db->query($qSub)->result_array();
			$data = array(
				'group_name' => $group_name,
				'menu_id' => $r->menu_id,
				'menu_name' => $r->menu_name,
				'lihat' => $lihat,
				'tambah' => $tambah,
				'ubah' => $ubah,
				'hapus' => $hapus,
				'children' => (count($rSub) > 0) ? $this->getChilds($r->menu_id, $group_name) : '',
			);
			array_push($temp, $data);
		}
		return $temp;
	}

	function getDept($group_name) {
		$q = "SELECT dept_cd AS dept_id, dept_name
			  FROM mst_dept
			  ORDER BY no ASC";
		$rs = $this->db->query($q)->result();
		$temp = array();
		foreach ($rs as $r) {
			$ltuh = $this->cekDept($group_name, $r->dept_id);
			$tambah = 0;
			if ($ltuh != "") {
				$ltuh2 = json_encode($ltuh);
				// print_r($ltuh2->lihat);die;
				$tambah = $ltuh->tambah;
			}
			$qSub = " SELECT id AS menu_id, menu_name, parent_id
					  FROM tb_menu
			  		-- WHERE parent_id = '1'
					  -- ORDER BY ordering ASC
					  -- 
					  WHERE parent_id = '" . $r->menu_id . "'";
			$rSub = $this->db->query($qSub)->result_array();
			$data = array(
				'group_name' => $group_name,
				'dept_id' => $r->dept_id,
				'dept_name' => $r->dept_name,
				'tambah' => $tambah,
			);
			array_push($temp, $data);
		}
		return $temp;
	}


	function getPro($group_name) {
		$q = "SELECT project_cd AS project_id, project_name
			  FROM mst_project
			  ORDER BY no ASC";
		$rs = $this->db->query($q)->result();
		$temp = array();
		foreach ($rs as $r) {
			$ltuh = $this->cekPro($group_name, $r->project_id);
			$tambah = 0;
			if ($ltuh != "") {
				$ltuh2 = json_encode($ltuh);
				// print_r($ltuh2->lihat);die;
				$tambah = $ltuh->tambah;
			}
			$qSub = " SELECT id AS menu_id, menu_name, parent_id
					  FROM tb_menu
			  		-- WHERE parent_id = '1'
					  -- ORDER BY ordering ASC
					  -- 
					  WHERE parent_id = '" . $r->menu_id . "'";
			$rSub = $this->db->query($qSub)->result_array();
			$data = array(
				'group_name' => $group_name,
				'project_id' => $r->project_id,
				'project_name' => $r->project_name,
				'tambah' => $tambah,
			);
			array_push($temp, $data);
		}
		return $temp;
	}


	
	function getChilds($menu_id, $group_name) {
		$q = "SELECT id AS menu_id, menu_name, parent_id
					  FROM tb_menu
					  
						WHERE parent_id = '$menu_id'
					  ORDER BY ordering ASC";
						
		$rs = $this->db->query($q)->result();
		$submenu = array();
		foreach ($rs as $r) {
			$ltuh = $this->cekAkses($group_name, $r->menu_id);
			$lihat = 0;
			$tambah = 0;
			$ubah = 0;
			$hapus = 0;
			if ($ltuh != "") {
				$ltuh2 = json_encode($ltuh);
				// print_r($ltuh2->lihat);die;
				$lihat = $ltuh->lihat;
				$tambah = $ltuh->tambah;
				$ubah = $ltuh->ubah;
				$hapus = $ltuh->hapus;
			}
			$qSub = "SELECT id AS menu_id, menu_name, parent_id
					  FROM tb_menu
						WHERE parent_id = '" . $r->menu_id . "'
					  ORDER BY ordering ASC";
			$rSub = $this->db->query($qSub)->result_array();
			$submenu[] = array(
				'group_name' => $group_name,
				'menu_id' => $r->menu_id,
				'menu_name' => $r->menu_name,
				'lihat' => $lihat,
				'tambah' => $tambah,
				'ubah' => $ubah,
				'hapus' => $hapus,
				'children' => (count($rSub) > 0) ? $this->getChilds($r->menu_id, $group_name) : '',
			);
		}
		return $submenu;
	}
	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 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 user_hak_akses 
			  WHERE user='$nm_opr' AND code_menu = '$id'";
		$rs = $this->db->query($q)->result();
		if ($rs){ 
			return $rs[0]; 
		} 
	}
	function cekDept($nm_opr,$id){
		$q = "SELECT * FROM user_dept
			  WHERE user='$nm_opr' AND DeptCd = '$id'";
		$rs = $this->db->query($q)->result();
		if ($rs){ 
			return $rs[0]; 
		} 
	}
	function cekPro($nm_opr,$id){
		$q = "SELECT * FROM user_project
			  WHERE user='$nm_opr' AND ProjectCd = '$id'";
		$rs = $this->db->query($q)->result();
		if ($rs){ 
			return $rs[0]; 
		} 
	}
	function setDept($data){		
		$user=explode("`", $data['User']);
		$DeptCd=explode("`", $data['DeptCd']);
		$tambah=explode("`", $data['tambah']);			
		try { 
			$sql[] = "START TRANSACTION";
			//TAMBAH HEADER
			$sql[] = "DELETE FROM user_dept
					WHERE User='$user[0]'";	
			//AKHIR TAMBAH HEADER
			
			//TAMBAH DETAIL		
			$jmlnodet=sizeof($user)-1;		
			for ($i=0; $i<$jmlnodet; $i++){			
			$sql[] = "INSERT INTO user_dept (
					  User,DeptCd,
					  tambah
					  ) VALUES (
					  '$user[$i]','$DeptCd[$i]','$tambah[$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 user then kill the process
		  	//var_dump($e->getMessage());
		}
				
	}

	function setPro($data){		
		$user=explode("`", $data['User']);
		$ProjectCd=explode("`", $data['ProjectCd']);
		$tambah=explode("`", $data['tambah']);			
		try { 
			$sql[] = "START TRANSACTION";
			//TAMBAH HEADER
			$sql[] = "DELETE FROM user_project
					WHERE User='$user[0]'";	
			//AKHIR TAMBAH HEADER
			
			//TAMBAH DETAIL		
			$jmlnodet=sizeof($user)-1;		
			for ($i=0; $i<$jmlnodet; $i++){			
			$sql[] = "INSERT INTO user_project (
					  User,ProjectCd,
					  tambah
					  ) VALUES (
					  '$user[$i]','$ProjectCd[$i]','$tambah[$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 user then kill the process
		  	//var_dump($e->getMessage());
		}
				
	}
	
	function setHakAkses($data){		
		$user=explode("`", $data['user']);
		$code_menu=explode("`", $data['code_menu']);
		$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 user_hak_akses 
					WHERE user='$user[0]'";	
			//AKHIR TAMBAH HEADER
			
			//TAMBAH DETAIL		
			$jmlnodet=sizeof($user)-1;		
			for ($i=0; $i<$jmlnodet; $i++){			
			$sql[] = "INSERT INTO user_hak_akses (
					  user,code_menu,
					  lihat,tambah,
					  ubah,hapus
					  ) VALUES (
					  '$user[$i]','$code_menu[$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 user then kill the process
		  	//var_dump($e->getMessage());
		}
				
	}
	function setHakAkses2($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 user then kill the process
		  	//var_dump($e->getMessage());
			
		}
				
	}
}