[crud] add step "delete"

This commit is contained in:
Julien Fastré 2020-03-26 13:14:48 +01:00
parent d5e9747e80
commit d4b498e2be
8 changed files with 207 additions and 8 deletions

View File

@ -110,5 +110,6 @@ Master branch
=============
- [translation] in french, replace "Modifier" by "Enregistrer" in the edit form
- [chill entity render] fix error when fallback to default entity render (usage of __toString())
- [chill entity render] fix error when fallback to default entity render (usage of `__toString()`)
- [CRUD] add step delete

View File

@ -33,6 +33,7 @@ use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Chill\MainBundle\CRUD\Resolver\Resolver;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\CRUD\Form\CRUDDeleteEntityForm;
/**
*
@ -54,6 +55,93 @@ class CRUDController extends AbstractController
$this->crudConfig = $config;
}
public function delete(Request $request, $id)
{
return $this->deleteAction('delete', $request, $id);
}
protected function deleteAction(string $action, Request $request, $id, $formClass = null)
{
$this->onPreDelete($action, $request, $id);
$entity = $this->getEntity($action, $id, $request);
$postFetch = $this->onPostFetchEntity($action, $request, $entity);
if ($postFetch instanceof Response) {
return $postFetch;
}
if (NULL === $entity) {
throw $this->createNotFoundException(sprintf("The %s with id %s "
. "is not found"), $this->getCrudName(), $id);
}
$response = $this->checkACL($action, $entity);
if ($response instanceof Response) {
return $response;
}
$response = $this->onPostCheckACL($action, $request, $entity);
if ($response instanceof Response) {
return $response;
}
$form = $this->createFormFor($action, $entity, $formClass);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$this->onPreRemove($action, $entity, $form, $request);
$this->removeEntity($action, $entity, $form, $request);
$this->onPostRemove($action, $entity, $form, $request);
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
$this->onPostFlush($action, $entity, $form, $request);
$this->addFlash('success', $this->generateFormSuccessMessage($action, $entity));
$result = $this->onBeforeRedirectAfterSubmission($action, $entity, $form, $request);
if ($result instanceof Response) {
return $result;
}
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
} elseif ($form->isSubmitted()) {
$this->addFlash('error', $this->generateFormErrorMessage($action, $form));
}
$defaultTemplateParameters = [
'form' => $form->createView(),
'entity' => $entity,
'crud_name' => $this->getCrudName()
];
return $this->render(
$this->getTemplateFor($action, $entity, $request),
$this->generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters)
);
}
protected function onPreDelete(string $action, Request $request) {}
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {}
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
{
$this->getDoctrine()
->getManager()
->remove($entity);
}
/**
* Base method called by index action.
*
@ -673,10 +761,20 @@ class CRUDController extends AbstractController
*/
protected function getFormClassFor($action)
{
if ($action === 'delete') {
return $this->crudConfig[$action]['form_class']
?? $this->getDefaultDeleteFormClass($action);
}
return $this->crudConfig[$action]['form_class']
?? $this->crudConfig['form_class'];
}
protected function getDefaultDeleteFormClass($action)
{
return CRUDDeleteEntityForm::class;
}
/**
* Create a form
*
@ -748,6 +846,9 @@ class CRUDController extends AbstractController
case 'new':
$msg = "crud.new.success";
break;
case 'delete':
$msg = "crud.delete.success";
break;
default:
$msg = "crud.default.success";
}
@ -815,6 +916,8 @@ class CRUDController extends AbstractController
return '@ChillMain/CRUD/index.html.twig';
case 'view':
return '@ChillMain/CRUD/view.html.twig';
case 'delete':
return '@ChillMain/CRUD/delete.html.twig';
default:
throw new \LogicException("the view for action $action is not "
. "defined. You should override ".__METHOD__." to add this "

View File

@ -0,0 +1,33 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2020, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\CRUD\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
/**
*
*
*/
class CRUDDeleteEntityForm extends AbstractType
{
}

View File

@ -246,6 +246,9 @@ crud:
save_and_close: Enregistrer & fermer
save_and_show: Enregistrer & voir
success: Les données ont été modifiées
delete:
success: Les données ont été supprimées
link_to_form: Supprimer
default:
success: Les données ont été enregistrées
view:

View File

