Skip to content

Commit

Permalink
Merge pull request #53 from WildCodeSchool-2023-09/feature-filtre-cat…
Browse files Browse the repository at this point in the history
…egories

Feature filtre categories
  • Loading branch information
KevinDavoust authored Nov 20, 2023
2 parents 17081b1 + ab16a29 commit 5bc826f
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 72 deletions.
46 changes: 38 additions & 8 deletions src/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ public function showAllArticles()
$articleManager = new ArticleManager();
$articles = $articleManager->getAllArticles();
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;


$articles = array_map(function ($article) {
if (isset($article['categories'])) {
$article['categories'] = explode(',', $article['categories']);
}
return $article;
}, $articles);


return $this->twig->render('Home/index.html.twig', ['articles' => $articles, 'userId' => $userId]);
}

public function showAllArticlesByUserID($userId)
{
$articleManager = new ArticleManager();
$articles = $articleManager->getArticlesByUserId($userId);

return $this->twig->render('profil.html.twig', ['articles' => $articles]);
}

Expand Down Expand Up @@ -182,8 +191,9 @@ public function deleteArticleById($articleId)
$article = $articleManager->getArticleById($articleId);

if (!$article) {
return $this->twig->render('Error/index.html.twig', ['message' =>
'L\'article n\'existe pas.']);
return $this->twig->render('Error/index.html.twig', [
'message' => 'L\'article n\'existe pas.'
]);
}

// Vérifie si l'utilisateur est connecté et est l'auteur de l'article
Expand All @@ -196,12 +206,32 @@ public function deleteArticleById($articleId)
}
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;

return $this->twig->render('Article/delete.html.twig', ['article' => $article, 'userId' => $userId]);
return $this->twig->render('Article/delete.html.twig', [
'article' => $article,
'userId' => $userId
]);
} else {
return $this->twig->render('Error/index.html.twig', ['message' =>
'Vous n\'êtes pas autorisé à supprimer cet article.
Vous devez être connecté et être l\'auteur de l\'article.']);
return $this->twig->render('Error/index.html.twig', [
'message' => 'Vous n\'êtes pas autorisé à supprimer cet article.
Vous devez être connecté et être l\'auteur de l\'article.'
]);
}
}

public function searchByCategoryName(string $searchTerm)
{
$articleManager = new ArticleManager();
$articles = $articleManager->getArticlesByCategoryName($searchTerm);

if (empty($articles)) {
return $this->twig->render('Error/index.html.twig', [
'message' => 'Aucun article trouvé pour la catégorie : ' . $searchTerm
]);
}
return $this->twig->render('Article/search_results.html.twig', [
'articles' => $articles,
'searchTerm' => $searchTerm
]);
}
}
// Add a newline at the end of the file
// A newline has been added here at the end of the file
9 changes: 8 additions & 1 deletion src/Controller/ProfilController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public function profil()

// Récupère les articles de l'utilisateur
$articleManager = new ArticleManager();
$articles = $articleManager->getArticlesWithCategoriesByUserId($userId);
$articles = $articleManager->getAllArticles();

$articles = array_map(function ($article) {
if (isset($article['categories'])) {
$article['categories'] = explode(',', $article['categories']);
}
return $article;
}, $articles);

