vendor/ibexa/fieldtype-query/src/lib/ContentView/QueryResultsInjector.php line 50

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. namespace Ibexa\FieldTypeQuery\ContentView;
  7. use Ibexa\Contracts\FieldTypeQuery\QueryFieldLocationService;
  8. use Ibexa\Contracts\FieldTypeQuery\QueryFieldServiceInterface;
  9. use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
  10. use Ibexa\Core\MVC\Symfony\View\ContentValueView;
  11. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewParametersEvent;
  12. use Ibexa\Core\MVC\Symfony\View\LocationValueView;
  13. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  14. use Pagerfanta\Pagerfanta;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. final class QueryResultsInjector implements EventSubscriberInterface
  18. {
  19.     /** @var \Ibexa\FieldTypeQuery\QueryFieldService */
  20.     private $queryFieldService;
  21.     /** @var array */
  22.     private $views;
  23.     /** @var \Symfony\Component\HttpFoundation\RequestStack */
  24.     private $requestStack;
  25.     public function __construct(QueryFieldServiceInterface $queryFieldService, array $viewsRequestStack $requestStack)
  26.     {
  27.         if (!isset($views['item']) || !isset($views['field'])) {
  28.             throw new \InvalidArgumentException("Both 'item' and 'field' views must be provided");
  29.         }
  30.         $this->queryFieldService $queryFieldService;
  31.         $this->views $views;
  32.         $this->requestStack $requestStack;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [ViewEvents::FILTER_VIEW_PARAMETERS => 'injectQueryResults'];
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function injectQueryResults(FilterViewParametersEvent $event)
  42.     {
  43.         if ($event->getView()->getViewType() === $this->views['field']) {
  44.             $builderParameters $event->getBuilderParameters();
  45.             if (!isset($builderParameters['queryFieldDefinitionIdentifier'])) {
  46.                 throw new InvalidArgumentException('queryFieldDefinitionIdentifier''missing');
  47.             }
  48.             $parameters = [
  49.                 'itemViewType' => $event->getBuilderParameters()['itemViewType'] ?? $this->views['item'],
  50.                 'items' => $this->buildResults($event),
  51.                 'fieldIdentifier' => $builderParameters['queryFieldDefinitionIdentifier'],
  52.             ];
  53.             $parameters['isPaginationEnabled'] = ($parameters['items'] instanceof Pagerfanta);
  54.             if ($parameters['isPaginationEnabled']) {
  55.                 $parameters['pageParameter'] = sprintf('[%s_page]'$parameters['fieldIdentifier']);
  56.             }
  57.             $event->getParameterBag()->add($parameters);
  58.         }
  59.     }
  60.     /**
  61.      * @param \Ibexa\Core\MVC\Symfony\View\Event\FilterViewParametersEvent $event
  62.      *
  63.      * @return iterable
  64.      *
  65.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  66.      */
  67.     private function buildResults(FilterViewParametersEvent $event): iterable
  68.     {
  69.         $view $event->getView();
  70.         $location $view instanceof LocationValueView $location $view->getLocation() : null;
  71.         $content $view instanceof ContentValueView $view->getContent() : null;
  72.         $viewParameters $event->getBuilderParameters();
  73.         $fieldDefinitionIdentifier $viewParameters['queryFieldDefinitionIdentifier'];
  74.         $paginationLimit $this->queryFieldService->getPaginationConfiguration($content$fieldDefinitionIdentifier);
  75.         $enablePagination = ($viewParameters['enablePagination'] === true);
  76.         $disablePagination = ($viewParameters['disablePagination'] === true);
  77.         if ($enablePagination === true && $disablePagination === true) {
  78.             // @todo custom exception
  79.             throw new \InvalidArgumentException("the 'enablePagination' and 'disablePagination' parameters can not both be true");
  80.         }
  81.         if (isset($viewParameters['itemsPerPage']) && is_numeric($viewParameters['itemsPerPage'])) {
  82.             // @todo custom exception
  83.             if ($viewParameters['itemsPerPage'] <= 0) {
  84.                 throw new \InvalidArgumentException('itemsPerPage must be a positive integer');
  85.             }
  86.             $paginationLimit $viewParameters['itemsPerPage'];
  87.         }
  88.         if (($enablePagination === true) && (!is_numeric($paginationLimit) || $paginationLimit === 0)) {
  89.             throw new \InvalidArgumentException("The 'itemsPerPage' parameter must be given with a positive integer value if 'enablePagination' is set");
  90.         }
  91.         if ($paginationLimit !== && $disablePagination !== true) {
  92.             $request $this->requestStack->getMainRequest();
  93.             $queryParameters $view->hasParameter('query') ? $view->getParameter('query') : [];
  94.             $limit $queryParameters['limit'] ?? $paginationLimit;
  95.             $pageParam sprintf('%s_page'$fieldDefinitionIdentifier);
  96.             $page = isset($request) ? $request->get($pageParam1) : 1;
  97.             if ($location !== null) {
  98.                 $pager = new Pagerfanta(
  99.                     new QueryResultsWithLocationPagerFantaAdapter(
  100.                         $this->queryFieldService,
  101.                         $location,
  102.                         $fieldDefinitionIdentifier
  103.                     )
  104.                 );
  105.             } else {
  106.                 $pager = new Pagerfanta(
  107.                     new QueryResultsPagerFantaAdapter(
  108.                         $this->queryFieldService,
  109.                         $content,
  110.                         $fieldDefinitionIdentifier
  111.                     )
  112.                 );
  113.             }
  114.             $pager->setMaxPerPage($limit);
  115.             $pager->setCurrentPage($page);
  116.             return $pager;
  117.         } else {
  118.             if ($this->queryFieldService instanceof QueryFieldLocationService && $location !== null) {
  119.                 return $this->queryFieldService->loadContentItemsForLocation(
  120.                     $location,
  121.                     $fieldDefinitionIdentifier
  122.                 );
  123.             } elseif ($content !== null) {
  124.                 return $this->queryFieldService->loadContentItems(
  125.                     $content,
  126.                     $fieldDefinitionIdentifier
  127.                 );
  128.             } else {
  129.                 throw new \Exception('No content nor location to get query results for');
  130.             }
  131.         }
  132.     }
  133. }
  134. class_alias(QueryResultsInjector::class, 'EzSystems\EzPlatformQueryFieldType\eZ\ContentView\QueryResultsInjector');