1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\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\Widget\AbstractWidgetController; |
18
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
19
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ResultPaginateController |
23
|
|
|
* |
24
|
|
|
* @author Frans Saris <[email protected]> |
25
|
|
|
* @author Timo Hund <[email protected]> |
26
|
|
|
* @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller |
27
|
|
|
*/ |
28
|
|
|
class ResultPaginateController extends AbstractWidgetController |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $configuration = ['insertAbove' => true, 'insertBelow' => true, 'maximumNumberOfLinks' => 10, 'addQueryStringMethod' => '']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet |
38
|
|
|
*/ |
39
|
|
|
protected $resultSet; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $currentPage = 1; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $displayRangeStart; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $displayRangeEnd; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
protected $maximumNumberOfLinks = 99; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var int |
63
|
|
|
*/ |
64
|
|
|
protected $numberOfPages = 1; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
22 |
|
public function initializeAction() |
70
|
|
|
{ |
71
|
22 |
|
$this->resultSet = $this->widgetConfiguration['resultSet']; |
72
|
|
|
|
73
|
22 |
|
ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false); |
74
|
22 |
|
$this->configuration['itemsPerPage'] = $this->getItemsPerPage(); |
75
|
22 |
|
$this->numberOfPages = (int)ceil($this->resultSet->getUsedSearch()->getNumberOfResults() / $this->configuration['itemsPerPage']); |
76
|
22 |
|
$this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks']; |
77
|
22 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determines the number of results per page. When nothing is configured 10 will be returned. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
22 |
|
protected function getItemsPerPage() |
85
|
|
|
{ |
86
|
22 |
|
$perPage = (int)$this->resultSet->getUsedSearch()->getQuery()->getResultsPerPage(); |
87
|
22 |
|
return $perPage > 0 ? $perPage : 10; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext |
92
|
|
|
* @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext |
93
|
|
|
*/ |
94
|
22 |
|
protected function setActiveSearchResultSet($controllerContext) { |
95
|
22 |
|
$controllerContext->setSearchResultSet($this->resultSet); |
96
|
22 |
|
return $controllerContext; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
22 |
|
public function indexAction() |
103
|
|
|
{ |
104
|
|
|
// set current page |
105
|
22 |
|
$this->currentPage = $this->resultSet->getUsedPage() + 1; |
106
|
22 |
|
if ($this->currentPage < 1) { |
107
|
|
|
$this->currentPage = 1; |
108
|
|
|
} |
109
|
22 |
|
$this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $this->getDocuments(), 'pagination' => $this->buildPagination()]); |
110
|
22 |
|
$this->view->assign('configuration', $this->configuration); |
111
|
22 |
|
$this->view->assign('resultSet', $this->resultSet); |
112
|
22 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* If a certain number of links should be displayed, adjust before and after |
116
|
|
|
* amounts accordingly. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
22 |
|
protected function calculateDisplayRange() |
121
|
|
|
{ |
122
|
22 |
|
$maximumNumberOfLinks = $this->maximumNumberOfLinks; |
123
|
22 |
|
if ($maximumNumberOfLinks > $this->numberOfPages) { |
124
|
22 |
|
$maximumNumberOfLinks = $this->numberOfPages; |
125
|
|
|
} |
126
|
22 |
|
$delta = floor($maximumNumberOfLinks / 2); |
127
|
22 |
|
$this->displayRangeStart = $this->currentPage - $delta; |
|
|
|
|
128
|
22 |
|
$this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0); |
|
|
|
|
129
|
22 |
|
if ($this->displayRangeStart < 1) { |
130
|
8 |
|
$this->displayRangeEnd -= $this->displayRangeStart - 1; |
131
|
|
|
} |
132
|
22 |
|
if ($this->displayRangeEnd > $this->numberOfPages) { |
133
|
|
|
$this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages; |
134
|
|
|
} |
135
|
22 |
|
$this->displayRangeStart = (int)max($this->displayRangeStart, 1); |
136
|
22 |
|
$this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages); |
137
|
22 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage" |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
22 |
|
protected function buildPagination() |
145
|
|
|
{ |
146
|
22 |
|
$this->calculateDisplayRange(); |
147
|
22 |
|
$pages = []; |
148
|
22 |
|
for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) { |
149
|
20 |
|
$pages[] = ['number' => $i, 'isCurrent' => $i === $this->currentPage]; |
150
|
|
|
} |
151
|
22 |
|
$pagination = ['pages' => $pages, 'current' => $this->currentPage, 'numberOfPages' => $this->numberOfPages, 'displayRangeStart' => $this->displayRangeStart, 'displayRangeEnd' => $this->displayRangeEnd, 'hasLessPages' => $this->displayRangeStart > 2, 'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages]; |
152
|
22 |
|
if ($this->currentPage < $this->numberOfPages) { |
153
|
8 |
|
$pagination['nextPage'] = $this->currentPage + 1; |
154
|
|
|
} |
155
|
22 |
|
if ($this->currentPage > 1) { |
156
|
1 |
|
$pagination['previousPage'] = $this->currentPage - 1; |
157
|
|
|
} |
158
|
22 |
|
return $pagination; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return \Apache_Solr_Document[] |
163
|
|
|
*/ |
164
|
22 |
|
protected function getDocuments() |
165
|
|
|
{ |
166
|
22 |
|
$extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
167
|
22 |
|
if (!empty($extbaseFrameworkConfiguration['features']['useRawDocuments'])) { |
168
|
20 |
|
return $this->resultSet->getUsedSearch()->getResultDocumentsRaw(); |
169
|
|
|
} else { |
170
|
2 |
|
return $this->resultSet->getUsedSearch()->getResultDocumentsEscaped(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.