Failed Conditions
Push — master ( a052b2...ee9c7d )
by Rafael
18:58
created

SearchController::resultsAction()   B

Complexity

Conditions 2
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 21
cp 0.7143
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 7
nop 0
crap 2.0932
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 36
    protected function initializeAction()
43
    {
44 36
        parent::initializeAction();
45 36
        $this->mapGlobalQueryStringWhenEnabled();
46 36
    }
47
48
    /**
49
     * @return void
50
     */
51 36
    protected function mapGlobalQueryStringWhenEnabled()
52
    {
53 36
        $query = GeneralUtility::_GET('q');
54
55 36
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
56
57 36
        if ($useGlobalQueryString) {
58 24
            $this->request->setArgument('q', $query);
59
        }
60 36
    }
61
62
    /**
63
     * @param ViewInterface $view
64
     */
65 36
    public function initializeView(ViewInterface $view)
66
    {
67 36
        if($view instanceof TemplateView) {
68 36
            $customTemplate = $this->getCustomTemplateFromConfiguration();
69 36
            if($customTemplate === '') {
70 35
                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 36
    protected function getCustomTemplateFromConfiguration()
85
    {
86 36
        $templateKey = str_replace('Action', '', $this->actionMethodName);
87 36
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
88 36
        return $customTemplate;
89
    }
90
91
    /**
92
     * Results
93
     */
94 30
    public function resultsAction()
95
    {
96
        try {
97 30
            $arguments = (array)$this->request->getArguments();
98 30
            $pageId = $this->typoScriptFrontendController->getRequestedId();
99 30
            $languageId = $this->typoScriptFrontendController->sys_language_uid;
100 30
            $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
101
102 30
            $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 29
            $this->controllerContext->setSearchResultSet($searchResultSet);
107
108 29
            $this->view->assignMultiple(
109
                [
110 29
                    'hasSearched' => $this->searchService->getHasSearched(),
111 29
                    'additionalFilters' => $this->searchService->getAdditionalFilters(),
112 29
                    'resultSet' => $searchResultSet,
113 29
                    'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
114
                ]
115
            );
116 1
        } catch (SolrUnavailableException $e) {
117 1
            $this->handleSolrUnavailable();
118
        }
119 29
    }
120
121
    /**
122
     * Form
123
     */
124 2
    public function formAction()
125
    {
126 2
        $this->view->assignMultiple(
127
            [
128 2
                'search' => $this->searchService->getSearch(),
129 2
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
130 2
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
131
            ]
132
        );
133 2
    }
134
135
    /**
136
     * Frequently Searched
137
     */
138 1
    public function frequentlySearchedAction()
139
    {
140
        /** @var  $searchResultSet SearchResultSet */
141 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
142
143 1
        $pageId = $this->typoScriptFrontendController->getRequestedId();
144 1
        $languageId = $this->typoScriptFrontendController->sys_language_uid;
145 1
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
146 1
        $searchResultSet->setUsedSearchRequest($searchRequest);
147
148 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
149 1
        $this->view->assignMultiple(
150
            [
151 1
                'hasSearched' => $this->searchService->getHasSearched(),
152 1
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
153 1
                'resultSet' => $searchResultSet
154
            ]
155
        );
156 1
    }
157
158
    /**
159
     * This action allows to render a detailView with data from solr.
160
     *
161
     * @param string $documentId
162
     */
163 2
    public function detailAction($documentId = '')
164
    {
165
        try {
166 2
            $document = $this->searchService->getDocumentById($documentId);
167 1
            $this->view->assign('document', $document);
168 1
        } catch (SolrUnavailableException $e) {
169 1
            $this->handleSolrUnavailable();
170
        }
171 1
    }
172
173
    /**
174
     * Rendered when no search is available.
175
     * @return string
176
     */
177 1
    public function solrNotAvailableAction()
178
    {
179 1
        if ($this->response instanceof Response) {
180 1
            $this->response->setStatus(503);
181
        }
182 1
    }
183
184
    /**
185
     * Called when the solr server is unavailable.
186
     *
187
     * @return void
188
     */
189 2
    protected function handleSolrUnavailable()
190
    {
191 2
        parent::handleSolrUnavailable();
192 2
        $this->forward('solrNotAvailable');
193
    }
194
}
195