add comment admin

This commit is contained in:
Simon Vieille 2021-03-30 19:35:49 +02:00
parent 703d07fa4a
commit 6034081a93
15 changed files with 759 additions and 0 deletions

View File

@ -0,0 +1,137 @@
<?php
namespace App\Controller\Blog;
use App\Core\Controller\Admin\AdminController;
use App\Core\Manager\EntityManager;
use App\Entity\Blog\Comment as Entity;
use App\Factory\Blog\CommentFactory as EntityFactory;
use App\Form\Blog\Filter\CommentFilterType;
use App\Form\Blog\CommentType as EntityType;
use App\Repository\Blog\CommentRepositoryQuery as RepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/admin/blog/comment")
*/
class CommentAdminController extends AdminController
{
protected $filters = [];
/**
* @Route("/{page}", name="admin_blog_comment_index", requirements={"page": "\d+"})
*/
public function index(int $page = 1, RepositoryQuery $query, Request $request, Session $session): Response
{
$this->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);
}
}
}

View File

@ -0,0 +1,145 @@
<?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;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use App\Entity\Blog\Post;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class CommentType 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(
'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,
]);
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace App\Form\Blog\Filter;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Entity\Blog\Post;
use Symfony\Component\Form\ChoiceList\ChoiceList;
class CommentFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->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,
]);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Repository\Blog;
use App\Core\Repository\RepositoryQuery;
use Knp\Component\Pager\PaginatorInterface;
/**
* class CommentRepositoryQuery.
*
* @author Simon Vieille <simon@deblan.fr>
*/
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;
}
}

View File

@ -1,5 +1,7 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">

View File

@ -1,5 +1,7 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 {% if pager.paginationData.pageCount < 2 %}pb-5{% endif %}">
<div class="d-flex">

View File

@ -1,5 +1,7 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">

View File

@ -1,5 +1,7 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Catégories'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">

View File

@ -0,0 +1,30 @@
<div class="row">
<div class="col-3 p-3">
<div class="row">
{% for item in ['author', 'website', 'email'] %}
<div class="col-12">
{{ form_row(form[item]) }}
</div>
{% endfor %}
</div>
</div>
<div class="col-7 p-3">
<div class="row">
{% for item in ['content'] %}
<div class="col-12">
{{ form_row(form[item]) }}
</div>
{% endfor %}
</div>
</div>
<div class="col-2 p-3">
<div class="row">
{% for item in ['post', 'parentComment', 'isActive'] %}
<div class="col-12">
{{ form_row(form[item]) }}
</div>
{% endfor %}
</div>
</div>
</div>

View File

@ -0,0 +1,59 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">
<div class="mr-auto w-50">
<h1 class="display-5">{{ entity.author }}</h1>
</div>
<div class="ml-auto">
<div class="btn-group">
<a href="{{ path('admin_blog_comment_index') }}" class="btn btn-light">
<span class="fa fa-list pr-1"></span>
Retour à la liste
</a>
<a href="{{ path('admin_blog_comment_show', {entity: entity.id}) }}" class="btn btn-secondary">
<span class="fa fa-eye pr-1"></span>
Voir
</a>
<button type="submit" form="form-main" class="btn btn-primary">
<span class="fa fa-save pr-1"></span>
Enregistrer
</button>
<button type="button" class="btn btn-white dropdown-toggle dropdown-toggle-hide-after" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="font-weight-bold">
⋅⋅⋅
</span>
</button>
<div class="dropdown-menu dropdown-menu-right">
<button type="submit" form="form-delete" class="dropdown-item">
Supprimer
</button>
</div>
</div>
</div>
</div>
</div>
<form action="{{ app.request.uri }}" method="post" id="form-main" enctype="multipart/form-data">
<div class="tab-content">
<div class="tab-pane active">
<div class="tab-form">
{{ include('blog/comment_admin/_form.html.twig') }}
</div>
</div>
</div>
{{ form_rest(form) }}
</form>
<form method="post" action="{{ path('admin_blog_comment_delete', {entity: entity.id}) }}" id="form-delete" data-form-confirm>
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ entity.id) }}">
</form>
{% endblock %}

View File

@ -0,0 +1,21 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Filtres
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="{{ path('admin_blog_comment_index') }}" id="form-filters" method="GET">
{{ form_widget(form) }}
</form>
</div>
<div class="modal-footer">
<a href="{{ path('admin_blog_comment_index', {post_filter: 0}) }}" class="btn btn-secondary">{{ 'Vider'|trans }}</a>
<button type="submit" form="form-filters" class="btn btn-primary">{{ 'Filter'|trans }}</button>
</div>
</div>
</div>

View File

