1
+ <?php
2
+
3
+ namespace TntSearch \Controller \Front ;
4
+
5
+ use OpenApi \Annotations as OA ;
6
+ use OpenApi \Model \Api \ModelFactory ;
7
+ use OpenApi \Service \OpenApiService ;
8
+ use Propel \Runtime \ActiveQuery \Criteria ;
9
+ use Symfony \Component \Routing \Annotation \Route ;
10
+ use Thelia \Controller \Front \BaseFrontController ;
11
+ use Thelia \Core \HttpFoundation \Request ;
12
+ use Thelia \Core \HttpFoundation \Response ;
13
+ use TntSearch \Service \Search ;
14
+
15
+ /**
16
+ * @Route("/open_api", name="tntsearch_search")
17
+ */
18
+ class SearchOpenApiController extends BaseFrontController
19
+ {
20
+ /**
21
+ *
22
+ * @OA\Get(
23
+ * path="/tnt-search",
24
+ * tags={"TNTSearch", "Search"},
25
+ * summary="Search products or category or folder or customer etc...",
26
+ * @OA\Parameter(
27
+ * name="q",
28
+ * in="query",
29
+ * @OA\Schema(
30
+ * type="string"
31
+ * )
32
+ * ),
33
+ * @OA\Parameter(
34
+ * name="indexes",
35
+ * in="query",
36
+ * @OA\Schema(
37
+ * type="string"
38
+ * )
39
+ * ),
40
+ * @OA\Parameter(
41
+ * name="limit",
42
+ * in="query",
43
+ * @OA\Schema(
44
+ * type="integer"
45
+ * )
46
+ * ),
47
+ * @OA\Parameter(
48
+ * name="offset",
49
+ * in="query",
50
+ * @OA\Schema(
51
+ * type="integer"
52
+ * )
53
+ * ),
54
+ * @OA\Response(
55
+ * response="200",
56
+ * description="Success",
57
+ * @OA\JsonContent()
58
+ * ),
59
+ * @OA\Response(
60
+ * response="400",
61
+ * description="Bad request",
62
+ * @OA\JsonContent(ref="#/components/schemas/Error")
63
+ * )
64
+ * )
65
+ */
66
+ public function apiSearch (): Response
67
+ {
68
+ /** @var Search $search */
69
+ $ search = $ this ->getContainer ()->get ('tntsearch.search ' );
70
+
71
+ /** @var Request $request */
72
+ $ request = $ this ->getRequest ();
73
+
74
+ /** @var ModelFactory $modelFactory */
75
+ $ modelFactory = $ this ->getContainer ()->get ('open_api.model.factory ' );
76
+
77
+
78
+ $ resultsByIndex = $ search ->search (
79
+ $ request ->get ('q ' ),
80
+ ($ index = $ request ->get ('indexes ' )) ? explode (', ' , $ index ) : null ,
81
+ $ request ->getSession ()->getLang ()->getLocale (),
82
+ $ request ->get ('offset ' , 0 ),
83
+ $ request ->get ('limit ' , 100 )
84
+ );
85
+
86
+ $ data = [];
87
+
88
+ foreach ($ resultsByIndex as $ index => $ ids ) {
89
+ if (empty ($ ids )) {
90
+ continue ;
91
+ }
92
+
93
+ $ model = $ search ->buildPropelModelFromIndex ($ index );
94
+ $ modelTableMap = $ search ->buildPropelTableMapFromIndex ($ index );
95
+
96
+ $ rows = $ model ->filterById ($ ids , Criteria::IN );
97
+
98
+ foreach ($ ids as $ singleId ) {
99
+ $ givenIdMatched = 'given_id_matched_ ' .$ singleId ;
100
+ $ rows ->withColumn ($ modelTableMap ::COL_ID ."=' $ singleId' " , $ givenIdMatched );
101
+ $ rows ->orderBy ($ givenIdMatched , Criteria::DESC );
102
+ }
103
+
104
+ $ data [$ index ] = array_map (function ($ row ) use ($ index , $ modelFactory ) {
105
+ return $ modelFactory ->buildModel (ucwords ($ index ), $ row );
106
+ }, iterator_to_array ($ rows ));
107
+ }
108
+
109
+ return $ this ->jsonResponse (json_encode ($ data ));
110
+ }
111
+ }
0 commit comments