echo $this->twig->render(
'Blog_user/profil.html.twig',
Expand Down
52 changes: 48 additions & 4 deletions src/Model/ArticleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public function getAllArticles()
public function getArticleById(int $articleId)
{
$query = "SELECT A.*, BU.name AS author_name
FROM " . static::TABLE . " AS A
LEFT JOIN blog_user BU ON A.blog_user_id = BU.id
WHERE A.id = :id";
FROM " . static::TABLE . " AS A
LEFT JOIN blog_user BU ON A.blog_user_id = BU.id
WHERE A.id = :id";

$statement = $this->pdo->prepare($query);
$statement->bindValue(':id', $articleId, \PDO::PARAM_INT);
$statement->execute();
Expand Down Expand Up @@ -111,10 +112,53 @@ public function getArticlesWithCategoriesByUserId($userId)
LEFT JOIN article_category AC ON A.id = AC.article_id
LEFT JOIN category CAT ON AC.category_id = CAT.id
WHERE A.blog_user_id = :userId
GROUP BY A.id";
GROUP BY A.id
ORDER BY A.date DESC";

$statement = $this->pdo->prepare($query);
$statement->bindValue(':userId', $userId, PDO::PARAM_INT);
$statement->execute();
return $statement->fetchAll();
}

public function getArticlesByCategory($categoryId = null)
{
$query = "SELECT A.*, BU.name AS author_name
FROM article A
INNER JOIN blog_user BU ON A.blog_user_id = BU.id";

if ($categoryId) {
$query .= " JOIN article_category AC ON A.id = AC.article_id
WHERE AC.category_id = :categoryId";
}

$query .= " GROUP BY A.id, BU.name";

$statement = $this->pdo->prepare($query);

if ($categoryId) {
$statement->bindValue(':categoryId', $categoryId, PDO::PARAM_INT);
}

$statement->execute();

return $statement->fetchAll(PDO::FETCH_ASSOC);
}

public function getArticlesByCategoryName($searchTerm)
{
$query = "SELECT A.*, BU.name AS author_name
FROM article A
INNER JOIN blog_user BU ON A.blog_user_id = BU.id
INNER JOIN article_category AC ON A.id = AC.article_id
INNER JOIN category C ON AC.category_id = C.id
WHERE C.name LIKE :searchTerm
GROUP BY A.id
ORDER BY A.date DESC";
$statement = $this->pdo->prepare($query);
$statement->bindValue(':searchTerm', '%' . $searchTerm . '%', PDO::PARAM_STR);
$statement->execute();

return $statement->fetchAll(PDO::FETCH_ASSOC);
}
}
14 changes: 9 additions & 5 deletions src/View/Article/add.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
<label for="image">Image (facultatif) :</label>
<input type="text" name="image" id="image" /><br />

<!-- Champ de sélection des catégories -->
<label for="categories">Catégories (optionnel):</label>
<select name="categories[]" id="categories" multiple>
<label for="categories">Catégories (facultatifl):</label>
<div id="categories">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.name }}</option>
<div class="checkbox">
<label>
<input type="checkbox" name="categories[]" value="{{ category.id }}">
{{ category.name }}
</label>
</div>
{% endfor %}
</select><br />
</div><br />

<!-- Champ pour une nouvelle catégorie -->
<label for="new_category">Ajouter une catégorie (optionnel) :</label>
Expand Down
23 changes: 15 additions & 8 deletions src/View/Article/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@
id="image"
value="{{ article.image }}"
/><br />
<label for="categories">Catégories :</label>
<select name="categories[]" id="categories" multiple>

<label for="categories">Catégories (falcutatif):</label>
<div id="categories">
{% for category in allCategories %}
<option value="{{ category.id }}"
{% if category.id in currentCategories %} selected {% endif %}>
{{ category.name }}
</option>
<div class="checkbox">
<label>
<input type="checkbox" name="categories[]" value="{{ category.id }}"
{% if category.id in currentCategories %} checked {% endif %}>
{{ category.name }}
</label>
</div>
{% endfor %}
</select><br />
</div><br />

<label for="new_category">Ajouter une nouvelle catégorie :</label>
<input type="text" id="new_category" name="new_category" placeholder="Nom de la nouvelle catégorie"><br>

<button class="submit">Enregistrer</button>


<button type="submit">Enregistrer</button>

</form>
{% endblock %}
35 changes: 35 additions & 0 deletions src/View/Article/search_results.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'layout.html.twig' %}

{% block title %}Résultats de Recherche{% endblock %}

{% block content %}

<form action="/search" method="get">
<input type="text" name="search_term" placeholder="Rechercher un article par catégorie...">
<button type="submit">Rechercher</button>
</form>


{% if search_term %}
<h1>Résultats de recherche pour la catégorie: "{{ search_term }}"</h1>
{% else %}
<h1>Affichage de tous les articles</h1>
{% endif %}

{% if articles is not empty %}
<ul>
{% for article in articles %}
<li>
<h2>{{ article.title }}</h2>
<p>{{ article.content | raw }}</p>
<p>Posté le {{ article.date | date('d/m/Y à H:i') }}</p>
<p>Par {{ article.author_name }}</p>
<a href="/show?id={{ article.id }}">Voir les commentaires</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>Aucun article trouvé pour "{{ searchTerm }}".</p>
{% endif %}

