update crud of categories

This commit is contained in:
Simon Vieille 2021-05-12 12:45:09 +02:00
parent 1831a40d3b
commit 77c234987d
6 changed files with 128 additions and 323 deletions

View file

@ -12,24 +12,72 @@ use App\Repository\Blog\PostRepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Core\Controller\Admin\Crud\CrudController;
use App\Core\Crud\CrudConfiguration;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Core\Crud\Field\TextField;
use App\Form\Blog\CategoryType;
use App\Core\Crud\Field\ButtonField;
use App\Core\Entity\EntityInterface;
/**
* @Route("/admin/blog/category")
*/
class CategoryAdminController extends AdminController
class CategoryAdminController extends CrudController
{
public function getConfiguration(): CrudConfiguration
{
return CrudConfiguration::create()
->setPageTitle('index', 'Catégories')
->setPageTitle('edit', '{title}')
->setPageTitle('new', 'Nouvelle catégorie')
->setPageTitle('show', '{title}')
->setPageRoute('index', 'admin_blog_category_index')
->setPageRoute('new', 'admin_blog_category_new')
->setPageRoute('edit', 'admin_blog_category_edit')
->setPageRoute('show', 'admin_blog_category_show')
->setPageRoute('delete', 'admin_blog_category_delete')
->setPageRoute('filter', 'admin_blog_category_filter')
->setForm('edit', CategoryType::class, [])
->setForm('new', CategoryType::class)
// ->setForm('filter', FooFilterType::class)
->setMaxPerPage('index', 100)
->setAction('index', 'new', true)
->setAction('index', 'show', true)
->setAction('index', 'edit', true)
->setAction('index', 'delete', true)
->setAction('edit', 'back', true)
->setAction('edit', 'show', true)
->setAction('edit', 'delete', true)
->setField('index', 'Titre', TextField::class, [
'property' => 'title',
])
->setField('index', 'Articles', ButtonField::class, [
'property_builder' => function(EntityInterface $entity) {
$count = $entity->getPosts()->count();
return '<strong>'.$count.'</strong> article'.($count > 1 ? 's' : '');
},
'button_attr' => ['class' => 'btn btn-sm btn-light border'],
'raw' => true,
])
;
}
/**
* @Route("/{page}", name="admin_blog_category_index", requirements={"page": "\d+"})
*/
public function index(int $page = 1, RepositoryQuery $query, Request $request): Response
public function index(int $page = 1, RepositoryQuery $query, Request $request, Session $session): Response
{
$pager = $query
->orderBy('.title', 'ASC')
->paginate($page, 100);
$query->orderBy('.title', 'ASC');
return $this->render('blog/category_admin/index.html.twig', [
'pager' => $pager,
]);
return $this->doIndex($page, $query, $request, $session);
}
/**
@ -37,27 +85,9 @@ class CategoryAdminController extends AdminController
*/
public function new(EntityFactory $factory, EntityManager $entityManager, Request $request): Response
{
$entity = $factory->create();
$form = $this->createForm(EntityType::class, $entity);
$this->getConfiguration()->setView('newForm', 'blog/category_admin/_form.html.twig');
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$entityManager->create($entity);
$this->addFlash('success', 'Donnée enregistrée.');
return $this->redirectToRoute('admin_blog_category_edit', [
'entity' => $entity->getId(),
]);
}
$this->addFlash('warning', 'Le formulaire est invalide.');
}
return $this->render('blog/category_admin/new.html.twig', [
'form' => $form->createView(),
'entity' => $entity,
]);
return $this->doNew($factory->create(), $entityManager, $request);
}
/**
@ -65,26 +95,9 @@ class CategoryAdminController extends AdminController
*/
public function edit(Entity $entity, EntityManager $entityManager, Request $request): Response
{
$form = $this->createForm(EntityType::class, $entity);
$this->getConfiguration()->setView('editForm', 'blog/category_admin/_form.html.twig');
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$entityManager->update($entity);
$this->addFlash('success', 'Donnée enregistrée.');
return $this->redirectToRoute('admin_blog_category_edit', [
'entity' => $entity->getId(),
]);
}
$this->addFlash('warning', 'Le formulaire est invalide.');
}
return $this->render('blog/category_admin/edit.html.twig', [
'form' => $form->createView(),
'entity' => $entity,
]);
return $this->doEdit($entity, $entityManager, $request);
}
/**
@ -99,10 +112,10 @@ class CategoryAdminController extends AdminController
->paginate(1, 10)
;
return $this->render('blog/category_admin/show.html.twig', [
'entity' => $entity,
'posts' => $posts,
]);
$this->getConfiguration()->setView('showEntity', 'blog/category_admin/_show.html.twig');
$this->getConfiguration()->addViewData('show', 'posts', $posts);
return $this->doShow($entity);
}
/**
@ -110,13 +123,7 @@ class CategoryAdminController extends AdminController
*/
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', 'Données supprimée..');
}
return $this->redirectToRoute('admin_blog_category_index');
return $this->doDelete($entity, $entityManager, $request);
}
public function getSection(): string