@ -0,0 +1,37 @@
<div class="{% block crud_content_main_div_class %}grid-10 centered{% endblock %}">
{% block crud_content_header %}
<h1>{{ ('crud.'~crud_name~'.title_delete')|trans({ '%as_string%': entity|chill_entity_render_string }) }}</h1>
{% endblock crud_content_header %}
<p class="message-confirm">{{ ('crud.'~crud_name~'.confirm_message_delete')|trans({ '%as_string%': entity|chill_entity_render_string }) }}</p>
{{ form_start(form) }}
<ul class="record_actions">
{% block content_form_actions_back %}
<li class="cancel">
<a class="sc-button bt-cancel" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_index') }}">
{{ 'Cancel'|trans }}
</a>
</li>
{% endblock %}
{% block content_form_actions_before %}{% endblock %}
{% block content_form_actions_view %}
{% if is_granted(chill_crud_config('role', crud_name, 'view'), entity) %}
<li class="">
<a class="sc-button bt-show" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_view', { 'id': entity.id }) }}">
{{ 'crud.edit.back_to_view'|trans }}
</a>
</li>
{% endif %}
{% endblock %}
{% block content_form_actions_confirm_delete %}
<li>
<button type="submit" class="sc-button bt-delete" value="delete-and-close">{{ ('crud.'~crud_name~'.button_delete')|trans }}</button>
</li>
{% endblock content_form_actions_confirm_delete %}
{% block content_form_actions_after %}{% endblock %}
</ul>
{{ form_end(form) }}
</div>

View File

@ -22,15 +22,20 @@
</li>
{% endblock %}
{% block content_form_actions_before %}{% endblock %}
{% block content_form_actions_delete %}
{% if is_granted(chill_crud_config('role', crud_name, 'delete'), entity) %}
<li class="">
<a class="sc-button bt-small bt-delete" href="{{ chill_path_add_return_path('chill_crud_'~crud_name~'_delete', { 'id': entity.id }) }}"></a>
</li>
{% endif %}
{% endblock content_form_actions_delete %}
{% block content_form_actions_view %}
{% if is_granted(chill_crud_config('role', crud_name, 'view'), entity) %}
<li class="">
<a class="sc-button bt-show" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_view', { 'id': entity.id }) }}">
{{ 'crud.edit.back_to_view'|trans }}
</a>
<a class="sc-button bt-small bt-show" href="{{ chill_path_forward_return_path('chill_crud_'~crud_name~'_view', { 'id': entity.id }) }}"></a>
</li>
{% endif %}
{% endblock %}
{% endblock content_form_actions_view %}
{% block content_form_actions_save_and_close %}
<li class="">
<button type="submit" name="submit" value="save-and-close" class="sc-button bt-update">

View File

@ -22,19 +22,28 @@
</li>
{% endblock %}
{% block content_view_actions_before %}{% endblock %}
{% block content_form_actions_delete %}
{% if is_granted(chill_crud_config('role', crud_name, 'delete'), entity) %}
<li class="">
<a class="sc-button bt-delete" href="{{ chill_path_add_return_path('chill_crud_'~crud_name~'_delete', { 'id': entity.id }) }}">
{{ 'crud.delete.link_to_form'|trans }}
</a>
</li>
{% endif %}
{% endblock content_form_actions_delete %}
{% block content_view_actions_duplicate_link %}
{% if is_granted(chill_crud_config('role', crud_name, 'new'), entity) %}
<li>
<a class="sc-button bt-duplicate" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_new', { 'duplicate_id': entity.id, 'duplicate': true }) }}">
<a class="sc-button bt-duplicate" href="{{ chill_path_add_return_path('chill_crud_'~crud_name~'_new', { 'duplicate_id': entity.id, 'duplicate': true }) }}">
{{ 'crud.view.link_duplicate'|trans }}
</a>
</li>
{% endif %}
{% endblock content_view_actions_duplicate_link %}
{% block content_view_actions_edit_link %}
{% if is_granted(chill_crud_config('role', crud_name, 'view'), entity) %}
{% if is_granted(chill_crud_config('role', crud_name, 'edit'), entity) %}
<li>
<a class="sc-button bt-edit" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_edit', { 'id': entity.id }) }}">
<a class="sc-button bt-edit" href="{{ chill_path_forward_return_path('chill_crud_'~crud_name~'_edit', { 'id': entity.id }) }}">
{{ 'crud.new.link_edit'|trans }}
</a>
</li>

View File

@ -0,0 +1,8 @@
{% extends '@ChillMain/layout.html.twig' %}
{% block title %}{{ ('crud.' ~ crud_name ~ '.delete.title')|trans({'%crud_name%': crud_name}) }}{% endblock %}
{% block content %}
{% embed '@ChillMain/CRUD/_delete_content.html.twig' %}
{% endembed %}
{% endblock content %}