Completed
Push — master ( e7445d...6e98ac )
by Timo
307:07 queued 304:17
created

SearchController::formAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.037

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 6
cts 9
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.037
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
18
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
19
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
20
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
21
use TYPO3\CMS\Extbase\Mvc\Web\Response;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Fluid\View\TemplateView;
24
25
/**
26
 * Class SearchController
27
 *
28
 * @author Frans Saris <[email protected]>
29
 * @author Timo Hund <[email protected]>
30
 * @package ApacheSolrForTypo3\Solr\Controller
31
 */
32
class SearchController extends AbstractBaseController
33
{
34
    /**
35
     * @var TemplateView
36
     */
37
    protected $view;
38
39
    /**
40
     * Provide search query in extbase arguments.
41
     */
42 42
    protected function initializeAction()
43
    {
44 42
        parent::initializeAction();
45 42
        $this->mapGlobalQueryStringWhenEnabled();
46 42
    }
47
48
    /**
49
     * @return void
50
     */
51 42
    protected function mapGlobalQueryStringWhenEnabled()
52
    {
53 42
        $query = GeneralUtility::_GET('q');
54
55 42
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
56
57 42
        if ($useGlobalQueryString) {
58 30
            $this->request->setArgument('q', $query);
59
        }
60 42
    }
61
62
    /**
63
     * @param ViewInterface $view
64
     */
65 42
    public function initializeView(ViewInterface $view)
66
    {
67 42
        if($view instanceof TemplateView) {
68 42
            $customTemplate = $this->getCustomTemplateFromConfiguration();
69 42
            if($customTemplate === '') {
70 41
                return;
71
            }
72
73 1
            if(strpos($customTemplate, 'EXT:') !== false) {
74 1
                $view->setTemplatePathAndFilename($customTemplate);
75
            } else {
76
                $view->setTemplate($customTemplate);
77
            }
78
        }
79 1
    }
80
81
    /**
82
     * @return string
83
     */
84 42
    protected function getCustomTemplateFromConfiguration()
85
    {
86 42
        $templateKey = str_replace('Action', '', $this->actionMethodName);
87 42
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
88 42
        return $customTemplate;
89
    }
90
91
    /**
92
     * Results
93
     */
94 36
    public function resultsAction()
95
    {
96
        try {
97 36
            $arguments = (array)$this->request->getArguments();
98 36
            $pageId = $this->typoScriptFrontendController->getRequestedId();
99 36
            $languageId = $this->typoScriptFrontendController->sys_language_uid;
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

99
            $languageId = /** @scrutinizer ignore-deprecated */ $this->typoScriptFrontendController->sys_language_uid;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
100 36
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
101
102 36
            $searchResultSet = $this->searchService->search($searchRequest);
103
104
            // we pass the search result set to the controller context, to have the possibility
105
            // to access it without passing it from partial to partial
106 35
            $this->controllerContext->setSearchResultSet($searchResultSet);
107
108
            $values = [
109 35
                'additionalFilters' => $this->getAdditionalFilters(),
110 35
                'resultSet' => $searchResultSet,
111 35
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
112 35
                'arguments' => $arguments
113
            ];
114
115 35
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
116
117 35
            $this->view->assignMultiple($values);
118 1
        } catch (SolrUnavailableException $e) {
119 1
            $this->handleSolrUnavailable();
120
        }
121 35
    }
122
123
    /**
124
     * Form
125
     */
126 2
    public function formAction()
127
    {
128
129
        $values = [
130 2
            'search' => $this->searchService->getSearch(),
131 2
            'additionalFilters' => $this->getAdditionalFilters(),
132 2
            'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
133
        ];
134 2
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
135
136 2
        $this->view->assignMultiple($values);
137 2
    }
138
139
    /**
140
     * Frequently Searched
141
     */
142 1
    public function frequentlySearchedAction()
143
    {
144
        /** @var  $searchResultSet SearchResultSet */
145 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
146
147 1
        $pageId = $this->typoScriptFrontendController->getRequestedId();
148 1
        $languageId = $this->typoScriptFrontendController->sys_language_uid;
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

148
        $languageId = /** @scrutinizer ignore-deprecated */ $this->typoScriptFrontendController->sys_language_uid;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
149 1
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
150 1
        $searchResultSet->setUsedSearchRequest($searchRequest);
151
152 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
153
154
        $values = [
155 1
            'additionalFilters' => $this->getAdditionalFilters(),
156 1
            'resultSet' => $searchResultSet
157
        ];
158 1
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
159
160 1
        $this->view->assignMultiple($values);
161 1
    }
162
163
    /**
164
     * This action allows to render a detailView with data from solr.
165
     *
166
     * @param string $documentId
167
     */
168 2
    public function detailAction($documentId = '')
169
    {
170
        try {
171 2
            $document = $this->searchService->getDocumentById($documentId);
172 1
            $this->view->assign('document', $document);
173 1
        } catch (SolrUnavailableException $e) {
174 1
            $this->handleSolrUnavailable();
175
        }
176 1
    }
177
178
    /**
179
     * Rendered when no search is available.
180
     * @return string
181
     */
182 1
    public function solrNotAvailableAction()
183
    {
184 1
        if ($this->response instanceof Response) {
185 1
            $this->response->setStatus(503);
186
        }
187 1
    }
188
189
    /**
190
     * Called when the solr server is unavailable.
191
     *
192
     * @return void
193
     */
194 2
    protected function handleSolrUnavailable()
195
    {
196 2
        parent::handleSolrUnavailable();
197 2
        $this->forward('solrNotAvailable');
198
    }
199
200
    /**
201
     * This method can be overwritten to add additionalFilters for the autosuggest.
202
     * By default the suggest controller will apply the configured filters from the typoscript configuration.
203
     *
204
     * @return array
205
     */
206 38
    protected function getAdditionalFilters()
207
    {
208 38
        return [];
209
    }
210
}
211