View file

@ -0,0 +1,63 @@
<div class="row">
<div class="col-md-4 pr-3">
<ul class="list-group">
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Titre</span>
{{ entity.title }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Sous-titre</span>
{{ entity.subTitle }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Slug</span>
{{ absolute_url(entity.slug) }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">En ligne</span>
{{ entity.isActive ? 'oui' : 'non' }}
</li>
</ul>
</div>
<div class="col-md-8">
<div class="font-weight-bold pb-2">Description</div>
{{ entity.description|raw|nl2br }}
</div>
<div class="col-md-12 mt-3">
<table class="table border">
<thead class="thead-light">
<tr>
<th>Derniers articles</th>
</tr>
</thead>
<tbody>
{% for item in configuration.viewDatas('show').posts %}
<tr>
<td>
<a href="{{ path('admin_blog_post_show', {entity: item.id}) }}" class="text-body">
{{ item.title }}
</a>
</td>
</tr>
{% else %}
<tr>
<td class="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>
</div>
</div>

View file

@ -1,57 +0,0 @@
{% 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="crud-header">
<h1 class="crud-header-title">{{ entity.title }}</h1>
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_blog_category_index') }}" class="btn btn-light">
<span class="fa fa-list pr-1"></span>
Retour à la liste
</a>
<a href="{{ path('admin_blog_category_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/category_admin/_form.html.twig') }}
</div>
</div>
</div>
{{ form_rest(form) }}
</form>
<form method="post" action="{{ path('admin_blog_category_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

@ -1,79 +0,0 @@
{% 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="crud-header">
<h1 class="crud-header-title">Catégories</h1>
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_blog_category_new') }}" class="btn btn-primary">
<span class="fa fa-plus pr-1"></span>
Nouveau
</a>
</div>
</div>
</div>
{{ knp_pagination_render(pager) }}
</div>
<div class="table-responsive">
<table class="table">
<thead class="thead-light">
<tr>
<th class="col-6 miw-200">Titre</th>
<th class="col-4 miw-150">Articles</th>
<th class="col-2 miw-100 text-right">Actions</th>
</tr>
</thead>
<tbody>
{% for item in pager %}
{% set edit = path('admin_blog_category_edit', {entity: item.id}) %}
{% set show = path('admin_blog_category_show', {entity: item.id}) %}
<tr data-dblclick="{{ edit }}">
<td class="col-6 miw-200">
<a href="{{ show }}" class="font-weight-bold text-body d-block">
{{ item.title }}
</a>
</td>
<td class="col-4 miw-150">
<a href="" class="btn btn-sm btn-light">
{% set postsCount = item.posts|length %}
<span class="font-weight-bold">{{ postsCount }}</span> {{ postsCount < 2 ? 'article' : 'articles' }}
</a>
</td>
<td class="col-2 miw-100 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_category_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>
</div>
{% endblock %}

View file

@ -1,39 +0,0 @@
{% 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="crud-header">
<h1 class="crud-header-title">Nouvelle catégorie</h1>
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_blog_category_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/category_admin/_form.html.twig') }}
</div>
</div>
</div>
{{ form_rest(form) }}
</form>
{% endblock %}

View file

@ -1,90 +0,0 @@
{% 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="crud-header">
<h1 class="crud-header-title">{{ entity.title }}</h1>
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_blog_category_index') }}" class="btn btn-secondary">
<span class="fa fa-list pr-1"></span>
Retour à la liste
</a>
<a href="{{ path('admin_blog_category_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-md-4 p-3">
<ul class="list-group">
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Titre</span>
{{ entity.title }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Sous-titre</span>
{{ entity.subTitle }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">Slug</span>
{{ absolute_url(entity.slug) }}
</li>
<li class="list-group-item">
<span class="font-weight-bold pb-2 d-block">En ligne</span>
{{ entity.isActive ? 'oui' : 'non' }}
</li>
</ul>
</div>
<div class="col-md-8 p-3">
<div class="font-weight-bold pb-2">Description</div>
{{ entity.description|raw|nl2br }}
</div>
<div class="col-md-12">
<table class="table">
<thead class="thead-light">
<tr>
<th>Derniers articles</th>
</tr>
</thead>
<tbody>
{% for item in posts %}
<tr>
<td>
<a href="{{ path('admin_blog_post_show', {entity: item.id}) }}" class="text-body">
{{ item.title }}
</a>
</td>
</tr>
{% else %}
<tr>
<td class="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>
</div>
</div>
{% endblock %}