<?php

session_name("SIGNADMAUTH");
session_start();

include "common/config/config.php";
include "common/config/dbconfig.php";
include "common/functions/funcconnect.php";
openDB();

include "common/functions/funcselectdb.php";
include "common/functions/funceditdb.php";
include "common/functions/funcauthen.php";

if (!authen())
{ header ("location: login.php");
		}
else
{ $iduser=$_GET['iduser'];
		$username=$_POST['username'];
		$fullname=$_POST['fullname'];
		$groupid=$_POST['groupid'];
		$usergroupid="";
		if ($groupid!="")
		{ foreach ($groupid as $id)
				{ $usergroupid.=$id."|";
						}
				}
		$usergroupid=rtrim($usergroupid, "|");		
		$useremail=$_POST['useremail'];
		$password1=$_POST['password1'];
		$activestatus=$_POST['status'];
		
		$tablename=$db_appusers;
		$fieldArray=array("userid");
		$condition="username='".mysqli_real_escape_string($dbConnection, $username)."' AND userid<>'".mysqli_real_escape_string($dbConnection, $iduser)."'";
		$orderArray=array();
		$orderrule="";
		$limit="";

		if (empty($username))
		{ $msg="Username tidak boleh kosong.<br><br>".$linkback;
				}
		else if (empty($fullname))
		{	$msg="Nama lengkap tidak boleh kosong.<br><br>".$linkback;
				}
		else if (empty($groupid))
		{	$msg="User group tidak boleh kosong.<br><br>".$linkback;
				}
		else if (mysqli_num_rows(simpleselect($tablename, $fieldArray, $condition, $orderArray, $orderrule, $limit))>0)
		{ $msg="Username yang dimasukan sudah dimiliki pengguna lain.<br><br>".$linkback;
				$err=1;
				}
		else
		{ if (!empty($password1))
				{ include "common/functions/funcpass.php";
						$userpass=saltpassword($password1);
						}
				$tablename=$db_appusers;
				if ($userpass!="")
				{ $fieldArray=array("fullname", "username", "userpass", "usergroupid", "useremail", "accessstatus");
						$valueArray=array($fullname, $username, $userpass, $usergroupid, $useremail, $activestatus);
						}
				else
				{ $fieldArray=array("fullname", "username", "usergroupid", "useremail", "accessstatus");
						$valueArray=array($fullname, $username, $usergroupid, $useremail, $activestatus);
						}
				$condition="userid='".mysqli_real_escape_string($dbConnection, $iduser)."'";
				$insertdb=simpleupdate($tablename, $fieldArray, $valueArray, $condition);
				if ($insertdb)
				{ $msg="Mengubah data pengguna sukses.";
						$logdesc="UPDATE PENGGUNA";
						include "logakses.php";
						}
				else
				{ $msg="Mengubah data pengguna gagal.<br><br>".$linkback;
								}
				}

		$qq="SELECT a.appmenuname, b.parentmenuname FROM $db_appmenus a, $db_parent_menus b ";
		$qq.="WHERE a.appmenuid='".mysqli_real_escape_string($dbConnection, $_GET['menuid'])."' AND b.parentmenuid=a.parentid";
		$rr=mysqli_query($dbConnection, $qq);
		$dd=mysqli_fetch_array($rr);

		$content="<table border='0' cellpadding='6px' cellspacing='0' width='100%'>
													<tr>
														<td align='left' valign='top' width='100%'>
															<b class='title'>".$dd['parentmenuname']." &gt; ".$dd['appmenuname']."</b>
															<br><hr size='1' color='#000000'><br>
															<table border='0' cellpadding='0' cellspacing='0'>
																<tr>
																	<td align='left' valign='top'>
																		".$msg."
																	</td>
																</tr>
															</table>		
														</td>
													</tr>
												</table>
												";
		}

include "webmenu.php";
include "common/include/template.php";
$temp=new template;
$temp->define_theme("themes/theme1.html");
$temp->define_tag("{PAGETITLE}", $namaaplikasi);
$temp->define_tag("{NAMAUSER}", strtoupper($_SESSION['SES_FULLNAME']));
$temp->define_tag("{APPMENU}", $webmenu);
$temp->define_tag("{APPCONTENT}", $content);
$temp->parse();
$temp->printproses();

closeDB();

?>