You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
15 KiB
304 lines
15 KiB
<?php
|
|
namespace Gateway;
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
Class QuoteGateway extends Gateway{
|
|
|
|
/**
|
|
* Searches for quotes that match the given criteria (quote content, language, and pagination).
|
|
*
|
|
* This method retrieves quotes from the `Quote` table that match a specified search term (`$quote`),
|
|
* belong to the specified language (`$language`), and are marked as valid. It also supports pagination.
|
|
*
|
|
* @param string $quote The search term to match within the content of the quotes.
|
|
* @param int $numpage The page number to retrieve, used for pagination. This number is used to calculate
|
|
* the offset in the query to return results for that specific page.
|
|
* @param string $language The language of the quotes to retrieve.
|
|
*
|
|
* @return array Returns an array of quotes matching the search criteria. Each quote is represented
|
|
* as an associative array with column names as keys.
|
|
*/
|
|
public function searchQuote(string $quote, int $numpage, string $language) : array
|
|
{
|
|
// SQL query to search for quotes with pagination and language filtering
|
|
$query = "SELECT q.id_quote, q.content, q.id_caracter, c.img_char, s.title, s.date, q.likes, q.langue
|
|
FROM Quote q
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
WHERE content LIKE '%:quote%' AND is_valid = true AND langue = :langue
|
|
LIMIT 20 OFFSET :page*20;";
|
|
|
|
// Execute the query with the provided parameters
|
|
$this->co->executeQuery($query, [
|
|
':quote' => array($quote, PDO::PARAM_STR),
|
|
':page' => array($numpage, PDO::PARAM_INT),
|
|
':langue' => array($language, PDO::PARAM_STR)
|
|
]);
|
|
|
|
// Return the search results
|
|
return $this->co->getResults();
|
|
}
|
|
|
|
|
|
/**
|
|
* Searches for a quote by its unique ID.
|
|
*
|
|
* This method retrieves a quote from the `Quote` table based on its ID (`$id`). It joins related tables
|
|
* (`Caracter`, `Source`, and `Image`) to fetch additional information such as character name, image path,
|
|
* source title, and the number of likes, only for valid quotes.
|
|
*
|
|
* @param string $id The unique identifier of the quote to retrieve.
|
|
*
|
|
* @return array Returns an associative array of the quote's details including content, character, image path,
|
|
* source title, date, likes, and language.
|
|
*/
|
|
public function searchId(string $id): array
|
|
{
|
|
// SQL query to search for a quote by its ID with related tables for additional information
|
|
$query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dates, q.likes, q.langue
|
|
FROM Quote q
|
|
JOIN Caracter c ON c.id_caracter = q.id_caracter
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
JOIN Image i ON c.id_img = i.id_img
|
|
WHERE q.id_quote = :id AND q.isvalide = true;";
|
|
|
|
// Execute the query with the provided quote ID
|
|
$this->co->executeQuery($query, [':id' => array($id, PDO::PARAM_STR)]);
|
|
|
|
// Return the result set
|
|
return $this->co->getResults();
|
|
}
|
|
|
|
|
|
/**
|
|
* Searches for quotes based on a search term and filter type.
|
|
*
|
|
* This method allows you to search for quotes by a specific criterion, such as character name (`personnage`),
|
|
* source title (`titre`), or the content of the quote itself. It joins related tables (`Caracter`, `Source`, and `Image`)
|
|
* to retrieve additional information such as the character's name, image, source title, and likes. The search is case-insensitive.
|
|
*
|
|
* @param string|null $type The type of search. Can be:
|
|
* - `personnage` to search by character name.
|
|
* - `titre` to search by source title.
|
|
* - `null` or any other value to search by quote content.
|
|
* If not provided or invalid, it defaults to searching by content.
|
|
*
|
|
* @param string|null $search The search term to look for in the chosen field (`personnage`, `titre`, or `content`).
|
|
* This parameter can be `null`, which will result in no filtering.
|
|
*
|
|
* @param array $filtre (Unused in this method, included for compatibility)
|
|
*
|
|
* @return array Returns an associative array of quotes matching the search criteria, including content, character name,
|
|
* image path, source title, date, likes, and language.
|
|
*/
|
|
public function search(?string $type, ?string $search, array $filtre): array
|
|
{
|
|
// Start building the query
|
|
$query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dates, q.likes, q.langue
|
|
FROM Quote q
|
|
JOIN Caracter c ON c.id_caracter = q.id_caracter
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
JOIN Image i ON c.id_img = i.id_img
|
|
WHERE lower()";
|
|
|
|
// Build the condition based on the $type parameter
|
|
if ($type == 'personnage') {
|
|
// Search by character name
|
|
$query .= "c.caracter) LIKE lower('%" . $search . "%')";
|
|
} elseif ($type == 'titre') {
|
|
// Search by source title
|
|
$query .= "s.title) LIKE lower('%" . $search . "%')";
|
|
} else {
|
|
// Default to searching by quote content
|
|
$query .= "q.content) LIKE lower('%" . $search . "%')";
|
|
}
|
|
|
|
// Execute the query
|
|
$this->co->executeQuery($query, array());
|
|
|
|
// Get the results and return them
|
|
$result = $this->co->getResults();
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves the quote of the day in the specified language.
|
|
*
|
|
* This method retrieves the quote of the day from the `DailyQuote` table, including associated information such as
|
|
* the character's name, image, source title, and the number of likes. The quote is filtered by its validity (`isValide = true`)
|
|
* and the language (`$language`). Only one quote is returned, and the most recent valid quote is chosen.
|
|
*
|
|
* @param string $language The language of the quote to retrieve (e.g., "English", "French", etc.).
|
|
*
|
|
* @return array Returns an associative array of the quote's details, including the quote content, character,
|
|
* character image path, source title, source date, likes, and language. If no result is found,
|
|
* an empty array is returned.
|
|
*/
|
|
public function getQuoteOfTheDay(string $language): array
|
|
{
|
|
// SQL query to retrieve the quote of the day in the specified language
|
|
$query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
|
|
FROM DailyQuote dq
|
|
JOIN Quote q ON dq.citation_id = q.id_quote
|
|
JOIN Caracter c ON c.id_caracter = q.id_caracter
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
JOIN Image i ON c.id_img = i.id_img
|
|
WHERE q.isValide = true AND q.langue = :language
|
|
ORDER BY q.id_quote DESC
|
|
LIMIT 1;";
|
|
|
|
try {
|
|
// Execute the query with the provided language parameter
|
|
$this->co->executeQuery($query, [':language' => [$language, PDO::PARAM_STR]]);
|
|
|
|
// Get the results and return the first (and only) result if available
|
|
$result = $this->co->getResults();
|
|
return $result[0] ?? []; // Return the first result or an empty array if no result is found
|
|
} catch (PDOException $e) {
|
|
// In case of a database error, catch the exception and display an error message
|
|
echo "Erreur dans getQuoteOfTheDay: " . $e->getMessage();
|
|
return [];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Retrieves a list of quote suggestions in the specified language with pagination.
|
|
*
|
|
* This method fetches a random selection of quotes that are marked as valid (`isValide = true`) in the specified language.
|
|
* The quotes are paginated, returning a set of 20 quotes per page, with each call providing a different set of suggestions
|
|
* due to the random ordering.
|
|
*
|
|
* @param int $numpage The page number for pagination. This value is used to calculate the offset for the query.
|
|
* Each page contains 20 quotes.
|
|
*
|
|
* @param string $language The language of the quotes to retrieve (e.g., "English", "French", etc.).
|
|
*
|
|
* @return array Returns an array of quotes, each with its content, associated character, character's image path,
|
|
* source title, source date, number of likes, and language. If no results are found, an empty array is returned.
|
|
*/
|
|
public function getSuggestions(int $numpage, string $language): array {
|
|
// SQL query to retrieve random quote suggestions based on the specified language and pagination
|
|
$query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
|
|
FROM Quote q
|
|
JOIN Caracter c ON c.id_caracter = q.id_caracter
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
JOIN Image i ON c.id_img = i.id_img
|
|
WHERE q.isValide = true AND q.langue = :language
|
|
ORDER BY RANDOM()
|
|
LIMIT 20 OFFSET :offset;";
|
|
|
|
// Execute the query with the provided language and offset for pagination
|
|
$this->co->executeQuery($query, [
|
|
':language' => [$language, PDO::PARAM_STR],
|
|
':offset' => [$numpage * 20, PDO::PARAM_INT] // Calculate the offset for pagination
|
|
]);
|
|
|
|
// Return the result set (quotes)
|
|
return $this->co->getResults();
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves a list of quotes marked as favorites by a specific user.
|
|
*
|
|
* This method fetches the quotes that are marked as favorites by the user, along with relevant information such as
|
|
* the character's name, image path, source title, and number of likes. It uses the `Favorite` table to identify the quotes
|
|
* favorited by the specified user.
|
|
*
|
|
* @param string $userId The ID of the user whose favorite quotes are to be retrieved.
|
|
*
|
|
* @return array Returns an array of quotes that are marked as favorites by the user, each with its content,
|
|
* associated character, character's image path, source title, source date, likes, and language.
|
|
* If no favorites are found, an empty array is returned.
|
|
*/
|
|
public function getFavorites(string $userId): array {
|
|
// SQL query to retrieve the list of favorite quotes for a specific user
|
|
$query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
|
|
FROM Favorite f
|
|
JOIN Quote q ON f.quote = q.id_quote
|
|
JOIN Caracter c ON c.id_caracter = q.id_caracter
|
|
JOIN Source s ON s.id_source = q.id_source
|
|
JOIN Image i ON c.id_img = i.id_img
|
|
WHERE f.users = :userId;";
|
|
|
|
try {
|
|
// Execute the query with the provided user ID
|
|
$this->co->executeQuery($query, [
|
|
':userId' => [$userId, PDO::PARAM_STR]
|
|
]);
|
|
|
|
// Return the results (favorites)
|
|
return $this->co->getResults();
|
|
} catch (PDOException $e) {
|
|
// In case of a database error, catch the exception and display an error message
|
|
echo "Erreur dans getFavorites: " . $e->getMessage();
|
|
return [];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves the next available auto-incremented quote ID.
|
|
*
|
|
* This method retrieves the next available `id_quote` by selecting the maximum `id_quote` in the `Quote` table and
|
|
* incrementing it by 1. This value can be used to set the `id_quote` for a new quote being inserted into the database.
|
|
*
|
|
* @return int Returns the next available quote ID (the maximum `id_quote` value + 1). If no quotes exist yet, it returns 1.
|
|
*/
|
|
public function autoincrement(): int
|
|
{
|
|
// SQL query to retrieve the next available quote ID by selecting the maximum id_quote and adding 1
|
|
$query = "SELECT Max(id_quote) + 1 as id
|
|
FROM Quote;";
|
|
|
|
// Execute the query to get the maximum id_quote value
|
|
$this->co->executeQuery($query);
|
|
|
|
// Return the next available id (the result of the query), or 1 if no quotes are found
|
|
return ($this->co->getResults())[0]['id'];
|
|
}
|
|
|
|
|
|
/**
|
|
* Inserts a new quote into the Quote table, associated with a user, source, and character.
|
|
*
|
|
* This method inserts a new quote into the `Quote` table, setting various attributes like the content, language,
|
|
* image path, the user who created the quote, the source of the quote, and the character associated with the quote.
|
|
* The method automatically generates a new quote ID by using the `autoincrement` function, ensuring a unique ID
|
|
* for each new quote.
|
|
*
|
|
* @param string $content The content of the quote to be inserted.
|
|
* @param string $img_path The path to the image associated with the quote.
|
|
* @param string $langage The language of the quote (e.g., "English", "French").
|
|
* @param int $user The ID of the user who created the quote.
|
|
* @param int $source The ID of the source of the quote (e.g., book, movie).
|
|
* @param int $character The ID of the character associated with the quote.
|
|
*
|
|
* @return bool Returns true if the query executed successfully, false otherwise.
|
|
*/
|
|
public function insert4User(string $content, string $img_path, string $langage, int $user, int $source, int $character) : bool
|
|
{
|
|
// SQL query to insert a new quote into the database
|
|
$query = "INSERT INTO Quote (id_quote, content, langue, reason, id_source, id_caracter, id_user_verif, img_path)
|
|
VALUES (:id, :content, :langage, :reason, :source, :character, :user, :img_path)";
|
|
|
|
// Execute the query with the provided parameters
|
|
return $this->co->executeQuery($query, [
|
|
':id' => array($this->autoincrement(), PDO::PARAM_INT), // Generate the next available quote ID
|
|
':content' => array($content, PDO::PARAM_STR), // Content of the quote
|
|
':img_path' => array($img_path, PDO::PARAM_STR), // Path to the image associated with the quote
|
|
':langage' => array($langage, PDO::PARAM_STR), // Language of the quote
|
|
':user' => array($user, PDO::PARAM_INT), // User ID who created the quote
|
|
':reason' => array('À vérifier', PDO::PARAM_STR), // Default value for the quote verification status
|
|
':source' => array($source, PDO::PARAM_STR), // Source ID (book, movie, etc.)
|
|
':character' => array($character, PDO::PARAM_STR) // Character ID associated with the quote
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|