Completed
Push — master ( 0ba0e3...23fac3 )
by Timo
18:26
created

mapGlobalQueryStringWhenEnabled()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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