Passed
Push — master ( 8a16f9...4dee65 )
by Timo
19:40
created

SearchController::detailAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 8
cp 0.625
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.2109
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
72 1
            if(strpos($customTemplate, 'EXT:') !== false) {
73 1
                $view->setTemplatePathAndFilename($customTemplate);
74
            } else {
75
                $view->setTemplate($customTemplate);
76
            }
77
        }
78 1
    }
79
80
    /**
81
     * @return string
82
     */
83 33
    protected function getCustomTemplateFromConfiguration()
84
    {
85 33
        $templateKey = str_replace('Action', '', $this->actionMethodName);
86 33
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
87 33
        return $customTemplate;
88
    }
89
90
    /**
91
     * Results
92
     */
93 28
    public function resultsAction()
94
    {
95 28
        if (!$this->searchService->getIsSolrAvailable()) {
96
            $this->forward('solrNotAvailable');
97
        }
98
99 28
        $searchRequest = $this->buildSearchRequest();
100 28
        $searchResultSet = $this->searchService->search($searchRequest);
101
102
103
        // we pass the search result set to the controller context, to have the possibility
104
        // to access it without passing it from partial to partial
105 28
        $this->controllerContext->setSearchResultSet($searchResultSet);
106
107 28
        $this->view->assignMultiple(
108
            [
109 28
                'hasSearched' => $this->searchService->getHasSearched(),
110 28
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
111 28
                'resultSet' => $searchResultSet,
112 28
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
113
            ]
114
        );
115 28
    }
116
117
    /**
118
     * @return SearchRequest
119
     */
120 28
    protected function buildSearchRequest()
121
    {
122 28
        $arguments = (array)$this->request->getArguments();
123 28
        $arguments = $this->adjustPageArgumentToPositiveInteger($arguments);
124
125
        /** @var $searchRequest SearchRequest */
126 28
        $argumentsNamespace = $this->typoScriptConfiguration->getSearchPluginNamespace();
127 28
        $searchRequest = $this->getRequest([$argumentsNamespace => $arguments]);
128
129 28
        return $searchRequest;
130
    }
131
132
    /**
133
     * This methods sets the page argument to an expected positive integer value in the arguments array.
134
     *
135
     * @param array $arguments
136
     * @return array
137
     */
138 28
    protected function adjustPageArgumentToPositiveInteger(array $arguments)
139
    {
140 28
        $page = isset($arguments['page']) ? intval($arguments['page']) - 1 : 0;
141 28
        $arguments['page'] = max($page, 0);
142
143 28
        return $arguments;
144
    }
145
146
    /**
147
     * @param array $requestArguments
148
     * @return SearchRequest
149
     */
150 29
    private function getRequest(array $requestArguments = [])
151
    {
152 29
        $searchRequest = GeneralUtility::makeInstance(
153 29
            SearchRequest::class,
154
            $requestArguments,
155 29
            $this->typoScriptFrontendController->getRequestedId(),
156 29
            $this->typoScriptFrontendController->sys_language_uid,
157 29
            $this->typoScriptConfiguration);
158 29
        return $searchRequest;
159
    }
160
161
    /**
162
     * Form
163
     */
164 2
    public function formAction()
165
    {
166 2
        $this->view->assignMultiple(
167
            [
168 2
                'search' => $this->searchService->getSearch(),
169 2
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
170 2
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
171
            ]
172
        );
173 2
    }
174
175
    /**
176
     * Frequently Searched
177
     */
178 1
    public function frequentlySearchedAction()
179
    {
180
        /** @var  $searchResultSet SearchResultSet */
181 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
182 1
        $searchResultSet->setUsedSearchRequest($this->getRequest());
183 1
        $this->controllerContext->setSearchResultSet($searchResultSet);
184
185 1
        $this->view->assignMultiple(
186
            [
187 1
                'hasSearched' => $this->searchService->getHasSearched(),
188 1
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
189 1
                'resultSet' => $searchResultSet
190
            ]
191
        );
192 1
    }
193
194
    /**
195
     * This action allows to render a detailView with data from solr.
196
     *
197
     * @param string $documentId
198
     */
199 1
    public function detailAction($documentId = '')
200
    {
201 1
        if (!$this->searchService->getIsSolrAvailable()) {
202
            $this->forward('solrNotAvailable');
203
        }
204
205 1
        $document = $this->searchService->getDocumentById($documentId);
206 1
        $this->view->assign('document', $document);
207 1
    }
208
209
    /**
210
     * Rendered when no search is available.
211
     * @return string
212
     */
213 1
    public function solrNotAvailableAction()
214
    {
215 1
        if ($this->response instanceof Response) {
216 1
            $this->response->setStatus(503);
217
        }
218 1
    }
219
}
220