Passed
Push — master ( 5adbd6...5901ba )
by Timo
20:55
created

SearchController::frequentlySearchedAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1.029

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 13
cp 0.6923
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1.029
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\SearchRequest;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
20
use TYPO3\CMS\Extbase\Mvc\Web\Response;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Fluid\View\TemplateView;
23
24
/**
25
 * Class SearchController
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\Controller
30
 */
31
class SearchController extends AbstractBaseController
32
{
33
    /**
34
     * @var TemplateView
35
     */
36
    protected $view;
37
38
    /**
39
     * Provide search query in extbase arguments.
40
     */
41 33
    protected function initializeAction()
42
    {
43 33
        parent::initializeAction();
44 33
        $this->mapGlobalQueryStringWhenEnabled();
45 33
    }
46
47
    /**
48
     * @return void
49
     */
50 33
    protected function mapGlobalQueryStringWhenEnabled()
51
    {
52 33
        $query = GeneralUtility::_GET('q');
53
54 33
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
55
56 33
        if ($useGlobalQueryString) {
57 22
            $this->request->setArgument('q', $query);
58
        }
59 33
    }
60
61
    /**
62
     * @param ViewInterface $view
63
     */
64 33
    public function initializeView(ViewInterface $view)
65
    {
66 33
        if($view instanceof TemplateView) {
67 33
            $customTemplate = $this->getCustomTemplateFromConfiguration();
68 33
            if($customTemplate === '') {
69 32
                return;
70
            }
71 1
            $view->setTemplatePathAndFilename($customTemplate);
72
        }
73 1
    }
74
75
    /**
76
     * @return string
77
     */
78 33
    protected function getCustomTemplateFromConfiguration()
79
    {
80 33
        $templateKey = str_replace('Action', '', $this->actionMethodName);
81 33
        $customTemplate = $this->typoScriptConfiguration->getTemplateByFileKey($templateKey);
82 33
        return $customTemplate;
83
    }
84
85
    /**
86
     * Results
87
     */
88 28
    public function resultsAction()
89
    {
90 28
        if (!$this->searchService->getIsSolrAvailable()) {
91
            $this->forward('solrNotAvailable');
92
        }
93
94 28
        $searchRequest = $this->buildSearchRequest();
95 28
        $searchResultSet = $this->searchService->search($searchRequest);
96
97
98
        // we pass the search result set to the controller context, to have the possibility
99
        // to access it without passing it from partial to partial
100 28
        $this->controllerContext->setSearchResultSet($searchResultSet);
101
102 28
        $this->view->assignMultiple(
103
            [
104 28
                'hasSearched' => $this->searchService->getHasSearched(),
105 28
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
106 28
                'resultSet' => $searchResultSet,
107 28
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
108
            ]
109
        );
110 28
    }
111
112
    /**
113
     * @return SearchRequest
114
     */
115 28
    protected function buildSearchRequest()
116
    {
117 28
        $arguments = (array)$this->request->getArguments();
118 28
        $arguments = $this->adjustPageArgumentToPositiveInteger($arguments);
119
120
        /** @var $searchRequest SearchRequest */
121 28
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
122 28
        $searchRequest = $this->getRequest([$argumentsNamespace => $arguments]);
123
124 28
        return $searchRequest;
125
    }
126
127
    /**
128
     * This methods sets the page argument to an expected positive integer value in the arguments array.
129
     *
130
     * @param array $arguments
131
     * @return array
132
     */
133 28
    protected function adjustPageArgumentToPositiveInteger(array $arguments)
134
    {
135 28
        $page = isset($arguments['page']) ? intval($arguments['page']) - 1 : 0;
136 28
        $arguments['page'] = max($page, 0);
137
138 28
        return $arguments;
139
    }
140
141
    /**
142
     * @param array $requestArguments
143
     * @return SearchRequest
144
     */
145 29
    private function getRequest(array $requestArguments = [])
146
    {
147 29
        $searchRequest = GeneralUtility::makeInstance(
148 29
            SearchRequest::class,
149
            $requestArguments,
150 29
            $this->typoScriptFrontendController->getRequestedId(),
151 29
            $this->typoScriptFrontendController->sys_language_uid,
152 29
            $this->typoScriptConfiguration);
153 29
        return $searchRequest;
154
    }
155
156
    /**
157
     * Form
158
     */
159 2
    public function formAction()
160
    {
161 2
        $this->view->assignMultiple(
162
            [
163 2
                'search' => $this->searchService->getSearch(),
164 2
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
165 2
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
166
            ]
167
        );
168 2
    }
169
170
    /**
171
     * Frequently Searched
172
     */
173 1
    public function frequentlySearchedAction()
174
    {
175
        /** @var  $searchResultSet SearchResultSet */
176 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
177 1
        $searchResultSet->setUsedSearchRequest($this->getRequest());
178 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
179
180 1
        $this->view->assignMultiple(
181
            [
182 1
                'hasSearched' => $this->searchService->getHasSearched(),
183 1
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
184 1
                'resultSet' => $searchResultSet
185
            ]
186
        );
187 1
    }
188
189
    /**
190
     * This action allows to render a detailView with data from solr.
191
     *
192
     * @param string $documentId
193
     */
194 1
    public function detailAction($documentId = '')
195
    {
196 1
        if (!$this->searchService->getIsSolrAvailable()) {
197
            $this->forward('solrNotAvailable');
198
        }
199
200 1
        $document = $this->searchService->getDocumentById($documentId);
201 1
        $this->view->assign('document', $document);
202 1
    }
203
204
    /**
205
     * Rendered when no search is available.
206
     * @return string
207
     */
208 1
    public function solrNotAvailableAction()
209
    {
210 1
        if ($this->response instanceof Response) {
211 1
            $this->response->setStatus(503);
212
        }
213 1
    }
214
}
215