add comments

Signed-off-by: Simon Vieille <simon@deblan.fr>
This commit is contained in:
Simon Vieille 2021-03-30 13:40:38 +02:00
parent 120dcdaa44
commit 472e254a5f
9 changed files with 314 additions and 28 deletions

View file

@ -4,14 +4,17 @@ namespace App\Controller\Blog;
use App\Core\Annotation\UrlGenerator; use App\Core\Annotation\UrlGenerator;
use App\Core\Controller\Site\PageController; use App\Core\Controller\Site\PageController;
use App\Core\Manager\EntityManager;
use App\Core\Site\SiteRequest; use App\Core\Site\SiteRequest;
use App\Core\Site\SiteStore;
use App\Entity\Blog\Category; use App\Entity\Blog\Category;
use App\Entity\Blog\Post; use App\Entity\Blog\Post;
use App\Factory\Blog\CommentFactory;
use App\Form\Blog\UserCommentType;
use App\Repository\Blog\PostRepositoryQuery; use App\Repository\Blog\PostRepositoryQuery;
use App\UrlGenerator\PostGenerator; use App\UrlGenerator\PostGenerator;
use Symfony\Component\HttpFoundation\Response;
use App\Core\Site\SiteStore;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PostController extends PageController class PostController extends PageController
{ {
@ -27,8 +30,13 @@ class PostController extends PageController
/** /**
* @UrlGenerator(service=PostGenerator::class, method="post") * @UrlGenerator(service=PostGenerator::class, method="post")
*/ */
public function post(Post $post, string $slug): Response public function post(
{ Post $post,
string $slug,
CommentFactory $commentFactory,
Request $request,
EntityManager $entityManager
): Response {
if (Post::DRAFT === $post->getStatus() && !$this->getUser()) { if (Post::DRAFT === $post->getStatus() && !$this->getUser()) {
throw $this->createNotFoundException(); throw $this->createNotFoundException();
} }
@ -43,8 +51,31 @@ class PostController extends PageController
); );
} }
$form = $this->createForm(UserCommentType::class, $commentFactory->create($post));
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$data = $request->request->get($form->getName());
$parentCommentId = (int) $data['parentCommentId'];
foreach ($post->getComments(['id' => $parentCommentId]) as $comment) {
$form->getData()->setParentComment($comment);
}
$entityManager->create($form->getData());
$this->addFlash('success', 'Commentaire publié !');
return $this->redirect($request->getUri());
}
$this->addFlash('error', 'Le formulaire n\'est pas valide.');
}
return $this->defaultRender('blog/post/post.html.twig', [ return $this->defaultRender('blog/post/post.html.twig', [
'post' => $post, 'post' => $post,
'form' => $form->createView(),
]); ]);
} }

View file

@ -3,14 +3,15 @@
namespace App\Entity\Blog; namespace App\Entity\Blog;
use App\Core\Doctrine\Timestampable; use App\Core\Doctrine\Timestampable;
use App\Core\Entity\EntityInterface;
use App\Repository\Blog\CommentRepository; use App\Repository\Blog\CommentRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use App\Core\Entity\EntityInterface;
/** /**
* @ORM\Entity(repositoryClass=CommentRepository::class) * @ORM\Entity(repositoryClass=CommentRepository::class)
* @ORM\HasLifecycleCallbacks
*/ */
class Comment implements EntityInterface class Comment implements EntityInterface
{ {
@ -56,6 +57,7 @@ class Comment implements EntityInterface
/** /**
* @ORM\ManyToOne(targetEntity=Comment::class, inversedBy="comments") * @ORM\ManyToOne(targetEntity=Comment::class, inversedBy="comments")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/ */
private $parentComment; private $parentComment;
@ -159,11 +161,21 @@ class Comment implements EntityInterface
} }
/** /**
* @return Collection|self[] * @return Collection|Comment[]
*/ */
public function getComments(): Collection public function getComments(array $criteria = []): Collection
{ {
return $this->comments; $collection = new ArrayCollection();
foreach ($this->comments as $comment) {
if (isset($criteria['isActive']) && $comment->getIsActive() !== $criteria['isActive']) {
continue;
}
$collection->add($comment);
}
return $collection;
} }
public function addComment(self $comment): self public function addComment(self $comment): self
@ -187,4 +199,12 @@ class Comment implements EntityInterface
return $this; return $this;
} }
/**
* Get the avatar URL using gravatar.
*/
public function getAvatar(): string
{
return 'https://secure.gravatar.com/avatar/'.md5($this->getEmail()).'.jpg?s=90&d=retro';
}
} }

