Completed
Push — master ( 7da5f6...0e708e )
by Timo
44:47 queued 41:16
created

SearchController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 62
c 1
b 0
f 0
dl 0
loc 177
ccs 72
cts 102
cp 0.7059
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A solrNotAvailableAction() 0 4 2
A handleSolrUnavailable() 0 4 1
A formAction() 0 11 1
A frequentlySearchedAction() 0 19 1
A initializeAction() 0 4 1
A getCustomTemplateFromConfiguration() 0 5 1
A mapGlobalQueryStringWhenEnabled() 0 8 3
A getAdditionalFilters() 0 3 1
A initializeView() 0 12 4
A detailAction() 0 7 2
A resultsAction() 0 26 2
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 ApacheSolrForTypo3\Solr\Util;
21
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
22
use TYPO3\CMS\Extbase\Mvc\Web\Response;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Fluid\View\TemplateView;
25
26
/**
27
 * Class SearchController
28
 *
29
 * @author Frans Saris <[email protected]>
30
 * @author Timo Hund <[email protected]>
31
 * @package ApacheSolrForTypo3\Solr\Controller
32
 */
33
class SearchController extends AbstractBaseController
34
{
35
    /**
36
     * @var TemplateView
37
     */
38
    protected $view;
39
40
    /**
41
     * Provide search query in extbase arguments.
42
     */
43 42
    protected function initializeAction()
44
    {
45 42
        parent::initializeAction();
46 42
        $this->mapGlobalQueryStringWhenEnabled();
47 42
    }
48
49
    /**
50
     * @return void
51
     */
52 42
    protected function mapGlobalQueryStringWhenEnabled()
53
    {
54 42
        $query = GeneralUtility::_GET('q');
55
56 42
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
57
58 42
        if ($useGlobalQueryString) {
59 30
            $this->request->setArgument('q', $query);
60
        }
61 42
    }
62
63
    /**
64
     * @param ViewInterface $view
65
     */
66 42
    public function initializeView(ViewInterface $view)
67
    {
68 42
        if($view instanceof TemplateView) {
69 42
            $customTemplate = $this->getCustomTemplateFromConfiguration();
70 42
            if($customTemplate === '') {
71 41
                return;
72
            }
73
74 1
            if(strpos($customTemplate, 'EXT:') !== false) {
75 1
                $view->setTemplatePathAndFilename($customTemplate);
76
            } else {
77
                $view->setTemplate($customTemplate);
78
            }
79
        }
80 1
    }
81
82
    /**
83
     * @return string
84
     */
85 42
    protected function getCustomTemplateFromConfiguration()
86
    {
87 42
        $templateKey = str_replace('Action', '', $this->actionMethodName);
88 42
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
89 42
        return $customTemplate;
90
    }
91
92
    /**
93
     * Results
94
     */
95 36
    public function resultsAction()
96
    {
97
        try {
98 36
            $arguments = (array)$this->request->getArguments();
99 36
            $pageId = $this->typoScriptFrontendController->getRequestedId();
100 36
            $languageId = Util::getLanguageUid();
101 36
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
102
103 36
            $searchResultSet = $this->searchService->search($searchRequest);
104
105
            // we pass the search result set to the controller context, to have the possibility
106
            // to access it without passing it from partial to partial
107 35
            $this->controllerContext->setSearchResultSet($searchResultSet);
108
109
            $values = [
110 35
                'additionalFilters' => $this->getAdditionalFilters(),
111 35
                'resultSet' => $searchResultSet,
112 35
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
113 35
                'arguments' => $arguments
114
            ];
115
116 35
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
117
118 35
            $this->view->assignMultiple($values);
119 1
        } catch (SolrUnavailableException $e) {
120 1
            $this->handleSolrUnavailable();
121
        }
122 35
    }
123
124
    /**
125
     * Form
126
     */
127 2
    public function formAction()
128
    {
129
130
        $values = [
131 2
            'search' => $this->searchService->getSearch(),
132 2
            'additionalFilters' => $this->getAdditionalFilters(),
133 2
            'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
134
        ];
135 2
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
136
137 2
        $this->view->assignMultiple($values);
138 2
    }
139
140
    /**
141
     * Frequently Searched
142
     */
143 1
    public function frequentlySearchedAction()
144
    {
145
        /** @var  $searchResultSet SearchResultSet */
146 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
147
148 1
        $pageId = $this->typoScriptFrontendController->getRequestedId();
149 1
        $languageId = Util::getLanguageUid();
150 1
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
151 1
        $searchResultSet->setUsedSearchRequest($searchRequest);
152
153 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
154
155
        $values = [
156 1
            'additionalFilters' => $this->getAdditionalFilters(),
157 1
            'resultSet' => $searchResultSet
158
        ];
159 1
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
160
161 1
        $this->view->assignMultiple($values);
162 1
    }
163
164
    /**
165
     * This action allows to render a detailView with data from solr.
166
     *
167
     * @param string $documentId
168
     */
169 2
    public function detailAction($documentId = '')
170
    {
171
        try {
172 2
            $document = $this->searchService->getDocumentById($documentId);
173 1
            $this->view->assign('document', $document);
174 1
        } catch (SolrUnavailableException $e) {
175 1
            $this->handleSolrUnavailable();
176
        }
177 1
    }
178
179
    /**
180
     * Rendered when no search is available.
181
     * @return string
182
     */
183 1
    public function solrNotAvailableAction()
184
    {
185 1
        if ($this->response instanceof Response) {
186 1
            $this->response->setStatus(503);
187
        }
188 1
    }
189
190
    /**
191
     * Called when the solr server is unavailable.
192
     *
193
     * @return void
194
     */
195 2
    protected function handleSolrUnavailable()
196
    {
197 2
        parent::handleSolrUnavailable();
198 2
        $this->forward('solrNotAvailable');
199
    }
200
201
    /**
202
     * This method can be overwritten to add additionalFilters for the autosuggest.
203
     * By default the suggest controller will apply the configured filters from the typoscript configuration.
204
     *
205
     * @return array
206
     */
207 38
    protected function getAdditionalFilters()
208
    {
209 38
        return [];
210
    }
211
}
212