Newer
Older
/***************************************************************
* Copyright notice
* (c) sgalinski Internet Services (https://www.sgalinski.de)
* All rights reserved
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace SGalinski\SgJobs\Pagination;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
/**
* This class is a representation of the pagination
* It contains functionality for handling the
*/
class Pagination {
/**
* @var QueryResult
*/
private $queryResult;
/**
* @var int
*/
private $currentPage;
/**
* @var int
*/
private $limit;
/**
* @var int
*/
private $nextPage;
/**
* @var int
*/
private $previousPage;
/**
* @var int
*/
private $lastPage;
/**
* @var int
*/
private $firstPage;
/**
* @var int
*/
private $startItem;
/**
* @var int
*/
private $endItem;
/**
* @var int
*/
private $totalItems;
/**
* @var array
*/
private $items;
public function __construct(QueryResult $queryResult, int $currentPage, int $limit) {
$this->queryResult = $queryResult;
$this->currentPage = $currentPage;
$this->limit = $limit;
$this->initialize();
}
public function initialize() {
$this->firstPage = 1;
$this->totalItems = $this->fetchTotalItemCount();
$this->lastPage = (int) ceil($this->totalItems / $this->limit);
// correct current page if out of bounds
$this->currentPage = $this->currentPage < 1 ? 1 : min($this->currentPage, $this->lastPage);
$this->previousPage = ($this->currentPage > 1) ? $this->currentPage - 1 : null;
$this->nextPage = ($this->currentPage < $this->lastPage) ? $this->currentPage + 1 : null;
$this->startItem = ($this->currentPage - 1) * $this->limit + 1;
// correct startItem if out of bounds
$this->startItem = max($this->startItem, 1);
$this->endItem = ($this->currentPage - 1) * $this->limit + $this->limit;
// correct endItem if out of bounds
$this->endItem = min($this->endItem, $this->totalItems);
$this->items = $this->fetchItems();
}
/**
* Reconfigure the query builder to fetch the total count of items
*
* @return int
*/
private function fetchTotalItemCount(): int {
$query = clone $this->queryResult->getQuery();
$query->setOffset(0)->setLimit(9999999);
}
/**
* Reconfigure the queryResult to fetch the items for the configured currentPage
*
* @return array
*/
private function fetchItems(): array {
$query = clone $this->queryResult->getQuery();
$query->setOffset($this->startItem - 1)->setLimit($this->limit);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
}
/**
* @return int
*/
public function getCurrentPage(): int {
return $this->currentPage;
}
/**
* @return int
*/
public function getLimit(): int {
return $this->limit;
}
/**
* @return mixed
*/
public function getNextPage() {
return $this->nextPage;
}
/**
* @return mixed
*/
public function getPreviousPage() {
return $this->previousPage;
}
/**
* @return mixed
*/
public function getLastPage() {
return $this->lastPage;
}
/**
* @return mixed
*/
public function getFirstPage() {
return $this->firstPage;
}
/**
* @return mixed
*/
public function getStartItem() {
return $this->startItem;
}
/**
* @return mixed
*/
public function getEndItem() {
return $this->endItem;
}
/**
* @return mixed
*/
public function getTotalItems() {
return $this->totalItems;
}
/**
* @return array
*/
public function getItems(): array {
return $this->items;
}
}