1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2017 Timo Hund <[email protected]> |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession; |
30
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The SearchRequestBuilder is responsible to build a valid SearchRequest. |
34
|
|
|
* |
35
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search |
36
|
|
|
*/ |
37
|
|
|
class SearchRequestBuilder |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TypoScriptConfiguration |
42
|
|
|
*/ |
43
|
|
|
protected $typoScriptConfiguration; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var FrontendUserSession |
47
|
|
|
*/ |
48
|
|
|
protected $session = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* SearchRequestBuilder constructor. |
52
|
|
|
* @param TypoScriptConfiguration $typoScriptConfiguration |
53
|
|
|
* @param FrontendUserSession $frontendUserSession |
54
|
|
|
*/ |
55
|
33 |
|
public function __construct(TypoScriptConfiguration $typoScriptConfiguration, FrontendUserSession $frontendUserSession = null) |
56
|
|
|
{ |
57
|
33 |
|
$this->typoScriptConfiguration = $typoScriptConfiguration; |
58
|
33 |
|
$this->session = is_null($frontendUserSession) ? GeneralUtility::makeInstance(FrontendUserSession::class) : $frontendUserSession; |
59
|
33 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $controllerArguments |
63
|
|
|
* @param int $pageId |
64
|
|
|
* @param int $languageId |
65
|
|
|
* @return SearchRequest |
66
|
|
|
*/ |
67
|
32 |
|
public function buildForSearch(array $controllerArguments, $pageId, $languageId) |
68
|
|
|
{ |
69
|
32 |
|
$controllerArguments = $this->adjustPageArgumentToPositiveInteger($controllerArguments); |
70
|
|
|
|
71
|
|
|
/** @var $searchRequest SearchRequest */ |
72
|
32 |
|
$argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace(); |
73
|
32 |
|
$searchRequest = $this->getRequest([$argumentsNamespace => $controllerArguments], $pageId, $languageId); |
74
|
32 |
|
$searchRequest = $this->applyPassedResultsPerPage($searchRequest); |
75
|
|
|
|
76
|
32 |
|
return $searchRequest; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks if the passed resultsPerPageValue is valid and applies it. If the perPage value was changed it is stored in |
81
|
|
|
* the session and the current page is set to 0, since the pagination should start from the beginning then. |
82
|
|
|
* |
83
|
|
|
* @param SearchRequest $searchRequest |
84
|
|
|
* @return SearchRequest |
85
|
|
|
*/ |
86
|
32 |
|
protected function applyPassedResultsPerPage(SearchRequest $searchRequest) |
87
|
|
|
{ |
88
|
32 |
|
$requestedPerPage = $searchRequest->getResultsPerPage(); |
89
|
|
|
|
90
|
32 |
|
$perPageSwitchOptions = $this->typoScriptConfiguration->getSearchResultsPerPageSwitchOptionsAsArray(); |
91
|
32 |
|
if (isset($requestedPerPage) && in_array($requestedPerPage, $perPageSwitchOptions)) { |
92
|
5 |
|
$this->session->setPerPage($requestedPerPage); |
|
|
|
|
93
|
5 |
|
$searchRequest->setPage(0); |
94
|
|
|
} |
95
|
|
|
|
96
|
32 |
|
$defaultResultsPerPage = $this->typoScriptConfiguration->getSearchResultsPerPage(); |
97
|
32 |
|
$currentNumberOfResultsShown = $defaultResultsPerPage; |
98
|
32 |
|
if ($this->session->getHasPerPage()) { |
99
|
4 |
|
$sessionResultPerPage = $this->session->getPerPage(); |
100
|
4 |
|
if (in_array($sessionResultPerPage, $perPageSwitchOptions)) { |
101
|
4 |
|
$currentNumberOfResultsShown = (int)$sessionResultPerPage; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
32 |
|
if ($this->shouldHideResultsFromInitialSearch($searchRequest)) { |
106
|
|
|
// initialize search with an empty query, which would by default return all documents |
107
|
|
|
// anyway, tell Solr to not return any result documents |
108
|
|
|
// Solr will still return facets though |
109
|
2 |
|
$currentNumberOfResultsShown = 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
32 |
|
$searchRequest->setResultsPerPage($currentNumberOfResultsShown); |
113
|
|
|
|
114
|
32 |
|
return $searchRequest; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Checks it the results should be hidden in the response. |
119
|
|
|
* |
120
|
|
|
* @param SearchRequest $searchRequest |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
32 |
|
protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest) |
124
|
|
|
{ |
125
|
32 |
|
return ($this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() || |
126
|
32 |
|
$this->typoScriptConfiguration->getSearchInitializeWithQuery()) && |
127
|
32 |
|
!$this->typoScriptConfiguration->getSearchShowResultsOfInitialEmptyQuery() && |
128
|
32 |
|
!$this->typoScriptConfiguration->getSearchShowResultsOfInitialQuery() && |
129
|
32 |
|
$searchRequest->getRawUserQueryIsNull(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int $pageId |
134
|
|
|
* @param int $languageId |
135
|
|
|
* @return SearchRequest |
136
|
|
|
*/ |
137
|
1 |
|
public function buildForFrequentSearches($pageId, $languageId) |
138
|
|
|
{ |
139
|
|
|
/** @var $searchRequest SearchRequest */ |
140
|
1 |
|
$searchRequest = $this->getRequest([], $pageId, $languageId); |
141
|
1 |
|
return $searchRequest; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $controllerArguments |
146
|
|
|
* @param string $rawUserQuery |
147
|
|
|
* @param int $pageId |
148
|
|
|
* @param int $languageId |
149
|
|
|
* @return SearchRequest |
150
|
|
|
*/ |
151
|
|
|
public function buildForSuggest(array $controllerArguments, $rawUserQuery, $pageId, $languageId) |
152
|
|
|
{ |
153
|
|
|
$controllerArguments['page'] = 0; |
154
|
|
|
$controllerArguments['q'] = $rawUserQuery; |
155
|
|
|
$argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace(); |
156
|
|
|
|
157
|
|
|
return $this->getRequest(['q' => $rawUserQuery, $argumentsNamespace => $controllerArguments], $pageId, $languageId); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Creates an instance of the SearchRequest. |
162
|
|
|
* |
163
|
|
|
* @param array $requestArguments |
164
|
|
|
* @param int $pageId |
165
|
|
|
* @param int $languageId |
166
|
|
|
* @return SearchRequest |
167
|
|
|
*/ |
168
|
33 |
|
protected function getRequest(array $requestArguments = [], $pageId = 0, $languageId = 0) |
169
|
|
|
{ |
170
|
33 |
|
$searchRequest = GeneralUtility::makeInstance(SearchRequest::class, $requestArguments, $pageId, $languageId, $this->typoScriptConfiguration); |
171
|
33 |
|
return $searchRequest; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* This methods sets the page argument to an expected positive integer value in the arguments array. |
176
|
|
|
* |
177
|
|
|
* @param array $arguments |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
32 |
|
protected function adjustPageArgumentToPositiveInteger(array $arguments) |
181
|
|
|
{ |
182
|
32 |
|
$page = isset($arguments['page']) ? intval($arguments['page']) : 0; |
183
|
32 |
|
$arguments['page'] = max($page, 0); |
184
|
|
|
|
185
|
32 |
|
return $arguments; |
186
|
|
|
} |
187
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: