vendor/easycorp/easyadmin-bundle/src/Controller/AbstractCrudController.php line 117

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Controller;
  3. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  12. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  13. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  14. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  15. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  16. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  17. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
  18. use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
  19. use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
  20. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  21. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  22. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  23. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  24. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  25. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  26. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  27. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  28. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  29. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  30. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  31. use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
  32. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  33. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  34. use EasyCorp\Bundle\EasyAdminBundle\Factory\ActionFactory;
  35. use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
  36. use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
  37. use EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory;
  38. use EasyCorp\Bundle\EasyAdminBundle\Factory\FormFactory;
  39. use EasyCorp\Bundle\EasyAdminBundle\Factory\PaginatorFactory;
  40. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  41. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  42. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\FileUploadType;
  43. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\FiltersFormType;
  44. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Model\FileUploadState;
  45. use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
  46. use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityUpdater;
  47. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  48. use EasyCorp\Bundle\EasyAdminBundle\Provider\FieldProvider;
  49. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  50. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  51. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  52. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  53. use Symfony\Component\Form\FormBuilderInterface;
  54. use Symfony\Component\Form\FormInterface;
  55. use Symfony\Component\HttpFoundation\JsonResponse;
  56. use Symfony\Component\HttpFoundation\RedirectResponse;
  57. use Symfony\Component\HttpFoundation\Response;
  58. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  59. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  60. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  61. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  62. use function Symfony\Component\String\u;
  63. /**
  64.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  65.  */
  66. abstract class AbstractCrudController extends AbstractController implements CrudControllerInterface
  67. {
  68.     abstract public static function getEntityFqcn(): string;
  69.     public function configureCrud(Crud $crud): Crud
  70.     {
  71.         return $crud;
  72.     }
  73.     public function configureAssets(Assets $assets): Assets
  74.     {
  75.         return $assets;
  76.     }
  77.     public function configureActions(Actions $actions): Actions
  78.     {
  79.         return $actions;
  80.     }
  81.     public function configureFilters(Filters $filters): Filters
  82.     {
  83.         return $filters;
  84.     }
  85.     public function configureFields(string $pageName): iterable
  86.     {
  87.         return $this->container->get(FieldProvider::class)->getDefaultFields($pageName);
  88.     }
  89.     public static function getSubscribedServices(): array
  90.     {
  91.         return array_merge(parent::getSubscribedServices(), [
  92.             'doctrine' => '?'.ManagerRegistry::class,
  93.             'event_dispatcher' => '?'.EventDispatcherInterface::class,
  94.             ActionFactory::class => '?'.ActionFactory::class,
  95.             AdminContextProvider::class => '?'.AdminContextProvider::class,
  96.             AdminUrlGenerator::class => '?'.AdminUrlGenerator::class,
  97.             ControllerFactory::class => '?'.ControllerFactory::class,
  98.             EntityFactory::class => '?'.EntityFactory::class,
  99.             EntityRepository::class => '?'.EntityRepository::class,
  100.             EntityUpdater::class => '?'.EntityUpdater::class,
  101.             FieldProvider::class => '?'.FieldProvider::class,
  102.             FilterFactory::class => '?'.FilterFactory::class,
  103.             FormFactory::class => '?'.FormFactory::class,
  104.             PaginatorFactory::class => '?'.PaginatorFactory::class,
  105.         ]);
  106.     }
  107.     public function index(AdminContext $context)
  108.     {
  109.         $event = new BeforeCrudActionEvent($context);
  110.         $this->container->get('event_dispatcher')->dispatch($event);
  111.         if ($event->isPropagationStopped()) {
  112.             return $event->getResponse();
  113.         }
  114.         if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::INDEX'entity' => null])) {
  115.             throw new ForbiddenActionException($context);
  116.         }
  117.         $fields FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
  118.         $context->getCrud()->setFieldAssets($this->getFieldAssets($fields));
  119.         $filters $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $fields$context->getEntity());
  120.         $queryBuilder $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields$filters);
  121.         $paginator $this->container->get(PaginatorFactory::class)->create($queryBuilder);
  122.         // this can happen after deleting some items and trying to return
  123.         // to a 'index' page that no longer exists. Redirect to the last page instead
  124.         if ($paginator->isOutOfRange()) {
  125.             return $this->redirect($this->container->get(AdminUrlGenerator::class)
  126.                 ->set(EA::PAGE$paginator->getLastPage())
  127.                 ->generateUrl());
  128.         }
  129.         $entities $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
  130.         $this->container->get(EntityFactory::class)->processFieldsForAll($entities$fields);
  131.         $actions $this->container->get(EntityFactory::class)->processActionsForAll($entities$context->getCrud()->getActionsConfig());
  132.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  133.             'pageName' => Crud::PAGE_INDEX,
  134.             'templateName' => 'crud/index',
  135.             'entities' => $entities,
  136.             'paginator' => $paginator,
  137.             'global_actions' => $actions->getGlobalActions(),
  138.             'batch_actions' => $actions->getBatchActions(),
  139.             'filters' => $filters,
  140.         ]));
  141.         $event = new AfterCrudActionEvent($context$responseParameters);
  142.         $this->container->get('event_dispatcher')->dispatch($event);
  143.         if ($event->isPropagationStopped()) {
  144.             return $event->getResponse();
  145.         }
  146.         return $responseParameters;
  147.     }
  148.     public function detail(AdminContext $context)
  149.     {
  150.         $event = new BeforeCrudActionEvent($context);
  151.         $this->container->get('event_dispatcher')->dispatch($event);
  152.         if ($event->isPropagationStopped()) {
  153.             return $event->getResponse();
  154.         }
  155.         if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DETAIL'entity' => $context->getEntity()])) {
  156.             throw new ForbiddenActionException($context);
  157.         }
  158.         if (!$context->getEntity()->isAccessible()) {
  159.             throw new InsufficientEntityPermissionException($context);
  160.         }
  161.         $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)));
  162.         $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
  163.         $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
  164.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  165.             'pageName' => Crud::PAGE_DETAIL,
  166.             'templateName' => 'crud/detail',
  167.             'entity' => $context->getEntity(),
  168.         ]));
  169.         $event = new AfterCrudActionEvent($context$responseParameters);
  170.         $this->container->get('event_dispatcher')->dispatch($event);
  171.         if ($event->isPropagationStopped()) {
  172.             return $event->getResponse();
  173.         }
  174.         return $responseParameters;
  175.     }
  176.     public function edit(AdminContext $context)
  177.     {
  178.         $event = new BeforeCrudActionEvent($context);
  179.         $this->container->get('event_dispatcher')->dispatch($event);
  180.         if ($event->isPropagationStopped()) {
  181.             return $event->getResponse();
  182.         }
  183.         if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::EDIT'entity' => $context->getEntity()])) {
  184.             throw new ForbiddenActionException($context);
  185.         }
  186.         if (!$context->getEntity()->isAccessible()) {
  187.             throw new InsufficientEntityPermissionException($context);
  188.         }
  189.         $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
  190.         $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
  191.         $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
  192.         $entityInstance $context->getEntity()->getInstance();
  193.         if ($context->getRequest()->isXmlHttpRequest()) {
  194.             if ('PATCH' !== $context->getRequest()->getMethod()) {
  195.                 throw new MethodNotAllowedHttpException(['PATCH']);
  196.             }
  197.             if (!$this->isCsrfTokenValid(BooleanField::CSRF_TOKEN_NAME$context->getRequest()->query->get('csrfToken'))) {
  198.                 if (class_exists(InvalidCsrfTokenException::class)) {
  199.                     throw new InvalidCsrfTokenException();
  200.                 } else {
  201.                     return new Response('Invalid CSRF token.'400);
  202.                 }
  203.             }
  204.             $fieldName $context->getRequest()->query->get('fieldName');
  205.             $newValue 'true' === mb_strtolower($context->getRequest()->query->get('newValue'));
  206.             try {
  207.                 $event $this->ajaxEdit($context->getEntity(), $fieldName$newValue);
  208.             } catch (\Exception $e) {
  209.                 throw new BadRequestHttpException($e->getMessage());
  210.             }
  211.             if ($event->isPropagationStopped()) {
  212.                 return $event->getResponse();
  213.             }
  214.             return new Response($newValue '1' '0');
  215.         }
  216.         $editForm $this->createEditForm($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context);
  217.         $editForm->handleRequest($context->getRequest());
  218.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  219.             $this->processUploadedFiles($editForm);
  220.             $event = new BeforeEntityUpdatedEvent($entityInstance);
  221.             $this->container->get('event_dispatcher')->dispatch($event);
  222.             $entityInstance $event->getEntityInstance();
  223.             $this->updateEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
  224.             $this->container->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
  225.             return $this->getRedirectResponseAfterSave($contextAction::EDIT);
  226.         }
  227.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  228.             'pageName' => Crud::PAGE_EDIT,
  229.             'templateName' => 'crud/edit',
  230.             'edit_form' => $editForm,
  231.             'entity' => $context->getEntity(),
  232.         ]));
  233.         $event = new AfterCrudActionEvent($context$responseParameters);
  234.         $this->container->get('event_dispatcher')->dispatch($event);
  235.         if ($event->isPropagationStopped()) {
  236.             return $event->getResponse();
  237.         }
  238.         return $responseParameters;
  239.     }
  240.     public function new(AdminContext $context)
  241.     {
  242.         $event = new BeforeCrudActionEvent($context);
  243.         $this->container->get('event_dispatcher')->dispatch($event);
  244.         if ($event->isPropagationStopped()) {
  245.             return $event->getResponse();
  246.         }
  247.         if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::NEW, 'entity' => null])) {
  248.             throw new ForbiddenActionException($context);
  249.         }
  250.         if (!$context->getEntity()->isAccessible()) {
  251.             throw new InsufficientEntityPermissionException($context);
  252.         }
  253.         $context->getEntity()->setInstance($this->createEntity($context->getEntity()->getFqcn()));
  254.         $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
  255.         $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
  256.         $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
  257.         $newForm $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions(), $context);
  258.         $newForm->handleRequest($context->getRequest());
  259.         $entityInstance $newForm->getData();
  260.         $context->getEntity()->setInstance($entityInstance);
  261.         if ($newForm->isSubmitted() && $newForm->isValid()) {
  262.             $this->processUploadedFiles($newForm);
  263.             $event = new BeforeEntityPersistedEvent($entityInstance);
  264.             $this->container->get('event_dispatcher')->dispatch($event);
  265.             $entityInstance $event->getEntityInstance();
  266.             $this->persistEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
  267.             $this->container->get('event_dispatcher')->dispatch(new AfterEntityPersistedEvent($entityInstance));
  268.             $context->getEntity()->setInstance($entityInstance);
  269.             return $this->getRedirectResponseAfterSave($contextAction::NEW);
  270.         }
  271.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  272.             'pageName' => Crud::PAGE_NEW,
  273.             'templateName' => 'crud/new',
  274.             'entity' => $context->getEntity(),
  275.             'new_form' => $newForm,
  276.         ]));
  277.         $event = new AfterCrudActionEvent($context$responseParameters);
  278.         $this->container->get('event_dispatcher')->dispatch($event);
  279.         if ($event->isPropagationStopped()) {
  280.             return $event->getResponse();
  281.         }
  282.         return $responseParameters;
  283.     }
  284.     public function delete(AdminContext $context)
  285.     {
  286.         $event = new BeforeCrudActionEvent($context);
  287.         $this->container->get('event_dispatcher')->dispatch($event);
  288.         if ($event->isPropagationStopped()) {
  289.             return $event->getResponse();
  290.         }
  291.         if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE'entity' => $context->getEntity()])) {
  292.             throw new ForbiddenActionException($context);
  293.         }
  294.         if (!$context->getEntity()->isAccessible()) {
  295.             throw new InsufficientEntityPermissionException($context);
  296.         }
  297.         $csrfToken $context->getRequest()->request->get('token');
  298.         if ($this->container->has('security.csrf.token_manager') && !$this->isCsrfTokenValid('ea-delete'$csrfToken)) {
  299.             return $this->redirectToRoute($context->getDashboardRouteName());
  300.         }
  301.         $entityInstance $context->getEntity()->getInstance();
  302.         $event = new BeforeEntityDeletedEvent($entityInstance);
  303.         $this->container->get('event_dispatcher')->dispatch($event);
  304.         if ($event->isPropagationStopped()) {
  305.             return $event->getResponse();
  306.         }
  307.         $entityInstance $event->getEntityInstance();
  308.         try {
  309.             $this->deleteEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
  310.         } catch (ForeignKeyConstraintViolationException $e) {
  311.             throw new EntityRemoveException(['entity_name' => $context->getEntity()->getName(), 'message' => $e->getMessage()]);
  312.         }
  313.         $this->container->get('event_dispatcher')->dispatch(new AfterEntityDeletedEvent($entityInstance));
  314.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  315.             'entity' => $context->getEntity(),
  316.         ]));
  317.         $event = new AfterCrudActionEvent($context$responseParameters);
  318.         $this->container->get('event_dispatcher')->dispatch($event);
  319.         if ($event->isPropagationStopped()) {
  320.             return $event->getResponse();
  321.         }
  322.         if (null !== $referrer $context->getReferrer()) {
  323.             return $this->redirect($referrer);
  324.         }
  325.         return $this->redirect($this->container->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->unset(EA::ENTITY_ID)->generateUrl());
  326.     }
  327.     public function batchDelete(AdminContext $contextBatchActionDto $batchActionDto): Response
  328.     {
  329.         $event = new BeforeCrudActionEvent($context);
  330.         $this->container->get('event_dispatcher')->dispatch($event);
  331.         if ($event->isPropagationStopped()) {
  332.             return $event->getResponse();
  333.         }
  334.         if (!$this->isCsrfTokenValid('ea-batch-action-'.Action::BATCH_DELETE$batchActionDto->getCsrfToken())) {
  335.             return $this->redirectToRoute($context->getDashboardRouteName());
  336.         }
  337.         $entityManager $this->container->get('doctrine')->getManagerForClass($batchActionDto->getEntityFqcn());
  338.         $repository $entityManager->getRepository($batchActionDto->getEntityFqcn());
  339.         foreach ($batchActionDto->getEntityIds() as $entityId) {
  340.             $entityInstance $repository->find($entityId);
  341.             if (!$entityInstance) {
  342.                 continue;
  343.             }
  344.             $entityDto $context->getEntity()->newWithInstance($entityInstance);
  345.             if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE'entity' => $entityDto])) {
  346.                 throw new ForbiddenActionException($context);
  347.             }
  348.             if (!$entityDto->isAccessible()) {
  349.                 throw new InsufficientEntityPermissionException($context);
  350.             }
  351.             $event = new BeforeEntityDeletedEvent($entityInstance);
  352.             $this->container->get('event_dispatcher')->dispatch($event);
  353.             $entityInstance $event->getEntityInstance();
  354.             try {
  355.                 $this->deleteEntity($entityManager$entityInstance);
  356.             } catch (ForeignKeyConstraintViolationException $e) {
  357.                 throw new EntityRemoveException(['entity_name' => $entityDto->toString(), 'message' => $e->getMessage()]);
  358.             }
  359.             $this->container->get('event_dispatcher')->dispatch(new AfterEntityDeletedEvent($entityInstance));
  360.         }
  361.         $responseParameters $this->configureResponseParameters(KeyValueStore::new([
  362.             'entity' => $context->getEntity(),
  363.             'batchActionDto' => $batchActionDto,
  364.         ]));
  365.         $event = new AfterCrudActionEvent($context$responseParameters);
  366.         $this->container->get('event_dispatcher')->dispatch($event);
  367.         if ($event->isPropagationStopped()) {
  368.             return $event->getResponse();
  369.         }
  370.         return $this->redirect($batchActionDto->getReferrerUrl());
  371.     }
  372.     public function autocomplete(AdminContext $context): JsonResponse
  373.     {
  374.         $queryBuilder $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), FieldCollection::new([]), FilterCollection::new());
  375.         $autocompleteContext $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  376.         /** @var CrudControllerInterface $controller */
  377.         $controller $this->container->get(ControllerFactory::class)->getCrudControllerInstance($autocompleteContext[EA::CRUD_CONTROLLER_FQCN], Action::INDEX$context->getRequest());
  378.         /** @var FieldDto|null $field */
  379.         $field FieldCollection::new($controller->configureFields($autocompleteContext['originatingPage']))->getByProperty($autocompleteContext['propertyName']);
  380.         /** @var \Closure|null $queryBuilderCallable */
  381.         $queryBuilderCallable $field?->getCustomOption(AssociationField::OPTION_QUERY_BUILDER_CALLABLE);
  382.         if (null !== $queryBuilderCallable) {
  383.             $queryBuilderCallable($queryBuilder);
  384.         }
  385.         $paginator $this->container->get(PaginatorFactory::class)->create($queryBuilder);
  386.         return JsonResponse::fromJsonString($paginator->getResultsAsJson());
  387.     }
  388.     public function createIndexQueryBuilder(SearchDto $searchDtoEntityDto $entityDtoFieldCollection $fieldsFilterCollection $filters): QueryBuilder
  389.     {
  390.         return $this->container->get(EntityRepository::class)->createQueryBuilder($searchDto$entityDto$fields$filters);
  391.     }
  392.     public function renderFilters(AdminContext $context): KeyValueStore
  393.     {
  394.         $fields FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
  395.         $this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
  396.         $filters $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());
  397.         /** @var FormInterface&FiltersFormType $filtersForm */
  398.         $filtersForm $this->container->get(FormFactory::class)->createFiltersForm($filters$context->getRequest());
  399.         $formActionParts parse_url($filtersForm->getConfig()->getAction());
  400.         $queryString $formActionParts[EA::QUERY] ?? '';
  401.         parse_str($queryString$queryStringAsArray);
  402.         unset($queryStringAsArray[EA::FILTERS], $queryStringAsArray[EA::PAGE]);
  403.         $responseParameters KeyValueStore::new([
  404.             'templateName' => 'crud/filters',
  405.             'filters_form' => $filtersForm,
  406.             'form_action_query_string_as_array' => $queryStringAsArray,
  407.         ]);
  408.         return $this->configureResponseParameters($responseParameters);
  409.     }
  410.     public function createEntity(string $entityFqcn)
  411.     {
  412.         return new $entityFqcn();
  413.     }
  414.     public function updateEntity(EntityManagerInterface $entityManager$entityInstance): void
  415.     {
  416.         $entityManager->persist($entityInstance);
  417.         $entityManager->flush();
  418.     }
  419.     public function persistEntity(EntityManagerInterface $entityManager$entityInstance): void
  420.     {
  421.         $entityManager->persist($entityInstance);
  422.         $entityManager->flush();
  423.     }
  424.     public function deleteEntity(EntityManagerInterface $entityManager$entityInstance): void
  425.     {
  426.         $entityManager->remove($entityInstance);
  427.         $entityManager->flush();
  428.     }
  429.     public function createEditForm(EntityDto $entityDtoKeyValueStore $formOptionsAdminContext $context): FormInterface
  430.     {
  431.         return $this->createEditFormBuilder($entityDto$formOptions$context)->getForm();
  432.     }
  433.     public function createEditFormBuilder(EntityDto $entityDtoKeyValueStore $formOptionsAdminContext $context): FormBuilderInterface
  434.     {
  435.         return $this->container->get(FormFactory::class)->createEditFormBuilder($entityDto$formOptions$context);
  436.     }
  437.     public function createNewForm(EntityDto $entityDtoKeyValueStore $formOptionsAdminContext $context): FormInterface
  438.     {
  439.         return $this->createNewFormBuilder($entityDto$formOptions$context)->getForm();
  440.     }
  441.     public function createNewFormBuilder(EntityDto $entityDtoKeyValueStore $formOptionsAdminContext $context): FormBuilderInterface
  442.     {
  443.         return $this->container->get(FormFactory::class)->createNewFormBuilder($entityDto$formOptions$context);
  444.     }
  445.     /**
  446.      * Used to add/modify/remove parameters before passing them to the Twig template.
  447.      */
  448.     public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  449.     {
  450.         return $responseParameters;
  451.     }
  452.     protected function getContext(): ?AdminContext
  453.     {
  454.         return $this->container->get(AdminContextProvider::class)->getContext();
  455.     }
  456.     protected function ajaxEdit(EntityDto $entityDto, ?string $propertyNamebool $newValue): AfterCrudActionEvent
  457.     {
  458.         $field $entityDto->getFields()->getByProperty($propertyName);
  459.         if (null === $field || true === $field->getFormTypeOption('disabled')) {
  460.             throw new AccessDeniedException(sprintf('The field "%s" does not exist or it\'s configured as disabled, so it can\'t be modified.'$propertyName));
  461.         }
  462.         $this->container->get(EntityUpdater::class)->updateProperty($entityDto$propertyName$newValue);
  463.         $event = new BeforeEntityUpdatedEvent($entityDto->getInstance());
  464.         $this->container->get('event_dispatcher')->dispatch($event);
  465.         $entityInstance $event->getEntityInstance();
  466.         $this->updateEntity($this->container->get('doctrine')->getManagerForClass($entityDto->getFqcn()), $entityInstance);
  467.         $this->container->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
  468.         $entityDto->setInstance($entityInstance);
  469.         $parameters KeyValueStore::new([
  470.             'action' => Action::EDIT,
  471.             'entity' => $entityDto,
  472.         ]);
  473.         $event = new AfterCrudActionEvent($this->getContext(), $parameters);
  474.         $this->container->get('event_dispatcher')->dispatch($event);
  475.         return $event;
  476.     }
  477.     protected function processUploadedFiles(FormInterface $form): void
  478.     {
  479.         /** @var FormInterface $child */
  480.         foreach ($form as $child) {
  481.             $config $child->getConfig();
  482.             if (!$config->getType()->getInnerType() instanceof FileUploadType) {
  483.                 if ($config->getCompound()) {
  484.                     $this->processUploadedFiles($child);
  485.                 }
  486.                 continue;
  487.             }
  488.             /** @var FileUploadState $state */
  489.             $state $config->getAttribute('state');
  490.             if (!$state->isModified()) {
  491.                 continue;
  492.             }
  493.             $uploadDelete $config->getOption('upload_delete');
  494.             if ($state->hasCurrentFiles() && ($state->isDelete() || (!$state->isAddAllowed() && $state->hasUploadedFiles()))) {
  495.                 foreach ($state->getCurrentFiles() as $file) {
  496.                     $uploadDelete($file);
  497.                 }
  498.                 $state->setCurrentFiles([]);
  499.             }
  500.             $filePaths = (array) $child->getData();
  501.             $uploadDir $config->getOption('upload_dir');
  502.             $uploadNew $config->getOption('upload_new');
  503.             foreach ($state->getUploadedFiles() as $index => $file) {
  504.                 $fileName u($filePaths[$index])->replace($uploadDir'')->toString();
  505.                 $uploadNew($file$uploadDir$fileName);
  506.             }
  507.         }
  508.     }
  509.     protected function getRedirectResponseAfterSave(AdminContext $contextstring $action): RedirectResponse
  510.     {
  511.         $submitButtonName $context->getRequest()->request->all()['ea']['newForm']['btn'];
  512.         $url = match ($submitButtonName) {
  513.             Action::SAVE_AND_CONTINUE => $this->container->get(AdminUrlGenerator::class)
  514.                 ->setAction(Action::EDIT)
  515.                 ->setEntityId($context->getEntity()->getPrimaryKeyValue())
  516.                 ->generateUrl(),
  517.             Action::SAVE_AND_RETURN => $context->getReferrer()
  518.                 ?? $this->container->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl(),
  519.             Action::SAVE_AND_ADD_ANOTHER => $this->container->get(AdminUrlGenerator::class)->setAction(Action::NEW)->generateUrl(),
  520.             default => $this->generateUrl($context->getDashboardRouteName()),
  521.         };
  522.         return $this->redirect($url);
  523.     }
  524.     protected function getFieldAssets(FieldCollection $fieldDtos): AssetsDto
  525.     {
  526.         $fieldAssetsDto = new AssetsDto();
  527.         $currentPageName $this->getContext()?->getCrud()?->getCurrentPage();
  528.         foreach ($fieldDtos as $fieldDto) {
  529.             $fieldAssetsDto $fieldAssetsDto->mergeWith($fieldDto->getAssets()->loadedOn($currentPageName));
  530.         }
  531.         return $fieldAssetsDto;
  532.     }
  533. }