{% endblock %}
59 changes: 16 additions & 43 deletions src/View/Blog_user/profil.html.twig
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
{# {% extends 'layout.html.twig' %} {% block title %}Profil de l'utilisateur{%
endblock %} {% block stylesheet %}<link
rel="stylesheet"
href="/assets/css/profil.css"
/>{% endblock %} {% block content %}
<h1>Profil de l'utilisateur</h1>
<h2>Informations personnelles</h2>
<p><strong>Nom :</strong> {{ userId.name }}</p>
<p>
<strong>Image :</strong> <img src="{{ userId.image }}" alt="User Image" />
</p>
<p><strong>Titre :</strong> {{ userId.title }}</p>
<p><strong>Description :</strong> {{ userId.description | raw }}</p>
<a href="/forgot_password">Réinitialiser le mot de passe</a>
<h2>Mes articles</h2>
<ul>
{% for article in articles %}
<li>
<h3>{{ article.title }}</h3>
<p>{{ article.content | raw }}</p>
<p>
Catégorie(s) :
{{ article.categories | default('Aucune catégorie') }}
</p>
<a href="/article/edit?id={{ article.id }}">Éditer</a>
<a href="/article/delete?id={{ article.id }}">Supprimer</a>
<p>
<a href="/show?id={{ article.id }}"
>Commentaires ({{ article.comment_count }})</a
>
</p>
</li>
{% endfor %}
</ul>
<a href="/article/add">Ajouter un article</a>
{% endblock %} #} {% extends 'layout.html.twig' %} {% block title %}Profil de
l'utilisateur{% endblock %} {% block stylesheet %}
{% extends 'layout.html.twig' %}
{% block title %}Profil de l'utilisateur{% endblock %}
{% block stylesheet %}
<link rel="stylesheet" href="/assets/css/profil.css" />
{% endblock %} {% block content %}
{% endblock %}
{% block content %}
<div class="blog">BLOG de {{ userId.name }}</div>
<div class="container">
<div class="article">
Expand All @@ -48,10 +13,18 @@ l'utilisateur{% endblock %} {% block stylesheet %}

<h3>{{ article.title }}</h3>
<p class="text">{{ article.content | raw }}</p>
<p>
Catégorie(s) :
{{ article.categories | default('Aucune catégorie') }}
<p>Catégorie(s) : {% if article.categories %}
{% for category in article.categories %}
<form action="/search" method="get" style="display: inline;">
<input type="hidden" name="search_term" value="{{ category }}">
<button type="submit">{{ category }}</button>
</form>
{% endfor %}
{% else %}
Aucune catégorie
{% endif %}
</p>
<p>Posté le {{ article.date | date('d/m/Y à H:i') }}</p>
<div class="button-container">
<a href="/article/edit?id={{ article.id }}" class="button"
>Éditer</a
Expand Down
23 changes: 20 additions & 3 deletions src/View/Home/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

{% block content %}

<form action="/search" method="get">
<input type="text" name="search_term" placeholder="Rechercher un article par catégorie...">
<button type="submit">Rechercher</button>
</form>

<h1>Articles récents</h1>
<ul class = 'flexArticle'>
{% for article in articles %}
Expand All @@ -15,12 +20,24 @@

<p>{{ article.content | raw }}</p>
<p class = "date">Posté le {{ article.date | date('d/m/Y à H:i') }}</p>

<a href="/profil?id={{ userId }}">
<p class = "author">Par {{ article.author_name }}</p></a>

<p>Catégorie(s) : {% if article.categories %}
{% for category in article.categories %}
<form action="/search" method="get" style="display: inline;">
<input type="hidden" name="search_term" value="{{ category }}">
<button type="submit">{{ category }}</button>
</form>
{% endfor %}
{% else %}
Aucune catégorie
{% endif %}
</p>

<a href="/show?id={{ article.id }}">
Commentaires ({{ article.comment_count }})</a>
Commentaires ({{ article.comment_count }})</a>



{% if article.blog_user_id is defined %} {% if userId %}
<form method="post" action="/comment/add">
Expand Down
1 change: 1 addition & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
'comment/delete' => ['CommentController', 'deleteCommentById', ['comment_id']],
'forgot_password' => ['ProfilController', 'forgotPassword'],
'comment/edit' => ['CommentController', 'editCommentById', ['comment_id']],
'search' => ['ArticleController', 'searchByCategoryName', ['search_term']]
];

0 comments on commit 5bc826f

Please sign in to comment.