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

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