vendor/bitbag/elasticsearch-plugin/src/Form/Type/SearchType.php line 30

  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.io and write us
  7.  * an email on hello@bitbag.io.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Form\Type;
  11. use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Model\Search;
  13. use BitBag\SyliusElasticsearchPlugin\Model\SearchBox;
  14. use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
  15. use Elastica\Query;
  16. use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
  17. use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
  18. use Pagerfanta\Adapter\AdapterInterface;
  19. use Symfony\Component\Form\AbstractType;
  20. use Symfony\Component\Form\FormBuilderInterface;
  21. use Symfony\Component\Form\FormEvent;
  22. use Symfony\Component\Form\FormEvents;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\OptionsResolver\OptionsResolver;
  25. final class SearchType extends AbstractType
  26. {
  27.     private PaginatedFinderInterface $finder;
  28.     private RegistryInterface $facetRegistry;
  29.     private QueryBuilderInterface $searchProductsQueryBuilder;
  30.     public function __construct(
  31.         PaginatedFinderInterface $finder,
  32.         RegistryInterface $facetRegistry,
  33.         QueryBuilderInterface $searchProductsQueryBuilder
  34.     ) {
  35.         $this->finder $finder;
  36.         $this->facetRegistry $facetRegistry;
  37.         $this->searchProductsQueryBuilder $searchProductsQueryBuilder;
  38.     }
  39.     public function buildForm(FormBuilderInterface $builder, array $options): void
  40.     {
  41.         $builder
  42.             ->add('box'SearchBoxType::class, ['label' => false])
  43.             ->setMethod('GET')
  44.         ;
  45.         $formModifier = function (FormInterface $formAdapterInterface $adapter) {
  46.             if (!$adapter instanceof FantaPaginatorAdapter) {
  47.                 return;
  48.             }
  49.             $form->add('facets'SearchFacetsType::class, ['facets' => $adapter->getAggregations(), 'label' => false]);
  50.         };
  51.         $builder
  52.             ->get('box')
  53.             ->addEventListener(
  54.                 FormEvents::POST_SUBMIT,
  55.                 function (FormEvent $event) use ($formModifier) {
  56.                     /** @var SearchBox $data */
  57.                     $data $event->getForm()->getData();
  58.                     if (!$data->getQuery()) {
  59.                         return;
  60.                     }
  61.                     $query = new Query($this->searchProductsQueryBuilder->buildQuery(['query' => $data->getQuery()]));
  62.                     foreach ($this->facetRegistry->getFacets() as $facetId => $facet) {
  63.                         $query->addAggregation($facet->getAggregation()->setName($facetId));
  64.                     }
  65.                     $query->setSize(0);
  66.                     $results $this->finder->findPaginated($query);
  67.                     if ($results->getAdapter()) {
  68.                         $formModifier($event->getForm()->getParent(), $results->getAdapter());
  69.                     }
  70.                 }
  71.             )
  72.         ;
  73.     }
  74.     public function configureOptions(OptionsResolver $resolver): void
  75.     {
  76.         $resolver->setDefaults([
  77.             'data_class' => Search::class,
  78.             'csrf_protection' => false,
  79.         ]);
  80.     }
  81.     public function getBlockPrefix(): string
  82.     {
  83.         return 'bitbag_elasticsearch_search';
  84.     }
  85. }