<?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/funcinsertdb.php";
include "common/functions/funceditdb.php";
include "common/functions/funcdeletedb.php";
include "common/functions/funcauthen.php";

if (!authen())
{ header ("location: login.php");
		}
else
{ $idgroup=$_GET['idgroup'];
		$namagroup=$_POST['groupname'];		
		$menudipilih=$_POST['menulist'];
		if (empty($namagroup))
		{ $msg="Nama group tidak boleh kosong.<br><br>".$linkback;
				}
		else if (count($menudipilih)<1)
		{	$msg="Hak akses tidak boleh kosong.<br><br>".$linkback;
				}
		else
		{ $err=0;
				$tablename=$db_appusergroups;
				$fieldArray=array("usergroupid");
				$condition="usergroupname='".mysqli_real_escape_string($dbConnection, $namagroup)."' AND usergroupid<>'".mysqli_real_escape_string($dbConnection, $idgroup)."'";
				$orderArray=array();
				$orderrule="";
				$limit="";
				$selectdb=simpleselect($tablename, $fieldArray, $condition, $orderArray, $orderrule, $limit);
				if (mysqli_num_rows($selectdb)>0)
				{ $msg="Nama group admin sudah ada dalam database.<br><br>".$linkback;
								$err=1;
						}
				else
				{ foreach ($menudipilih as $menuid)
						{ $fieldname="userauth".$menuid;
								$useraccess=$_POST[$fieldname];
								if (count($useraccess)<1)
								{ $tablename=$db_appmenu;
										$fieldArray=array("appmenuname");
										$condition="appmenuid='".mysqli_real_escape_string($dbConnection, $menuid)."'";
										$orderArray=array();
										$orderrule="";
										$limit="";
										$selectdb=simpleselect($tablename, $fieldArray, $condition, $orderArray, $orderrule, $limit);
										$menudata=mysqli_fetch_array($selectdb);
										$msg="Hak akses untuk menu ".$menudata['appmenuname']." tidak boleh kosong.<br><br>".$linkback;
																$err=1;
										break;
										}
								}		
						}
				if ($err==0)
				{ // Update nama group
						$tablename=$db_appusergroups;
						$fieldArray=array("usergroupname");
						$valueArray=array($namagroup);
						$condition="usergroupid='".mysqli_real_escape_string($dbConnection, $idgroup)."'";
						$editdb=simpleupdate($tablename, $fieldArray, $valueArray, $condition);
						if ($editdb)
						{ $logdesc="UPDATE GROUP PENGGUNA";
								include "logakses.php";
								// Menghapus otorisasi yang sekarang
								$tablenameauth=$db_appauthorization;
								$conditionauth="appgroupid='".mysqli_real_escape_string($dbConnection, $idgroup)."'";
								$deletedbauth=simpledelete($tablenameauth, $conditionauth);
								// Otorisasi Pengguna
								foreach ($menudipilih as $menuid)
								{ $fieldname="userauth".$menuid;
										$useraccess=$_POST[$fieldname];
										$semua=0;
										$baca=0;
										$tambah=0;
										$edit=0;
										$hapus=0;
										foreach ($useraccess as $userauth)
										{ if ($userauth=="semua")
												{ $semua=1;
														$baca=1;
														$tambah=1;
														$edit=1;
														$hapus=1;
														}
												if ($userauth=="baca")
												{ $baca=1;
														}
												else if ($userauth=="tambah")
												{ $tambah=1;
														$baca=1;
														}
												else if ($userauth=="edit")
												{ $edit=1;
														$baca=1;
														}
												else if ($userauth=="hapus")
												{ $hapus=1;
														$baca=1;
														}
												}
										if ($baca==1 && $tambah==1 && $edit==1 && $hapus==1)
										{ $semua=1;
												}
										// Update data otorisasi sekarang
										$tablenameauth=$db_appauthorization;
										$fieldArrayauth=array("appgroupid", "appmenuid", "semua", "baca", "tambah", "edit", "hapus");
										$valueArrayauth=array($idgroup, $menuid, $semua, $baca, $tambah, $edit, $hapus);
										$insertdbauth=simpleinsert($tablenameauth, $fieldArrayauth, $valueArrayauth);			
										}		
								$msg="Mengubah group admin sukses.";
										}
						else
						{ $msg="Mengubah group admin gagal.";
												}
						}
				}		

		$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();

?>