1 | <?php |
||
43 | class ResultsCommand implements PluginCommand |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * @var Search |
||
48 | */ |
||
49 | protected $search; |
||
50 | |||
51 | /** |
||
52 | * Parent plugin |
||
53 | * |
||
54 | * @var Results |
||
55 | */ |
||
56 | protected $parentPlugin; |
||
57 | |||
58 | /** |
||
59 | * Configuration |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $configuration; |
||
64 | |||
65 | /** |
||
66 | * Constructor. |
||
67 | * |
||
68 | * @param CommandPluginBase $parentPlugin Parent plugin object. |
||
69 | */ |
||
70 | 18 | public function __construct(CommandPluginBase $parentPlugin) |
|
71 | { |
||
72 | 18 | $this->parentPlugin = $parentPlugin; |
|
|
|||
73 | 18 | $this->configuration = $parentPlugin->typoScriptConfiguration; |
|
74 | 18 | $this->search = $parentPlugin->getSearchResultSetService()->getSearch(); |
|
75 | 18 | } |
|
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | 18 | public function execute() |
|
81 | { |
||
82 | 18 | $numberOfResults = $this->search->getNumberOfResults(); |
|
83 | |||
84 | 18 | $query = $this->search->getQuery(); |
|
85 | 18 | $rawQueryTerms = $query->getKeywordsRaw(); |
|
86 | 18 | $queryTerms = $query->getKeywordsCleaned(); |
|
87 | |||
88 | 18 | $searchedFor = strtr( |
|
89 | 18 | $this->parentPlugin->pi_getLL('results_searched_for'), |
|
90 | 18 | ['@searchWord' => '<span class="tx-solr-search-word">' . $queryTerms . '</span>'] |
|
91 | 18 | ); |
|
92 | |||
93 | 18 | $foundResultsInfo = strtr( |
|
94 | 18 | $this->parentPlugin->pi_getLL('results_found'), |
|
95 | [ |
||
96 | 18 | '@resultsTotal' => $this->search->getNumberOfResults(), |
|
97 | 18 | '@resultsTime' => $this->search->getQueryTime() |
|
98 | 18 | ] |
|
99 | 18 | ); |
|
100 | |||
101 | return [ |
||
102 | 18 | 'searched_for' => $searchedFor, |
|
103 | 18 | 'query' => $queryTerms, |
|
104 | 18 | 'query_urlencoded' => rawurlencode($rawQueryTerms), |
|
105 | 18 | 'query_raw' => $rawQueryTerms, |
|
106 | 18 | 'found' => $foundResultsInfo, |
|
107 | 18 | 'range' => $this->getPageBrowserRange(), |
|
108 | 18 | 'count' => $this->search->getNumberOfResults(), |
|
109 | 18 | 'offset' => ($this->search->getResultOffset() + 1), |
|
110 | 18 | 'query_time' => $this->search->getQueryTime(), |
|
111 | 18 | 'pagebrowser' => $this->getPageBrowser($numberOfResults), |
|
112 | 18 | 'filtered' => $this->isFiltered(), |
|
113 | 18 | 'filtered_by_user' => $this->isFilteredByUser(), |
|
114 | /* construction of the array key: |
||
115 | * loop_ : tells the plugin that the content of that field should be processed in a loop |
||
116 | * result_documents : is the loop name as in the template |
||
117 | * result_document : is the marker name for the single items in the loop |
||
118 | */ |
||
119 | 18 | 'loop_result_documents|result_document' => $this->getResultDocuments() |
|
120 | 18 | ]; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return \Apache_Solr_Document[] |
||
125 | */ |
||
126 | 18 | protected function getResultDocuments() |
|
127 | { |
||
128 | 18 | $responseDocuments = $this->search->getResultDocumentsEscaped(); |
|
129 | 18 | $resultDocuments = []; |
|
130 | |||
131 | 18 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyResultSet'])) { |
|
132 | 18 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyResultSet'] as $classReference) { |
|
133 | 18 | $resultSetModifier = GeneralUtility::getUserObj($classReference); |
|
134 | |||
135 | 18 | if ($resultSetModifier instanceof ResultSetModifier) { |
|
136 | 18 | $responseDocuments = $resultSetModifier->modifyResultSet($this, |
|
137 | 18 | $responseDocuments); |
|
138 | 18 | } else { |
|
139 | throw new \UnexpectedValueException( |
||
140 | get_class($resultSetModifier) . ' must implement interface' . ResultSetModifier::class, |
||
141 | 1310386927 |
||
142 | ); |
||
143 | } |
||
144 | 18 | } |
|
145 | 18 | } |
|
146 | |||
147 | 18 | foreach ($responseDocuments as $resultDocument) { |
|
148 | /** @var $resultDocument SearchResult */ |
||
149 | 18 | $temporaryResultDocument = $this->processDocumentFieldsToArray($resultDocument); |
|
150 | |||
151 | 18 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyResultDocument'])) { |
|
152 | 18 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyResultDocument'] as $classReference) { |
|
153 | 18 | $resultDocumentModifier = GeneralUtility::getUserObj($classReference); |
|
154 | |||
155 | 18 | if ($resultDocumentModifier instanceof ResultDocumentModifier) { |
|
156 | 18 | $temporaryResultDocument = $resultDocumentModifier->modifyResultDocument($this, |
|
157 | 18 | $temporaryResultDocument); |
|
158 | 18 | } else { |
|
159 | throw new \UnexpectedValueException( |
||
160 | get_class($resultDocumentModifier) . ' must implement interface ' . ResultDocumentModifier::class, |
||
161 | 1310386725 |
||
162 | ); |
||
163 | } |
||
164 | 18 | } |
|
165 | 18 | } |
|
166 | |||
167 | 18 | $renderedResultDocument = $this->renderDocumentFields($temporaryResultDocument); |
|
168 | 18 | $renderedResultDocument = $this->renderVariants($resultDocument, $renderedResultDocument); |
|
169 | |||
170 | 18 | $resultDocuments[] = $renderedResultDocument; |
|
171 | 18 | unset($temporaryResultDocument); |
|
172 | 18 | } |
|
173 | |||
174 | 18 | return $resultDocuments; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Renders the collapsedDocuments/variants of a document and adds them into the virtual field "variants". |
||
179 | * |
||
180 | * @param SearchResult $resultDocument |
||
181 | * @param array $renderedResultDocument |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 18 | protected function renderVariants(SearchResult $resultDocument, $renderedResultDocument) |
|
199 | |||
200 | /** |
||
201 | * takes a search result document and processes its fields according to the |
||
202 | * instructions configured in TS. Currently available instructions are |
||
203 | * * timestamp - converts a date field into a unix timestamp |
||
204 | * * serialize - uses serialize() to encode multivalue fields which then can be put out using the MULTIVALUE view helper |
||
205 | * * skip - skips the whole field so that it is not available in the result, useful for the spell field f.e. |
||
206 | * The default is to do nothing and just add the document's field to the |
||
207 | * resulting array. |
||
208 | * |
||
209 | * @param \Apache_Solr_Document $document the Apache_Solr_Document result document |
||
210 | * @return array An array with field values processed like defined in TS |
||
211 | */ |
||
212 | 18 | protected function processDocumentFieldsToArray( |
|
250 | |||
251 | /** |
||
252 | * @param array $document |
||
253 | * @return array |
||
254 | */ |
||
255 | 18 | protected function renderDocumentFields(array $document) |
|
274 | |||
275 | /** |
||
276 | * @param $numberOfResults |
||
277 | * @return string |
||
278 | */ |
||
279 | 18 | protected function getPageBrowser($numberOfResults) |
|
280 | { |
||
281 | 18 | $pageBrowserMarkup = ''; |
|
282 | 18 | $solrPageBrowserConfiguration = $this->configuration->getSearchResultsPageBrowserConfiguration(); |
|
283 | |||
284 | 18 | if ($solrPageBrowserConfiguration['enabled']) { |
|
285 | 18 | $resultsPerPage = $this->parentPlugin->getSearchResultSetService()->getLastResultSet()->getResultsPerPage(); |
|
286 | 18 | $numberOfPages = intval($numberOfResults / $resultsPerPage) |
|
287 | 18 | + (($numberOfResults % $resultsPerPage) == 0 ? 0 : 1); |
|
288 | |||
289 | 18 | $solrGetParameters = GeneralUtility::_GET('tx_solr'); |
|
290 | 18 | if (!is_array($solrGetParameters)) { |
|
291 | 17 | $solrGetParameters = []; |
|
292 | 17 | } |
|
293 | 18 | $currentPage = $solrGetParameters['page']; |
|
294 | 18 | unset($solrGetParameters['page']); |
|
295 | |||
296 | 18 | $pageBrowserConfiguration = array_merge( |
|
297 | 18 | $solrPageBrowserConfiguration, |
|
298 | [ |
||
299 | 18 | 'numberOfPages' => $numberOfPages, |
|
300 | 18 | 'currentPage' => $currentPage, |
|
301 | 18 | 'extraQueryString' => GeneralUtility::implodeArrayForUrl('tx_solr', |
|
302 | 18 | $solrGetParameters), |
|
303 | 18 | 'templateFile' => $this->configuration->getTemplateByFileKey('pagebrowser') |
|
304 | 18 | ] |
|
305 | 18 | ); |
|
306 | |||
307 | 18 | $pageBrowser = GeneralUtility::makeInstance( |
|
308 | 18 | PageBrowser::class, |
|
309 | 18 | $pageBrowserConfiguration, |
|
310 | 18 | $this->getPageBrowserLabels() |
|
311 | 18 | ); |
|
312 | |||
313 | 18 | $pageBrowserMarkup = $pageBrowser->render(); |
|
314 | 18 | } |
|
315 | |||
316 | 18 | return $pageBrowserMarkup; |
|
317 | 18 | } |
|
318 | |||
319 | /** |
||
320 | * Gets the labels for us in the page browser |
||
321 | * |
||
322 | * @return array page browser labels |
||
323 | */ |
||
324 | 18 | protected function getPageBrowserLabels() |
|
340 | |||
341 | /** |
||
342 | * @return string |
||
343 | */ |
||
344 | 18 | protected function getPageBrowserRange() |
|
345 | { |
||
346 | 18 | $resultsFrom = $this->search->getResponseBody()->start + 1; |
|
347 | 18 | $resultsTo = $resultsFrom + count($this->search->getResultDocumentsEscaped()) - 1; |
|
348 | 18 | $resultsTotal = $this->search->getNumberOfResults(); |
|
349 | |||
350 | 18 | $label = strtr( |
|
351 | 18 | $this->parentPlugin->pi_getLL('results_range'), |
|
352 | [ |
||
353 | 18 | '@resultsFrom' => $resultsFrom, |
|
354 | 18 | '@resultsTo' => $resultsTo, |
|
355 | '@resultsTotal' => $resultsTotal |
||
356 | 18 | ] |
|
357 | 18 | ); |
|
358 | |||
359 | 18 | return $label; |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * Gets the parent plugin. |
||
364 | * |
||
365 | * @return Results |
||
366 | */ |
||
367 | 18 | public function getParentPlugin() |
|
371 | |||
372 | /** |
||
373 | * Determines whether filters have been applied to the query or not. |
||
374 | * |
||
375 | * @return string 1 if filters are applied, 0 if not (for use in templates) |
||
376 | */ |
||
377 | 18 | protected function isFiltered() |
|
384 | |||
385 | /** |
||
386 | * Determines whether filters have been applied by the user (facets for |
||
387 | * example) to the query or not. |
||
388 | * |
||
389 | * @return string 1 if filters are applied, 0 if not (for use in templates) |
||
390 | */ |
||
391 | 18 | protected function isFilteredByUser() |
|
402 | } |
||
403 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.