Passed
Push — master ( 4e40a2...cc3f84 )
by Timo
24:09
created

SearchController::frequentlySearchedAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0098

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
ccs 11
cts 14
cp 0.7856
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0098
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
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
110 34
                'resultSet' => $searchResultSet,
111 34
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
112
            ];
113
114 34
            $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
115
116 34
            $this->view->assignMultiple($values);
117 1
        } catch (SolrUnavailableException $e) {
118 1
            $this->handleSolrUnavailable();
119
        }
120 34
    }
121
122
    /**
123
     * Form
124
     */
125 2
    public function formAction()
126
    {
127
128
        $values = [
129 2
            'search' => $this->searchService->getSearch(),
130 2
            'additionalFilters' => $this->searchService->getAdditionalFilters(),
131 2
            'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
132
        ];
133 2
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
134
135 2
        $this->view->assignMultiple($values);
136 2
    }
137
138
    /**
139
     * Frequently Searched
140
     */
141 1
    public function frequentlySearchedAction()
142
    {
143
        /** @var  $searchResultSet SearchResultSet */
144 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
145
146 1
        $pageId = $this->typoScriptFrontendController->getRequestedId();
147 1
        $languageId = $this->typoScriptFrontendController->sys_language_uid;
148 1
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
149 1
        $searchResultSet->setUsedSearchRequest($searchRequest);
150
151 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
152
153
        $values = [
154 1
            'additionalFilters' => $this->searchService->getAdditionalFilters(),
155 1
            'resultSet' => $searchResultSet
156
        ];
157 1
        $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);
158
159 1
        $this->view->assignMultiple($values);
160 1
    }
161
162
    /**
163
     * This action allows to render a detailView with data from solr.
164
     *
165
     * @param string $documentId
166
     */
167 2
    public function detailAction($documentId = '')
168
    {
169
        try {
170 2
            $document = $this->searchService->getDocumentById($documentId);
171 1
            $this->view->assign('document', $document);
172 1
        } catch (SolrUnavailableException $e) {
173 1
            $this->handleSolrUnavailable();
174
        }
175 1
    }
176
177
    /**
178
     * Rendered when no search is available.
179
     * @return string
180
     */
181 1
    public function solrNotAvailableAction()
182
    {
183 1
        if ($this->response instanceof Response) {
184 1
            $this->response->setStatus(503);
185
        }
186 1
    }
187
188
    /**
189
     * Called when the solr server is unavailable.
190
     *
191
     * @return void
192
     */
193 2
    protected function handleSolrUnavailable()
194
    {
195 2
        parent::handleSolrUnavailable();
196 2
        $this->forward('solrNotAvailable');
197
    }
198
}
199