Passed
Push — master ( ee4dec...064f0a )
by Timo
53s
created

SearchController::initializeView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3.3332
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller\Frontend;
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 28
     * Provide search query in extbase arguments.
40
     */
41 28
    protected function initializeAction()
42
    {
43 28
        parent::initializeAction();
44 28
45 21
        $query = GeneralUtility::_GET('q');
46
        if ($query !== null) {
47 28
            $this->request->setArgument('q', $query);
48
        }
49
    }
50
51
    /**
52 23
     * @param ViewInterface $view
53
     */
54 23
    public function initializeView(ViewInterface $view)
55
    {
56
        if($view instanceof TemplateView) {
57
            $customTemplate = $this->getCustomTemplateFromConfiguration();
58 23
            if($customTemplate === '') {
59 23
                return;
60
            }
61
            $view->setTemplatePathAndFilename($customTemplate);
62
        }
63 23
    }
64
65 23
    /**
66
     * @return string
67 23
     */
68 23
    protected function getCustomTemplateFromConfiguration()
69 23
    {
70
        $templateKey = str_replace('Action', '', $this->actionMethodName);
71
        $customTemplate = $this->typoScriptConfiguration->getTemplateByFileKey($templateKey);
72 23
        return $customTemplate;
73
    }
74
75
    /**
76
     * Results
77 23
     */
78
    public function resultsAction()
79 23
    {
80 23
        if (!$this->searchService->getIsSolrAvailable()) {
81 21
            $this->forward('solrNotAvailable');
82
        }
83
84 23
        $searchRequest = $this->buildSearchRequest();
85 23
        $searchResultSet = $this->searchService->search($searchRequest);
86 23
87
        // we pass the search result set to the controller context, to have the possibility
88
        // to access it without passing it from partial to partial
89 23
        $this->controllerContext->setSearchResultSet($searchResultSet);
90
91 23
        $this->view->assignMultiple(
92
            [
93
                'hasSearched' => $this->searchService->getHasSearched(),
94
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
95
                'resultSet' => $searchResultSet
96
            ]
97
        );
98 24
    }
99
100 24
    /**
101 24
     * @return SearchRequest
102
     */
103 24
    protected function buildSearchRequest()
104 24
    {
105 24
        $rawUserQuery = null;
106 24
        if ($this->request->hasArgument('q')) {
107
            $rawUserQuery = $this->request->getArgument('q');
108
        }
109
110
        $arguments = $this->request->getArguments();
111
        $page = isset($arguments['page']) ? $arguments['page'] - 1 : 0;
112 2
        $arguments['page'] = max($page, 0);
113
114 2
        /** @var $searchRequest SearchRequest */
115
        $searchRequest = $this->getRequest(['q' => $rawUserQuery, 'tx_solr' => $arguments]);
116 2
117 2
        return $searchRequest;
118
    }
119
120 2
    /**
121
     * @param array $requestArguments
122
     * @return SearchRequest
123
     */
124
    private function getRequest(array $requestArguments = [])
125 1
    {
126
        $searchRequest = GeneralUtility::makeInstance(
127
            SearchRequest::class,
128 1
            $requestArguments,
129 1
            $this->typoScriptFrontendController->getRequestedId(),
130 1
            $this->typoScriptFrontendController->sys_language_uid,
131
            $this->typoScriptConfiguration);
132 1
        return $searchRequest;
133
    }
134 1
135 1
    /**
136 1
     * Form
137
     */
138
    public function formAction()
139 1
    {
140
        $this->view->assignMultiple(
141
            [
142
                'search' => $this->searchService->getSearch(),
143
                'additionalFilters' => $this->searchService->getAdditionalFilters()
144
            ]
145
        );
146 1
    }
147
148 1
    /**
149
     * Frequently Searched
150
     */
151
    public function frequentlySearchedAction()
152 1
    {
153 1
        /** @var  $searchResultSet SearchResultSet */
154 1
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
155
        $searchResultSet->setUsedSearchRequest($this->getRequest());
156
        $this->controllerContext->setSearchResultSet($searchResultSet);
157
158
        $this->view->assignMultiple(
159
            [
160 1
                'hasSearched' => $this->searchService->getHasSearched(),
161
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
162 1
                'resultSet' => $searchResultSet
163 1
            ]
164
        );
165 1
    }
166
167
    /**
168
     * This action allows to render a detailView with data from solr.
169
     *
170
     * @param string $documentId
171
     */
172
    public function detailAction($documentId = '')
173
    {
174
        if (!$this->searchService->getIsSolrAvailable()) {
175
            $this->forward('solrNotAvailable');
176
        }
177
178
        $document = $this->searchService->getDocumentById($documentId);
179
        $this->view->assign('document', $document);
180
    }
181
182
    /**
183
     * Rendered when no search is available.
184
     * @return string
185
     */
186
    public function solrNotAvailableAction()
187
    {
188
        if ($this->response instanceof Response) {
189
            $this->response->setStatus(503);
190
        }
191
    }
192
}
193