Passed
Push — master ( e08192...f74695 )
by Timo
21:08
created

SearchController::initializeAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.0156
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 41
    protected function initializeAction()
43
    {
44 41
        parent::initializeAction();
45 41
        $this->mapGlobalQueryStringWhenEnabled();
46 41
    }
47
48
    /**
49
     * @return void
50
     */
51 41
    protected function mapGlobalQueryStringWhenEnabled()
52
    {
53 41
        $query = GeneralUtility::_GET('q');
54
55 41
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
56
57 41
        if ($useGlobalQueryString) {
58 29
            $this->request->setArgument('q', $query);
59
        }
60 41
    }
61
62
    /**
63
     * @param ViewInterface $view
64
     */
65 41
    public function initializeView(ViewInterface $view)
66
    {
67 41
        if($view instanceof TemplateView) {
68 41
            $customTemplate = $this->getCustomTemplateFromConfiguration();
69 41
            if($customTemplate === '') {
70 40
                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 41
    protected function getCustomTemplateFromConfiguration()
85
    {
86 41
        $templateKey = str_replace('Action', '', $this->actionMethodName);
87 41
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
88 41
        return $customTemplate;
89
    }
90
91
    /**
92
     * Results
93
     */
94 35
    public function resultsAction()
95
    {
96
        try {
97 35
            $arguments = (array)$this->request->getArguments();
98 35
            $pageId = $this->typoScriptFrontendController->getRequestedId();
99 35
            $languageId = $this->typoScriptFrontendController->sys_language_uid;
100 35
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
101
102 35
            $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 34
            $this->controllerContext->setSearchResultSet($searchResultSet);
107
108
            $values = [
109 34
                'hasSearched' => $this->searchService->getHasSearched(),
110 34
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
111 34
                'resultSet' => $searchResultSet,
112 34
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
113
            ];
114
115 34
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
116
117 34
            $this->view->assignMultiple($values);
118 1
        } catch (SolrUnavailableException $e) {
119 1
            $this->handleSolrUnavailable();
120
        }
121 34
    }
122
123
    /**
124
     * Form
125
     */
126 2
    public function formAction()
127
    {
128
129
        $values = [
130 2
            'search' => $this->searchService->getSearch(),
131 2
            'additionalFilters' => $this->searchService->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;
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
            'hasSearched' => $this->searchService->getHasSearched(),
156 1
            'additionalFilters' => $this->searchService->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