Completed
Push — master ( 6a4750...2879ec )
by Timo
16s
created

SearchController::buildSearchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1.0156
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\ResultSet\SearchResultSet;
18
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
19
use TYPO3\CMS\Extbase\Mvc\Web\Response;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Fluid\View\TemplateView;
22
23
/**
24
 * Class SearchController
25
 *
26
 * @author Frans Saris <[email protected]>
27
 * @author Timo Hund <[email protected]>
28
 * @package ApacheSolrForTypo3\Solr\Controller
29
 */
30
class SearchController extends AbstractBaseController
31
{
32
    /**
33
     * @var TemplateView
34
     */
35
    protected $view;
36
37
    /**
38
     * Provide search query in extbase arguments.
39
     */
40
    protected function initializeAction()
41 34
    {
42
        parent::initializeAction();
43 34
        $this->mapGlobalQueryStringWhenEnabled();
44 34
    }
45 34
46
    /**
47
     * @return void
48
     */
49
    protected function mapGlobalQueryStringWhenEnabled()
50 34
    {
51
        $query = GeneralUtility::_GET('q');
52 34
53
        $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter();
54 34
55
        if ($useGlobalQueryString) {
56 34
            $this->request->setArgument('q', $query);
57 23
        }
58
    }
59 34
60
    /**
61
     * @param ViewInterface $view
62
     */
63
    public function initializeView(ViewInterface $view)
64 34
    {
65
        if($view instanceof TemplateView) {
66 34
            $customTemplate = $this->getCustomTemplateFromConfiguration();
67 34
            if($customTemplate === '') {
68 34
                return;
69 33
            }
70
71
            if(strpos($customTemplate, 'EXT:') !== false) {
72 1
                $view->setTemplatePathAndFilename($customTemplate);
73 1
            } else {
74
                $view->setTemplate($customTemplate);
75
            }
76
        }
77
    }
78 1
79
    /**
80
     * @return string
81
     */
82
    protected function getCustomTemplateFromConfiguration()
83 34
    {
84
        $templateKey = str_replace('Action', '', $this->actionMethodName);
85 34
        $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey);
86 34
        return $customTemplate;
87 34
    }
88
89
    /**
90
     * Results
91
     */
92
    public function resultsAction()
93 29
    {
94
        if (!$this->searchService->getIsSolrAvailable()) {
95 29
            $this->forward('solrNotAvailable');
96
        }
97
98
        $arguments = (array)$this->request->getArguments();
99 29
        $pageId = $this->typoScriptFrontendController->getRequestedId();
100 29
        $languageId = $this->typoScriptFrontendController->sys_language_uid;
101
        $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId);
102
103
        $searchResultSet = $this->searchService->search($searchRequest);
104
105 29
106
        // we pass the search result set to the controller context, to have the possibility
107 29
        // to access it without passing it from partial to partial
108
        $this->controllerContext->setSearchResultSet($searchResultSet);
109 29
110 29
        $this->view->assignMultiple(
111 29
            [
112 29
                'hasSearched' => $this->searchService->getHasSearched(),
113
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
114
                'resultSet' => $searchResultSet,
115 29
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
116
            ]
117
        );
118
    }
119
120 29
    /**
121
     * Form
122 29
     */
123 29
    public function formAction()
124
    {
125
        $this->view->assignMultiple(
126 29
            [
127 29
                'search' => $this->searchService->getSearch(),
128
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
129 29
                'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace()
130
            ]
131
        );
132
    }
133
134
    /**
135
     * Frequently Searched
136
     */
137
    public function frequentlySearchedAction()
138 29
    {
139
        /** @var  $searchResultSet SearchResultSet */
140 29
        $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class);
141 29
142
        $pageId = $this->typoScriptFrontendController->getRequestedId();
143 29
        $languageId = $this->typoScriptFrontendController->sys_language_uid;
144
        $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId);
145
        $searchResultSet->setUsedSearchRequest($searchRequest);
146
147
        $this->controllerContext->setSearchResultSet($searchResultSet);
148
        $this->view->assignMultiple(
149
            [
150 30
                'hasSearched' => $this->searchService->getHasSearched(),
151
                'additionalFilters' => $this->searchService->getAdditionalFilters(),
152 30
                'resultSet' => $searchResultSet
153 30
            ]
154
        );
155 30
    }
156 30
157 30
    /**
158 30
     * This action allows to render a detailView with data from solr.
159
     *
160
     * @param string $documentId
161
     */
162
    public function detailAction($documentId = '')
163
    {
164 2
        if (!$this->searchService->getIsSolrAvailable()) {
165
            $this->forward('solrNotAvailable');
166 2
        }
167
168 2
        $document = $this->searchService->getDocumentById($documentId);
169 2
        $this->view->assign('document', $document);
170 2
    }
171
172
    /**
173 2
     * Rendered when no search is available.
174
     * @return string
175
     */
176
    public function solrNotAvailableAction()
177
    {
178 1
        if ($this->response instanceof Response) {
179
            $this->response->setStatus(503);
180
        }
181 1
    }
182
}
183