<?
if(!defined('IN_BANDTOPIA')) {
	exit ;
}

class UserObj {

	/*************************************************************/
	/* List Users
	 /*************************************************************/
	public function UserList() {
		$query = <<<EOD
			SELECT
				*
			FROM
				Users AS u 
				LEFT JOIN
					UserTypes AS ut ON
						ut.UserTypeId = u.UserTypeId
EOD;
		if(isset($this->AccountId))
		{
			$query .= " WHERE AccountId = {$this->AccountId}";
		}
		$query .= " ORDER BY u.LastName, u.FirstName";

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$userArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new UserObj;
			$tempObj -> UserId = $row['UserId'];
			$tempObj -> UserName = $row['UserName'];
			$tempObj -> UserFullName = $row['FirstName'].' '.$row['LastName'];
			$tempObj -> UserTypeId = $row['UserTypeId'];
			$tempObj -> FirstName = $row['FirstName'];
			$tempObj -> LastName = $row['LastName'];
			$tempObj -> AccountAdmin = $row['AccountAdmin'];
			$tempObj -> ProfileId = $row['ProfileId'];

			$userArray[] = $tempObj;
		}
		return $userArray;
	}

	/*************************************************************/
	/* Get User
	 /*************************************************************/
	public function GetUser() {
		$query = <<<EOD
			SELECT
				u.*, a.AccountName
			FROM
				Users AS u
				INNER JOIN
					UserTypes AS ut ON
						ut.UserTypeId = u.UserTypeId
				LEFT JOIN
					Accounts AS a ON
						a.AccountId = u.AccountId
			WHERE
EOD;
		if($this -> UserId != '') {
			$query .= " u.UserId = {$this->UserId}";
		} 
		else
		{
			$query .= " u.UserName = '{$this->UserName}'";
		}

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$row = mysql_fetch_assoc($result);
		$this -> UserId = $row['UserId'];
		$this -> UserName = $row['UserName'];
		$this -> UserFullName = $row['FirstName'].' '.$row['LastName'];
		$this -> FirstName = $row['FirstName'];
		$this -> MI = $row['MI'];
		$this -> LastName = $row['LastName'];
		$this -> Email = $row['Email'];
		$this -> UserTypeId = $row['UserTypeId'];
		$this -> UserActive = $row['UserActive'];
		$this -> AccountId = $row['AccountId'];
		$this -> AccountAdmin = $row['AccountAdmin'];
		$this -> AccountName = $row['AccountName'];
		$this -> ProfileId = $row['ProfileId'];
	}

	/*************************************************************/
	/* Verify Password
	 /*************************************************************/
	public function VerifyPassword($userPass) {
		$crypt = new cryptRoutineObj();
		$query = <<<EOD
			SELECT
				Password
			FROM 
				Users 
			WHERE
				UserName = '{$this->UserName}'
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$rows = mysql_fetch_assoc($result);
		if(mysql_num_rows($result) > 0 && $userPass == $crypt->DecryptPass($rows['Password'])) {
			return true;
		} else {
			return false;
		}
	}

	/*************************************************************/
	/* Update User
	 /*************************************************************/
	public function UpdateUser() {
		if($this -> UserId != '') {
			$query = <<<EOD
				UPDATE
					users
				SET
					UserName = '{$this->UserName}',
					UserActive = {$this->UserActive},
					FirstName = '{$this->FirstName}',
					LastName = '{$this->LastName}',
					MI = '{$this->MI}',
					Email = '{$this->Email}',
					UserTypeId = {$this->UserTypeId},
					AccountId = {$this->AccountId},
					AccountAdmin = {$this->AccountAdmin},
					ProfileId = {$this->Profileid}
				WHERE
					UserId = {$this->UserId}
EOD;
			$result = mysql_query($query);
			$userMsg = "User information has successfully updated.";
		} else {
			$query = <<<EOD
				INSERT INTO
					Users
					(UserName, 
					UserActive,
					FirstName,
					LastName,
					MI,
					Email,
					UserTypeId,
					AccountId,
					AccountAdmin,
					ProfileId)
				VALUES
					('{$this->UserName}',
					{$this->UserActive},
					'{$this->FirstName}',
					'{$this->LastName}',
					'{$this->MI}',
					'{$this->Email}',
					{$this->UserTypeId},
					{$this->AccountId},
					{$this->AccountAdmin},
					{$this->ProfileId})
EOD;

			$result = mysql_query($query) or die(mysql_error()."<br>".$query);
			$this -> UserId = mysql_insert_id();
			$userMsg = "User has been added successfully.";
		}
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}

	/*************************************************************/
	/* Change Password
	 /*************************************************************/
	public function ChangePassword() {
		$query = <<<EOD
			UPDATE
				Users
			SET
				Password = '{$this->Password}'
			WHERE
				UserId = {$this->UserId}
EOD;
		$userMsg = "Password has successfully updated.";
		$result = mysql_query($query) or die(mysql_error().'<br>'.$query);
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}

	/*************************************************************/
	/* Delete(Deactivate) User
	 /*************************************************************/
	public function delUser() {
		$query = <<<EOD
			UPDATE
				users
			SET
				user_active = 0
			WHERE
				UserId = {$this->int_UserId}
EOD;
		$userMsg = "User has successfully been removed.  If this was done in error, please contact your website administrator.";
		$result = mysql_query($query);
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}
}
?>