<?php
 // Include the class:

require_once("../include/constants.php");
require_once("../include/db.class.php"); 

     
 class user
{
     
     //This functions returns a data set with all users
     function showAllusers($pageNum=NULL, $limit=NULL)
     {
        
        // Open the database (construct the object):
        $db = new DB();
        
        if(!is_null($pageNum) && !is_null($limit))
        {
           $queryResult= $db->query("SELECT adminID, firstName, lastName, email, username, phone FROM
		   							 Administrator  where userLevel='2'
                                    ORDER BY adminID ASC LIMIT ".(($pageNum-1)*$limit).", $limit");  
        }
        else
        {
            $queryResult= $db->query("SELECT firstName, lastName, email, username FROM
		   							 Administrator where userLevel='2' 
                                    ORDER BY adminID ASC");
        }
        return $queryResult;
     
     }

     //This function returns a data row for the given user
     function queryUser($adminID)
     {
     
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM Administrator
                                    WHERE adminID='$adminID'");
        $result=$db->fetchNextObject($queryResult); 
        return $result;
     
     
     }

      //This function adds a user to the database
     function addUser($firstName,$lastName,$adminComments, $email, $phone, $backupPhone, $backupEmail, $username, $password, $userLevel)
     {
         $firstName = mysql_real_escape_string($firstName);
         $lastName = mysql_real_escape_string($lastName); 
         $adminComments = mysql_real_escape_string($adminComments);
		 $username= mysql_real_escape_string($username); 
		 if($password==null){
         	return false;
		 }else{
		 	$password = md5($password);
		 }
         
         $db = new DB();
           
         $sql1="INSERT INTO Administrator (firstName,lastName,adminComments,email, phone, backupPhone, backupEmail, username, password, userLevel)
                VALUES ('$firstName','$lastName','$adminComments','$email','$phone','$backupPhone','$backupEmail','$username','$password','$userLevel')";
          
         $queryResult1= $db->execute($sql1);
         return $queryResult1;   

     }
     //This function updates user info in the database
     function updateUser($adminID,$firstName,$lastName,$adminComments, $email, $phone, $backupPhone, $backupEmail, $username, $password)
     {
	 	 if(strlen(trim($password))==0 || is_null($password)){
		 	$s="UPDATE Administrator SET firstName='%s',lastName='%s',adminComments='%s',email='%s',phone='%s',backupPhone='%s',
			backupEmail='%s',username='%s' WHERE adminID='$adminID'";
			$sql=sprintf($s,
                get_magic_quotes_gpc() ? $firstName : addslashes($firstName),
                get_magic_quotes_gpc() ?$lastName : addslashes($lastName),
				get_magic_quotes_gpc() ?$adminComments : addslashes($adminComments),
				get_magic_quotes_gpc() ?$email : addslashes($email),
				get_magic_quotes_gpc() ?$phone : addslashes($phone),
				get_magic_quotes_gpc() ?$backupPhone : addslashes($backupPhone),
				get_magic_quotes_gpc() ?$backupEmail : addslashes($backupEmail),
				get_magic_quotes_gpc() ?$username : addslashes($username));
		 }else{
		 	$s="UPDATE Administrator SET firstName='%s',lastName='%s',adminComments='%s',email='%s',phone='%s',backupPhone='%s',
			backupEmail='%s',username='%s',password='%s' WHERE adminID='$adminID'";
		 	$password = md5($password);
			$sql=sprintf($s,
                get_magic_quotes_gpc() ? $firstName : addslashes($firstName),
                get_magic_quotes_gpc() ?$lastName : addslashes($lastName),
				get_magic_quotes_gpc() ?$adminComments : addslashes($adminComments),
				get_magic_quotes_gpc() ?$email : addslashes($email),
				get_magic_quotes_gpc() ?$phone : addslashes($phone),
				get_magic_quotes_gpc() ?$backupPhone : addslashes($backupPhone),
				get_magic_quotes_gpc() ?$backupEmail : addslashes($backupEmail),
				get_magic_quotes_gpc() ?$username : addslashes($username),
				get_magic_quotes_gpc() ? $password : addslashes($password));
		 }

                
                
            $db = new DB();
                
            $queryResult= $db->execute($sql);
             
            return $queryResult;
         
     }
     //------------------------------------------------------------------------------------------
       //This function deletes a user from the database
       //return true if successfull, return message if failed 
     function deleteUser($userID)
     {
     
        $db = new DB();

         $queryResult=$db->execute("DELETE FROM Administrator WHERE adminID='$userID'");   
        return $queryResult;
     
     }
     //This function checks to see if a user with this name exists in the system.
     //if a user with this name exists, return true; else false;
     function userEmailDuplicateCheck($email)
     {
         $db = new DB(); 
        if(!get_magic_quotes_gpc()){
            $email = addslashes($email);
        }
        $q = "SELECT adminID FROM Administrator WHERE email='$email'";
        $result = $db->query($q);
        return (mysql_numrows($result) > 0);
     }
     function userNameDuplicateCheck($name)
     {
         $db = new DB(); 
        if(!get_magic_quotes_gpc()){
            $name = addslashes($name);
        }
        $q = "SELECT adminID FROM Administrator WHERE username='$name'";
        $result = $db->query($q);
        return (mysql_numrows($result) > 0);
     }
   function displayPageNav($totalRows,$limit,$pageNum,$pageAddr)
    {

        if (($totalRows%$limit)<>0) {
            $pmax=floor($totalRows/$limit)+1;  // Divide to total result by the number of query you want
        // to display per page($limit) and create a Max page
        } else {
            $pmax=floor($totalRows/$limit);
        }
        if($pmax==1)  //if there is only 1 page, no need to display page number
            return;
            
        echo "<div class='navpage'>"; 

        // We need to create a Previous page, so we need $pageNum to be bigger than 1, otherwise at the first page we would get a 0.
        //For the previous to show up we need at least to be at the second page.
        if($pageNum > 1) {
            $prevp="<a href='".$pageAddr."?pageNum=".($pageNum-1)."' title='Previous Page'>Previous Page</a>&nbsp;&nbsp;"; 
            echo $prevp;
        } else {echo "";}   

        // Here We want create a page from the results we got by dividing the total by the the limit. So let say you got
        //45 results and you want 5 results per page; these simple lines will create you 8 pages.
        $pid=1;
        while ($pid<=$pmax) {
            $paging= "<a href='".$pageAddr."?pageNum=$pid' title='Page $pid of $pmax'>$pid</a>&nbsp;&nbsp;";
        //So here, let say we are at the 3rd page and we want the 3 to be blank so the user can know where is he now.
        //This will act as Current Page! We need to replace the url by a text
            if($pid==$pageNum)
                echo "<span>$pageNum</span>&nbsp;&nbsp;";
            else 
                echo $paging; 
            $pid++;  // create pages until reach the result
        } 
        //We want to create a next page and a last page, $pageNum have to be less than $pmax.
        if($pageNum < $pmax) {
            $nextp="<a href='".$pageAddr."?pageNum=".($pageNum+1)."' title='Next Page'>Next Page</a>&nbsp;&nbsp;"; 
            echo $nextp;
        } else {echo "";}
        if($pageNum != $pmax) {      
            echo  "<a href='".$pageAddr."?pageNum=$pmax' title='Last Page'>Last Page</a>"; }
        
        echo "</div>";
    }
}
?>
