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