1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Controller; |
13
|
|
|
|
14
|
|
|
use StingerSoft\EntitySearchBundle\Form\QueryType; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Model\PaginatableResultSet; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
18
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\DocumentToEntityMapperInterface; |
19
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
21
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Basic implementation of a search controller to offer a frontend access to the search service |
27
|
|
|
*/ |
28
|
|
|
class SearchController extends Controller { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Maximum number of suggestions which should be provided by the autocomplete action |
32
|
|
|
* |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
const SUGGESTION_COUNT = 10; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Prefix to store information in the session variable |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
const SESSION_PREFIX = 'stinger_soft_entity_search'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Number of results which should be displayed per page |
46
|
|
|
* |
47
|
|
|
* @var integer |
48
|
|
|
*/ |
49
|
|
|
const RESULTS_PER_PAGE = 10; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @var SearchService |
54
|
|
|
*/ |
55
|
|
|
private $searchService; |
|
|
|
|
56
|
|
|
|
57
|
|
|
public function searchAction(Request $request) { |
58
|
|
|
if($request->query->get('term', false) !== false) { |
59
|
|
|
$this->setSearchTerm($request->getSession(), $request->query->get('term')); |
|
|
|
|
60
|
|
|
return $this->redirectToRoute('stinger_soft_entity_search_search'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$term = $this->getSearchTerm($request->getSession()); |
|
|
|
|
64
|
|
|
$facets = $this->getSearchFacets($request->getSession()); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$query = new Query($term, $facets, $this->getAvailableFacets()); |
67
|
|
|
|
68
|
|
|
$facetForm = $this->createForm(QueryType::class, $query, array( |
69
|
|
|
'used_facets' => $query->getUsedFacets() |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
if($request->isMethod('POST')) { |
73
|
|
|
$facetForm->handleRequest($request); |
74
|
|
|
|
75
|
|
|
if($facetForm->get('clear')->isClicked()) { |
|
|
|
|
76
|
|
|
$query->setFacets($this->getDefaultFacets()); |
77
|
|
|
} |
78
|
|
|
$this->setSearchTerm($request->getSession(), $query->getSearchTerm()); |
|
|
|
|
79
|
|
|
$this->setSearchFacets($request->getSession(), $query->getFacets()); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
$result = $this->getSearchService()->search($query); |
82
|
|
|
|
83
|
|
|
$facetForm = $this->createForm(QueryType::class, $query, array( |
84
|
|
|
'result' => $result, |
85
|
|
|
'used_facets' => $query->getUsedFacets(), |
86
|
|
|
'facet_formatter' => $this->getFacetFormatter() |
87
|
|
|
)); |
88
|
|
|
|
89
|
|
|
$page = $request->query->get('page', 1); |
90
|
|
|
$results = array(); |
|
|
|
|
91
|
|
|
if($result instanceof PaginatableResultSet) { |
92
|
|
|
$results = $result->paginate($page, self::RESULTS_PER_PAGE); |
93
|
|
|
} else { |
94
|
|
|
$results = $result->getResults(($page - 1) * self::RESULTS_PER_PAGE, self::RESULTS_PER_PAGE); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->render('StingerSoftEntitySearchBundle:Search:results.html.twig', array( |
98
|
|
|
'results' => $results, |
99
|
|
|
'resultSet' => $result, |
100
|
|
|
'term' => $query->getSearchTerm(), |
101
|
|
|
'mapper' => $this->get(DocumentToEntityMapperInterface::SERVICE_ID), |
102
|
|
|
'facetForm' => $facetForm->createView() |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getFacetFormatter() { |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns an array of preselected facets (no facet selected is the default) |
112
|
|
|
* |
113
|
|
|
* @return string[string][] |
|
|
|
|
114
|
|
|
*/ |
115
|
|
|
protected function getDefaultFacets() { |
116
|
|
|
$availableFacets = $this->getAvailableFacets(); |
117
|
|
|
return array_combine($availableFacets, array_fill(0, count($availableFacets), array())); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns an array of the available facets which should be offered the user as a filter |
122
|
|
|
* |
123
|
|
|
* @return string[] |
124
|
|
|
*/ |
125
|
|
|
protected function getAvailableFacets() { |
126
|
|
|
return array( |
127
|
|
|
Document::FIELD_AUTHOR, |
128
|
|
|
Document::FIELD_EDITORS, |
129
|
|
|
Document::FIELD_TYPE |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Fetches the searched facets from the session object |
135
|
|
|
* |
136
|
|
|
* @param SessionInterface $session |
137
|
|
|
* @return string[string][] |
|
|
|
|
138
|
|
|
*/ |
139
|
|
|
protected function getSearchFacets(SessionInterface $session) { |
140
|
|
|
$facets = $session()->get(self::SESSION_PREFIX . '_facets', false); |
141
|
|
|
return $facets ? json_decode($facets, true) : $this->getDefaultFacets(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function setSearchFacets(SessionInterface $session, $facets) { |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Fetches the search term from the session object |
149
|
|
|
* |
150
|
|
|
* @param SessionInterface $session |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
protected function getSearchTerm(SessionInterface $session) { |
154
|
|
|
return $session->get(self::SESSION_PREFIX . '_term', false); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sets the given search term in the user's session |
159
|
|
|
* |
160
|
|
|
* @param SessionInterface $session |
161
|
|
|
* @param string $term |
162
|
|
|
*/ |
163
|
|
|
protected function setSearchTerm(SessionInterface $session, $term) { |
164
|
|
|
$session->set(self::SESSION_PREFIX . '_term', $term); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns a JSON array of autocompletions for the given term. |
169
|
|
|
* The term can be provided as a GET or POST paramater with the name <em>term</em> |
170
|
|
|
* |
171
|
|
|
* @param Request $request |
172
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
173
|
|
|
*/ |
174
|
|
|
public function autocompleteAction(Request $request) { |
175
|
|
|
$term = $request->get('term'); |
176
|
|
|
return new JsonResponse($this->getSearchService()->autocomplete($term, self::SUGGESTION_COUNT)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Provides an online help for the configured search service. |
181
|
|
|
* If the search service has no online help defined a warning message will be displayed to the user. |
182
|
|
|
* |
183
|
|
|
* @param Request $request |
184
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
185
|
|
|
*/ |
186
|
|
|
public function onlineHelpAction(Request $request) { |
187
|
|
|
$template = $this->getSearchService()->getOnlineHelp($request->getLocale(), $this->getDefaultLocale()); |
|
|
|
|
188
|
|
|
return $this->render($template ?: 'StingerSoftEntitySearchBundle:Help:no_help.html.twig'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Inits and returns the configured search service |
193
|
|
|
* |
194
|
|
|
* @return SearchService |
195
|
|
|
*/ |
196
|
|
|
protected function getSearchService() { |
197
|
|
|
$this->service = $this->get(SearchService::SERVICE_ID); |
|
|
|
|
198
|
|
|
$this->service->setObjectManager($this->getObjectManager()); |
|
|
|
|
199
|
|
|
return $this->service; |
|
|
|
|
200
|
|
|
} |
201
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.