1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
19
|
|
|
use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The SearchRequestBuilder is responsible to build a valid SearchRequest. |
24
|
|
|
* @author Timo Hund <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SearchRequestBuilder |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TypoScriptConfiguration |
31
|
|
|
*/ |
32
|
|
|
protected $typoScriptConfiguration; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FrontendUserSession |
36
|
|
|
*/ |
37
|
|
|
protected $session = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SearchRequestBuilder constructor. |
41
|
|
|
* @param TypoScriptConfiguration $typoScriptConfiguration |
42
|
|
|
* @param FrontendUserSession $frontendUserSession |
43
|
|
|
*/ |
44
|
39 |
|
public function __construct(TypoScriptConfiguration $typoScriptConfiguration, FrontendUserSession $frontendUserSession = null) |
45
|
|
|
{ |
46
|
39 |
|
$this->typoScriptConfiguration = $typoScriptConfiguration; |
47
|
39 |
|
$this->session = $frontendUserSession ?? GeneralUtility::makeInstance(FrontendUserSession::class); |
48
|
39 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $controllerArguments |
52
|
|
|
* @param int $pageId |
53
|
|
|
* @param int $languageId |
54
|
|
|
* @return SearchRequest |
55
|
|
|
*/ |
56
|
36 |
|
public function buildForSearch(array $controllerArguments, $pageId, $languageId) |
57
|
|
|
{ |
58
|
36 |
|
$controllerArguments = $this->adjustPageArgumentToPositiveInteger($controllerArguments); |
59
|
|
|
|
60
|
|
|
/** @var $searchRequest SearchRequest */ |
61
|
36 |
|
$argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace(); |
62
|
36 |
|
$searchRequest = $this->getRequest([$argumentsNamespace => $controllerArguments], $pageId, $languageId); |
63
|
36 |
|
$searchRequest = $this->applyPassedResultsPerPage($searchRequest); |
64
|
|
|
|
65
|
36 |
|
return $searchRequest; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if the passed resultsPerPageValue is valid and applies it. If the perPage value was changed it is stored in |
70
|
|
|
* the session and the current page is set to 0, since the pagination should start from the beginning then. |
71
|
|
|
* |
72
|
|
|
* @param SearchRequest $searchRequest |
73
|
|
|
* @return SearchRequest |
74
|
|
|
*/ |
75
|
36 |
|
protected function applyPassedResultsPerPage(SearchRequest $searchRequest): SearchRequest |
76
|
|
|
{ |
77
|
36 |
|
$requestedPerPage = $searchRequest->getResultsPerPage(); |
78
|
|
|
|
79
|
36 |
|
$perPageSwitchOptions = $this->typoScriptConfiguration->getSearchResultsPerPageSwitchOptionsAsArray(); |
80
|
36 |
|
if (isset($requestedPerPage) && in_array($requestedPerPage, $perPageSwitchOptions)) { |
81
|
4 |
|
$this->session->setPerPage($requestedPerPage); |
82
|
4 |
|
$searchRequest->setPage(0); |
83
|
|
|
} |
84
|
|
|
|
85
|
36 |
|
$defaultResultsPerPage = $this->typoScriptConfiguration->getSearchResultsPerPage(); |
86
|
36 |
|
$currentNumberOfResultsShown = $defaultResultsPerPage; |
87
|
36 |
|
if ($this->session->getHasPerPage()) { |
88
|
4 |
|
$sessionResultPerPage = $this->session->getPerPage(); |
89
|
4 |
|
if (in_array($sessionResultPerPage, $perPageSwitchOptions)) { |
90
|
4 |
|
$currentNumberOfResultsShown = (int)$sessionResultPerPage; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
36 |
|
if ($this->shouldHideResultsFromInitialSearch($searchRequest)) { |
95
|
|
|
// initialize search with an empty query, which would by default return all documents |
96
|
|
|
// anyway, tell Solr to not return any result documents |
97
|
|
|
// Solr will still return facets though |
98
|
2 |
|
$currentNumberOfResultsShown = 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
36 |
|
$searchRequest->setResultsPerPage($currentNumberOfResultsShown); |
102
|
|
|
|
103
|
36 |
|
return $searchRequest; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Checks it the results should be hidden in the response. |
108
|
|
|
* |
109
|
|
|
* @param SearchRequest $searchRequest |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
36 |
|
protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest) |
113
|
|
|
{ |
114
|
36 |
|
return ($this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() || |
115
|
36 |
|
$this->typoScriptConfiguration->getSearchInitializeWithQuery()) && |
116
|
36 |
|
!$this->typoScriptConfiguration->getSearchShowResultsOfInitialEmptyQuery() && |
117
|
36 |
|
!$this->typoScriptConfiguration->getSearchShowResultsOfInitialQuery() && |
118
|
36 |
|
$searchRequest->getRawUserQueryIsNull(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $pageId |
123
|
|
|
* @param int $languageId |
124
|
|
|
* @return SearchRequest |
125
|
|
|
*/ |
126
|
1 |
|
public function buildForFrequentSearches($pageId, $languageId) |
127
|
|
|
{ |
128
|
|
|
/** @var $searchRequest SearchRequest */ |
129
|
1 |
|
$searchRequest = $this->getRequest([], $pageId, $languageId); |
130
|
1 |
|
return $searchRequest; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $controllerArguments |
135
|
|
|
* @param string $rawUserQuery |
136
|
|
|
* @param int $pageId |
137
|
|
|
* @param int $languageId |
138
|
|
|
* @return SearchRequest |
139
|
|
|
*/ |
140
|
2 |
|
public function buildForSuggest(array $controllerArguments, $rawUserQuery, $pageId, $languageId) |
141
|
|
|
{ |
142
|
2 |
|
$controllerArguments['page'] = 0; |
143
|
2 |
|
$controllerArguments['q'] = $rawUserQuery; |
144
|
2 |
|
$argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace(); |
145
|
|
|
|
146
|
2 |
|
return $this->getRequest(['q' => $rawUserQuery, $argumentsNamespace => $controllerArguments], $pageId, $languageId); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Creates an instance of the SearchRequest. |
151
|
|
|
* |
152
|
|
|
* @param array $requestArguments |
153
|
|
|
* @param int $pageId |
154
|
|
|
* @param int $languageId |
155
|
|
|
* @return SearchRequest |
156
|
|
|
*/ |
157
|
39 |
|
protected function getRequest(array $requestArguments = [], int $pageId = 0, int $languageId = 0): SearchRequest |
158
|
|
|
{ |
159
|
39 |
|
return GeneralUtility::makeInstance( |
160
|
39 |
|
SearchRequest::class, |
161
|
39 |
|
/** @scrutinizer ignore-type */ $requestArguments, |
162
|
39 |
|
/** @scrutinizer ignore-type */ $pageId, |
163
|
39 |
|
/** @scrutinizer ignore-type */ $languageId, |
164
|
39 |
|
/** @scrutinizer ignore-type */ $this->typoScriptConfiguration |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* This methods sets the page argument to an expected positive integer value in the arguments array. |
170
|
|
|
* |
171
|
|
|
* @param array $arguments |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
36 |
|
protected function adjustPageArgumentToPositiveInteger(array $arguments): array |
175
|
|
|
{ |
176
|
36 |
|
$page = isset($arguments['page']) ? intval($arguments['page']) : 0; |
177
|
36 |
|
$arguments['page'] = max($page, 0); |
178
|
|
|
|
179
|
36 |
|
return $arguments; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|