vendor/monsieurbiz/sylius-search-plugin/src/Controller/SearchController.php line 174

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Monsieur Biz' Search plugin for Sylius.
  4.  *
  5.  * (c) Monsieur Biz <sylius@monsieurbiz.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace MonsieurBiz\SyliusSearchPlugin\Controller;
  12. use MonsieurBiz\SyliusSearchPlugin\Context\TaxonContextInterface;
  13. use MonsieurBiz\SyliusSearchPlugin\Exception\MissingLocaleException;
  14. use MonsieurBiz\SyliusSearchPlugin\Exception\NotSupportedTypeException;
  15. use MonsieurBiz\SyliusSearchPlugin\Helper\RenderDocumentUrlHelper;
  16. use MonsieurBiz\SyliusSearchPlugin\Model\Config\GridConfig;
  17. use MonsieurBiz\SyliusSearchPlugin\Model\Document\Index\Search;
  18. use MonsieurBiz\SyliusSearchPlugin\Model\Document\Result;
  19. use MonsieurBiz\SyliusSearchPlugin\Model\Document\ResultSet;
  20. use Sylius\Component\Channel\Context\ChannelContextInterface;
  21. use Sylius\Component\Currency\Context\CurrencyContextInterface;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Twig\Environment;
  27. class SearchController extends AbstractController
  28. {
  29.     public const SORT_ASC 'asc';
  30.     public const SORT_DESC 'desc';
  31.     /** @var Environment */
  32.     private $templatingEngine;
  33.     /** @var Search */
  34.     private $documentSearch;
  35.     /** @var ChannelContextInterface */
  36.     private $channelContext;
  37.     /** @var CurrencyContextInterface */
  38.     private $currencyContext;
  39.     /** @var TaxonContextInterface */
  40.     private $taxonContext;
  41.     /** @var GridConfig */
  42.     private $gridConfig;
  43.     /** @var RenderDocumentUrlHelper */
  44.     private $renderDocumentUrlHelper;
  45.     public function __construct(
  46.         Environment $templatingEngine,
  47.         Search $documentSearch,
  48.         ChannelContextInterface $channelContext,
  49.         CurrencyContextInterface $currencyContext,
  50.         TaxonContextInterface $taxonContext,
  51.         GridConfig $gridConfig,
  52.         RenderDocumentUrlHelper $renderDocumentUrlHelper
  53.     ) {
  54.         $this->templatingEngine $templatingEngine;
  55.         $this->documentSearch $documentSearch;
  56.         $this->channelContext $channelContext;
  57.         $this->currencyContext $currencyContext;
  58.         $this->taxonContext $taxonContext;
  59.         $this->gridConfig $gridConfig;
  60.         $this->renderDocumentUrlHelper $renderDocumentUrlHelper;
  61.     }
  62.     /**
  63.      * Post search.
  64.      *
  65.      * @param Request $request
  66.      *
  67.      * @return RedirectResponse
  68.      */
  69.     public function postAction(Request $request)
  70.     {
  71.         $query $request->request->get('monsieurbiz_searchplugin_search')['query'] ?? null;
  72.         return new RedirectResponse(
  73.             $this->generateUrl('monsieurbiz_sylius_search_search',
  74.                 ['query' => urlencode($query)])
  75.         );
  76.     }
  77.     /**
  78.      * Perform the search action & display results. User can add page, limit or sorting.
  79.      *
  80.      * @param Request $request
  81.      *
  82.      * @return Response
  83.      */
  84.     public function searchAction(Request $request): Response
  85.     {
  86.         // Init grid config depending on request
  87.         $this->gridConfig->init(GridConfig::SEARCH_TYPE$request);
  88.         // Perform search
  89.         /** @var ResultSet $resultSet */
  90.         $resultSet $this->documentSearch->search($this->gridConfig);
  91.         // Redirect to document if only one result and no filter applied
  92.         $appliedFilters $this->gridConfig->getAppliedFilters();
  93.         if (=== $resultSet->getTotalHits() && empty($appliedFilters)) {
  94.             /** @var Result $document */
  95.             $document current($resultSet->getResults());
  96.             try {
  97.                 $urlParams $this->renderDocumentUrlHelper->getUrlParams($document);
  98.                 return new RedirectResponse($this->generateUrl($urlParams->getPath(), $urlParams->getParams()));
  99.             } catch (NotSupportedTypeException $e) {
  100.                 // Return list of results if cannot redirect, so ignore Exception
  101.             } catch (MissingLocaleException $e) {
  102.                 // Return list of results if locale is missing
  103.             }
  104.         }
  105.         // Get number formatter for currency
  106.         $currencyCode $this->currencyContext->getCurrencyCode();
  107.         $formatter = new \NumberFormatter($request->getLocale() . '@currency=' $currencyCode, \NumberFormatter::CURRENCY);
  108.         // Display result list
  109.         return new Response($this->templatingEngine->render('@MonsieurBizSyliusSearchPlugin/Search/result.html.twig', [
  110.             'query' => $this->gridConfig->getQuery(),
  111.             'limits' => $this->gridConfig->getLimits(),
  112.             'resultSet' => $resultSet,
  113.             'channel' => $this->channelContext->getChannel(),
  114.             'currencyCode' => $this->currencyContext->getCurrencyCode(),
  115.             'moneySymbol' => $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL),
  116.             'gridConfig' => $this->gridConfig,
  117.         ]));
  118.     }
  119.     /**
  120.      * Perform the instant search action & display results.
  121.      *
  122.      * @param Request $request
  123.      *
  124.      * @return Response
  125.      */
  126.     public function instantAction(Request $request): Response
  127.     {
  128.         // Init grid config depending on request
  129.         $this->gridConfig->init(GridConfig::INSTANT_TYPE$request);
  130.         // Perform instant search
  131.         /** @var ResultSet $resultSet */
  132.         $resultSet $this->documentSearch->instant($this->gridConfig);
  133.         // Display instant result list
  134.         return new Response($this->templatingEngine->render('@MonsieurBizSyliusSearchPlugin/Instant/result.html.twig', [
  135.             'query' => $this->gridConfig->getQuery(),
  136.             'resultSet' => $resultSet,
  137.             'channel' => $this->channelContext->getChannel(),
  138.             'currencyCode' => $this->currencyContext->getCurrencyCode(),
  139.             'gridConfig' => $this->gridConfig,
  140.         ]));
  141.     }
  142.     /**
  143.      * Perform the taxon action & display results.
  144.      *
  145.      * @param Request $request
  146.      *
  147.      * @return Response
  148.      */
  149.     public function taxonAction(Request $request): Response
  150.     {
  151.         // Init grid config depending on request
  152.         $this->gridConfig->init(GridConfig::TAXON_TYPE$request$this->taxonContext->getTaxon());
  153.         // Perform search
  154.         /** @var ResultSet $resultSet */
  155.         $resultSet $this->documentSearch->taxon($this->gridConfig);
  156.         // Get number formatter for currency
  157.         $currencyCode $this->currencyContext->getCurrencyCode();
  158.         $formatter = new \NumberFormatter($request->getLocale() . '@currency=' $currencyCode, \NumberFormatter::CURRENCY);
  159.         // Display result list
  160.         return new Response($this->templatingEngine->render('@MonsieurBizSyliusSearchPlugin/Taxon/result.html.twig', [
  161.             'taxon' => $this->gridConfig->getTaxon(),
  162.             'limits' => $this->gridConfig->getLimits(),
  163.             'resultSet' => $resultSet,
  164.             'channel' => $this->channelContext->getChannel(),
  165.             'currencyCode' => $this->currencyContext->getCurrencyCode(),
  166.             'moneySymbol' => $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL),
  167.             'gridConfig' => $this->gridConfig,
  168.         ]));
  169.     }
  170. }