vendor/ibexa/admin-ui/src/lib/View/Filter/AdminSearchViewFilter.php line 82

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\AdminUi\View\Filter;
  8. use Ibexa\AdminUi\Form\Type\Search\SearchType;
  9. use Ibexa\AdminUi\Specification\SiteAccess\IsAdmin;
  10. use Ibexa\Bundle\Search\Form\Data\SearchData;
  11. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  12. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  13. use Ibexa\Contracts\Core\Repository\SectionService;
  14. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  15. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  16. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  17. use Ibexa\Search\View\SearchViewFilter;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. class AdminSearchViewFilter implements EventSubscriberInterface
  23. {
  24.     /** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
  25.     private $configResolver;
  26.     /** @var \Symfony\Component\Form\FormFactoryInterface */
  27.     private $formFactory;
  28.     /** @var \Ibexa\Contracts\Core\Repository\SectionService */
  29.     private $sectionService;
  30.     /** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
  31.     private $contentTypeService;
  32.     /** @var array */
  33.     private $siteAccessGroups;
  34.     /** @var \Ibexa\Search\View\SearchViewFilter */
  35.     private $innerFilter;
  36.     /** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
  37.     private $urlGenerator;
  38.     public function __construct(
  39.         ConfigResolverInterface $configResolver,
  40.         FormFactoryInterface $formFactory,
  41.         SectionService $sectionService,
  42.         ContentTypeService $contentTypeService,
  43.         array $siteAccessGroups,
  44.         SearchViewFilter $innerFilter,
  45.         UrlGeneratorInterface $urlGenerator
  46.     ) {
  47.         $this->configResolver $configResolver;
  48.         $this->formFactory $formFactory;
  49.         $this->sectionService $sectionService;
  50.         $this->contentTypeService $contentTypeService;
  51.         $this->siteAccessGroups $siteAccessGroups;
  52.         $this->innerFilter $innerFilter;
  53.         $this->urlGenerator $urlGenerator;
  54.     }
  55.     public static function getSubscribedEvents()
  56.     {
  57.         return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleSearchForm'];
  58.     }
  59.     /**
  60.      * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  61.      * @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  62.      * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
  63.      * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  64.      * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
  65.      * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  66.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  67.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  68.      */
  69.     public function handleSearchForm(FilterViewBuilderParametersEvent $event): void
  70.     {
  71.         $controllerAction $event->getParameters()->get('_controller');
  72.         if (
  73.             'Ibexa\Bundle\Search\Controller\SearchController::searchAction' !== $controllerAction
  74.         ) {
  75.             return;
  76.         }
  77.         $request $event->getRequest();
  78.         if (!$this->isAdminSiteAccess($request)) {
  79.             $this->innerFilter->handleSearchForm($event);
  80.             return;
  81.         }
  82.         $search $request->query->all('search');
  83.         $limit = isset($search['limit']) ? (int)$search['limit'] : $this->configResolver->getParameter('pagination.search_limit');
  84.         $page = isset($search['page']) ? (int)$search['page'] : 1;
  85.         $query $search['query'] ?? '';
  86.         $section null;
  87.         $creator null;
  88.         $contentTypes = [];
  89.         $lastModified $search['last_modified'] ?? [];
  90.         $created $search['created'] ?? [];
  91.         $subtree $search['subtree'] ?? null;
  92.         $searchLanguage null;
  93.         if (!empty($search['section'])) {
  94.             try {
  95.                 $section $this->sectionService->loadSection((int)$search['section']);
  96.             } catch (NotFoundException $e) {
  97.                 $section null;
  98.             }
  99.         }
  100.         if (!empty($search['content_types']) && \is_array($search['content_types'])) {
  101.             foreach ($search['content_types'] as $identifier) {
  102.                 $contentTypes[] = $this->contentTypeService->loadContentTypeByIdentifier($identifier);
  103.             }
  104.         }
  105.         $form $this->formFactory->create(
  106.             SearchType::class,
  107.             new SearchData(
  108.                 $limit,
  109.                 $page,
  110.                 $query,
  111.                 $section,
  112.                 $contentTypes,
  113.                 $lastModified,
  114.                 $created,
  115.                 $creator,
  116.                 $subtree,
  117.                 $searchLanguage
  118.             ),
  119.             [
  120.                 'method' => Request::METHOD_GET,
  121.                 'csrf_protection' => false,
  122.                 'action' => $this->urlGenerator->generate('ibexa.search'),
  123.             ]
  124.         );
  125.         $event->getParameters()->add([
  126.             'form' => $form->handleRequest($request),
  127.         ]);
  128.     }
  129.     protected function isAdminSiteAccess(Request $request): bool
  130.     {
  131.         return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($request->attributes->get('siteaccess'));
  132.     }
  133. }
  134. class_alias(AdminSearchViewFilter::class, 'EzSystems\EzPlatformAdminUi\View\Filter\AdminSearchViewFilter');