Passed
Push — release-11.5.x ( 39fc07...8ccd81 )
by Markus
34:52 queued 29:33
created

Classes/Controller/SearchController.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Controller;
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\Pagination\ResultsPagination;
20
use ApacheSolrForTypo3\Solr\Pagination\ResultsPaginator;
21
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
22
use ApacheSolrForTypo3\Solr\Util;
23
use Psr\Http\Message\ResponseInterface;
24
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Http\ForwardResponse;
27
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
28
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
29
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
30
use TYPO3\CMS\Fluid\View\TemplateView;
31
use TYPO3Fluid\Fluid\View\ViewInterface;
32
33
/**
34
 * Class SearchController
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 */
39
class SearchController extends AbstractBaseController
40
{
41
    /**
42
     * @var TemplateView
43
     */
44
    protected $view;
45
46
    /**
47
     * Provide search query in extbase arguments.
48
     */
49 35
    protected function initializeAction()
50
    {
51 35
        parent::initializeAction();
52 35
        $this->mapGlobalQueryStringWhenEnabled();
53
    }
54
55 35
    protected function mapGlobalQueryStringWhenEnabled()
56
    {
57 35
        $query = GeneralUtility::_GET('q');
58
59 35
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
60
61 35
        if ($useGlobalQueryString) {
62 1
            $this->request->setArgument('q', $query);
63
        }
64
    }
65
66
    /**
67
     * @param ViewInterface $view
68
     */
69 35
    public function initializeView($view)
70
    {
71 35
        if ($view instanceof TemplateView) {
72 35
            $customTemplate = $this->getCustomTemplateFromConfiguration();
73 35
            if ($customTemplate === '') {
74 34
                return;
75
            }
76
77 1
            if (strpos($customTemplate, 'EXT:') !== false) {
78 1
                $view->setTemplatePathAndFilename($customTemplate);
79
            } else {
80
                $view->setTemplate($customTemplate);
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88 35
    protected function getCustomTemplateFromConfiguration(): string
89
    {
90 35
        $templateKey = str_replace('Action', '', $this->actionMethodName);
91 35
        return $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
92
    }
93
94
    /**
95
     * Results
96
     * @return ResponseInterface
97
     * @throws AspectNotFoundException
98
     * @throws NoSuchArgumentException
99
     * @throws InvalidSlotException
100
     * @throws InvalidSlotReturnException
101
     */
102 33
    public function resultsAction(): ResponseInterface
103
    {
104
        try {
105 33
            $arguments = $this->request->getArguments();
106 33
            $pageId = $this->typoScriptFrontendController->getRequestedId();
107 33
            $languageId = Util::getLanguageUid();
108
109 33
            $arguments = $this->emitActionSignal(__CLASS__, 'beforeSearch', [$arguments]);
110
111 33
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
112 33
            $searchResultSet = $this->searchService->search($searchRequest);
0 ignored issues
show
The method search() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            /** @scrutinizer ignore-call */ 
113
            $searchResultSet = $this->searchService->search($searchRequest);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
            // we pass the search result set to the controller context, to have the possibility
115
            // to access it without passing it from partial to partial
116 33
            $this->controllerContext->setSearchResultSet($searchResultSet);
117
118 33
            $currentPage = $this->request->hasArgument('page') ? (int)$this->request->getArgument('page') : 1;
119
120
            // prevent currentPage < 1 (i.e for GET request like &tx_solr[page]=0)
121 33
            if ($currentPage < 1) {
122
                $currentPage = 1;
123
            }
124
125 33
            $itemsPerPage = ($searchResultSet->getUsedResultsPerPage() ?: $this->typoScriptConfiguration->getSearchResultsPerPage(10));
126 33
            $paginator = GeneralUtility::makeInstance(ResultsPaginator::class, $searchResultSet, $currentPage, $itemsPerPage);
127 33
            $pagination = GeneralUtility::makeInstance(ResultsPagination::class, $paginator);
128 33
            $pagination->setMaxPageNumbers((int)$this->typoScriptConfiguration->getMaxPaginatorLinks(0));
129
130 33
            $values = [
131 33
                'additionalFilters' => $this->getAdditionalFilters(),
132 33
                'resultSet' => $searchResultSet,
133 33
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
134 33
                'arguments' => $arguments,
135 33
                'pagination' => $pagination,
136 33
                'currentPage' => $currentPage,
137 33
            ];
138
139 33
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
140
141 33
            $this->view->assignMultiple($values);
142
        } catch (SolrUnavailableException $e) {
143
            return $this->handleSolrUnavailable();
144
        }
145 33
        return $this->htmlResponse();
146
    }
147
148
    /**
149
     * Form
150
     */
151 1
    public function formAction(): ResponseInterface
152
    {
153 1
        $values = [
154 1
            'search' => $this->searchService->getSearch(),
155 1
            'additionalFilters' => $this->getAdditionalFilters(),
156 1
            'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
157 1
        ];
158 1
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
159
160 1
        $this->view->assignMultiple($values);
161 1
        return $this->htmlResponse();
162
    }
163
164
    /**
165
     * Frequently Searched
166
     */
167
    public function frequentlySearchedAction(): ResponseInterface
168
    {
169
        /** @var  $searchResultSet SearchResultSet */
170
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
171
172
        $pageId = $this->typoScriptFrontendController->getRequestedId();
173
        $languageId = Util::getLanguageUid();
174
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
175
        $searchResultSet->setUsedSearchRequest($searchRequest);
176
177
        $this->controllerContext->setSearchResultSet($searchResultSet);
178
179
        $values = [
180
            'additionalFilters' => $this->getAdditionalFilters(),
181
            'resultSet' => $searchResultSet,
182
        ];
183
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
184
185
        $this->view->assignMultiple($values);
186
        return $this->htmlResponse();
187
    }
188
189
    /**
190
     * This action allows to render a detailView with data from solr.
191
     *
192
     * @param string $documentId
193
     * @return ResponseInterface
194
     */
195 1
    public function detailAction(string $documentId = ''): ResponseInterface
196
    {
197
        try {
198 1
            $document = $this->searchService->getDocumentById($documentId);
199 1
            $this->view->assign('document', $document);
200
        } catch (SolrUnavailableException $e) {
201
            return $this->handleSolrUnavailable();
202
        }
203 1
        return $this->htmlResponse();
204
    }
205
206
    /**
207
     * Rendered when no search is available.
208
     * @return ResponseInterface
209
     */
210
    public function solrNotAvailableAction(): ResponseInterface
211
    {
212
        return $this->htmlResponse()
213
            ->withStatus(503, self::STATUS_503_MESSAGE);
214
    }
215
216
    /**
217
     * Called when the solr server is unavailable.
218
     */
219
    protected function handleSolrUnavailable(): ResponseInterface
220
    {
221
        parent::logSolrUnavailable();
222
        return new ForwardResponse('solrNotAvailable');
223
    }
224
225
    /**
226
     * This method can be overwritten to add additionalFilters for the autosuggest.
227
     * By default, suggest controller will apply the configured filters from the typoscript configuration.
228
     *
229
     * @return array
230
     */
231 34
    protected function getAdditionalFilters(): array
232
    {
233 34
        return [];
234
    }
235
}
236