From 6034081a937320f69ce91d3d361f8e0cd2d07486 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Mar 2021 19:35:49 +0200 Subject: [PATCH] add comment admin --- .../Blog/CommentAdminController.php | 137 +++++++++++++++++ src/Form/Blog/CommentType.php | 145 ++++++++++++++++++ src/Form/Blog/Filter/CommentFilterType.php | 86 +++++++++++ .../Blog/CommentRepositoryQuery.php | 44 ++++++ templates/blog/category_admin/edit.html.twig | 2 + templates/blog/category_admin/index.html.twig | 2 + templates/blog/category_admin/new.html.twig | 2 + templates/blog/category_admin/show.html.twig | 2 + templates/blog/comment_admin/_form.html.twig | 30 ++++ templates/blog/comment_admin/edit.html.twig | 59 +++++++ .../blog/comment_admin/filters.html.twig | 21 +++ templates/blog/comment_admin/index.html.twig | 86 +++++++++++ templates/blog/comment_admin/new.html.twig | 41 +++++ templates/blog/comment_admin/show.html.twig | 84 ++++++++++ templates/core/admin/module/metas.html.twig | 18 +++ 15 files changed, 759 insertions(+) create mode 100644 src/Controller/Blog/CommentAdminController.php create mode 100644 src/Form/Blog/CommentType.php create mode 100644 src/Form/Blog/Filter/CommentFilterType.php create mode 100644 src/Repository/Blog/CommentRepositoryQuery.php create mode 100644 templates/blog/comment_admin/_form.html.twig create mode 100644 templates/blog/comment_admin/edit.html.twig create mode 100644 templates/blog/comment_admin/filters.html.twig create mode 100644 templates/blog/comment_admin/index.html.twig create mode 100644 templates/blog/comment_admin/new.html.twig create mode 100644 templates/blog/comment_admin/show.html.twig create mode 100644 templates/core/admin/module/metas.html.twig diff --git a/src/Controller/Blog/CommentAdminController.php b/src/Controller/Blog/CommentAdminController.php new file mode 100644 index 0000000..2b14f7c --- /dev/null +++ b/src/Controller/Blog/CommentAdminController.php @@ -0,0 +1,137 @@ +updateFilters($request, $session); + + $pager = $query + ->orderBy('.id', 'DESC') + ->useFilters($this->filters) + ->paginate($page) + ; + + return $this->render('blog/comment_admin/index.html.twig', [ + 'pager' => $pager, + 'hasFilters' => !empty($this->filters), + ]); + } + + /** + * @Route("/edit/{entity}", name="admin_blog_comment_edit") + */ + public function edit(Entity $entity, EntityManager $entityManager, Request $request): Response + { + $form = $this->createForm(EntityType::class, $entity); + + if ($request->isMethod('POST')) { + $form->handleRequest($request); + + if ($form->isValid()) { + $entityManager->update($entity); + $this->addFlash('success', 'The form is not valid.'); + + return $this->redirectToRoute('admin_blog_comment_edit', [ + 'entity' => $entity->getId(), + ]); + } + $this->addFlash('warning', 'The form is not valid.'); + } + + return $this->render('blog/comment_admin/edit.html.twig', [ + 'form' => $form->createView(), + 'entity' => $entity, + ]); + } + + /** + * @Route("/show/{entity}", name="admin_blog_comment_show") + */ + public function show(Entity $entity): Response + { + return $this->render('blog/comment_admin/show.html.twig', [ + 'entity' => $entity, + ]); + } + + /** + * @Route("/filters", name="admin_blog_comment_filters") + */ + public function filters(Session $session): Response + { + $form = $this->createForm(CommentFilterType::class); + $form->submit($session->get('comment_filter')); + + return $this->render('blog/comment_admin/filters.html.twig', [ + 'form' => $form->createView(), + ]); + } + + /** + * @Route("/delete/{entity}", name="admin_blog_comment_delete", methods={"DELETE"}) + */ + public function delete(Entity $entity, EntityManager $entityManager, Request $request): Response + { + if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) { + $entityManager->delete($entity); + + $this->addFlash('success', 'The data has been removed.'); + } + + return $this->redirectToRoute('admin_blog_comment_index'); + } + + public function getSection(): string + { + return 'blog_comment'; + } + + protected function updateFilters(Request $request, Session $session) + { + if ($request->query->has('comment_filter')) { + $filters = $request->query->get('comment_filter'); + + if ('0' === $filters) { + $filters = []; + } + } elseif ($session->has('comment_filter')) { + $filters = $session->get('comment_filter'); + } else { + $filters = []; + } + + $form = $this->createForm(CommentFilterType::class); + $form->submit($filters); + + if (empty($filters)) { + $this->filters = $filters; + $session->set('comment_filter', $filters); + } elseif ($form->isValid()) { + $this->filters = $form->getData(); + $session->set('comment_filter', $filters); + } + } +} diff --git a/src/Form/Blog/CommentType.php b/src/Form/Blog/CommentType.php new file mode 100644 index 0000000..dc6f12e --- /dev/null +++ b/src/Form/Blog/CommentType.php @@ -0,0 +1,145 @@ +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( + 'post', + EntityType::class, + [ + 'label' => 'Article', + 'class' => Post::class, + 'choice_label' => 'title', + 'required' => true, + 'multiple' => false, + 'attr' => [ + ], + 'query_builder' => function (EntityRepository $repo) { + return $repo->createQueryBuilder('p') + ->orderBy('p.id', 'DESC') + ; + }, + 'constraints' => [ + new NotBlank(), + ], + ] + ); + + $builder->add( + 'parentComment', + EntityType::class, + [ + 'label' => 'Réponse à', + 'class' => Comment::class, + 'choice_label' => 'author', + 'required' => false, + 'multiple' => false, + 'attr' => [ + ], + 'query_builder' => function (EntityRepository $repo) { + return $repo->createQueryBuilder('c') + ->orderBy('c.id', 'DESC') + ; + }, + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'isActive', + CheckboxType::class, + [ + 'label' => 'En ligne', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Comment::class, + ]); + } +} diff --git a/src/Form/Blog/Filter/CommentFilterType.php b/src/Form/Blog/Filter/CommentFilterType.php new file mode 100644 index 0000000..c96efad --- /dev/null +++ b/src/Form/Blog/Filter/CommentFilterType.php @@ -0,0 +1,86 @@ +add( + 'author', + TextType::class, + [ + 'required' => false, + 'label' => 'Auteur', + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'website', + TextType::class, + [ + 'required' => false, + 'label' => 'Site web', + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'email', + TextType::class, + [ + 'label' => 'E-mail (non publié)', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'post', + EntityType::class, + [ + 'label' => 'Article', + 'class' => Post::class, + 'choice_label' => 'title', + 'choice_value' => 'id', + 'required' => false, + 'attr' => [ + ], + 'query_builder' => function (EntityRepository $repo) { + return $repo->createQueryBuilder('p') + ->orderBy('p.id', 'DESC') + ; + }, + 'constraints' => [ + ], + ] + ); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => null, + 'csrf_protection' => false, + ]); + } +} diff --git a/src/Repository/Blog/CommentRepositoryQuery.php b/src/Repository/Blog/CommentRepositoryQuery.php new file mode 100644 index 0000000..5838af6 --- /dev/null +++ b/src/Repository/Blog/CommentRepositoryQuery.php @@ -0,0 +1,44 @@ + + */ +class CommentRepositoryQuery extends RepositoryQuery +{ + public function __construct(CommentRepository $repository, PaginatorInterface $paginator) + { + parent::__construct($repository, 'c', $paginator); + } + + public function useFilters(array $filters) + { + foreach ($filters as $name => $value) { + if (null === $value) { + continue; + } + + if (is_int($value)) { + $this->andWhere('.'.$name.' = :'.$name); + $this->setParameter(':'.$name, $value); + } elseif (is_string($value)) { + $this->andWhere('.'.$name.' LIKE :'.$name); + $this->setParameter(':'.$name, '%'.$value.'%'); + } else { + if ('post' === $name) { + $this + ->andWhere('.post = :postId') + ->setParameter(':postId', $value->getId()); + } + } + } + + return $this; + } +} diff --git a/templates/blog/category_admin/edit.html.twig b/templates/blog/category_admin/edit.html.twig index b7be271..c5ffc0b 100644 --- a/templates/blog/category_admin/edit.html.twig +++ b/templates/blog/category_admin/edit.html.twig @@ -1,5 +1,7 @@ {% extends '@Core/admin/layout.html.twig' %} +{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %} + {% block body %}
diff --git a/templates/blog/category_admin/index.html.twig b/templates/blog/category_admin/index.html.twig index c74d72d..b38944f 100644 --- a/templates/blog/category_admin/index.html.twig +++ b/templates/blog/category_admin/index.html.twig @@ -1,5 +1,7 @@ {% extends '@Core/admin/layout.html.twig' %} +{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %} + {% block body %}
diff --git a/templates/blog/category_admin/new.html.twig b/templates/blog/category_admin/new.html.twig index 06aa0e5..feaa3ee 100644 --- a/templates/blog/category_admin/new.html.twig +++ b/templates/blog/category_admin/new.html.twig @@ -1,5 +1,7 @@ {% extends '@Core/admin/layout.html.twig' %} +{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %} + {% block body %}
diff --git a/templates/blog/category_admin/show.html.twig b/templates/blog/category_admin/show.html.twig index a493ea5..ecb2572 100644 --- a/templates/blog/category_admin/show.html.twig +++ b/templates/blog/category_admin/show.html.twig @@ -1,5 +1,7 @@ {% extends '@Core/admin/layout.html.twig' %} +{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %} + {% block body %}
diff --git a/templates/blog/comment_admin/_form.html.twig b/templates/blog/comment_admin/_form.html.twig new file mode 100644 index 0000000..ac1bea2 --- /dev/null +++ b/templates/blog/comment_admin/_form.html.twig @@ -0,0 +1,30 @@ +
+
+
+ {% for item in ['author', 'website', 'email'] %} +
+ {{ form_row(form[item]) }} +
+ {% endfor %} +
+
+
+
+ {% for item in ['content'] %} +
+ {{ form_row(form[item]) }} +
+ {% endfor %} +
+
+
+
+ {% for item in ['post', 'parentComment', 'isActive'] %} +
+ {{ form_row(form[item]) }} +
+ {% endfor %} +
+
+
+ diff --git a/templates/blog/comment_admin/edit.html.twig b/templates/blog/comment_admin/edit.html.twig new file mode 100644 index 0000000..794a6e1 --- /dev/null +++ b/templates/blog/comment_admin/edit.html.twig @@ -0,0 +1,59 @@ +{% extends '@Core/admin/layout.html.twig' %} + +{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %} + +{% block body %} +
+
+
+

