Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/easycorp/easyadmin-bundle/src/Factory/ActionFactory.php line 72

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
  3. use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  11. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  12. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  13. use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableMessageBuilder;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use function Symfony\Component\Translation\t;
  18. use Symfony\Contracts\Translation\TranslatableInterface;
  19. /**
  20.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  21.  */
  22. final class ActionFactory
  23. {
  24.     private AdminContextProvider $adminContextProvider;
  25.     private AuthorizationCheckerInterface $authChecker;
  26.     private AdminUrlGenerator $adminUrlGenerator;
  27.     private ?CsrfTokenManagerInterface $csrfTokenManager;
  28.     public function __construct(AdminContextProvider $adminContextProviderAuthorizationCheckerInterface $authCheckerAdminUrlGenerator $adminUrlGenerator, ?CsrfTokenManagerInterface $csrfTokenManager null)
  29.     {
  30.         $this->adminContextProvider $adminContextProvider;
  31.         $this->authChecker $authChecker;
  32.         $this->adminUrlGenerator $adminUrlGenerator;
  33.         $this->csrfTokenManager $csrfTokenManager;
  34.     }
  35.     public function processEntityActions(EntityDto $entityDtoActionConfigDto $actionsDto): void
  36.     {
  37.         $currentPage $this->adminContextProvider->getContext()->getCrud()->getCurrentPage();
  38.         $entityActions = [];
  39.         foreach ($actionsDto->getActions()->all() as $actionDto) {
  40.             if (!$actionDto->isEntityAction()) {
  41.                 continue;
  42.             }
  43.             if (false === $this->authChecker->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => $actionDto'entity' => $entityDto])) {
  44.                 continue;
  45.             }
  46.             if (false === $actionDto->shouldBeDisplayedFor($entityDto)) {
  47.                 continue;
  48.             }
  49.             if ('' === $actionDto->getCssClass()) {
  50.                 $defaultCssClass 'action-'.$actionDto->getName();
  51.                 if (Crud::PAGE_INDEX !== $currentPage) {
  52.                     $defaultCssClass .= ' btn';
  53.                 }
  54.                 $actionDto->setCssClass($defaultCssClass);
  55.             }
  56.             $entityActions[] = $this->processAction($currentPage$actionDto$entityDto);
  57.         }
  58.         $entityDto->setActions(ActionCollection::new($entityActions));
  59.     }
  60.     public function processGlobalActions(ActionConfigDto $actionsDto null): ActionCollection
  61.     {
  62.         if (null === $actionsDto) {
  63.             $actionsDto $this->adminContextProvider->getContext()->getCrud()->getActionsConfig();
  64.         }
  65.         $currentPage $this->adminContextProvider->getContext()->getCrud()->getCurrentPage();
  66.         $globalActions = [];
  67.         foreach ($actionsDto->getActions()->all() as $actionDto) {
  68.             if (!$actionDto->isGlobalAction() && !$actionDto->isBatchAction()) {
  69.                 continue;
  70.             }
  71.             if (false === $this->authChecker->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => $actionDto'entity' => null])) {
  72.                 continue;
  73.             }
  74.             if (Crud::PAGE_INDEX !== $currentPage && $actionDto->isBatchAction()) {
  75.                 throw new \RuntimeException(sprintf('Batch actions can be added only to the "index" page, but the "%s" batch action is defined in the "%s" page.'$actionDto->getName(), $currentPage));
  76.             }
  77.             if ('' === $actionDto->getCssClass()) {
  78.                 $actionDto->setCssClass('btn action-'.$actionDto->getName());
  79.             }
  80.             $globalActions[] = $this->processAction($currentPage$actionDto);
  81.         }
  82.         return ActionCollection::new($globalActions);
  83.     }
  84.     private function processAction(string $pageNameActionDto $actionDto, ?EntityDto $entityDto null): ActionDto
  85.     {
  86.         $adminContext $this->adminContextProvider->getContext();
  87.         $translationDomain $adminContext->getI18n()->getTranslationDomain();
  88.         $defaultTranslationParameters $adminContext->getI18n()->getTranslationParameters();
  89.         $currentPage $adminContext->getCrud()->getCurrentPage();
  90.         $actionDto->setHtmlAttribute('data-action-name'$actionDto->getName());
  91.         if (false === $actionDto->getLabel()) {
  92.             $actionDto->setHtmlAttribute('title'$actionDto->getName());
  93.         } elseif (!$actionDto->getLabel() instanceof TranslatableInterface) {
  94.             $translationParameters array_merge(
  95.                 $defaultTranslationParameters,
  96.                 $actionDto->getTranslationParameters()
  97.             );
  98.             $label $actionDto->getLabel();
  99.             $translatableActionLabel = (null === $label || '' === $label) ? $label t($label$translationParameters$translationDomain);
  100.             $actionDto->setLabel($translatableActionLabel);
  101.         } else {
  102.             $actionDto->setLabel(TranslatableMessageBuilder::withParameters($actionDto->getLabel(), $defaultTranslationParameters));
  103.         }
  104.         $defaultTemplatePath $adminContext->getTemplatePath('crud/action');
  105.         $actionDto->setTemplatePath($actionDto->getTemplatePath() ?? $defaultTemplatePath);
  106.         $actionDto->setLinkUrl($this->generateActionUrl($currentPage$adminContext->getRequest(), $actionDto$entityDto));
  107.         if (!$actionDto->isGlobalAction() && \in_array($pageName, [Crud::PAGE_EDITCrud::PAGE_NEW], true)) {
  108.             $actionDto->setHtmlAttribute('form'sprintf('%s-%s-form'$pageName$entityDto->getName()));
  109.         }
  110.         if (Action::DELETE === $actionDto->getName()) {
  111.             $actionDto->addHtmlAttributes([
  112.                 'formaction' => $this->adminUrlGenerator->setAction(Action::DELETE)->setEntityId($entityDto->getPrimaryKeyValue())->removeReferrer()->generateUrl(),
  113.                 'data-bs-toggle' => 'modal',
  114.                 'data-bs-target' => '#modal-delete',
  115.             ]);
  116.         }
  117.         if ($actionDto->isBatchAction()) {
  118.             $actionDto->addHtmlAttributes([
  119.                 'data-bs-toggle' => 'modal',
  120.                 'data-bs-target' => '#modal-batch-action',
  121.                 'data-action-csrf-token' => $this->csrfTokenManager?->getToken('ea-batch-action-'.$actionDto->getName()),
  122.                 'data-action-batch' => 'true',
  123.                 'data-entity-fqcn' => $adminContext->getCrud()->getEntityFqcn(),
  124.                 'data-action-url' => $actionDto->getLinkUrl(),
  125.             ]);
  126.         }
  127.         return $actionDto;
  128.     }
  129.     private function generateActionUrl(string $currentActionRequest $requestActionDto $actionDto, ?EntityDto $entityDto null): string
  130.     {
  131.         if (null !== $url $actionDto->getUrl()) {
  132.             if (\is_callable($url)) {
  133.                 return null !== $entityDto $url($entityDto->getInstance()) : $url();
  134.             }
  135.             return $url;
  136.         }
  137.         if (null !== $routeName $actionDto->getRouteName()) {
  138.             $routeParameters $actionDto->getRouteParameters();
  139.             if (\is_callable($routeParameters) && null !== $entityInstance $entityDto->getInstance()) {
  140.                 $routeParameters $routeParameters($entityInstance);
  141.             }
  142.             return $this->adminUrlGenerator->unsetAll()->includeReferrer()->setRoute($routeName$routeParameters)->generateUrl();
  143.         }
  144.         $requestParameters = [
  145.             EA::CRUD_CONTROLLER_FQCN => $request->query->get(EA::CRUD_CONTROLLER_FQCN),
  146.             EA::CRUD_ACTION => $actionDto->getCrudActionName(),
  147.             EA::REFERRER => $this->generateReferrerUrl($request$actionDto$currentAction),
  148.         ];
  149.         if (\in_array($actionDto->getName(), [Action::INDEXAction::NEW, Action::SAVE_AND_ADD_ANOTHERAction::SAVE_AND_RETURN], true)) {
  150.             $requestParameters[EA::ENTITY_ID] = null;
  151.         } elseif (null !== $entityDto) {
  152.             $requestParameters[EA::ENTITY_ID] = $entityDto->getPrimaryKeyValueAsString();
  153.         }
  154.         return $this->adminUrlGenerator->unsetAllExcept(EA::FILTERSEA::PAGE)->setAll($requestParameters)->generateUrl();
  155.     }
  156.     private function generateReferrerUrl(Request $requestActionDto $actionDtostring $currentAction): ?string
  157.     {
  158.         $nextAction $actionDto->getName();
  159.         if (Action::DETAIL === $currentAction) {
  160.             if (Action::EDIT === $nextAction) {
  161.                 return $this->adminUrlGenerator->removeReferrer()->generateUrl();
  162.             }
  163.         }
  164.         if (Action::INDEX === $currentAction) {
  165.             return $this->adminUrlGenerator->removeReferrer()->generateUrl();
  166.         }
  167.         if (Action::NEW === $currentAction) {
  168.             return null;
  169.         }
  170.         $referrer $request->query->get(EA::REFERRER);
  171.         $referrerParts parse_url((string) $referrer);
  172.         parse_str($referrerParts[EA::QUERY] ?? ''$referrerQueryStringVariables);
  173.         $referrerCrudAction $referrerQueryStringVariables[EA::CRUD_ACTION] ?? null;
  174.         if (Action::EDIT === $currentAction) {
  175.             if (\in_array($referrerCrudAction, [Action::INDEXAction::DETAIL], true)) {
  176.                 return $referrer;
  177.             }
  178.         }
  179.         return $this->adminUrlGenerator->removeReferrer()->generateUrl();
  180.     }
  181. }