Skip to content

Commit

Permalink
✅ Commentaires + modération #23
Browse files Browse the repository at this point in the history
  • Loading branch information
BOYER Thomas committed Jan 23, 2024
1 parent 912438e commit 7727dd7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/Controller/AdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,36 @@ public function articles_remove(int $id): RedirectResponse {
}
return new RedirectResponse('administration_articles');
}

#[isGranted('Administrateur')]
#[Route('/administration/comments/{id}/validate', name: 'administration_comments_validate', methods: ['GET'])]
public function comments_validate(int $id): RedirectResponse {
$comment = $this->commentRepository->find($id);
if ($comment !== null) {
$comment->setValidated(true);
$this->commentRepository->save($comment);
}
return new RedirectResponse('article', ['id' => $comment->getBlogPost()->getId()]);
}

#[isGranted('Administrateur')]
#[Route('/administration/comments/{id}/unvalidate', name: 'administration_comments_unvalidate', methods: ['GET'])]
public function comments_unvalidate(int $id): RedirectResponse {
$comment = $this->commentRepository->find($id);
if ($comment !== null) {
$comment->setValidated(false);
$this->commentRepository->save($comment);
}
return new RedirectResponse('article', ['id' => $comment->getBlogPost()->getId()]);
}

#[isGranted('Administrateur')]
#[Route('/administration/comments/{id}/remove', name: 'administration_comments_remove', methods: ['GET'])]
public function comments_remove(int $id): RedirectResponse {
$comment = $this->commentRepository->find($id);
if ($comment !== null) {
$this->commentRepository->delete($comment);
}
return new RedirectResponse('article', ['id' => $comment->getBlogPost()->getId()]);
}
}
13 changes: 8 additions & 5 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
use Core\AbstractController;
use App\Repository\BlogPostRepository;
use App\Repository\CategoryRepository;
use App\Repository\CommentRepository;
use League\CommonMark\GithubFlavoredMarkdownConverter;

class BlogController extends AbstractController {

private readonly BlogPostRepository $blogPostRepository;

private readonly CommentRepository $commentRepository;

public function __construct() {
$this->blogPostRepository = new BlogPostRepository();
$this->commentRepository = new CommentRepository();
}

#[Route(path: '/article/{id}', name: 'blog_show')]
public function show(int $id): Response {
#[Route(path: '/article/{id}', name: 'article')]
public function article(int $id): Response {
$article = $this->blogPostRepository->find($id);
if (!$article) {
throw $this->createNotFoundException('Article non trouvé');
Expand All @@ -32,11 +34,12 @@ public function show(int $id): Response {

return new Response('blog/article.html.twig', [
'article' => $article,
'post' => $converter->convert($article->getPost())
'post' => $converter->convert($article->getPost()),
'comments' => $this->commentRepository->findBy(['blog_post_id' => $article->getId()]),
]);
}

#[Route(path: '/category/{id}', name: 'category_show', methods: ['GET'])]
#[Route(path: '/category/{id}', name: 'category', methods: ['GET'])]
public function category(int $id): Response {
$categoryRepository = new CategoryRepository();
$blogPostRepository = new BlogPostRepository();
Expand Down
36 changes: 36 additions & 0 deletions templates/blog/article.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@
{{ post|raw }}
</article>
</div>
<div class="mt-24 flex justify-between items-start">
<h2>Commentaires</h2>
<button class="fr-btn mt-[2px]">
<i class="fa-solid fa-plus"></i>
<span class="hidden sm:inline sm:ml-3">Ajouter un commentaire</span>
</button>
</div>
<hr>
{% for key,comment in comments %}
{% if comment.validated or is_granted('Administrateur') %}
<div class="flex rounded-sm flex-col gap-5 {% if comment.validated %} bg-gray-50 dark:bg-neutral-950 {% else %} bg-red-100 dark:bg-red-900 {% endif %} p-4 mt-5">
<div class="flex flex-col gap-2">
<div class="flex flex-row gap-2 items-center font-bold text-lg">
<span>Écris par
{{ comment.user.lastname|upper ~ ' ' ~ comment.user.firstname|capitalize }}</span>
<span class="inline-block">●</span>
<span>{{comment.createdAt|date("d/m/Y à H:i")}}</span>
</div>
<p class="text-lg">{{comment.comment}}</p>
</div>
<div class="flex gap-4">
{% if is_granted('Administrateur') %}
{% if not comment.validated %}
<a class="fr-btn" href="/administration/comments/{{comment.id}}/validate">
<i class="fa-solid fa-check mr-3"></i>Valider</a>
{% else %}
<a class="fr-btn" href="/administration/comments/{{comment.id}}/unvalidate">
<i class="fa-solid fa-xmark mr-3"></i>Cacher (dévalidation)</a>
{% endif %}
<a class="fr-btn" href="/administration/comments/{{comment.id}}/remove">
<i class="fa-solid fa-trash mr-3"></i>Supprimer</a>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
</div>
</main>
{% endblock %}

0 comments on commit 7727dd7

Please sign in to comment.