1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Controller; |
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\Domain\Search\Suggest\SuggestService; |
19
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException; |
20
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
21
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class SuggestController |
25
|
|
|
* |
26
|
|
|
* @author Frans Saris <[email protected]> |
27
|
|
|
* @author Timo Hund <[email protected]> |
28
|
|
|
* @copyright (c) 2017 Timo Hund <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SuggestController extends AbstractBaseController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* This method creates a suggest json response that can be used in a suggest layer. |
34
|
|
|
* |
35
|
|
|
* @param string $queryString |
36
|
|
|
* @param string $callback |
37
|
|
|
* @param array $additionalFilters |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function suggestAction($queryString, $callback = null, $additionalFilters = []) |
41
|
|
|
{ |
42
|
|
|
// Get suggestions |
43
|
|
|
$rawQuery = htmlspecialchars(mb_strtolower(trim($queryString))); |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
/** @var SuggestService $suggestService */ |
47
|
|
|
$suggestService = GeneralUtility::makeInstance( |
48
|
|
|
SuggestService::class, |
49
|
|
|
/** @scrutinizer ignore-type */ $this->typoScriptFrontendController, |
50
|
|
|
/** @scrutinizer ignore-type */ $this->searchService, |
51
|
|
|
/** @scrutinizer ignore-type */ $this->typoScriptConfiguration); |
52
|
|
|
|
53
|
|
|
$additionalFilters = is_array($additionalFilters) ? array_map("htmlspecialchars", $additionalFilters) : []; |
|
|
|
|
54
|
|
|
$pageId = $this->typoScriptFrontendController->getRequestedId(); |
55
|
|
|
$languageId = Util::getLanguageUid(); |
56
|
|
|
$arguments = (array)$this->request->getArguments(); |
57
|
|
|
|
58
|
|
|
$searchRequest = $this->getSearchRequestBuilder()->buildForSuggest($arguments, $rawQuery, $pageId, $languageId); |
59
|
|
|
$result = $suggestService->getSuggestions($searchRequest, $additionalFilters); |
60
|
|
|
} catch (SolrUnavailableException $e) { |
61
|
|
|
$this->handleSolrUnavailable(); |
62
|
|
|
$result = ['status' => false]; |
63
|
|
|
} |
64
|
|
|
if ($callback) { |
65
|
|
|
return htmlspecialchars($callback) . '(' . json_encode($result, JSON_UNESCAPED_SLASHES) . ')'; |
66
|
|
|
} |
67
|
|
|
else { |
68
|
|
|
return json_encode($result, JSON_UNESCAPED_SLASHES); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|