<?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 if (!fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
{ header("location: index.php");
		}
else
{ $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='6px' cellspacing='0'>
														<tr bgcolor='#ffffff'>
															<td align='left' valign='middle'>
																<input type='checkbox' id='allmenu' style='margin:0; padding:0' onclick='cekmenu(this)'>
															</td>
															<td align='left' valign='middle'>
																Semua Menu
															</td>
															<td align='left' valign='middle' colspan='10'>
															</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))
		{ if ($bgcolor=="#ffffff")
				{ $bgcolor="#efefef";
						}
				else if ($bgcolor=="#efefef")
				{ $bgcolor="#ffffff";
						}
				$menulist.="<tr bgcolor='".$bgcolor."'>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='menulist[]' value='".$dm['appmenuid']."' style='margin:0; padding:0'> 
																	</td>
																	<td align='left' valign='middle'>
																		".$dm['parentmenuname']." &gt; ".$dm['appmenuname']."
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='semua' style='margin:0; padding:0' onclick='ceksemua(this, ".$dm['appmenuid'].")'>
																	</td>
																	<td align='left' valign='middle'>
																		Semua
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='baca' style='margin:0; padding:0'>
																	</td>
																	<td align='left' valign='middle'>
																		Baca
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='tambah' style='margin:0; padding:0'>
																	</td>	
																	<td align='left' valign='middle'>
																		Tambah
																	</td>	
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='edit' style='margin:0; padding:0'>
																	</td>
																	<td align='left' valign='middle'>
																		Edit
																	</td>
																	<td align='left' valign='middle'>
																		<input type='checkbox' name='userauth".$dm['appmenuid']."[]' value='hapus' style='margin:0; padding:0'>
																	</td>	
																	<td align='left' valign='middle'>
																		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' width='100%'>
																<tr>
																	<td align='left' valign='top' width='100%'>
																		<form method='post' action='user_group_add_process.php?menuid=".$_GET['menuid']."' style='padding:0; margin:0'>
																			<table border='0' cellpadding='8px' cellspacing='0'>
																				<tr>
																					<td align='left' valign='middle' nowrap>
																						Nama Group
																					</td>
																					<td align='left' valign='middle'>
																						<input type='text' name='groupname' size='50'>
																					</td>
																				</tr>
																				<tr>	
																					<td align='left' valign='top' style='padding-top:15px'>
																						Akses Ke-
																					</td>
																					<td align='left' valign='top'>
																						".$menulist."
																					</td>
																				</tr>
																				<tr>
																					<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>
																	</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();

?>