|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: FlorianMeyer |
|
5
|
|
|
* Date: 18.01.2018 |
|
6
|
|
|
* Time: 13:04 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace StingerSoft\EntitySearchBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use StingerSoft\EntitySearchBundle\Form\QueryType; |
|
12
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
|
13
|
|
|
use StingerSoft\EntitySearchBundle\Model\PaginatableResultSet; |
|
14
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
|
15
|
|
|
use StingerSoft\EntitySearchBundle\Services\Facet\FacetServiceInterface; |
|
16
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\DocumentToEntityMapperInterface; |
|
17
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
21
|
|
|
|
|
22
|
|
|
trait SearchControllerTrait { |
|
23
|
|
|
|
|
24
|
|
|
use AbstractControllerTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @var SearchService |
|
29
|
|
|
*/ |
|
30
|
|
|
private $searchService; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private $availableFacets; |
|
34
|
|
|
|
|
35
|
|
|
private $facetFormatter; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public function searchAction(Request $request) { |
|
40
|
|
|
if($request->query->get('term', false) !== false) { |
|
41
|
|
|
$this->setSearchTerm($request->getSession(), $request->query->get('term')); |
|
|
|
|
|
|
42
|
|
|
return $this->redirectToRoute('stinger_soft_entity_search_search'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$term = $this->getSearchTerm($request->getSession()); |
|
|
|
|
|
|
46
|
|
|
$facets = $this->getSearchFacets($request->getSession()); |
|
|
|
|
|
|
47
|
|
|
$availableFacets = $this->getAvailableFacets(); |
|
48
|
|
|
|
|
49
|
|
|
$query = new Query($term, $facets, array_keys($availableFacets)); |
|
50
|
|
|
|
|
51
|
|
|
$facetForm = $this->createForm(QueryType::class, $query, array( |
|
52
|
|
|
'used_facets' => $this->getConfiguredUsedFacets($query->getUsedFacets()) |
|
53
|
|
|
)); |
|
54
|
|
|
|
|
55
|
|
|
$facetForm->handleRequest($request); |
|
56
|
|
|
if($facetForm->isSubmitted()) { |
|
57
|
|
|
if($facetForm->get('clear')->isClicked()) { |
|
58
|
|
|
$query->setFacets($this->getDefaultFacets()); |
|
59
|
|
|
} |
|
60
|
|
|
$this->setSearchTerm($request->getSession(), $query->getSearchTerm()); |
|
|
|
|
|
|
61
|
|
|
$this->setSearchFacets($request->getSession(), $query->getFacets()); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
$result = $this->getSearchService()->search($query); |
|
64
|
|
|
|
|
65
|
|
|
$facetForm = $this->createForm(QueryType::class, $query, array( |
|
66
|
|
|
'result' => $result, |
|
67
|
|
|
'used_facets' => $this->getConfiguredUsedFacets($query->getUsedFacets()), |
|
68
|
|
|
'facet_formatter' => $this->getFacetFormatter() |
|
69
|
|
|
)); |
|
70
|
|
|
|
|
71
|
|
|
$page = $request->query->get('page', 1); |
|
72
|
|
|
$results = array(); |
|
|
|
|
|
|
73
|
|
|
if($result instanceof PaginatableResultSet) { |
|
74
|
|
|
$results = $result->paginate($page, $this->getResultsPerPage()); |
|
75
|
|
|
} else { |
|
76
|
|
|
$results = $result->getResults(($page - 1) * $this->getResultsPerPage(), $this->getResultsPerPage()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->render($this->getTemplate(), array( |
|
80
|
|
|
'results' => $results, |
|
81
|
|
|
'resultSet' => $result, |
|
82
|
|
|
'term' => $query->getSearchTerm(), |
|
83
|
|
|
'mapper' => $this->get(DocumentToEntityMapperInterface::SERVICE_ID), |
|
|
|
|
|
|
84
|
|
|
'facetForm' => $facetForm->createView() |
|
85
|
|
|
)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getConfiguredUsedFacets(array $queryUsedFacets) { |
|
89
|
|
|
$availableFacets = $this->getAvailableFacets(); |
|
90
|
|
|
$usedFacets = array(); |
|
91
|
|
|
foreach($queryUsedFacets as $queryUsedFacet) { |
|
92
|
|
|
$usedFacets[$queryUsedFacet] = $availableFacets[$queryUsedFacet]; |
|
93
|
|
|
} |
|
94
|
|
|
return $usedFacets; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Maximum number of suggestions which should be provided by the autocomplete action |
|
99
|
|
|
* |
|
100
|
|
|
* @return integer |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getSuggestionCount() { |
|
103
|
|
|
return 10; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Prefix to store information in the session variable |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getSessionPrefix() { |
|
112
|
|
|
return 'stinger_soft_entity_search'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Number of results which should be displayed per page |
|
117
|
|
|
* |
|
118
|
|
|
* @return integer |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getResultsPerPage() { |
|
121
|
|
|
return 10; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function getTemplate() { |
|
125
|
|
|
return 'StingerSoftEntitySearchBundle:Search:results.html.twig'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function getFacetFormatter() { |
|
129
|
|
|
$this->initFacets(); |
|
130
|
|
|
return $this->facetFormatter; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns an array of preselected facets (no facet selected is the default) |
|
135
|
|
|
* |
|
136
|
|
|
* @return string[string][] |
|
|
|
|
|
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getDefaultFacets() { |
|
139
|
|
|
$availableFacets = $this->getAvailableFacets(); |
|
140
|
|
|
return array_combine(array_keys($availableFacets), array_fill(0, count($availableFacets), array())); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns an array of the available facets which should be offered the user as a filter |
|
145
|
|
|
* |
|
146
|
|
|
* @return string[] |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getAvailableFacets() { |
|
149
|
|
|
$this->initFacets(); |
|
150
|
|
|
return $this->availableFacets; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
protected function initFacets() { |
|
155
|
|
|
if(!$this->availableFacets) { |
|
156
|
|
|
$this->availableFacets = array(); |
|
157
|
|
|
$this->facetFormatter = array(); |
|
158
|
|
|
|
|
159
|
|
|
$facetServices = $this->getParameter('stinger_soft.entity_search.available_facets'); |
|
|
|
|
|
|
160
|
|
|
foreach($facetServices as $facetServiceId) { |
|
161
|
|
|
/** @var FacetServiceInterface $facetService */ |
|
162
|
|
|
$facetService = $this->get($facetServiceId); |
|
|
|
|
|
|
163
|
|
|
$this->availableFacets[$facetService->getField()] = $facetService->getFormOptions(); |
|
164
|
|
|
if($facetService->getFacetFormatter()) { |
|
165
|
|
|
$this->facetFormatter[$facetService->getField()] = $facetService->getFacetFormatter(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Fetches the searched facets from the session object |
|
173
|
|
|
* |
|
174
|
|
|
* @param SessionInterface $session |
|
175
|
|
|
* @return string[string][] |
|
|
|
|
|
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getSearchFacets(SessionInterface $session) { |
|
178
|
|
|
$facets = $session->get($this->getSessionPrefix() . '_facets', false); |
|
179
|
|
|
return $facets ? json_decode($facets, true) : $this->getDefaultFacets(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sets the given search facets in the user's session |
|
184
|
|
|
* |
|
185
|
|
|
* @param SessionInterface $session |
|
186
|
|
|
* @param string[string][] $facets |
|
|
|
|
|
|
187
|
|
|
*/ |
|
188
|
|
|
protected function setSearchFacets(SessionInterface $session, $facets) { |
|
189
|
|
|
$session->set($this->getSessionPrefix() . '_facets', json_encode($facets)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Fetches the search term from the session object |
|
194
|
|
|
* |
|
195
|
|
|
* @param SessionInterface $session |
|
196
|
|
|
* @return mixed |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function getSearchTerm(SessionInterface $session) { |
|
199
|
|
|
return $session->get($this->getSessionPrefix() . '_term', false); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets the given search term in the user's session |
|
204
|
|
|
* |
|
205
|
|
|
* @param SessionInterface $session |
|
206
|
|
|
* @param string $term |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function setSearchTerm(SessionInterface $session, $term) { |
|
209
|
|
|
$session->set($this->getSessionPrefix() . '_term', $term); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns a JSON array of autocompletions for the given term. |
|
214
|
|
|
* The term can be provided as a GET or POST paramater with the name <em>term</em> |
|
215
|
|
|
* |
|
216
|
|
|
* @param Request $request |
|
217
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
218
|
|
|
*/ |
|
219
|
|
|
public function autocompleteAction(Request $request) { |
|
220
|
|
|
$term = $request->get('term'); |
|
221
|
|
|
return new JsonResponse($this->getSearchService()->autocomplete($term, $this->getSuggestionCount())); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Provides an online help for the configured search service. |
|
226
|
|
|
* If the search service has no online help defined a warning message will be displayed to the user. |
|
227
|
|
|
* |
|
228
|
|
|
* @param Request $request |
|
229
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
230
|
|
|
*/ |
|
231
|
|
|
public function onlineHelpAction(Request $request) { |
|
232
|
|
|
$template = $this->getSearchService()->getOnlineHelp($request->getLocale(), $this->getDefaultLocale()); |
|
|
|
|
|
|
233
|
|
|
return $this->render($template ?: 'StingerSoftEntitySearchBundle:Help:no_help.html.twig'); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Inits and returns the configured search service |
|
238
|
|
|
* |
|
239
|
|
|
* @return SearchService |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function getSearchService() { |
|
242
|
|
|
$this->service = $this->get(SearchService::SERVICE_ID); |
|
|
|
|
|
|
243
|
|
|
$this->service->setObjectManager($this->getDoctrine()->getManager()); |
|
|
|
|
|
|
244
|
|
|
return $this->service; |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: