@ -4,44 +4,117 @@ use PDO;
Class UserGateway extends Gateway{
/**
* Gets the total number of users in the Users table.
*
* This method executes a `SELECT COUNT(*)` SQL query to count all the records in the `Users` table.
* It returns the count as an array.
*
* @return array An array containing the total number of users in the `Users` table.
*/
public function getNumberOfUsers() : array
{
// SQL query to count all users in the Users table
$query = "SELECT Count(*) FROM Users";
$this -> co -> executeQuery($query);
// Execute the query
$this->co->executeQuery($query);
return $this -> co -> getResults();
// Return the result of the query
return $this->co->getResults();
}
public function firstIdUser():int{
$query = "Select id_user from Users;";
$this -> co -> executeQuery($query);
$res = $this -> co -> getResults();
foreach($res as $r){
/**
* Finds the first available ID for a new user.
*
* This method fetches all existing user IDs from the `Users` table and
* returns the first integer ID that is not already in use.
*
* @return int The first available user ID.
*/
public function firstIdUser(): int
{
// SQL query to get all user IDs from the Users table
$query = "SELECT id_user FROM Users;";
// Execute the query to fetch all user IDs
$this->co->executeQuery($query);
$res = $this->co->getResults();
// Initialize an empty array to store the user IDs
foreach ($res as $r) {
$tab[] = $r["id_user"];
}
$id=1;
while(in_array($id,$tab)){$id=$id+1;}
// Start checking for the first available ID from 1
$id = 1;
while (in_array($id, $tab)) {
// If the ID already exists, increment and check again
$id = $id + 1;
}
// Return the first available ID
return $id;
}
public function randomImg():int{
$query = "SELECT id_img FROM image ORDER BY Random() LIMIT 1";
/**
* Fetches a random image ID from the `image` table.
*
* This method selects a random row from the `image` table using the SQL
* clause `ORDER BY RANDOM()` and returns the `id_img` of the randomly selected image.
*
* @return int The ID of a random image.
*/
public function randomImg(): int
{
// SQL query to select a random image ID
$query = "SELECT id_img
FROM image
ORDER BY RANDOM()
LIMIT 1";
// Execute the query
$this->co->executeQuery($query);
// Fetch the result
$res = $this->co->getResults();
// Return the ID of the randomly selected image
return $res[0][0];
}
public function insertUser(string $pseudo, string $email, string $password, bool $isAdmin, int $imgPrfl) : bool {
$id=$this->firstIdUser();
$idImg=$this->randomImg();
$query = "
INSERT INTO Users(id_user,username,email,password,creation,img)
VALUES (:id, :pseudo, :email, :password, CURRENT_DATE, :imgPrfl);
";
return $this -> co -> executeQuery($query, [
/**
* Inserts a new user into the `Users` table.
*
* This method first generates a new user ID, retrieves a random image ID,
* and inserts the provided user details (pseudo, email, password, profile image)
* into the `Users` table.
*
* @param string $pseudo The username (pseudo) of the new user.
* @param string $email The email address of the new user.
* @param string $password The password for the new user.
* @param bool $isAdmin Whether the user is an admin (not used in this query).
* @param int $imgPrfl The ID of the profile image for the new user (assigned randomly).
*
* @return bool Whether the insertion was successful.
*/
public function insertUser(string $pseudo, string $email, string $password, bool $isAdmin, int $imgPrfl): bool
{
// Get the next available user ID using the firstIdUser method
$id = $this->firstIdUser();
// Get a random image ID using the randomImg method
$idImg = $this->randomImg();
// SQL query to insert the new user into the Users table
$query = "INSERT INTO Users(id_user, username, email, password, creation, img)
VALUES (:id, :pseudo, :email, :password, CURRENT_DATE, :imgPrfl);";
// Execute the query with the provided data
return $this->co->executeQuery($query, [
":id" => [$id, PDO::PARAM_INT],
":pseudo" => [$pseudo, PDO::PARAM_STR],
":email" => [$email, PDO::PARAM_STR],
@ -50,187 +123,477 @@ Class UserGateway extends Gateway{
]);
}
public function delete(string $id) : bool{
// supretion user
$query='DELETE FROM Users WHERE id_user = :id;';
return $this->co->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR)));
/**
* Deletes a user from the `Users` table.
*
* This method takes a user ID (as a string) and removes the corresponding
* user from the `Users` table using the provided ID.
*
* @param string $id The ID of the user to be deleted.
* @return bool Whether the deletion was successful.
*/
public function delete(string $id): bool
{
// SQL query to delete a user from the Users table based on the user ID
$query = 'DELETE FROM Users
WHERE id_user = :id;';
// Execute the query with the provided user ID
return $this->co->executeQuery($query, [
':id' => [$id, PDO::PARAM_STR]
]);
}
public function inFavorite(string $username, int $idq):bool{
$query = 'SELECT count(*) FROM Favorite f JOIN Users u ON f.users = u.id_user WHERE u.username = :user AND f.quote = :id';
$this->co->executeQuery($query, array(':user'=>array($username, PDO::PARAM_STR),':id'=>array($idq, PDO::PARAM_INT)));
/**
* Checks if a quote is in the user's favorite list.
*
* This method checks if a specific quote, identified by its ID,
* is already marked as a favorite by a user, identified by their username.
*
* @param string $username The username of the user.
* @param int $idq The ID of the quote.
* @return bool True if the quote is in the user's favorite list, false otherwise.
*/
public function inFavorite(string $username, int $idq): bool
{
// SQL query to count the number of favorites for the given user and quote
$query = 'SELECT count(*)
FROM Favorite f
JOIN Users u ON f.users = u.id_user
WHERE u.username = :user AND f.quote = :id';
// Execute the query with the provided parameters (username and quote ID)
$this->co->executeQuery($query, array(
':user' => array($username, PDO::PARAM_STR),
':id' => array($idq, PDO::PARAM_INT)
));
// Fetch the results and check if count is greater than or equal to 1
$result = $this->co->getResults();
// Return true if the quote is in the favorites, otherwise false
return ($result[0]['count'] >= 1);
}
public function addFavorite(string $username, int $id){
$query = 'INSERT INTO Favorite VALUES ((SELECT id_user FROM Users WHERE username = :username), :id)';
$this->co->executeQuery($query, array(':username' => array($username,PDO::PARAM_STR), ':id' => array($id,PDO::PARAM_INT)));
/**
* Adds a quote to a user's favorite list and increments the like count for the quote.
*
* This method performs two actions:
* 1. Adds the quote (identified by its ID) to the user's favorite list.
* 2. Increments the number of likes for the given quote.
*
* @param string $username The username of the user who is adding the quote to their favorites.
* @param int $id The ID of the quote being added to the favorites.
*/
public function addFavorite(string $username, int $id)
{
// First query: Insert a new favorite for the user and the specified quote.
$query = 'INSERT INTO Favorite VALUES (
(SELECT id_user FROM Users WHERE username = :username),
:id
)';
// Execute the query to add the quote to the user's favorites
$this->co->executeQuery($query, array(
':username' => array($username, PDO::PARAM_STR),
':id' => array($id, PDO::PARAM_INT)
));
// Second query: Increment the likes for the given quote.
$query = 'UPDATE Quote SET likes = (likes + 1) WHERE id_quote = :id';
$this->co->executeQuery($query, array(':id' => array($id,PDO::PARAM_INT)));
// Execute the query to update the like count for the quote
$this->co->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
}
public function supFavorite(string $username, int $id){
$query = 'SELECT id_user FROM Users WHERE username = :username';
$this->co->executeQuery($query, array(':username' => array($username,PDO::PARAM_STR)));
/**
* Removes a quote from a user's favorite list and decrements the like count for the quote.
*
* This method performs two actions:
* 1. Removes the specified quote from the user's favorites.
* 2. Decrements the number of likes for the given quote.
*
* @param string $username The username of the user who is removing the quote from their favorites.
* @param int $id The ID of the quote being removed from the favorites.
*/
public function supFavorite(string $username, int $id)
{
// First query: Get the user id based on the username.
$query = 'SELECT id_user
FROM Users
WHERE username = :username';
$this->co->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
$result = $this->co->getResults()[0]['id_user'];
$query = 'DELETE FROM Favorite WHERE users = :user AND quote = :id;';
$this->co->executeQuery($query, array(':user' => array($result,PDO::PARAM_INT), ':id' => array($id,PDO::PARAM_INT)));
$query = 'UPDATE Quote SET likes = (likes - 1) WHERE id_quote = :id';
$this->co->executeQuery($query, array(':id' => array($id,PDO::PARAM_INT)));
// Second query: Delete the quote from the user's favorites.
$query = 'DELETE FROM Favorite
WHERE users = :user AND quote = :id;';
$this->co->executeQuery($query, array(':user' => array($result, PDO::PARAM_INT), ':id' => array($id, PDO::PARAM_INT)));
// Third query: Decrement the like count for the quote.
$query = 'UPDATE Quote
SET likes = (likes - 1)
WHERE id_quote = :id';
$this->co->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
}
public function deleteAllCommentaryUser(string $user){
$query = 'DELETE FROM Commentary WHERE users IN ( SELECT id_user FROM Users WHERE username = :user);';
$this->co->executeQuery($query, array(':user'=>array($user, PDO::PARAM_STR)));
// ===================== DELETE FUNCTION =====================
/**
* Deletes all commentaries made by a user.
*
* This method performs the following actions:
* 1. Finds all commentaries made by a specified user using their username.
* 2. Deletes all the commentaries associated with that user from the Commentary table.
*
* @param string $user The username of the user whose commentaries need to be deleted.
*/
public function deleteAllCommentaryUser(string $user)
{
// Query to delete all commentaries made by the user.
$query = 'DELETE FROM Commentary
WHERE users IN ( SELECT id_user
FROM Users
WHERE username = :user);';
// Execute the query with the provided username.
$this->co->executeQuery($query, array(':user' => array($user, PDO::PARAM_STR)));
}
public function deleteAllFavoriteUser(string $user){
$query = 'DELETE FROM Favorite WHERE users IN ( SELECT id_user FROM Users WHERE username = :user);';
$this->co->executeQuery($query, array(':user'=>array($user, PDO::PARAM_STR)));
/**
* Deletes all favorites associated with a user.
*
* This method performs the following actions:
* 1. Finds all favorite quotes associated with the user using their username.
* 2. Deletes all the entries in the Favorite table where this user is marked as the owner.
*
* @param string $user The username of the user whose favorites need to be deleted.
*/
public function deleteAllFavoriteUser(string $user)
{
// Query to delete all favorites associated with the user.
$query = 'DELETE FROM Favorite
WHERE users IN ( SELECT id_user
FROM Users
WHERE username = :user);';
// Execute the query with the provided username.
$this->co->executeQuery($query, array(':user' => array($user, PDO::PARAM_STR)));
}
public function deleteUser(string $user){
$query = 'DELETE FROM Users WHERE username=:user;';
$this->co->executeQuery($query, array(':user'=>array($user, PDO::PARAM_STR)));
}
// ===================== GET FUNCTION =====================
/**
* Deletes a user from the Users table based on their username.
*
* @param string $user The username of the user to be deleted.
*/
public function deleteUser(string $user)
{
// SQL query to delete the user from the Users table based on their username.
$query = 'DELETE FROM Users
WHERE username = :user;';
// Execute the query with the username parameter.
$this->co->executeQuery($query, array(':user' => array($user, PDO::PARAM_STR)));
}
public function getFavorite(string $id):array{
//obtention favoris d'un user
$query='SELECT * FROM Quote WHERE id_quote IN (SELECT id_quote FROM Favorite f JOIN users u ON u.id_user = f.user_f WHERE u.id_user = :id);';
$this->co->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR)));
$result=$this->co->getResults();
return $result;
}
// ===================== GET FUNCTION =====================
public function getIdUser(string $username):array{
$query = 'SELECT id_user FROM Users WHERE username=:username';
$this->co->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR)));
/**
* Retrieves the favorite quotes of a user based on their user ID.
*
* @param string $id The ID of the user whose favorite quotes are to be fetched.
* @return array The list of favorite quotes of the user.
*/
public function getFavorite(string $id): array
{
// SQL query to retrieve all quotes that the user has marked as favorites.
$query = 'SELECT *
FROM Quote
WHERE id_quote IN (SELECT id_quote
FROM Favorite f JOIN Users u ON u.id_user = f.user_f
WHERE u.id_user = :id);';
// Execute the query with the user ID as a parameter.
$this->co->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR)));
// Get the results of the query and return them.
$result = $this->co->getResults();
return $result;
}
// ===================== FIND FUNCTION =====================
//obtenir les information d'un user
public function findDataUser(int $id):array{
$query = 'SELECT u.id_user , u.username , u.email , u.password , i.imgPath , u.creation FROM Users WHERE id_user=:idUser';
$this->co->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR)));
/**
* Retrieves the user ID based on the given username.
*
* @param string $username The username for which to retrieve the user ID.
* @return array The user ID corresponding to the given username.
*/
public function getIdUser(string $username): array
{
// SQL query to retrieve the user ID based on the username.
$query = 'SELECT id_user
FROM Users
WHERE username = :username';
// Execute the query with the provided username parameter.
$this->co->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
// Get the results of the query and return them.
$result = $this->co->getResults();
return $result;
}
// obtenir les informations d'un user selon son pseudo
public function findUsername(string $username):array{
$query = 'SELECT * FROM Users u Join Image i on i.id_img=u.img WHERE username= :username';
$this->co->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR)));
// ===================== FIND FUNCTION =====================
/**
* Retrieves user information including their image based on the username.
*
* @param string $username The username of the user whose details are to be fetched.
* @return array An array containing user details and image information.
*/
public function findUsername(string $username): array
{
// SQL query to retrieve user details and their associated image.
$query = 'SELECT * FROM Users u
JOIN Image i ON i.id_img = u.img
WHERE username = :username';
// Execute the query with the provided username as a parameter.
$this->co->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
// Get the result of the query and return it.
return $this->co->getResults();
}
public function findEmail(string $email):array{
$query = 'SELECT * FROM Users WHERE email = :email';
$this->co->executeQuery($query, array(':email'=>array($email, PDO::PARAM_STR)));
/**
* Retrieves user information based on their email.
*
* @param string $email The email of the user whose details are to be fetched.
* @return array An array containing the user's details.
*/
public function findEmail(string $email): array
{
// SQL query to retrieve user details based on email.
$query = 'SELECT *
FROM Users
WHERE email = :email';
// Execute the query with the provided email as a parameter.
$this->co->executeQuery($query, array(':email' => array($email, PDO::PARAM_STR)));
// Get the result of the query and return it.
return $this->co->getResults();
}
// ===================== CHECK FUNCTION =====================
public function IsExisteUsername(string $username): bool {
$query = 'SELECT COUNT(*) as count FROM Users WHERE username = :username';
/**
* Checks if a username already exists in the database.
*
* @param string $username The username to check.
* @return bool Returns true if the username exists, false otherwise.
*/
public function IsExisteUsername(string $username): bool
{
// SQL query to check if the username exists in the "Users" table.
$query = 'SELECT COUNT(*) as count
FROM Users
WHERE username = :username';
// Execute the query, binding the provided username parameter.
$this->co->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
// Get the query results.
$results = $this->co->getResults();
return $results[0]['count'] > 0; // retourne true si "count" > 0)
// Return true if the "count" is greater than 0, meaning the username exists.
return $results[0]['count'] > 0;
}
public function IsExisteEmail(string $email): bool {
$query = 'SELECT COUNT(*) as count FROM Users WHERE email = :email';
/**
* Checks if an email already exists in the database.
*
* @param string $email The email to check.
* @return bool Returns true if the email exists, false otherwise.
*/
public function IsExisteEmail(string $email): bool
{
// SQL query to check if the email exists in the "Users" table.
$query = 'SELECT COUNT(*) as count
FROM Users
WHERE email = :email';
// Execute the query, binding the provided email parameter.
$this->co->executeQuery($query, array(':email' => array($email, PDO::PARAM_STR)));
// Get the query results.
$results = $this->co->getResults();
return $results[0]['count'] > 0; // retourne true si "count" > 0)
// Return true if the "count" is greater than 0, meaning the email exists.
return $results[0]['count'] > 0;
}
/**
* Retrieves the password hash for a given username.
*
* @param string $username The username to search for.
* @return string|null The password hash if the username is found, null otherwise.
*/
public function getPasswordHash(string $username): ?string {
$query = 'SELECT password FROM Users WHERE username = :username';
// SQL query to retrieve the password hash for a given username.
$query = 'SELECT password
FROM Users
WHERE username = :username';
// Execute the query with the provided username as the parameter.
$this->co->executeQuery($query, array(':username' => array($username, PDO::PARAM_STR)));
// Get the results of the query.
$results = $this->co->getResults();
// Si un utilisateur est trouvé, retourner le hash du mot de passe, sinon null
// If results are found, return the password hash, otherwise return null.
return $results ? $results[0]['password'] : null;
}
// ===================== UPDATE FUNCTION =====================
//Update Username
public function updateUsername(string $username, string $newUsername):array{
//Update le nom du user passé en paramètre
$queryUpdate = 'UPDATE Users SET username=:newUsername WHERE username=:username';
$this->co->executeQuery($queryUpdate, array(':username'=>array($username, PDO::PARAM_STR), ':newUsername'=> array($newUsername, PDO::PARAM_STR)));
//Renvoie le nouveau nom du user
/**
* Updates the username for a user.
*
* @param string $username The current username.
* @param string $newUsername The new username to set.
* @return array The result of the update, including the new username.
*/
public function updateUsername(string $username, string $newUsername): array {
// SQL query to update the username for the user with the current username
$queryUpdate = 'UPDATE Users
SET username=:newUsername
WHERE username=:username';
// Execute the update query with the provided parameters
$this->co->executeQuery($queryUpdate, array(
':username' => array($username, PDO::PARAM_STR),
':newUsername' => array($newUsername, PDO::PARAM_STR)
));
// SQL query to retrieve the updated username from the Users table
$queryReponse = 'SELECT username FROM Users WHERE username=:idUser';
$this->co->executeQuery($queryReponse, array(':idUser'=>array($newUsername, PDO::PARAM_STR)));
// Execute the query to fetch the updated username
$this->co->executeQuery($queryReponse, array(':idUser' => array($newUsername, PDO::PARAM_STR)));
// Return the result, which will contain the new username
return $this->co->getResults();
}
//Update Email
public function updateEmail(string $username, string $newEmail):array{
//Update le email du user passé en paramètre
$queryUpdate = 'UPDATE Users SET email=:newEmail WHERE username=:username';
$this->co->executeQuery($queryUpdate, array(':username'=>array($username, PDO::PARAM_STR), ':newEmail'=> array($newEmail, PDO::PARAM_STR)));
//Renvoie le nouveau email du user
/**
* Updates the email for a user based on their username.
*
* @param string $username The username of the user whose email will be updated.
* @param string $newEmail The new email to set for the user.
* @return array The result of the update, including the new email.
*/
public function updateEmail(string $username, string $newEmail): array {
// SQL query to update the email for the user with the specified username
$queryUpdate = 'UPDATE Users
SET email=:newEmail
WHERE username=:username';
// Execute the update query with the provided parameters (username and newEmail)
$this->co->executeQuery($queryUpdate, array(
':username' => array($username, PDO::PARAM_STR),
':newEmail' => array($newEmail, PDO::PARAM_STR)
));
// SQL query to retrieve the updated email from the Users table
$queryReponse = 'SELECT email FROM Users WHERE username=:username';
$this->co->executeQuery($queryReponse, array(':username'=>array($username, PDO::PARAM_STR)));
// Execute the query to fetch the updated email
$this->co->executeQuery($queryReponse, array(':username' => array($username, PDO::PARAM_STR)));
// Return the result, which will contain the new email
return $this->co->getResults();
}
public function updateImg(string $username,string $newImage):array{
if($newImage==null){
/**
* Updates the profile image for a user based on their username.
*
* @param string $username The username of the user whose image will be updated.
* @param string|null $newImage The new image ID or `null` to use a random image.
* @return array The result of the update, including the new image ID.
*/
public function updateImg(string $username, string $newImage): array {
// Determine the image ID to use
if ($newImage == null) {
// If no image provided, use a random image ID
$id_image = $this->randomImg();
}
else if(is_int((int)$newImage)){
$id_image=(int)$newImage;
}
else{
} else if (is_int((int)$newImage)) {
// If a valid image ID is provided, use it
$id_image = (int)$newImage;
} else {
// If the provided value is not valid, use a random image ID
$id_image = $this->randomImg();
}
// SQL query to update the image for the user with the specified username
$query = 'UPDATE Users
SET img=:id_image
WHERE username=:username';
$this->co->executeQuery($query, array(
':username' => array($username, PDO::PARAM_STR),
':id_image' => array($id_image, PDO::PARAM_INT)
));
//Update l'image du user passé en paramètre
$query = 'UPDATE Users SET img=:id_image WHERE username=:username';
$this->co->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR), ':id_image'=> array($id_image, PDO::PARAM_INT)));
//Renvoie la nouvelle image du user
// SQL query to retrieve the updated image for the user
$queryReponse = 'SELECT img FROM Users WHERE username=:username';
$this->co->executeQuery($queryReponse, array(':username'=>array($username, PDO::PARAM_STR)));
$this->co->executeQuery($queryReponse, array(':username' => array($username, PDO::PARAM_STR)));
// Return the result, which will contain the new image ID
return $this->co->getResults();
}
public function updatePasswd(string $username, string $newPassWd):void{
//Update le passwd du user passé en paramètre
$query = 'UPDATE Users SET password=:newPassWd WHERE username=:username';
$this->co->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR), ':newPassWd'=> array($newPassWd, PDO::PARAM_STR)));
}
public function emailWithUser(string $user):array{
$query = 'SELECT email FROM Users WHERE username = :user';
$this->co->executeQuery($query, array(':user'=>array($user, PDO::PARAM_STR)));
return $this->co->getResults();
/**
* Updates the password for a user based on their username.
*
* @param string $username The username of the user whose password will be updated.
* @param string $newPassWd The new password to set for the user.
*/
public function updatePasswd(string $username, string $newPassWd): void {
// SQL query to update the password for the user with the specified username
$query = 'UPDATE Users
SET password=:newPassWd
WHERE username=:username';
// Execute the query with the provided parameters (username and new password)
$this->co->executeQuery($query, array(
':username' => array($username, PDO::PARAM_STR),
':newPassWd' => array($newPassWd, PDO::PARAM_STR)
));
}
}
?>