<?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
{ $userlist="";
		$quser="SELECT * FROM $db_appusers
										WHERE usergroupid<>'1'
										ORDER BY fullname ASC
										";
		$ruser=mysqli_query($dbConnection, $quser);
		$datataken=mysqli_num_rows($ruser);
		if (mysqli_num_rows($ruser)>0)
		{ while ($userdata=mysqli_fetch_array($ruser))
				{ $tablename2=$db_appusergroups;
						$fieldArray2=array("usergroupname");
						$cond="";
						$idgroup=explode("|", rtrim(trim($userdata['usergroupid']), "|"));
						$jmlhid=count($idgroup);
						$a=1;
						foreach ($idgroup as $id)
						{ $cond.="usergroupid='".mysqli_real_escape_string($dbConnection, $id)."'";
								if ($a<$jmlhid)
								{ $cond.=" OR ";
										}
								$a++;
								}
						$condition2=$cond;
						$orderArray2=array();
						$orderrule2="";
						$limit2="";
						$usergroupname="";
						$selectdb2=simpleselect($tablename2, $fieldArray2, $condition2, $orderArray2, $orderrule2, $limit2);
						$jmlhgroup=mysqli_num_rows($selectdb2);
						$b=1;
						while ($userdata2=mysqli_fetch_array($selectdb2))
						{ $usergroupname.=$userdata2['usergroupname'];
								if ($b<$jmlhgroup)
								{ $usergroupname.=", ";
										}
								$groupke=$b%3;
								if ($groupke==0)
								{ $usergroupname.="<br>";
										}
								$b++;				
								}
						$lastlogin=$userdata['lastlogin'];
						if ($userdata['accessstatus']==1)
						{ $access="Aktif";
								}
						else
						{ $access="<font color='#ff0000'>Tidak Aktif</font>";
								}
						$userlist.="<tr>
																			<td align='left' valign='middle'>
																				".$userdata['fullname']."
																			</td>
																			<td align='left' valign='middle'>
																				".$userdata['username']."
																			</td>
																			<td align='left' valign='middle'>
																				".$usergroupname."
																			</td>
																			<td align='left' valign='middle' nowrap>
																				".$userdata['useremail']."
																			</td>
																			<td align='center' valign='middle'>
																				".$lastlogin."
																			</td>
																			<td align='center' valign='middle'>
																				".$access."
																			</td>
																			";
						if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || editaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
						{ $userlist.="<td align='center' valign='middle' nowrap>
																					<a href='user_edit.php?iduser=".$userdata['userid']."&menuid=".$_GET['menuid']."'>
																						<img src='images/database_edit.png' align='absmiddle' title='Edit'>
																					</a>
																				</td>
																				";
								}
						else
						{ $userlist.="<td></td>";
								}
						if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || deleteaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
						{ $userlist.="<td align='center' valign='middle' nowrap>
																					<a href='user_delete.php?iduser=".$userdata['userid']."&menuid=".$_GET['menuid']."'>
																						<img src='images/database_delete.png' align='absmiddle' title='Hapus'>
																					</a>
																				</td>
																				";
								}											
						else
						{ $userlist.="<td></td>";
								}
						$userlist.="</tr>";
						}
				}
		else
		{ $userlist.="<tr>
																	<td align='center' valign='middle' colspan='8' nowrap>
																		<br>Data user tidak ditemukan.<br><br>
																	</td>
																</tr>
																";
				
				}				

		$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%'>
																	";
		if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || addaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
		{ $content.="<input type='button' value=' Tambah User ' onclick=\"document.location='user_add.php?menuid=".$_GET['menuid']."'\"><br><br>";
				}
		$content.="<table id='' class='display'>
														<thead>
															<tr>
																<th align='center' valign='middle' nowrap>
																	<b>Nama Lengkap</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Username</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>User Group</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Alamat Email</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Last Login</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Status</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Edit</b>
																</td>
																<th align='center' valign='middle' nowrap>
																	<b>Hapus</b>
																</td>
															</tr>
														</thead>
														<tbody>	
															".$userlist."
														</tbody>	
													</table>
												</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();

?>