{{ entity.author }}

+
+ +
+
+ + + Retour à la liste + + + + Voir + + + + + + +
+
+
+
+ +
+
+
+
+ {{ include('blog/comment_admin/_form.html.twig') }} +
+
+
+ + {{ form_rest(form) }} +
+ +
+ + +
+{% endblock %} diff --git a/templates/blog/comment_admin/filters.html.twig b/templates/blog/comment_admin/filters.html.twig new file mode 100644 index 0000000..509b210 --- /dev/null +++ b/templates/blog/comment_admin/filters.html.twig @@ -0,0 +1,21 @@ + diff --git a/templates/blog/comment_admin/index.html.twig b/templates/blog/comment_admin/index.html.twig new file mode 100644 index 0000000..3003ff7 --- /dev/null +++ b/templates/blog/comment_admin/index.html.twig @@ -0,0 +1,86 @@ +{% extends '@Core/admin/layout.html.twig' %} + +{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %} + +{% block body %} +
+
+
+

Articles

+
+
+ +
+
+ +
+
+ {{ knp_pagination_render(pager) }} +
+
+
+ + + + + + + + + + + + {% for item in pager %} + {% set edit = path('admin_blog_comment_edit', {entity: item.id}) %} + {% set show = path('admin_blog_comment_show', {entity: item.id}) %} + + + + + + + + {% else %} + + + + {% endfor %} + +
AuteurDateStatutActions
+ + {{ item.author }} + + + Dans {{ item.post.title }} + + + + {{ item.updatedAt|date('d/m/Y H:i') }} + + + + + + + + + +
+ + +
+
+
+ +
+
+ Aucun résultat +
+
+{% endblock %} diff --git a/templates/blog/comment_admin/new.html.twig b/templates/blog/comment_admin/new.html.twig new file mode 100644 index 0000000..7f8a582 --- /dev/null +++ b/templates/blog/comment_admin/new.html.twig @@ -0,0 +1,41 @@ +{% extends '@Core/admin/layout.html.twig' %} + +{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %} + +{% block body %} +
+
+
+

