11<?php
2+ /**
3+ * DISCLAIMER
4+ *
5+ * Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
6+ *
7+ * @package Gally
8+ * @author Stephan Hochdörfer <S.Hochdoerfer@bitexpert.de>, Gally Team <elasticsuite@smile.fr>
9+ * @copyright 2022-present Smile
10+ * @license Open Software License v. 3.0 (OSL-3.0)
11+ */
212
313declare (strict_types=1 );
414
515namespace Gally \SyliusPlugin \Controller \Shop ;
616
17+ use Gally \Sdk \GraphQl \Response as GallyResponse ;
718use Gally \SyliusPlugin \Form \Type \SearchFormType ;
19+ use Gally \SyliusPlugin \Model \GallyChannelInterface ;
820use Gally \SyliusPlugin \Search \Finder ;
21+ use Sylius \Component \Channel \Context \ChannelContextInterface ;
922use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
1023use Symfony \Component \HttpFoundation \JsonResponse ;
1124use Symfony \Component \HttpFoundation \RedirectResponse ;
1528
1629class SearchController extends AbstractController
1730{
18- public function __construct (private Finder $ finder )
19- {
31+ public function __construct (
32+ private Finder $ finder ,
33+ private ChannelContextInterface $ channelContext ,
34+ ) {
2035 }
2136
22- public function renderForm (RequestStack $ requestStack ): Response
37+ public function getForm (RequestStack $ requestStack ): Response
2338 {
24- $ query = $ requestStack -> getMainRequest ()-> get ( ' query ' );
25- // Try the search filter
39+ /** @var string $ query */
40+ $ query = $ requestStack -> getMainRequest ()?->get( ' query ' );
2641 if (empty ($ query )) {
27- $ query = $ requestStack ->getMainRequest ()->get ('criteria ' , [])['search ' ]['value ' ] ?? '' ;
42+ /** @var array $query */
43+ $ query = $ requestStack ->getMainRequest ()?->get('criteria ' , []);
44+ $ query = $ query ['search ' ]['value ' ] ?? '' ;
2845 }
2946
3047 $ searchForm = $ this ->createForm (
3148 SearchFormType::class,
3249 ['query ' => $ query ],
33- [
34- 'action ' => $ this ->generateUrl ('gally_search_result_page ' ),
35- 'method ' => 'POST ' ,
36- ]
50+ ['action ' => $ this ->generateUrl ('gally_search_result_page ' ), 'method ' => 'POST ' ]
3751 );
3852
39- return $ this ->render ('@GallySyliusPlugin/shop/shared/components/header/search/form.html.twig ' , [
40- 'searchForm ' => $ searchForm ->createView (),
41- ]);
53+ return $ this ->render (
54+ '@GallySyliusPlugin/shop/shared/components/header/search/form.html.twig ' ,
55+ ['searchForm ' => $ searchForm ->createView ()]
56+ );
4257 }
4358
4459 public function getResults (Request $ request ): Response
4560 {
4661 $ searchForm = $ this ->createForm (SearchFormType::class);
47-
4862 $ searchForm ->handleRequest ($ request );
4963
5064 if ($ searchForm ->isSubmitted () && $ searchForm ->isValid ()) {
@@ -60,24 +74,83 @@ public function getPreview(Request $request): Response
6074 {
6175 $ searchForm = $ this ->createForm (SearchFormType::class);
6276 $ searchForm ->handleRequest ($ request );
77+ /** @var GallyChannelInterface $currentChannel */
78+ $ currentChannel = $ this ->channelContext ->getChannel ();
6379
6480 if ($ searchForm ->isSubmitted () && $ searchForm ->isValid ()) {
81+ /** @var string $query */
6582 $ query = $ searchForm ->get ('query ' )->getData ();
6683
84+ $ products = $ this ->getProductAutocomplete ($ query , $ currentChannel );
85+
6786 return new JsonResponse ([
6887 'htmlResults ' => $ this ->renderView (
6988 '@GallySyliusPlugin/shop/shared/components/header/search/autocomplete/results.html.twig ' ,
7089 [
71- 'products ' => $ this ->finder ->getAutocompleteResults ($ query ),
72- 'categories ' => $ this ->finder ->getAutocompleteResults ($ query , 'category ' , ['source ' , 'path ' , 'name ' ]),
73- 'view_all_results_link ' => $ this ->generateUrl ('gally_search_result_page ' , [
74- 'query ' => $ query
75- ]),
90+ 'products ' => $ products ->getCollection (),
91+ 'categories ' => $ this ->getCategoryAutocomplete ($ query , $ currentChannel ),
92+ 'attributes ' => $ this ->getAttributeAutocomplete ($ products , $ currentChannel ),
93+ 'query ' => $ query , $ this ->generateUrl ('gally_search_result_page ' , ['query ' => $ query ]),
7694 ]
77- )
95+ ),
7896 ]);
7997 }
8098
8199 return new JsonResponse (['gallyError ' => true ]);
82100 }
101+
102+ private function getProductAutocomplete (string $ query , GallyChannelInterface $ channel ): GallyResponse
103+ {
104+ return $ this ->finder ->getAutocompleteResults (
105+ $ query ,
106+ $ channel ->getGallyAutocompleteProductMaxSize (),
107+ 'product ' ,
108+ ['sku ' , 'name ' , 'slug ' , 'image ' ]
109+ );
110+ }
111+
112+ private function getCategoryAutocomplete (string $ query , GallyChannelInterface $ channel ): array
113+ {
114+ $ categories = $ this ->finder
115+ ->getAutocompleteResults (
116+ $ query ,
117+ $ channel ->getGallyAutocompleteCategoryMaxSize (),
118+ 'category ' ,
119+ ['id ' , 'name ' , 'path ' , 'slug ' ]
120+ )
121+ ->getCollection ();
122+
123+ foreach ($ categories as &$ category ) {
124+ $ category ['path ' ] = implode (
125+ ' > ' ,
126+ array_map (
127+ fn (string $ slug ) => ucfirst ($ slug ),
128+ explode ('/ ' , $ category ['slug ' ])
129+ )
130+ );
131+ }
132+
133+ return $ categories ;
134+ }
135+
136+ private function getAttributeAutocomplete (GallyResponse $ products , GallyChannelInterface $ channel ): array
137+ {
138+ $ attributes = [];
139+ $ count = 0 ;
140+ foreach ($ products ->getAggregations () as $ aggregation ) {
141+ foreach ($ aggregation ['options ' ] as $ option ) {
142+ $ attributes [] = [
143+ 'field ' => $ aggregation ['field ' ],
144+ 'label ' => $ aggregation ['label ' ],
145+ 'option_label ' => $ option ['label ' ],
146+ ];
147+ ++$ count ;
148+ if ($ count >= $ channel ->getGallyAutocompleteAttributeMaxSize ()) {
149+ break 2 ;
150+ }
151+ }
152+ }
153+
154+ return $ attributes ;
155+ }
83156}
0 commit comments