Completed
Push — master ( 2879ec...6b0a9d )
by Timo
15s
created

SearchController::formAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

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