Nouvel article

+
+ +
+
+ + + + Retour à la liste + + + +
+
+
+
+ +
+
+
+
+ {{ include('blog/comment_admin/_form.html.twig') }} +
+
+
+ + {{ form_rest(form) }} +
+{% endblock %} diff --git a/templates/blog/comment_admin/show.html.twig b/templates/blog/comment_admin/show.html.twig new file mode 100644 index 0000000..6fb1a85 --- /dev/null +++ b/templates/blog/comment_admin/show.html.twig @@ -0,0 +1,84 @@ +{% extends '@Core/admin/layout.html.twig' %} + +{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %} + +{% block body %} +
+
+
+

{{ entity.author }}

+
+ + +
+
+ +
+
+
    +
  • + Auteur + + {{ entity.author }} +
  • +
  • + Site web + + {{ entity.website }} +
  • +
  • + E-mail + + {{ entity.email }} +
  • +
  • + Article + + {{ entity.post.title }} +
  • +
  • + Réponse à + + {{ entity.parentComment ? entity.parentComment.author : '-' }} +
  • +
  • + Status + + {{ entity.isActive ? 'En ligne' : 'Hors ligne' }} +
  • +
+
+
+
Contenu
+ + {% if entity.createdAt.timestamp > 1538118768 %} {# 28/09/2018 #} + {{- entity.content|markdown('comment') -}} + {% else %} + {{- entity.content|comment -}} + {% endif %} +
+
+{% endblock %} + +{% block css %} + {{ parent() }} + + +{% endblock %} diff --git a/templates/core/admin/module/metas.html.twig b/templates/core/admin/module/metas.html.twig new file mode 100644 index 0000000..c1b0f35 --- /dev/null +++ b/templates/core/admin/module/metas.html.twig @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +