<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<?php
include ("../navigation/header.php");
$page_name = "AD-permissions";
include ("../navigation/access.php");


$user_access_id = $_GET['user_select'];
$set = $_GET['set'];
$message='';

$table_name = "system_users";
$stay_query = "SELECT stay FROM $table_name WHERE access_id = '".$access_id."' ";
$stay_result = mySQL_query($stay_query);
$stay_num = mysql_num_rows($stay_result);
$stay = mysql_result($stay_result, 0, "stay");




if (isset($_GET['set_button']) )
	   {
   $page_name = $_GET['page_select'];

   $table_name = $_SESSION['db_prefix']."_permissions";
   $check_query = "SELECT page_name FROM $table_name WHERE access_id = '". $user_access_id."' AND page_name = '". $page_name."' ";
   $check_result = execsql($check_query);
   $check_num = mysql_num_rows($check_result);
   if ($check_num!=0) {
	   $message= "&nbsp;&nbsp;&nbsp;<img src=../images/exclamation_mark.png width=24 height=24/><font color=orange><b>&nbsp;&nbsp;&nbsp;Permission was allready set.</b></font>";
   }
   else {
   $table_name = $_SESSION['db_prefix']."_permissions";
   $set_query  = "INSERT INTO $table_name (access_id, page_name) VALUES ('" . $user_access_id . "','" . $page_name . "')";
   execsql($set_query);	
   $message= "&nbsp;&nbsp;&nbsp;<img src=../images/check_mark_green.png width=24 height=24/><font color=green><b>&nbsp;&nbsp;&nbsp;Permission was set.</b></font>";
   }
}

if (isset($_GET['remove_button']) )
	   {
	$page_name = $_GET['page_select'];
	$table_name = $_SESSION['db_prefix']."_permissions";
	$del_query = "DELETE FROM $table_name WHERE access_id = '".$user_access_id."' AND page_name ='".$page_name."'";
	$del_result = execsql($del_query);
    $message= "&nbsp;&nbsp;&nbsp;<img src=../images/error.png width=24 height=24/><font color=red><b>&nbsp;&nbsp;&nbsp;Permission was unset.</b></font>";
}

if (isset($_GET['home_button']) )
	   {
	$user_access_id = $_GET['user_select'];
    $home_page = $_GET['home_page_group'];
	$table_name = $_SESSION['db_prefix']."_users";
    $page_query = "UPDATE $table_name SET home_page = '". $home_page ."' WHERE access_id = '". $user_access_id  ."' ";
    $page_result = mySQL_query($page_query);
}
	
if ($set==5) {
$user_id = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$table_name = $_SESSION['db_prefix']."_tracking_admin";
$action_query = "INSERT INTO $table_name (entry_date, user, action, details) VALUES ('".$entry_date."' ,'".$user_id."','set permissions' ,'set permissions' )";
execsql($action_query);
}

$table_name = $_SESSION['db_prefix']."_users";
$user_query = "SELECT username, home_page FROM $table_name WHERE access_id = '". $user_access_id."' ";
$user_result = execsql($user_query);
$user_num = mysql_num_rows($user_result);
if ($user_num==1) {
$username = mysql_result($user_result, 0, "username");
$home_page = mysql_result($user_result, 0, "home_page");
}
else {
	$username="";
	$home_page="";
}

if  ($username=="system manager") {
	$message="<font color=red><b>Permission for the System Manager cannot be removed</b></font>";
}

$table_name = $_SESSION['db_prefix']."_permissions";
$perm_list_query = "SELECT page_name FROM $table_name WHERE access_id = '". $user_access_id." ' ";
$perm_list_result = execsql($perm_list_query);
$perm_list_num = mysql_num_rows($perm_list_result);


$table_name = $_SESSION['db_prefix']."_users";
$select_query = "SELECT access_id, username FROM $table_name ORDER BY username";
	$arrId = array ();
	$arrName = array ();
	$select_result = execsql($select_query);
	$select_num = mysql_num_rows($select_result);
	$a = 0;
	while ($a < $select_num) {
		$arrId[$a] = mysql_result($select_result, $a, "access_id");
		$arrName[$a] = mysql_result($select_result, $a, "username");
		$a++;
	}
	
$table_name = $_SESSION['db_prefix']."_permissions";
$page_query = "SELECT access_id, page_name FROM $table_name WHERE access_id ='".$user_access_id."' ORDER BY page_name ";
	$arrId2 = array ();
	$arrName2 = array ();
	$page_result = execsql($page_query);
	$page_num = mysql_num_rows($page_result);
	$b = 0;
	while ($b < $page_num) {
		$arrId2[$b] = mysql_result($page_result, $b, "page_name");
		$arrName2[$b] = mysql_result($page_result, $b, "page_name");
		$b++;
	}



?>



<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  
</head>

