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

class UserObj {
	private $int_UserId;
	private $int_UserName;
	private $int_Password;
	private $int_UserFullName;
	private $int_FirstName;
	private $int_LastName;
	private $int_user_type_id;
	private $int_user_info_id;
	private $int_user_active;
	private $int_user_type_title;
	private $int_info_name;
	private $int_UserTypeId;

	/*************************************************************/
	/* List Users
	 /*************************************************************/
	public function list_users() {
		$query = <<<EOD
			SELECT
				*
			FROM
				Users AS u 
				LEFT JOIN
					UserTypes AS ut ON
						ut.UserTypeId = u.UserTypeId
			WHERE
				UserActive = 1
			ORDER BY
				u.UserFullName
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$userArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new UserObj;
			$tempObj -> set_UserId($row['UserId']);
			$tempObj -> set_UserName($row['UserName']);
			$tempObj -> set_UserFullName($row['FirstName'].' '.$row['LastName']);
			$tempObj -> set_user_type_id($row['user_type_id']);
			$tempObj -> set_user_info_id($row['user_info_id']);
			$tempObj -> set_user_active($row['user_active']);
			$tempObj -> set_user_type_title(ucfirst(strtolower($row['user_type_title'])));
			$tempObj -> set_UserTypeId($row['UserTypeId']);

			if($$row['user_info_id'] > 0) {
				$query = <<<EOD
					SELECT
						{$row['user_type_title']}_id,
						{$row['user_type_title']}_name
					FROM 
						{$row['user_type_title']}
					WHERE
						{$row['user_type_title']}_id = {$row['user_info_id']}
EOD;
				$result = mysql_query($query) or die(mysql_error() . "<br>$query");
				$row2 = mysql_fetch_assoc($result);
				$tempObj -> set_info_name($row2[$row['user_type_title'] . '_name']);
			}

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

	/*************************************************************/
	/* Get User
	 /*************************************************************/
	public function getUser() {
		$query = <<<EOD
			SELECT
				u.*
			FROM
				Users AS u
				INNER JOIN
					UserTypes AS ut ON
						ut.UserTypeId = u.UserTypeId
			WHERE
EOD;
		if($this -> int_UserId != '') {
			$query .= " tu.UserTableId = {$this->int_UserId}";
		} elseif($this -> int_UserName != '') {
			$query .= " u.UserName = '{$this->int_UserName}'";
		} else {
			exit ;
		}
		$query .= " AND u.UserActive = 1";
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$row = mysql_fetch_assoc($result);
		$this -> set_UserId($row['UserId']);
		$this -> set_UserName($row['UserName']);
		//$this -> set_Password($row['Password']);
		$this -> set_UserFullName($row['FirstName'].' '.$row['LastName']);
		$this -> set_UserTypeId($row['UserTypeId']);
		$this -> set_user_info_id($row['user_info_id']);
		$this -> set_user_active($row['UserActive']);
		$this -> set_user_type_title(ucfirst(strtolower($row['user_type_title'])));

		if($row['user_info_id'] > 0) {
			$query = <<<EOD
				SELECT
					{$row['user_type_title']}_id,
					{$row['user_type_title']}_name
				FROM 
					{$row['user_type_title']}
				WHERE
					{$row['user_type_title']}_id = {$row['user_info_id']}
EOD;
			$result = mysql_query($query) or die(mysql_error() . "<br>$query");
			$row2 = mysql_fetch_assoc($result);
			$this -> set_info_name($row2[$row['user_type_title'] . '_name']);
		}
	}

	/*************************************************************/
	/* Verify Password
	 /*************************************************************/
	public function verifyPassword($userPass) {
		$crypt = new cryptRoutineObj();
		$query = <<<EOD
			SELECT
				Password
			FROM 
				Users 
			WHERE
				UserName = '{$this->int_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 -> int_UserId != '') {
			$query = <<<EOD
				UPDATE
					users
				SET
					UserName = '{$this->int_UserName}',
					UserFullName = '{$this->int_UserFullName}',
					user_type_id = {$this->int_user_type_id},
					user_info_id = {$this->int_user_info_id}
				WHERE
					UserId = {$this->int_UserId}
EOD;
			$result = mysql_query($query);
			$userMsg = "User information has successfully updated.";
		} else {
			$crypt = new cryptRoutineObj();
			$password = $crypt->EncryptPass($this->int_Password);
			$query = <<<EOD
				INSERT INTO
					Users
					(UserName, 
					Password,
					UserActive,
					FirstName,
					LastName,
					MI,
					UserTypeId)
				VALUES
					('{$this->int_UserName}',
					'$password',
					1,
					'{$this->int_FirstName}',
					'{$this->int_LastName}',
					'',
					3)
EOD;
			$result = mysql_query($query);
			$this -> int_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 updatePass() {
		$query = <<<EOD
			UPDATE
				users
			SET
				Password = '{$this->int_Password}'
			WHERE
				UserId = {$this->int_UserId}
EOD;
		$userMsg = "Password has successfully updated.";
		$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;
	}

	/*************************************************************/
	/* 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;
	}

	// Get User Table Id
	public function get_UserId() {
		return $this -> int_UserId;
	}

	// Set User Table Id
	public function set_UserId($value) {
		$this -> int_UserId = $value;
	}

	// Get UserName
	public function get_UserName() {
		return $this -> int_UserName;
	}

	// Set UserName
	public function set_UserName($value) {
		$this -> int_UserName = $value;
	}

	// Get Password
	public function get_Password() {
		return $this -> int_Password;
	}

	// Set Password
	public function set_Password($value) {
		$this -> int_Password = $value;
	}

	// Get Full Name
	public function get_UserFullName() {
		return $this -> int_UserFullName;
	}

	// Set Full Name
	public function set_UserFullName($value) {
		$this -> int_UserFullName = $value;
	}
	
	// Get First Name
	public function get_FirstName() {
		return $this -> int_FirstName;
	}

	// Set First Name
	public function set_FirstName($value) {
		$this -> int_FirstName = $value;
	}
	
	// Get Last Name
	public function get_LastName() {
		return $this -> int_LastName;
	}

	// Set Last Name
	public function set_LastName($value) {
		$this -> int_LastName = $value;
	}

	// Get User Type Id
	public function get_user_type_id() {
		return $this -> int_user_type_id;
	}

	// Set User Type Id
	public function set_user_type_id($value) {
		if($value == '') {
			$value = 0;
		}
		$this -> int_user_type_id = $value;
	}

	// Get User Info Id
	public function get_user_info_id() {
		return $this -> int_user_info_id;
	}

	// Set User Info Id
	public function set_user_info_id($value) {
		if($value == '') {
			$value = 0;
		}
		$this -> int_user_info_id = $value;
	}

	// Get User Active
	public function get_user_active() {
		return $this -> int_user_active;
	}

	// Set User Info Id
	public function set_user_active($value) {
		if($value == '') {
			$value = 0;
		}
		$this -> int_user_active = $value;
	}

	// Get User Type Title
	public function get_user_type_title() {
		return $this -> int_user_type_title;
	}

	// Set User Type Title
	public function set_user_type_title($value) {
		$this -> int_user_type_title = $value;
	}

	// Get Artist Id
	public function get_artist_id() {
		return $this -> int_artist_id;
	}

	// Get Info Name
	public function get_info_name() {
		return $this -> int_info_name;
	}

	// Set Artist Name
	public function set_info_name($value) {
		$this -> int_info_name = $value;
	}
	
	// Get Info Name
	public function get_UserTypeId() {
		return $this -> int_UserTypeId;
	}

	// Set Artist Name
	public function set_UserTypeId($value) {
		$this -> int_UserTypeId = $value;
	}

}
?>