[crud] add step "delete"

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

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
{
}