@ -0,0 +1,86 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5">
<div class="d-flex">
<div class="mr-auto w-50">
<h1 class="display-5">Articles</h1>
</div>
</div>
<div class="row pb-3">
<div class="col-auto ml-auto {% if pager.getPaginationData.pageCount > 1 %}mr-3{% endif %}">
<button data-modal="{{ path('admin_blog_comment_filters') }}" class="btn btn-sm btn-secondary">
Filtrer {% if hasFilters %}(oui){% endif %}
</button>
</div>
<div class="col-auto">
{{ knp_pagination_render(pager) }}
</div>
</div>
</div>
<table class="table" data-table-fixed>
<thead class="thead-light">
<tr>
<th class="col-6">Auteur</th>
<th class="col-2">Date</th>
<th class="col-1">Statut</th>
<th class="col-3 text-right">Actions</th>
</tr>
</thead>
<tbody>
{% for item in pager %}
{% set edit = path('admin_blog_comment_edit', {entity: item.id}) %}
{% set show = path('admin_blog_comment_show', {entity: item.id}) %}
<tr>
<td class="col-6">
<a href="{{ show }}" class="font-weight-bold text-body d-block">
{{ item.author }}
</a>
Dans <a href="">{{ item.post.title }}</a>
</td>
<td class="col-2">
<span class="btn btn-sm btn-light">
<span class="fa fa-calendar-alt text-black-50 mr-1"></span>
{{ item.updatedAt|date('d/m/Y H:i') }}
</span>
</td>
<td class="col-1">
<button class="btn btn-sm btn-{{ item.isActive ? 'success' : 'warning' }}">
{{ item.isActive ? 'En ligne' : 'Hors ligne' }}
</button>
</td>
<td class="col-3 text-right">
<a href="{{ edit }}" class="btn btn-sm btn-primary mr-1">
<span class="fa fa-edit"></span>
</a>
<button type="submit" form="form-delete-{{ item.id }}" class="btn btn-sm btn-danger">
<span class="fa fa-trash"></span>
</button>
<form method="post" action="{{ path('admin_blog_comment_delete', {entity: item.id}) }}" id="form-delete-{{ item.id }}" data-form-confirm>
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ item.id) }}">
</form>
</td>
</tr>
{% else %}
<tr>
<td class="col-12 text-center p-4 text-black-50">
<div class="display-1">
<span class="fa fa-search"></span>
</div>
<div class="display-5 mt-3">
Aucun résultat
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -0,0 +1,41 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">
<div class="mr-auto w-50">
<h1 class="display-5">Nouvel article</h1>
</div>
<div class="ml-auto">
<div class="btn-group">
<a href="{{ path('admin_blog_comment_index') }}" class="btn btn-light">
<span class="fa fa-list pr-1"></span>
Retour à la liste
</a>
<button type="submit" form="form-main" class="btn btn-primary">
<span class="fa fa-save pr-1"></span>
Enregistrer
</button>
</div>
</div>
</div>
</div>
<form action="{{ app.request.uri }}" method="post" id="form-main" enctype="multipart/form-data">
<div class="tab-content">
<div class="tab-pane active">
<div class="tab-form">
{{ include('blog/comment_admin/_form.html.twig') }}
</div>
</div>
</div>
{{ form_rest(form) }}
</form>
{% endblock %}

View File

@ -0,0 +1,84 @@
{% extends '@Core/admin/layout.html.twig' %}
{% block title %}{{ 'Commentaires'|trans }} - {{ parent() }}{% endblock %}
{% block body %}
<div class="bg-light pl-5 pr-4 pt-5 pb-5">
<div class="d-flex">
<div class="mr-auto w-50">
<h1 class="display-5">{{ entity.author }}</h1>
</div>
<div class="ml-auto">
<div class="btn-group">
<a href="{{ path('admin_blog_comment_index') }}" class="btn btn-secondary">
<span class="fa fa-list pr-1"></span>
Retour à la liste
</a>
<a href="{{ path('admin_blog_comment_edit', {entity: entity.id}) }}" class="btn btn-primary">
<span class="fa fa-edit pr-1"></span>
Éditer
</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-4 p-3">
<ul class="list-group">
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Auteur</span>
{{ entity.author }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Site web</span>
{{ entity.website }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">E-mail</span>
{{ entity.email }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Article</span>
{{ entity.post.title }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Réponse à</span>
{{ entity.parentComment ? entity.parentComment.author : '-' }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Status</span>
{{ entity.isActive ? 'En ligne' : 'Hors ligne' }}
</li>
</ul>
</div>
<div class="col-8 p-3">
<div class="font-weight-bold pb-2">Contenu</div>
{% if entity.createdAt.timestamp > 1538118768 %} {# 28/09/2018 #}
{{- entity.content|markdown('comment') -}}
{% else %}
{{- entity.content|comment -}}
{% endif %}
</div>
</div>
{% endblock %}
{% block css %}
{{ parent() }}
<style>
#post-content img {
max-width: 100%;
}
</style>
{% endblock %}

View File

@ -0,0 +1,18 @@
<link rel="apple-touch-icon" sizes="57x57" href="{{ asset('build/webapp/apple-icon-57x57.png') }}">
<link rel="apple-touch-icon" sizes="60x60" href="{{ asset('build/webapp/apple-icon-60x60.png') }}">
<link rel="apple-touch-icon" sizes="72x72" href="{{ asset('build/webapp/apple-icon-72x72.png') }}">
<link rel="apple-touch-icon" sizes="76x76" href="{{ asset('build/webapp/apple-icon-76x76.png') }}">
<link rel="apple-touch-icon" sizes="114x114" href="{{ asset('build/webapp/apple-icon-114x114.png') }}">
<link rel="apple-touch-icon" sizes="120x120" href="{{ asset('build/webapp/apple-icon-120x120.png') }}">
<link rel="apple-touch-icon" sizes="144x144" href="{{ asset('build/webapp/apple-icon-144x144.png') }}">
<link rel="apple-touch-icon" sizes="152x152" href="{{ asset('build/webapp/apple-icon-152x152.png') }}">
<link rel="apple-touch-icon" sizes="180x180" href="{{ asset('build/webapp/apple-icon-180x180.png') }}">
<link rel="icon" type="image/png" href="{{ asset('build/webapp/favicon.png') }}" >
<link rel="icon" type="image/png" sizes="192x192" href="{{ asset('build/webapp/android-icon-192x192.png') }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ asset('build/webapp/favicon.png') }}">
<link rel="icon" type="image/png" sizes="96x96" href="{{ asset('build/webapp/favicon-96x96.png') }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ asset('build/webapp/favicon-16x16.png') }}">
<link rel="manifest" href="{{ asset('build/webapp/manifest.json') }}">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="{{ asset('build/webapp/ms-icon-144x144.png') }}">
<meta name="theme-color" content="#ffffff">