<?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/funcauthen.php";

if (!authen())
{ header ("location: login.php");
		}
else
{ $idgroup=$_GET['idgroup'];
		// nama group admin
		$tablename=$db_appusergroups;
		$fieldArray=array("usergroupname");
		$condition="usergroupid='".mysqli_real_escape_string($dbConnection, $idgroup)."'";
		$orderArray=array();
		$orderrule="";
		$limit="";
		$selectdb=simpleselect($tablename, $fieldArray, $condition, $orderArray, $orderrule, $limit);
		$groupdata=mysqli_fetch_array($selectdb);
		$groupname=$groupdata['usergroupname'];
		
		$jscript="<script language='javascript'>
													function cekmenu(source)
													{	checkboxes=document.getElementsByName('menulist[]');
															for(var i=0, n=checkboxes.length; i<n; i++)
															{ checkboxes[i].checked=source.checked;
  															}
															}
													function ceksemua(source, appmenuid)
													{	var namafield='userauth'+appmenuid+'[]';
															checkboxes=document.getElementsByName(namafield);
															for(var i=0, n=checkboxes.length; i<n; i++)
															{ var j=i+1;
																	checkboxes[i].checked=source.checked;
																	if (source.checked)
																	{ checkboxes[j].disabled=true;
																			}
  															else
																	{ checkboxes[j].disabled=false;
																			}
  															}
															}
												</script>
												";
		
		$menulist=$jscript.
												"<table border='0' cellpadding='8px' cellspacing='0'>
														<tr>
															<td align='left' valign='middle'>
																<input type='checkbox' id='allmenu' style='margin:0; padding:0; vertical-align:bottom' onclick='cekmenu(this)'> 
																&nbsp;Semua Menu
															</td>
															<td align='left' valign='middle' colspan='5'>
															</td>	
														</tr>
														";
		
		$bgcolor="#ffffff";
		$qm="SELECT a.parentmenuname, b.* FROM $db_parent_menu a, $db_appmenu b WHERE b.parentid=a.parentmenuid ORDER BY a.parentmenuid ASC";
		$rm=mysqli_query($dbConnection, $qm);
		while ($dm=mysqli_fetch_array($rm))
		{ // otorisasi
				$selected="";
				$check1="";
				$check2="";
				$check3="";
				$check4="";
				$tablename2=$db_appauthorization;
				$fieldArray2=array("semua", "baca", "tambah", "edit", "hapus");
				$condition2="appgroupid='".mysqli_real_escape_string($dbConnection, $idgroup)."' AND appmenuid='".$dm['appmenuid']."'";
				$orderArray2=array();
				$orderrule2="";
				$limit2="";
				$selectdb2=simpleselect($tablename2, $fieldArray2, $condition2, $orderArray2, $orderrule2, $limit2);
				if (mysqli_num_rows($selectdb2)>0)
				{ $selected=" checked";
						$authdata=mysqli_fetch_array($selectdb2);
						if ($authdata['semua']==1)
						{	$check0=" checked";
								$check1=" checked";
								$check2=" checked";
								$check3=" checked";
								$check4=" checked";
								$disabled=" disabled";
								}
						else
						{ if ($authdata['baca']==1)
								{	$check1=" checked";
										}
								if ($authdata['tambah']==1)
								{ $check2=" checked";
										}		
								if ($authdata['edit']==1)
								{ $check3=" checked";
										}		
								if ($authdata['hapus']==1)
								{ $check4=" checked";
										}		
								}	
						}
				if ($bgcolor=="#ffffff")
				{ $bgcolor="#dfdfdf";
						}
				else if ($bgcolor=="#dfdfdf")
				{ $bgcolor="#ffffff";
						}
				$menulist.="<tr bgcolor='".mysqli_real_escape_string($dbConnection, $bgcolor)."'>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='menulist[]' value='".$dm['appmenuid']."'".$selected." style='margin:0; padding:0; vertical-align:middle'>
																		&nbsp;".$dm['parentmenuname']." &gt; ".$dm['appmenuname']."
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='semua'".$check1." style='margin:0; padding:0; vertical-align:bottom' onclick='ceksemua(this, ".$dm['appmenuid'].")'>
																		Semua
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='baca'".$check1.$disabled." style='margin:0; padding:0; vertical-align:bottom'>
																		Baca
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='tambah'".$check2.$disabled." style='margin:0; padding:0; vertical-align:bottom'>
																		Tambah
																	</td>	
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='edit'".$check3.$disabled." style='margin:0; padding:0; vertical-align:bottom'>
																		Edit
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='hapus'".$check4.$disabled." style='margin:0; padding:0; vertical-align:bottom'>
																		Hapus
																	</td>	
																</tr>	
																";
				}
		$menulist.="</table>";

		$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'>
																		<form method='post' action='user_group_edit_process.php?menuid=".$_GET['menuid']."&idgroup=".$idgroup."' style='padding:0; margin:0'>
																			<table border='0' cellpadding='6px' cellspacing='1px'>
																				<tr>
																					<td align='left' valign='middle' nowrap>
																						Nama Group
																					</td>
																					<td align='left' valign='middle'>
																						:
																					</td>
																					<td align='left' valign='middle'>
																						<input type='text' name='groupname' size='83' value='".$groupname."'>
																					</td>
																				</tr>
																				<tr>	
																					<td align='left' valign='top'>
																						Hak Akses
																					</td>
																					<td align='left' valign='top'>
																						:
																					</td>
																					<td align='left' valign='top'>
																						".$menulist."
																					</td>
																				</tr>
																				<tr>
																					<td></td>
																					<td></td>
																					<td align='left' valign='middle'>
																						<input type='submit' value=' Simpan '> &nbsp;
																						<input type='button' value=' Batal ' onclick='history.back(-1)'>
																					</td>
																				</tr>
																			</table>
																		</form><br><br><br>
																	</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();

?>