mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-04 13:54:59 +00:00
[crud] add step "delete"
This commit is contained in:
@@ -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 "
|
||||
|
Reference in New Issue
Block a user