<?php
	include('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_arlstats.php'); 
      
  //  if (!isset($_SERVER['PHP_AUTH_USER'])) 
	//{
	//    header('WWW-Authenticate: Basic realm="libitdb.lang.uc.edu"');
	 //   header('HTTP/1.0 401 Unauthorized');
	//    echo '<a href='http://libitdb.lang.uc.edu/uclit/default.php">Main - Home</a>';
	//    exit();
	//}
	//else
	{
		//format date 2003-05-06 13:20:59
		$datetimenow = date('Y-m-d')." ".strftime("%H:%M:%S");
		    	    
		if(!isset($_POST['type']))
	    {
	    	//page was not called from another page
	    	@header("Location: http://ul.uc.edu/uclit/protected/itcoordinator/show_allemployee.php");
	    	echo "<a href='http://ul.uc.edu/uclit/protected/itcoordinator/show_allemployee.php'>View All Employees</a>";
	    	exit;    	
	    }
	    else 
	    {
	    	$statid = $_POST['statid'];			
	    	$type = $_POST['type'];
	    	$caltype = $_POST['caltype'];
		$usertype = $_POST['usertype'];
		$cattype = $_POST['cattype'];
		$loctype = $_POST['loctype'];
		$mattype = $_POST['mattype'];
		$stype = $_POST['stype'];
            $value = $_POST['value'];
			
	    	  	
	    	//select database to be used equipment database
		$db = mysql_select_db('arlstats', $arlConn) or die ("Unable to select uclit_db database");
			
		if($_POST['type'] == 'edit')
			{
			//sql statement to track user activity		    
		    	$sql_viewed = "VIEWED stat_id='$statid',  type='$type',  caltype='$caltype',  loctype='$loctype',  mattype='$mattype',  stype='$stype', value='$value' ";
		    	
	       	//update sql statement
		    	$sql = "UPDATE Current_Stats SET QID='$caltype', UID='$usertype',  CID='$cattype',  LID='$loctype', MID='$mattype', SID='$stype', Value='$value'WHERE Stat_ID='$statid' ";
		    	
		    	//exercute sql statement
			$sql_result = mysql_query($sql, $arlConn) or die ("Couldn't exercute query<p>$sql</p>");
		    	
				if(!mysql_errno())
				{
					//log user access
					$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql_viewed', '$datetimenow')";
					mysql_query($sql_useraccess, $equipConn);
					$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql', '$datetimenow')";
					mysql_query($sql_useraccess, $equipConn);
					
					$h2 = "Statistical Record [ '$pid' ] has been successfully updated.";
				}
		    	
		    }
		    elseif($_POST['uaction'] == 'delete')
		    {
		    	$sql = "UPDATE personnel set removed='1' WHERE personnel_id='$pid'";
		    	$sql_viewed = "VIEWED    personnel_id='$pid',  lname='$lname',  fname='$fname',  mname='$mname',  dept_id='$dept_id',  mail_location='$mail_location', email='$email', phone='$phone', fax='$fax', web_role='$web_role'  ";
		    	
		    	$sql_result = mysql_query($sql, $uclit_dbConn) or die ("Couldn't execute query:<p>$sql</P>");
		    	
				$sql_dept = "SELECT dept_id, dept_name FROM departments WHERE dept_id = '$dept_id'";
				$dept_result = mysql_query($sql_dept, $uclit_dbConn) or die ("Unable to exercute query. <p>$sql_dept</p>");
				while ($var = mysql_fetch_array($dept_result)) 
				{
					$deptname = $var['dept_name'];
				}
				
				if(!mysql_errno())
		    	{
		    		//log user access
		    		$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql_viewed', '$datetimenow')";
					mysql_query($sql_useraccess, $equipConn);
					$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql', '$datetimenow')";
					mysql_query($sql_useraccess, $equipConn);
					
		    		$h2 = "Personnel ID[ '$pid' ] has been successfully deleted.";
		    	}//end if for mysql_errno()
		    	
		    }
	    }
    }
	echo "
    <h2><i>$h2</i></h2>
	<a href='http://ul.uc.edu/uclit/protected/personnel/show_addemployee.php'>Add New Employee</a>
	&nbsp;&nbsp;|&nbsp;&nbsp<a href='./show_employee_dept.php'>Select Staff by Department</a>

	<br><br>
    <div align='bottom' class='footer'>";
     $last_modified = filemtime("process_employeeud.php");
     print("Last Modified ");
     print(date("m/j/y", $last_modified));
     echo "</div>";
	 
	$mail_to = "erma.fritsche@uc.edu";
	$mail_subj = "An employee has been deleted from the personnel database.";
	$mail_text = "Erma,
	$suser deleted employee $fname $mname $lname from the personnel database in department $deptname with 
	a title of $title at $datetimenow.";
	$mail_text2 = "Erma,	$suser updated employee $fname $mname $lname from the personnel database in department $deptname with 
	a title of $title at $datetimenow.";
	$mail_header = "From: libhelp@uc.edu\r\n";
	$mail_header .= "X-Mailer: server\r\n";
	$mail_header .= "Reply To: libhelp@uc.edu\r\n";
	$sendmail_param = "libhelp@uc.edu";
	if($_POST['uaction'] == 'edit') // email if staff is edited
		mail("$mail_to","$mail_subj","$mail_text2","$mail_header","-f$sendmail_param");
	else //email if staff is deleted
		mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
	 
include('/var/www/html/uclit/protected/common/css_login/css_foot2.php');   
?>