<body  onload="<?php if ($set==5) {?>document.getElementById('hidden_frame').style.display = 'none'; <?php } else { ?>document.getElementById('hidden_frame').style.display = 'block'; <?php } ?>" >

<?php
if ($permission_num==1) {
	include ("../navigation/admin_sidebar.php");
?>

<div class="main_frame"  style="height:auto; min-height:570px;">

<div class="page_head_frame">
        <table width="809" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><label class="label_page_head">Set User Permissions for&nbsp;<?php echo $username ?></label></td>
            </tr>
        </table>
      </div>
        <br /><br />
        <div class="page_middle_frame">
<div class="page_inside_frame" style="margin-left:10px;" >
<form name="form1" id="form1" action="permissions.php" method="get"> <label class="label_std">Select a user to view and set permissions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
  <select name="user_select" class="selectbox120" onchange="window.location.href= this.form.user_select.options[this.form.user_select.selectedIndex].value" >
    <option value=""></option>
    <?php
        	for ($i = 0; $i < count($arrId); $i++) {
    		echo "<option value=permissions.php?user_select=".$arrId[$i]."&set=0>".$arrName[$i]."</option>";
	}
?>
  </select>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("user_select","req","Please select user.");
</script></div>
<br />
<br />

      <div class="hidden_frame" id="hidden_frame">
        <form id="form2" name="form2" method="get" action="permissions.php">
            <div class="page_inside_frame">
              <label style="font-size:16px;">Permissions and home page settings for: <?php echo $username ?></label><br />
</div>  
              
              <div class="page_inside_frame">
                Set home page:
                <input type="radio" name="home_page_group" value="hr" id="home_page_group_1" <?php if ($home_page=="hr") { ?> checked="checked" <?php } ?>/>
                HR
                <input type="radio" name="home_page_group" value="production" id="home_page_group_2" <?php if ($home_page=="production") { ?> checked="checked" <?php } ?>/>
                Production
                 
               <input type="radio" name="home_page_group" value="stores" id="home_page_group_4" <?php if ($home_page=="stores") { ?> checked="checked" <?php } ?>/>Stores
               <input type="radio" name="home_page_group" value="quality" id="home_page_group_5" <?php if ($home_page=="quality") { ?> checked="checked" <?php } ?>/>Quality control
               <input type="radio" name="home_page_group" value="admin" id="home_page_group_5" <?php if ($home_page=="admin") { ?> checked="checked" <?php } ?>/>Admin
                &nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input name="user_select" type="hidden" id="user_select" value="<?php echo $user_access_id ?>" />
                <input name="set" type="hidden" id="set" value="3" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input name="home_button" type="submit" class="imagebutton" id="home_button" value="Set home page" />
                <br />
          </div>
        </form>
            <div class="page_inside_frame">
            <label class="label_std">To add permissions for this user, select from the list and click 'Set'.</label>
              <form action="permissions.php" name="set_form" method="get">
                  <table width="600" height="424" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td width="283" height="68" valign="top"><label class="label_std"><br />
                        <br />
                        Permissions for: <span class="label_page_head"><?php echo $username ?></span></label></td>
                      <td width="277" rowspan="4" valign="top"><select name="page_select" size="30" class="selectbox160" id="page_select"  style="color:#FF0000; width:250px;" onmouseover="MM_showHideLayers('message','','hide')">
                        <option disabled="disabled" style="font-weight:700; color:#000">Admin</option>
                        <?php if ($stay==1) { ?>
                        <option <?php if (in_array("AD-user list", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-user list">User list (main page)</option>
                        <option <?php if (in_array("AD-permissions", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-permissions">Set permissions</option>
                        <?php } ?>
                        <option <?php if (in_array("AD-presets", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-presets">Presets</option>
                          
                        <option <?php if (in_array("AD-pre defined", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-pre defined">Pre defined lists</option>
                        <option <?php if (in_array("AD-error reporting", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-error reporting">Error reporting</option>
                        <option <?php if (in_array("AD-tracking", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-tracking">Tracking</option>
                        <option <?php if (in_array("AD-history", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-history">History</option>
                        
                  		<option disabled="disabled" style="font-weight:700; color:#000">HR</option>
                        <option <?php if (in_array("HR-main", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-main">Main</option>
                       <option <?php if (in_array("HR-new employee", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-new employee">New employee</option>
                       <option <?php if (in_array("HR-edit employee", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-edit employee">Edit employee</option>
                       <option <?php if (in_array("HR-overtime", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-overtime">Overtime</option>
                       <option <?php if (in_array("HR-leave", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-leave">Leave</option>
                       <option <?php if (in_array("HR-discipline", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-discipline">Discipline</option>
                       <option <?php if (in_array("HR-time recording", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-time recording">Time recording </option>
                        <option <?php if (in_array("HR-wages", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-wages">View wages</option>
                        <option <?php if (in_array("HR-management", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-management">View management</option>
                  
                  
                        <option disabled="disabled" style="font-weight:700; color:#000">Production</option>
                        <option <?php if (in_array("Production-main", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-main">Main page</option>
                        <option <?php if (in_array("Production-plan", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-plan">Production plan</option>
                        <option <?php if (in_array("Production-wet count", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-wet count">Wet count</option>
                        <option <?php if (in_array("Production-new mix design", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-new mix design">New mix design</option>
                        <option <?php if (in_array("Production-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit">Editing</option>
                        
                        <option disabled="disabled" style="font-weight:700; color:#000">Orders</option>
                        <option <?php if (in_array("Orders-new order", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-new order">New order</option>
                        <option <?php if (in_array("Orders-order deliver", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-order deliver">Deliver order</option>
                        <option <?php if (in_array("Orders-new account", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-new account">New account</option>
                        <option <?php if (in_array("Orders-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-edit">Edit</option>
                        <option <?php if (in_array("Orders-order edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-order edit">Edit order</option>
                        <option <?php if (in_array("Orders-order cancel", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-order cancel">Cancel order</option>
                        <option <?php if (in_array("Orders-edit delivery date", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-edit delivery date">Edit delivery date</option>
                        <option <?php if (in_array("Orders-account edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Orders-account edit">Edit account</option>
                                                                        
                        <option disabled="disabled" style="font-weight:700; color:#000">Stores</option>
                        <option <?php if (in_array("Stores-main", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-main">Main page</option>
                        <option <?php if (in_array("Stores-new item", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-new item">New store item</option>
                        <option <?php if (in_array("Stores-new vehicle", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-new vehicle">New vehicle</option>
                        <option <?php if (in_array("Stores-receive", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-receive">Receive</option>
                        <option <?php if (in_array("Stores-order deliver", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-order deliver">Deliver order</option>
                        <option <?php if (in_array("Stores-order return", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-order return">Return order</option>
                        
                        <option <?php if (in_array("Stores-supply", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-supply">Supply</option>
                        
                        <option <?php if (in_array("Stores-employee supply", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-employee supply">Employee supply</option>
                        <option <?php if (in_array("Stores-employee return", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-employee return">Employee return</option>
                        <option <?php if (in_array("Stores-department supply", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-department supply">Department supply</option>
                        <option <?php if (in_array("Stores-department return", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-department return">Department return</option>
                        <option <?php if (in_array("Stores-fuel", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-fuel">Fuel supply</option>
                        <option <?php if (in_array("Stores-edit fuel", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-edit fuel">Edit fuel receipt</option>
                        
                        <option <?php if (in_array("Stores-stock take", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-stock take">Stock take</option>
                        <option <?php if (in_array("Stores-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-edit">Editing</option>
                        <option <?php if (in_array("Stores-edit vehicle", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-edit vehicle">Edit vehicle</option>
                        <option <?php if (in_array("Stores-print-copy", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-print-copy">Print copy</option>
                        <option <?php if (in_array("Stores-pallet return", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-pallet return">Pallet return</option>
                        
                        <option disabled="disabled" style="font-weight:700; color:#000">Quality control</option>
                        <option <?php if (in_array("Quality-main", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quality-main">Main page</option>
                        <option <?php if (in_array("Quality-wet inspection", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quality-wet inspection">Wet tile inspection</option>
                        <option <?php if (in_array("Quality-dry inspection", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quality-dry inspection">Dry tile inspection</option>
                        <option <?php if (in_array("Quality-approve", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quality-approve">Approve reports</option>
                        <option <?php if (in_array("Quality-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quality-edit">Quality edit</option>
                                               
                        </select></td>
                      </tr>
                    <tr valign="top">
                      <td height="30" align="center"><p>
                        <input name="set_button" type="submit" class="imagebutton" id="set_button" value="Set" />
                        </p>
                        <p>
                          <input name="remove_button" type="submit" class="imagebutton" id="remove_button" value="Unset" />
                          </p></td>
                      </tr>
                    <tr valign="top">
                      <td align="center"><div id="message"><?php echo $message ?></div></td>
                      </tr>
                    <tr valign="top">
                      <td align="center"><br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br />
                        <br /></td>
                    </tr>
                    </table>
                  <br />
                  
                  
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <input name="user_select" type="hidden" id="user_select" value="<?php echo $user_access_id ?>" />
                  <input name="set" type="hidden" id="set" value="" />
                  </form>
                <script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("set_form");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("page_select","req","- permission to set.");
</script>
</div>
</div>
</div>
</div>



<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else {
    $_SESSION['denied_return'] = "admin_module/user_list.php";	
    include ("../navigation/admin_sidebar.php");?>
	<body onLoad="accessdenied(); return false">	
	</body>
<?php }
?>

</html>