Completed
Push — master ( 3c4bb4...068b1b )
by Timo
43s
created

Results::postInitialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.0156
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\CommandResolver;
28
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
29
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
30
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase;
31
use ApacheSolrForTypo3\Solr\Plugin\PluginCommand;
32
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
33
use ApacheSolrForTypo3\Solr\Template;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
36
/**
37
 * Plugin 'Solr Search' for the 'solr' extension.
38
 *
39
 * @author Ingo Renner <[email protected]>
40
 * @author Timo Schmidt <[email protected]>
41
 */
42
class Results extends CommandPluginBase
43
{
44
    /**
45
     * Path to this script relative to the extension dir.
46
     *
47
     * @var string
48
     */
49
    public $scriptRelPath = 'Classes/Plugin/Results/Results.php';
50
51
    /**
52
     * @var SearchResultSet
53
     */
54
    protected $searchResultSet;
55
56
    /**
57
     * Perform the action for the plugin. In this case it calls the search()
58
     * method which internally performs the search.
59
     *
60
     * @return void
61
     */
62 23
    protected function performAction()
63
    {
64 23
        if ($this->getSearchResultSetService()->getIsSolrAvailable()) {
65 23
            $searchRequest = $this->buildSearchRequest();
66 23
            $this->searchResultSet = $this->getSearchResultSetService()->search($searchRequest);
67 23
        }
68 23
    }
69
70
    /**
71
     * @return SearchRequest
72
     */
73 23
    private function buildSearchRequest()
74
    {
75 23
        $solrParameters = [];
76 23
        $solrPostParameters = GeneralUtility::_POST('tx_solr');
77 23
        $solrGetParameters = GeneralUtility::_GET('tx_solr');
78
79
        // check for GET parameters, POST takes precedence
80 23
        if (isset($solrGetParameters) && is_array($solrGetParameters)) {
81 1
            $solrParameters = $solrGetParameters;
82 1
        }
83 23
        if (isset($solrPostParameters) && is_array($solrPostParameters)) {
84
            $solrParameters = $solrPostParameters;
85
        }
86
87
        /** @var $searchRequest SearchRequest */
88 23
        $searchRequest = GeneralUtility::makeInstance(
89 23
            SearchRequest::class,
90 23
            array('tx_solr' => $solrParameters),
91 23
            $GLOBALS['TSFE']->id,
92 23
            $GLOBALS['TSFE']->sys_language_uid,
93 23
            $this->typoScriptConfiguration
94 23
        );
95 23
        $searchRequest->mergeArguments(array('tx_solr' => $this->piVars));
96 23
        $searchRequest->mergeArguments(array('q' => $this->getRawUserQuery()));
97
98 23
        return $searchRequest;
99
    }
100
101
    /**
102
     * Implementation of preRender() method. Used to include CSS files.
103
     *
104
     */
105
    protected function preRender()
106
    {
107
        $resultsCss = $this->typoScriptConfiguration->getCssFileByFileKey('results');
108
        if ($resultsCss !== '') {
109
            $cssFile = GeneralUtility::createVersionNumberedFilename($GLOBALS['TSFE']->tmpl->getFileName($resultsCss));
110
            $GLOBALS['TSFE']->additionalHeaderData['tx_solr-resultsCss'] = '<link href="' . $cssFile . '" rel="stylesheet" type="text/css" />';
111
        }
112
    }
113
114
    /**
115
     * Returns an initialized CommandResolver.
116
     *
117 23
     */
118
    protected function getCommandResolver()
119 23
    {
120 23
        return GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\CommandResolver');
121 23
    }
122 23
123 23
    /**
124 23
     * Retrieves the list of commands to process for the results view.
125
     *
126
     * @return array An array of command names to process for the result view
127
     */
128
    protected function getCommandList()
129
    {
130 23
        $requirements = PluginCommand::REQUIREMENT_NONE;
131
132 23
        if ($this->getSearchResultSetService()->getHasSearched()) {
133
            $requirements = PluginCommand::REQUIREMENT_HAS_SEARCHED;
134
135
            if ($this->searchResultSet->getUsedSearch()->getNumberOfResults() > 0) {
136
                $requirements += PluginCommand::REQUIREMENT_HAS_RESULTS;
137
            } else {
138
                $requirements += PluginCommand::REQUIREMENT_NO_RESULTS;
139
            }
140 23
        }
141
142 23
        return CommandResolver::getPluginCommands('results', $requirements);
143
    }
144 23
145 20
    /**
146
     * Performs post initialization.
147 20
     *
148 17
     */
149 17
    protected function postInitialize()
150 3
    {
151
        // disable caching
152 20
        $this->pi_USER_INT_obj = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $pi_USER_INT_obj was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
153
    }
154 23
155
    /**
156
     * Overrides certain TypoScript configuration options with their values
157
     * from FlexForms.
158
     *
159
     */
160
    protected function overrideTyposcriptWithFlexformSettings()
161 23
    {
162
        $flexFormConfiguration = [];
163
164 23
        // initialize with empty query, useful when no search has been
165 23
        // conducted yet but needs to show facets already.
166
        $initializeWithEmptyQuery = $this->getFlexFormValue('initializeWithEmptyQuery', 'sQuery');
167
168
        if (boolval($initializeWithEmptyQuery)) {
169
            $flexFormConfiguration['search.']['initializeWithEmptyQuery'] = 1;
170
        }
171
172 23
        $showResultsOfInitialEmptyQuery = $this->getFlexFormValue('showResultsOfInitialEmptyQuery', 'sQuery');
173
        if (boolval($showResultsOfInitialEmptyQuery)) {
174 23
            $flexFormConfiguration['search.']['showResultsOfInitialEmptyQuery'] = 1;
175
        }
176
177
        // initialize with non-empty query
178 23
        $initialQuery = trim((string)$this->getFlexFormValue('initializeWithQuery', 'sQuery'));
179
        if ($initialQuery !== '') {
180 23
            $flexFormConfiguration['search.']['initializeWithQuery'] = $initialQuery;
181
        }
182
183
        $showResultsOfInitialQuery = $this->getFlexFormValue('showResultsOfInitialQuery', 'sQuery');
184 23
        if (boolval($showResultsOfInitialQuery)) {
185 23
            $flexFormConfiguration['search.']['showResultsOfInitialQuery'] = 1;
186
        }
187
188
        // target page
189
        $flexformTargetPage = $this->getFlexFormValue('targetPage');
190 23
        if (!empty($flexformTargetPage)) {
191 23
            $flexFormConfiguration['search.']['targetPage'] = (int)$flexformTargetPage;
192
        }
193
194
        // boost function
195 23
        $boostFunction = trim((string)$this->getFlexFormValue('boostFunction', 'sQuery'));
196 23
        if ($boostFunction !== '') {
197
            $flexFormConfiguration['search.']['query.']['boostFunction'] = $boostFunction;
198
        }
199
200
        // boost query
201 23
        $boostQuery = trim((string)$this->getFlexFormValue('boostQuery', 'sQuery'));
202 23
        if ($boostQuery !== '') {
203
            $flexFormConfiguration['search.']['query.']['boostQuery'] = $boostQuery;
204
        }
205
206
        // sorting
207 23
        $flexformSorting = trim((string)$this->getFlexFormValue('sortBy', 'sQuery'));
208 23
        if ($flexformSorting !== '') {
209
            $flexFormConfiguration['search.']['query.']['sortBy'] = $flexformSorting;
210
        }
211
212
        // results per page
213 23
        $resultsPerPage = trim((string)$this->getFlexFormValue('resultsPerPage', 'sQuery'));
214 23
        if ($resultsPerPage !== '') {
215
            $flexFormConfiguration['search.']['results.']['resultsPerPage'] = intval($resultsPerPage);
216
        }
217
218
        // flexform overwrites _all_ filters set through TypoScript
219 23
        $flexformFilters = $this->getFlexFormValue('filter', 'sQuery');
220 23
        if (!empty($flexformFilters)) {
221
            $additionalFilters = GeneralUtility::trimExplode('|', $flexformFilters);
222
223
                // we keep the pageSections filter but replace all other filters
224
            $filterConfiguration = $this->typoScriptConfiguration->getSearchQueryFilterConfiguration();
225 23
            if (isset($filterConfiguration['__pageSections'])) {
226 23
                $additionalFilters['__pageSections'] = $filterConfiguration['__pageSections'];
227
            }
228
            $this->typoScriptConfiguration->setSearchQueryFilterConfiguration($additionalFilters);
229
        }
230
231 23
        $this->typoScriptConfiguration->mergeSolrConfiguration($flexFormConfiguration);
232 23
    }
233
234
    /**
235
     * Post initialization of the template engine, adding some Solr variables.
236
     *
237
     * @param Template $template The template object as initialized thus far.
238
     * @return Template The modified template instance with additional variables available for rendering.
239
     */
240
    protected function postInitializeTemplateEngine(Template $template)
241
    {
242
        $template->addVariable('tx_solr', $this->getSolrVariables());
243 23
244 23
        return $template;
245
    }
246
247
    /**
248
     * Gets a list of EXT:solr variables like the prefix ID.
249
     *
250
     * @return array array of EXT:solr variables
251
     */
252 23
    protected function getSolrVariables()
253
    {
254 23
        $currentUrl = $this->getCurrentUrlWithQueryLinkBuilder();
255
256 23
        return array('prefix' => $this->prefixId, 'query_parameter' => 'q', 'current_url' => $currentUrl, 'q' => $this->getCleanUserQuery());
257
    }
258
259
    /**
260
     * Gets the plugin's configuration.
261
     *
262
     * @return TypoScriptConfiguration Configuration
263
     */
264 23
    public function getConfiguration()
265
    {
266 23
        return $this->typoScriptConfiguration;
267
    }
268 23
269
    /**
270
     * Returns the key which is used to determine the template file from the typoscript setup.
271
     *
272
     * @return string
273
     */
274
    protected function getTemplateFileKey()
275
    {
276 15
        return 'results';
277
    }
278 15
279
    /**
280
     * Returns the plugin key, used in various base methods.
281
     *
282
     * @return string
283
     */
284
    protected function getPluginKey()
285
    {
286 23
        return 'PiResults';
287
    }
288 23
289
    /**
290
     * Returns the main subpart to work on.
291
     *
292
     */
293
    protected function getSubpart()
294
    {
295
        return 'solr_search';
296 23
    }
297
}
298