<!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");

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

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 = '". $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_small.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 ('" . $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 = '".$access_id."' AND page_name ='".$page_name."'";
	$del_result = execsql($del_query);
    $message= "&nbsp;&nbsp;&nbsp;<img src=../images/error_32.png width=24 height=24/><font color=red><b>&nbsp;&nbsp;&nbsp;Permission was unset.</b></font>";
}

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

$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 = '". $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="";
}

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 = '". $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 WHERE username <> 'system_manager' ";
	$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 ='".$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 type="text/javascript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//-->
</script>
</head>
<?php
if ($permission_num==1) {
	include ("../navigation/admin_sidebar.php");
?>
<body  onload="<?php if ($set==5) { ?> document.getElementById('hidden_frame').style.display = 'none'; <?php } else { ?> document.getElementById('hidden_frame').style.display = 'block'; <?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"><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">
              <input name="user_select" type="hidden" value="<?php echo $access_id ?>" />
              <input name="set" type="hidden" value="3" />
              
              Set home page:
                &nbsp;&nbsp;
              <label>
                            <input type="radio" name="home_page_group" value="hr" id="home_page_group_0" <?php if ($home_page=="hr") { ?> checked="checked" <?php } ?>/>
                            hr</label>
                          
                          <label>
                            <input type="radio" name="home_page_group" value="production" id="home_page_group_1" <?php if ($home_page=="production") { ?> checked="checked" <?php } ?>/>
                            production</label>
                          
                          <label>
                            <input type="radio" name="home_page_group" value="stores" id="home_page_group_2" <?php if ($home_page=="stores") { ?> checked="checked" <?php } ?>/>
                            stores</label>
                          
                          <label>
                            <input type="radio" name="home_page_group" value="sales" id="home_page_group_3" <?php if ($home_page=="sales") { ?> checked="checked" <?php } ?>/>
                            sales</label>
                          
                          <label>
                            <input type="radio" name="home_page_group" value="admin" id="home_page_group_4" <?php if ($home_page=="admin") { ?> checked="checked" <?php } ?>/>
                            admin</label>
                          
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;              
                <input name="home_button" type="submit" class="imagebutton" id="home_button" value="Set home page" />
                
                
                </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>
                        <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-new user", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-new user">New user</option>
                        <option <?php if (in_array("AD-remove user", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-remove user">Remove user</option>
                        <option <?php if (in_array("AD-reset password", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-reset password">Reset password</option>
                        <option <?php if (in_array("AD-permissions", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-permissions">Set permissions</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-tracking", $arrName2)) {?> style="color:#009900;" <?php }?> value="AD-tracking">Tracking</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-timekeeping", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-timekeeping">Time recording </option>
                        <option <?php if (in_array("HR-timekeeping-2", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-timekeeping-2">Delayed time recording</option>
                        <option <?php if (in_array("HR-overtime", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-overtime">Overtime recording</option>
                        <option <?php if (in_array("HR-discipline", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-discipline">Discipline</option>
                        <option <?php if (in_array("HR-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-edit">Editing</option>
                        <option <?php if (in_array("HR-employee", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-employee">Employee list</option>
                        <option <?php if (in_array("HR-wages", $arrName2)) {?> style="color:#009900;" <?php }?> value="HR-wages">Wages list</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-capture", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-capture">Capture job</option>
                        <option <?php if (in_array("Production-cutting", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-cutting">Cutting bill</option>
                        <option <?php if (in_array("Production-produce", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-produce">Truss production</option>
                        <option <?php if (in_array("Production-authorisation", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-authorisation">Authorise job</option>
                        <option <?php if (in_array("Production-edit quote", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit quote">Edit captured quote</option>
                        <option <?php if (in_array("Production-price edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-price edit">Edit cost price</option>
                        <option <?php if (in_array("Production-edit cutting bill", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit cutting bill">Edit cutting bill</option>
                        <option <?php if (in_array("Production-edit authorisation", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit authorisation">Edit authorisation</option>
                        <option <?php if (in_array("Production-edit job details", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit job details">Edit job details</option>
                        <option <?php if (in_array("Production-edit set complete", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-edit set complete">Edit set job complete</option>
                        <option <?php if (in_array("Production-costing", $arrName2)) {?> style="color:#009900;" <?php }?> value="Production-costing">Costing</option>
                        
                        <option disabled="disabled" style="font-weight:700; color:#000">Sales</option>
                        <option <?php if (in_array("Sales-main", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-main">Main page</option>
                        <option <?php if (in_array("Quote-enquiry", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quote-enquiry">Quote enquiry</option>
                        <option <?php if (in_array("Quote-capture", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quote-capture">Quote capture</option>
                        <option <?php if (in_array("Quote-status", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quote-status">Quote status</option>
                        <option <?php if (in_array("Sales-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-edit">Editing</option>
                        <option <?php if (in_array("Quote-edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Quote-edit">Quote edit</option>
                        <option <?php if (in_array("Sales-price edit", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-price edit">Edit sales price</option>
                        <option <?php if (in_array("Sales-cash quote", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-cash quote">Cash quote</option>
                        <option <?php if (in_array("Sales-Cash sales", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-Cash sales">Cash sales</option>
                        <option <?php if (in_array("Sales-discount", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-discount">No limit on discount</option>
                        <option <?php if (in_array("Sales-costing", $arrName2)) {?> style="color:#009900;" <?php }?> value="Sales-costing">Costing report</option>
                        <option <?php if (in_array("Capture-job", $arrName2)) {?> style="color:#009900;" <?php }?> value="Capture-job">Capture job</option>     
                        <option <?php if (in_array("Create-job", $arrName2)) {?> style="color:#009900;" <?php }?> value="Create-job">Create job</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-receive", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-receive">Receive</option>
                        <option <?php if (in_array("Stores-supply", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-supply">Supply</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-print-copy", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-print-copy">Print copy</option>
                        <option <?php if (in_array("Stores-variations", $arrName2)) {?> style="color:#009900;" <?php }?> value="Stores-variations">Delivery variations</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 $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","Please select permission to set.");
</script>
</div>
</div>
</div>
</div>

</body>

<?php include ('../navigation/side_bar.php'); ?>

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