vendor/ibexa/core/src/lib/MVC/Symfony/Controller/Content/QueryController.php line 48

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\Core\MVC\Symfony\Controller\Content;
  7. use Ibexa\Contracts\Core\Repository\SearchService;
  8. use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
  9. use Ibexa\Contracts\Core\Repository\Values\Content\Query;
  10. use Ibexa\Core\MVC\Symfony\View\ContentView;
  11. use Ibexa\Core\Pagination\Pagerfanta\ContentSearchHitAdapter;
  12. use Ibexa\Core\Pagination\Pagerfanta\LocationSearchHitAdapter;
  13. use Ibexa\Core\Pagination\Pagerfanta\Pagerfanta;
  14. use Ibexa\Core\QueryType\ContentViewQueryTypeMapper;
  15. use Pagerfanta\Adapter\AdapterInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. /**
  18.  * A content view controller that runs queries based on the matched view configuration.
  19.  *
  20.  * The action used depends on which type of search is needed: location, content or contentInfo.
  21.  */
  22. class QueryController
  23. {
  24.     /** @var \Ibexa\Contracts\Core\Repository\SearchService */
  25.     private $searchService;
  26.     /** @var \Ibexa\Core\QueryType\ContentViewQueryTypeMapper */
  27.     private $contentViewQueryTypeMapper;
  28.     public function __construct(
  29.         ContentViewQueryTypeMapper $contentViewQueryTypeMapper,
  30.         SearchService $searchService
  31.     ) {
  32.         $this->contentViewQueryTypeMapper $contentViewQueryTypeMapper;
  33.         $this->searchService $searchService;
  34.     }
  35.     /**
  36.      * Runs a content search.
  37.      *
  38.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  39.      *
  40.      * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  41.      */
  42.     public function contentQueryAction(ContentView $view)
  43.     {
  44.         $this->runQuery($view'findContent');
  45.         return $view;
  46.     }
  47.     /**
  48.      * Runs a location search.
  49.      *
  50.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  51.      *
  52.      * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  53.      */
  54.     public function locationQueryAction(ContentView $view)
  55.     {
  56.         $this->runQuery($view'findLocations');
  57.         return $view;
  58.     }
  59.     /**
  60.      * Runs a contentInfo search.
  61.      *
  62.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  63.      *
  64.      * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  65.      */
  66.     public function contentInfoQueryAction(ContentView $view)
  67.     {
  68.         $this->runQuery($view'findContentInfo');
  69.         return $view;
  70.     }
  71.     /**
  72.      * Runs the Query defined in $view using $method on SearchService.
  73.      *
  74.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  75.      * @param string $method Name of the SearchService method to run.
  76.      */
  77.     private function runQuery(ContentView $view$method)
  78.     {
  79.         $searchResults $this->searchService->$method(
  80.             $this->contentViewQueryTypeMapper->map($view)
  81.         );
  82.         $view->addParameters([$view->getParameter('query')['assign_results_to'] => $searchResults]);
  83.     }
  84.     /**
  85.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  86.      * @param \Symfony\Component\HttpFoundation\Request $request
  87.      *
  88.      * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  89.      */
  90.     public function pagingQueryAction(ContentView $viewRequest $request)
  91.     {
  92.         $this->runPagingQuery($view$request);
  93.         return $view;
  94.     }
  95.     /**
  96.      * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  97.      * @param \Symfony\Component\HttpFoundation\Request $request
  98.      */
  99.     private function runPagingQuery(ContentView $viewRequest $request)
  100.     {
  101.         $queryParameters $view->getParameter('query');
  102.         $limit $queryParameters['limit'] ?? 10;
  103.         $pageParam $queryParameters['page_param'] ?? 'page';
  104.         $page $request->get($pageParam1);
  105.         $pager = new Pagerfanta(
  106.             $this->getAdapter($this->contentViewQueryTypeMapper->map($view))
  107.         );
  108.         $pager->setMaxPerPage($limit);
  109.         $pager->setCurrentPage($page);
  110.         $view->addParameters([$queryParameters['assign_results_to'] => $pager]);
  111.     }
  112.     /**
  113.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Query $query
  114.      *
  115.      * @return \Pagerfanta\Adapter\AdapterInterface
  116.      */
  117.     private function getAdapter(Query $query): AdapterInterface
  118.     {
  119.         if ($query instanceof LocationQuery) {
  120.             return new LocationSearchHitAdapter($query$this->searchService);
  121.         }
  122.         return new ContentSearchHitAdapter($query$this->searchService);
  123.     }
  124. }
  125. class_alias(QueryController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\QueryController');