View file

@ -4,7 +4,6 @@ namespace App\Entity\Blog;
use App\Core\Doctrine\Timestampable; use App\Core\Doctrine\Timestampable;
use App\Core\Entity\EntityInterface; use App\Core\Entity\EntityInterface;
use App\Entity\User;
use App\Repository\Blog\PostRepository; use App\Repository\Blog\PostRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
@ -23,6 +22,9 @@ class Post implements EntityInterface
{ {
use Timestampable; use Timestampable;
const DRAFT = 0;
const PUBLISHED = 1;
/** /**
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
@ -110,9 +112,6 @@ class Post implements EntityInterface
*/ */
private $comments; private $comments;
const DRAFT = 0;
const PUBLISHED = 1;
public function __construct() public function __construct()
{ {
$this->categories = new ArrayCollection(); $this->categories = new ArrayCollection();
@ -343,9 +342,27 @@ class Post implements EntityInterface
/** /**
* @return Collection|Comment[] * @return Collection|Comment[]
*/ */
public function getComments(): Collection public function getComments(array $criteria = []): Collection
{ {
return $this->comments; $collection = new ArrayCollection();
foreach ($this->comments as $comment) {
if (isset($criteria['isActive']) && $comment->getIsActive() !== $criteria['isActive']) {
continue;
}
if (isset($criteria['isFirstLevel']) && $criteria['isFirstLevel'] !== (null === $comment->getParentComment())) {
continue;
}
if (isset($criteria['id']) && $comment->getId() !== $criteria['id']) {
continue;
}
$collection->add($comment);
}
return $collection;
} }
public function addComment(Comment $comment): self public function addComment(Comment $comment): self

View file

@ -0,0 +1,54 @@
<?php
namespace App\EventSuscriber\Blog;
use App\Core\Entity\EntityInterface;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
use App\Core\Notification\MailNotifier;
use App\Entity\Blog\Comment;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* class CommentEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CommentEventSubscriber extends EntityManagerEventSubscriber
{
protected MailNotifier $notifier;
protected UrlGeneratorInterface $urlGenerator;
public function __construct(MailNotifier $notifier, UrlGeneratorInterface $urlGenerator)
{
$this->notifier = $notifier;
$this->urlGenerator = $urlGenerator;
}
public function support(EntityInterface $entity)
{
return $entity instanceof Comment;
}
public function onCreate(EntityManagerEvent $event)
{
if (!$this->support($event->getEntity())) {
return;
}
$this->notifier
->init()
->addRecipient('simon@deblan.fr')
->setSubject('[Deblan] Nouveau commentaire')
->notify('mail/comment.html.twig', [
'post' => $event->getEntity()->getPost(),
'links' => [
'post' => $this->urlGenerator->generate('blog_menu_post', [
'post' => $event->getEntity()->getPost()->getId(),
'slug' => $event->getEntity()->getPost()->getSlug(),
], UrlGeneratorInterface::ABSOLUTE_URL).'#review-'.$event->getEntity()->getId(),
],
], 'text/plain')
;
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace App\Factory\Blog;
use App\Entity\Blog\Comment;
use App\Entity\Blog\Post;
/**
* class CommentFactory.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CommentFactory
{
public function create(Post $post): Comment
{
$entity = new Comment();
$entity
->setPost($post)
->setIsActive(true);
return $entity;
}
}

View file

@ -0,0 +1,93 @@
<?php
namespace App\Form\Blog;
use App\Entity\Blog\Comment;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
class UserCommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'author',
TextType::class,
[
'required' => true,
'label' => 'Auteur',
'attr' => [
],
'constraints' => [
new NotBlank(),
],
]
);
$builder->add(
'website',
UrlType::class,
[
'required' => false,
'label' => 'Site web',
'attr' => [
],
'constraints' => [
new Url(),
],
]
);
$builder->add(
'email',
EmailType::class,
[
'label' => 'E-mail (non publié)',
'required' => false,
'attr' => [
],
'constraints' => [
new Email(),
],
]
);
$builder->add(
'content',
TextareaType::class,
[
'label' => 'Commentaire',
'required' => true,
'attr' => [
],
'constraints' => [
new NotBlank(),
],
]
);
$builder->add(
'parentCommentId',
HiddenType::class,
[
'mapped' => false,
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class,
]);
}
}

View file

@ -0,0 +1,46 @@
{% set level = min(7, level) %}
{% set col = 12 - (level - 1) %}
<div class="review col-12 offset-{{ level - 1 }} " id="review-{{ comment.id }}">
<ul class="list--unstyled grid">
<li class="review-avatar">
<img src="{{ comment.avatar }}" alt="{{ comment.author }}" title="{{ comment.author }}" class="border round">
</li>
<li class="review-content">
<ul class="list--unstyled">
<li class="review-header">
<strong>
<a rel="author" href="{{ comment.website ? comment.website : ('#review-' ~ comment.id) }}">{{ comment.author }}</a>,
</strong>
<a class="review-anchor-link" href="#review-{{ comment.id }}">
<time datetime="{{ comment.createdAt|date("Y-m-d") }}" title="{{ comment.createdAt|date("r") }}">
{{- comment.createdAt|date("d/m/Y à H\\hi") -}}
</time>
</a>
</li>
<li class="review-body">
{% if comment.createdAt.timestamp > 1538118768 %} {# 28/09/2018 #}
{{- comment.content|markdown('comment') -}}
{% else %}
{{- comment.content|comment -}}
{% endif %}
</li>
<li class="review-footer">
<a href="#form" class="button small" data-answer="{{ comment.id }}">
<span class="deblan-icon deblan-icon-response" data-answer="{{ comment.id }}"></span>
{{- 'Répondre' -}}
</a>
</li>
</ul>
</li>
</ul>
</div>
{% set level = level + 1 %}
{% for child in comment.comments %}
{{ include('blog/post/_comment.html.twig', {level: level, comment: child}) }}
{% endfor %}

View file

@ -97,24 +97,26 @@
</div> </div>
{% if full %} {% if full %}
{#
<div class="col-12"> <div class="col-12">
<div class="reviews"> <div class="reviews">
<hr> <hr>
{% set hasActiveComments = post.hasActiveComments(true) %} {% set comments = post.comments({
isActive: true,
isFirstLevel: true
}) %}
{% if hasActiveComments %} {% if comments|length %}
<div class="grid"> <div class="grid">
{% for comment in post.orderedComments(true) %} {% for comment in comments %}
{{ include('DeblanBlogBundle:skin2018:_comment.html.twig', {comment: comment, level: 1}) }} {{ include('blog/post/_comment.html.twig', {comment: comment, level: 1}) }}
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
<div class="grid" id="form"> <div class="grid" id="form">
<form class="form col-12" method="POST" data-form-bot action="{{ path('form_without_javascript', {page: app.request.uri}) }}"> <form class="form col-12" method="POST" data-form-bot action="{{ safe_url('blog_tech_form_without_javascript', {page: app.request.uri}) }}">
{% if hasActiveComments %} {% if comments|length %}
<hr> <hr>
{% endif %} {% endif %}
@ -142,7 +144,7 @@
<div class="field"> <div class="field">
<p class="no-margin"> <p class="no-margin">
{{- 'Votre commentaire - Vous pouvez utiliser du markdown ' }} {{- 'Votre commentaire - Vous pouvez utiliser du markdown ' }}
[<a title="Afficher l'aide" href="{{ cms_path('mardkown_help') }}" target="_blank">?</a>] [<a title="Afficher l'aide" href="{{ safe_path('blog_tech_mardown') }}" target="_blank">?</a>]
</p> </p>
{{ form_errors(form.content) }} {{ form_errors(form.content) }}
@ -164,11 +166,6 @@
</div> </div>
</div> </div>
</div> </div>
<div class="field">
{{ form_errors(form.follow) }}
{{ form_widget(form.follow) }}
{{ form_label(form.follow) }}
</div>
<div class="field"> <div class="field">
<input type="checkbox" id="rgpd" required> <input type="checkbox" id="rgpd" required>
@ -187,7 +184,6 @@
</div> </div>
</div> </div>
</div> </div>
#}
{% if not post.isQuick %} {% if not post.isQuick %}
{% {%

View file

@ -0,0 +1,4 @@
Un nouveau commentaire a été déposé sur l'article suivant :
{{ post.title|raw }}
{{ links.post }}