Passed
Push — master ( 064f0a...ae02be )
by Timo
49s
created

SearchController::initializeView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

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