Completed
Push — master ( 2c204f...0461be )
by Timo
23s
created

getCustomTemplateFromConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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