<?php
# Baca Variabel Form
$message = array();
$Act		 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] :'';
$id			 = isset($_REQUEST['id']) ? $_REQUEST['id'] :'';
$user_level  = isset($_REQUEST['user_level']) ? $_REQUEST['user_level'] :'';
$user_idn	 = isset($_REQUEST['user_idn']) ? $_REQUEST['user_idn'] :'';
$user_name 	 = isset($_REQUEST['user_name']) ? $_REQUEST['user_name'] :'';
$user_pass 	 = isset($_REQUEST['user_pass']) ? $_REQUEST['user_pass'] :'';
if($Act=="SAVE"){
	if(count($message)==0){	
		if(empty($id)){
			$qrySave=mysql_query("INSERT INTO user SET user_level='$user_level',user_id='$user_idn',user_name='$user_name',user_pass='$user_pass'") 
								  or die ("Gagal query".mysql_error());									  
			#Aktivitas User
			$useractSave=mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data user - $user_id', 
									   user_date='$date', user_time='$time'") or die ("Gagal query".mysql_error());					  
		}else{
			$qrySave=mysql_query("UPDATE user SET user_level='$user_level',user_id='$user_idn',user_name='$user_name',user_pass='$user_pass' WHERE id='$id'") 
								  or die ("Gagal query".mysql_error());
			#Aktivitas User
			$useractSave=mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data user - $user_id', 
									   user_date='$date', user_time='$time'") or die ("Gagal query".mysql_error());
		}				
		if($qrySave){		
			$message[] = "Data user telah tersimpan.";			
			$status	   = "OK";	
		}
	}			
}
#Proses Edit
if($Act=="DETAIL" || $Act=="UACCESS"){
	$qd = mysql_query("SELECT * FROM user WHERE id='$id'");
	$dd=mysql_fetch_object($qd);		
	$id		 		= $dd->id;
	$user_level 	= $dd->user_level;
	$user_idn 		= $dd->user_id;
	$user_name 		= $dd->user_name;
	$user_pass 		= $dd->user_pass;
}
#Proses Hapus
if($Act=="DELETE"){
	$qd = mysql_query("UPDATE user SET user_level=100 WHERE id='$id'");
	if($qd){
		#Aktivitas User
		$useractSave=mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data user - ID :$user', 
								   user_date='$date', user_time='$time'") or die ("Gagal query".mysql_error());
		$message[] = "Data User telah dihapus.";				
		$status	   = "OK";
	}
}
#New User
if ($Act=="ADD" || $Act=="DETAIL"){
	require_once "add_user.php";
} 
#Open User Acces
if($Act=="UACCESS"){
	require_once "access_user.php";
}
#Save User Acces
if($Act=="UASAVE"){
	$pilih	= $_REQUEST['pilih']; 		
	foreach ( $pilih as $i => $val){	
		$menu      = $_REQUEST['title'][$val];
		$uaccess   = isset($_REQUEST['uaccess'][$val]) ? $_REQUEST['uaccess'][$val] : '';
		$uadd 	   = isset($_REQUEST['uadd'][$val]) ? $_REQUEST['uadd'][$val] : '';
		$uedit     = isset($_REQUEST['uedit'][$val]) ? $_REQUEST['uedit'][$val] : '';
		$udelete   = isset($_REQUEST['udelete'][$val]) ? $_REQUEST['udelete'][$val] : '';
		$upost     = isset($_REQUEST['upost'][$val]) ? $_REQUEST['upost'][$val] : '';
		$uunpost   = isset($_REQUEST['uunpost'][$val]) ? $_REQUEST['uunpost'][$val] : '';
		$uprint    = isset($_REQUEST['uprint'][$val]) ? $_REQUEST['uprint'][$val] : '';
		if(!empty($uaccess)){$uaccess="Y";}else{$uaccess="N";}
		if(!empty($uadd)){$uadd="Y";}else{$uadd="N";}
		if(!empty($uedit)){$uedit="Y";}else{$uedit="N";}
		if(!empty($udelete)){$udelete="Y";}else{$udelete="N";}
		if(!empty($upost)){$upost="Y";}else{$upost="N";}
		if(!empty($uunpost)){$uunpost="Y";}else{$uunpost="N";}
		if(!empty($ubackdate)){$ubackdate="Y";}else{$ubackdate="N";}
		if(!empty($uprint)){$uprint="Y";}else{$uprint="N";}		
		#cek
		$ck=mysql_fetch_array(mysql_query("SELECT id FROM menu_access WHERE ma_mmid='$val' AND ma_userid='$user_idn' AND  ma_status=10"));
		if(empty($ck['id'])){
			mysql_query("INSERT INTO menu_access SET ma_userid='$user_idn', ma_mmid='$val', ma_uaccess='$uaccess', ma_uadd='$uadd', ma_uedit='$uedit', ma_udelete='$udelete',
			ma_upost='$upost', ma_uunpost='$uunpost', ma_uprint='$uprint', ma_status=10");
			#Aktivitas User
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data akses menu $menu', user_date='$date', user_time='$time'");
		}else{
			mysql_query("UPDATE menu_access SET ma_uaccess='$uaccess', ma_uadd='$uadd', ma_uedit='$uedit', ma_udelete='$udelete', ma_upost='$upost', ma_uunpost='$uunpost', 
			ma_uprint='$uprint' WHERE ma_userid='$user_idn' AND ma_mmid='$val' AND ma_status=10");
			#insert user activity
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data akses menu $menu', user_date='$date', user_time='$time'");
		}
	}	
	$message[] = "Akses user sukses disimpan.";
	$status = "OK";
}
if ($Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-User'>";
}	
# Popup pesan
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php 		
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-User'>";	
	}
}
?>