|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\Pagination; |
|
19
|
|
|
|
|
20
|
|
|
use TYPO3\CMS\Core\Pagination\PaginationInterface; |
|
21
|
|
|
use TYPO3\CMS\Core\Pagination\PaginatorInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ResultsPagination |
|
25
|
|
|
* |
|
26
|
|
|
* @author Rudy Gnodde <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class ResultsPagination implements PaginationInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var PaginatorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected PaginatorInterface $paginator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected int $maxPageNumbers = 0; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected bool $hasMorePages = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
protected bool $hasLessPages = false; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected int $pageRangeFirst = 1; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var int |
|
57
|
|
|
*/ |
|
58
|
|
|
protected int $pageRangeLast = 1; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param PaginatorInterface $paginator |
|
62
|
|
|
*/ |
|
63
|
33 |
|
public function __construct(PaginatorInterface $paginator) |
|
64
|
|
|
{ |
|
65
|
33 |
|
$this->paginator = $paginator; |
|
66
|
33 |
|
$this->calculatePageRange(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get maximum page numbers |
|
71
|
|
|
* |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
33 |
|
public function getMaxPageNumbers(): int |
|
75
|
|
|
{ |
|
76
|
33 |
|
return $this->maxPageNumbers; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set maximum page numbers |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $maxPageNumbers |
|
83
|
|
|
*/ |
|
84
|
33 |
|
public function setMaxPageNumbers(int $maxPageNumbers): void |
|
85
|
|
|
{ |
|
86
|
33 |
|
$this->maxPageNumbers = $maxPageNumbers; |
|
87
|
33 |
|
$this->calculatePageRange(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get has more pages |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
26 |
|
public function getHasMorePages(): bool |
|
96
|
|
|
{ |
|
97
|
26 |
|
return $this->hasMorePages; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get has less pages |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
26 |
|
public function getHasLessPages(): bool |
|
106
|
|
|
{ |
|
107
|
26 |
|
return $this->hasLessPages; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get page range first |
|
112
|
|
|
* |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getPageRangeFirst(): int |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->pageRangeFirst; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get page range last |
|
122
|
|
|
* |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getPageRangeLast(): int |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->pageRangeLast; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get previous page number |
|
132
|
|
|
* |
|
133
|
|
|
* @return int|null |
|
134
|
|
|
*/ |
|
135
|
26 |
|
public function getPreviousPageNumber(): ?int |
|
136
|
|
|
{ |
|
137
|
26 |
|
$previousPage = $this->paginator->getCurrentPageNumber() - 1; |
|
138
|
|
|
|
|
139
|
26 |
|
if ($previousPage > $this->paginator->getNumberOfPages()) { |
|
140
|
|
|
return null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
26 |
|
return $previousPage >= $this->getFirstPageNumber() |
|
144
|
1 |
|
? $previousPage |
|
145
|
26 |
|
: null |
|
146
|
|
|
; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get next page number |
|
151
|
|
|
* |
|
152
|
|
|
* @return int|null |
|
153
|
|
|
*/ |
|
154
|
26 |
|
public function getNextPageNumber(): ?int |
|
155
|
|
|
{ |
|
156
|
26 |
|
$nextPage = $this->paginator->getCurrentPageNumber() + 1; |
|
157
|
|
|
|
|
158
|
26 |
|
return $nextPage <= $this->paginator->getNumberOfPages() |
|
159
|
2 |
|
? $nextPage |
|
160
|
26 |
|
: null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get first page number |
|
165
|
|
|
* |
|
166
|
|
|
* @return int |
|
167
|
|
|
*/ |
|
168
|
26 |
|
public function getFirstPageNumber(): int |
|
169
|
|
|
{ |
|
170
|
26 |
|
return 1; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get last page number |
|
175
|
|
|
* |
|
176
|
|
|
* @return int |
|
177
|
|
|
*/ |
|
178
|
33 |
|
public function getLastPageNumber(): int |
|
179
|
|
|
{ |
|
180
|
33 |
|
return $this->paginator->getNumberOfPages(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get start record number |
|
185
|
|
|
* |
|
186
|
|
|
* @return int |
|
187
|
|
|
*/ |
|
188
|
26 |
|
public function getStartRecordNumber(): int |
|
189
|
|
|
{ |
|
190
|
26 |
|
if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) { |
|
191
|
|
|
return 0; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
26 |
|
return $this->paginator->getKeyOfFirstPaginatedItem() + 1; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get end record number |
|
199
|
|
|
* |
|
200
|
|
|
* @return int |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getEndRecordNumber(): int |
|
203
|
|
|
{ |
|
204
|
|
|
if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) { |
|
205
|
|
|
return 0; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $this->paginator->getKeyOfLastPaginatedItem() + 1; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get all page numbers |
|
213
|
|
|
* |
|
214
|
|
|
* @return int[] |
|
215
|
|
|
*/ |
|
216
|
26 |
|
public function getAllPageNumbers(): array |
|
217
|
|
|
{ |
|
218
|
26 |
|
return range($this->pageRangeFirst, $this->pageRangeLast); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Calculate page range |
|
223
|
|
|
*/ |
|
224
|
33 |
|
protected function calculatePageRange(): void |
|
225
|
|
|
{ |
|
226
|
33 |
|
$this->pageRangeFirst = 1; |
|
227
|
33 |
|
$this->pageRangeLast = $this->getLastPageNumber(); |
|
228
|
33 |
|
$this->hasLessPages = false; |
|
229
|
33 |
|
$this->hasMorePages = false; |
|
230
|
|
|
|
|
231
|
33 |
|
$maxNumberOfLinks = $this->getMaxPageNumbers(); |
|
232
|
33 |
|
if ($maxNumberOfLinks > 0) { |
|
233
|
|
|
$numberOfPages = $this->paginator->getNumberOfPages(); |
|
234
|
|
|
|
|
235
|
|
|
if ($numberOfPages > $maxNumberOfLinks) { |
|
236
|
|
|
$currentPage = $this->paginator->getCurrentPageNumber(); |
|
237
|
|
|
$pagesBeforeAndAfter = ($maxNumberOfLinks - 1) / 2; |
|
238
|
|
|
$this->pageRangeFirst = (int)($currentPage - floor($pagesBeforeAndAfter)); |
|
239
|
|
|
$this->pageRangeLast = (int)($currentPage + ceil($pagesBeforeAndAfter)); |
|
240
|
|
|
|
|
241
|
|
|
if ($this->pageRangeFirst < 1) { |
|
242
|
|
|
$this->pageRangeLast -= $this->pageRangeFirst - 1; |
|
243
|
|
|
} |
|
244
|
|
|
if ($this->pageRangeLast > $numberOfPages) { |
|
245
|
|
|
$this->pageRangeFirst -= $this->pageRangeLast - $numberOfPages; |
|
246
|
|
|
} |
|
247
|
|
|
$this->pageRangeFirst = max($this->pageRangeFirst, 1); |
|
248
|
|
|
$this->pageRangeLast = min($this->pageRangeLast, $numberOfPages); |
|
249
|
|
|
$this->hasLessPages = $this->pageRangeFirst > 1; |
|
250
|
|
|
$this->hasMorePages = $this->pageRangeLast < $numberOfPages; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|