Issues (202)

Classes/Controller/SuggestController.php (1 issue)

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