<? 
/*
Author		: Sue
Version		: 1.01
Date			: 25th March 2002
Function		: User Listing
*/ 
session_start();
require ("auth.php");
unset($msg);
if ($Transmarco["accesslevel"] < $adminlevel) {
	authfail();
} else 	{ 

####################################################
# Deactivate a User's Access                       #
# Updating database if admin decides to Deactivate #
####################################################
if ($action == 'Deactivate') {
	$sql = "update userdetail set active='no' where userid=$id";
	db_query($sql);
	$msg = "<br><b>User Deactivated</b><br>";
}

##################################################
# Activate a User's Access                       #
# Updating database if admin decides to Activate #
##################################################
if ($action == 'Activate') {
	$sql = "update userdetail set active='yes' where userid=$id";
	db_query($sql);
	$msg = "<br><b>User Activated</b><br>";
}

##################################################
# Delete a User                                  #
# Delete selected user from database             #
##################################################
if ($action == 'Delete') {
	$sql = "delete from usersession where userid = $id";
	db_query($sql);
	$sql = "delete from userdetail where userid = $id";
	db_query($sql);
	$msg = "<br><b>User Deleted</b><br>";
}

###################################################
# Edit User's Email and Password                  #
# Inserting into database if user decides to Edit #
###################################################
if (($action == 'Edit') && ($submitted == 'yes')) {

	// Do edit form check
	if($newpass !="" || $confirmpass !=""){
	if ($newpass == $confirmpass) {
		if (strlen($newpass) < 6) {
			$error=1;
			$errmsg.= "*Password length is too short<br>&nbsp;(must be longer than 6 characters.)<br>";
		}
	} else {
		$error=1;
		$errmsg.= "*New Password does not match.<br>";
		}
	}

	if ($email != "") {
		if(!isValidEmail($email)){
			$error=1;
			$errmsg.= "*Your Email Address is not valid<br>";
		}
	} else {
		$error=1;
		$errmsg.= "*Email must be filled<br>";			
	}

	//NO errors write the data to database`

	if ($error!=1){
		$password = md5($newpass);
		$sql = "update usersession set password='$password' where userid=$id";
		db_query($sql);

		$sql = "update userdetail set email='$email' where userid=$id";
		db_query($sql);

		$errmsg = "<b>Update Successful!<br></b>";		
	}
}
?>
<html>
<? include($style); ?>
<body>
<p>&nbsp;</p>
<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
  <tr valign="top"> 
    <td align="center"><font class="sub"><b>List of Users</b></font><br><? echo $msg?></td>
  </tr>
</table>
<table class="bigbox" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr> 
    <td width="16" height="16" valign="top" align="left"><img src="<? echo $images ?>a.gif" width="16" height="18"></td>
    <td width="363"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
    <td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
  </tr>
  <tr> 
    <td width="16">&nbsp;</td>
    <td valign="top" class="bgline"> 
      <table cellpadding="5" cellspacing="1" border="0" align="center">
        <tr class="highlights"> 
          <td><b>No</b></td>
          <td><b>User Login</b></td>
          <td><b>First Name</b></td>
          <td><b>Surname</b></td>
          <td><b>Email</b></td>
          <td><b>Date Created</b></td>
          <td><b>Action</b></td>
        </tr>
        <?
		//get details from usersession table
		$sql = "select * from usersession,userdetail where usersession.userid = userdetail.userid";
		$result = db_query($sql);
		$counter = 1;
		for ($x = 0;$x < db_numrows($result);$x++) {
			$row = db_fetch_array($result,1);
			$userid = $row["userid"];
			$login = $row["login"]; 
			$firstname = $row["firstname"]; 
			$surname = $row["surname"]; 
			$email = $row["email"]; 
			$datecreated = $row["acctcreated"];
			$active = $row["active"];
		?>
        <?$blank="&nbsp"?>
        <tr class="tdfill"> 
          <td> 
            <?=$counter?>
          </td>
          <td> 
            <? echo $login ?>
          </td>
          <td> 
            <? echo $firstname ?>
          </td>
          <td> 
            <? if ($surname == "") {
				echo $blank;
				} else {
				echo $surname;
				}
				?>
          </td>
          <td> 
            <? if ($email == "") {
				echo $blank;
				} else {
				echo $email;
				}
				?>
          </td>
          <td> 
            <? echo $datecreated ?>
          </td>
          <td> 
			<a href="<? echo $docroot ?>/<? echo $userlist ?><? echo "?action=Edit&id=" . $userid ?>">Edit</a>
			<? if ($firstname != "administrator") {
				if ($active == "yes") {
					echo "<a href=" . $docroot . "/" . $userlist . "?action=Deactivate&id=" . $userid . ">Deactivate</a>&nbsp;";
				} else {
					echo "<a href=" . $docroot . "/" . $userlist . "?action=Activate&id=" . $userid . ">Activate</a>&nbsp;";
				}
				echo "<a href=" . $docroot . "/" . $userlist . "?action=Delete&id=" . $userid . ">Delete</a>";
			}
			?>
          </td>
        </tr>
		<? $counter++;
		} ?>
      </table>
    </td>
    <td width="17" align="right">&nbsp;</td>
  </tr>
  <tr> 
    <td width="16" valign="bottom" align="left"><img src="<? echo $images ?>c.gif" width="16" height="18"></td>
    <td width="363">&nbsp;</td>
    <td width="17" align="right" valign="bottom"><img src="<? echo $images ?>d.gif" width="16" height="16"></td>
  </tr>
</table>

<? 	
##############
# EDIT TABLE #
##############

	//Display form below if edit
	if ($action == 'Edit'){ 
	//Prefilling the fields in the form with values from db
	if ($action=='Edit') {
		$sql = "select * from userdetail where userid = $id";
		$result = db_query($sql);
		for($x=0; $x<db_numrows($result); $x++){
			$row = db_fetch_array($result,1);
			$userid = $row["userid"]; 
			$email = $row["email"]; 
		}
	}
?>

<form method=POST action="<? echo $PHP_SELF ?>">
  <table width="360" border="0" cellspacing="0" cellpadding="8" align="center">
    <tr valign="top"> 
      <td><font class="sub"><b>Edit User Info</b></font></td>
    </tr>
  </table>
  <table class="bigbox" border="0" cellspacing="0" cellpadding="0" align="center" width="360">
    <tr> 
      <td width="16" height="16" valign="top" align="left"><img src="<? echo $images ?>a.gif" width="16" height="18"></td>
      <td width="363"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
      <td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
    </tr>
    <tr> 
      <td width="16">&nbsp;</td>
      <td valign="top" class="bgline"> 
        <table cellpadding="5" cellspacing="1" border="0" align="center" width="100%">
          <tr> 
            <td width="47%" class="highlights"><b>New Password</b></td>
            <td width="53%" class="tdfill"> 
              <input type="password" name="newpass" size="20" maxlength="12">
              * </td>
          </tr>
          <tr> 
            <td width="47%" class="highlights"><b>Reconfirm Password</b></td>
            <td width="53%" class="tdfill"> 
              <input type="password" name="confirmpass" size="20" maxlength="12">
              * </td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr> 
            <td class="highlights">*Password must be longer than 6 characters. 
            </td>
          </tr>
        </table>
        <table cellpadding="5" cellspacing="1" border="0" align="center" width="100%">
          <tr> 
            <td width="47%" class="highlights"><b>Email</b></td>
            <td width="53%" class="tdfill"> 
              <input type="text" name="email" size="20" maxlength="64" value="<? echo $email ?>">
            </td>
          </tr>
        </table>
      </td>
      <td width="17" align="right">&nbsp;</td>
    </tr>
    <tr> 
      <td width="16" valign="bottom" align="left"><img src="<? echo $images ?>c.gif" width="16" height="18"></td>
      <td width="363">&nbsp;</td>
      <td width="17" align="right" valign="bottom"><img src="<? echo $images ?>d.gif" width="16" height="16"></td>
    </tr>
  </table>
  <table width="360" border="0" cellspacing="0" cellpadding="14" align="center">
    <tr valign="top"> 
      <td width="249"> 
        <p> 
          <?if(isset($error)){?>
          <b>ERROR:</b><br>
          <?}?>
          <? if(isset($errmsg)) { ?>
          <? echo $errmsg; ?>
        <? } ?>
      </td>
      <td align="right" width="55">
		<input type="hidden" name="submitted" value="yes">
		<? if ($action == "Edit") { ?>
		<input type="hidden" name=id value=<? echo $id ?>>
		<? } ?>
        <input type="submit" name="action" value="Edit">
      </td>
    </tr>
  </table>
  </form>
	<? } ?>
</body>
</html>
<? } ?>
