1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\FilterManagerBundle\Filter\ViewData; |
13
|
|
|
|
14
|
|
|
use ONGR\FilterManagerBundle\Filter\Helper\OptionsAwareTrait; |
15
|
|
|
use ONGR\FilterManagerBundle\Filter\ViewData; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class represents view data with page choices. |
19
|
|
|
*/ |
20
|
|
|
class PagerAwareViewData extends ViewData |
21
|
|
|
{ |
22
|
|
|
use OptionsAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int Current page. |
26
|
|
|
*/ |
27
|
|
|
private $currentPage = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int Total amount of items to show. |
31
|
|
|
*/ |
32
|
|
|
private $totalItems = 1; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int Maximum pages to show. |
36
|
|
|
*/ |
37
|
|
|
private $maxPages = 10; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int Maximum items show per page. |
41
|
|
|
*/ |
42
|
|
|
private $itemsPerPage = 12; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Number of pages to show. |
46
|
|
|
*/ |
47
|
|
|
private $numPages; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes data for pagination. |
51
|
|
|
* |
52
|
|
|
* @param $totalItems |
53
|
|
|
* @param $currentPage |
54
|
|
|
* @param int $itemsPerPage |
55
|
|
|
* @param int $maxPages |
56
|
|
|
*/ |
57
|
|
|
public function setData($totalItems, $currentPage, $itemsPerPage = 12, $maxPages = 10) |
58
|
|
|
{ |
59
|
|
|
$this->totalItems = $totalItems; |
60
|
|
|
$this->currentPage = $currentPage; |
61
|
|
|
$this->itemsPerPage = $itemsPerPage; |
62
|
|
|
|
63
|
|
|
// if ($maxPages < 3) { |
|
|
|
|
64
|
|
|
// throw new \InvalidArgumentException('Max pages has to be more than 3.'); |
65
|
|
|
// } |
66
|
|
|
$this->maxPages = $maxPages; |
67
|
|
|
|
68
|
|
|
$this->numPages = (int) ceil($this->totalItems/$this->itemsPerPage); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getSerializableData() |
75
|
|
|
{ |
76
|
|
|
$data = parent::getSerializableData(); |
77
|
|
|
|
78
|
|
|
$data['pager'] = [ |
79
|
|
|
'total_items' => $this->totalItems, |
80
|
|
|
'num_pages' => $this->numPages, |
81
|
|
|
'first_page' => 1, |
82
|
|
|
'previous_page' => $this->getPreviousPage(), |
83
|
|
|
'current_page' => $this->currentPage, |
84
|
|
|
'next_page' => $this->getNextPage(), |
85
|
|
|
'last_page' => $this->numPages, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get previous page number. |
93
|
|
|
* |
94
|
|
|
* @return int|null |
95
|
|
|
*/ |
96
|
|
|
public function getPreviousPage() |
97
|
|
|
{ |
98
|
|
|
if ($this->currentPage > 1) { |
99
|
|
|
return $this->currentPage - 1; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns current page number. |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getCurrentPage() |
111
|
|
|
{ |
112
|
|
|
return $this->currentPage; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get next page number. |
117
|
|
|
* |
118
|
|
|
* @return int|null |
119
|
|
|
*/ |
120
|
|
|
public function getNextPage() |
121
|
|
|
{ |
122
|
|
|
if ($this->currentPage < $this->numPages) { |
123
|
|
|
return $this->currentPage + 1; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the last page number. |
131
|
|
|
* |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function getLastPage() |
135
|
|
|
{ |
136
|
|
|
return ceil($this->totalItems / $this->itemsPerPage); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns true if the current page is first. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isFirstPage() |
145
|
|
|
{ |
146
|
|
|
return $this->currentPage == 1; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the first page number. |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
public function getFirstPage() |
155
|
|
|
{ |
156
|
|
|
return 1; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns true if the current page is last. |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function isLastPage() |
165
|
|
|
{ |
166
|
|
|
return $this->currentPage == $this->getLastPage(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Generates a page list. |
171
|
|
|
* |
172
|
|
|
* @return array The page list. |
173
|
|
|
*/ |
174
|
|
|
public function getPages() |
175
|
|
|
{ |
176
|
|
|
$numAdjacents = (int) floor(($this->maxPages - 3) / 2); |
177
|
|
|
|
178
|
|
|
if ($this->currentPage + $numAdjacents > $this->numPages) { |
179
|
|
|
$begin = $this->numPages - $this->maxPages + 2; |
180
|
|
|
} else { |
181
|
|
|
$begin = $this->currentPage - $numAdjacents; |
182
|
|
|
} |
183
|
|
|
if ($begin < 2) { |
184
|
|
|
$begin = 2; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$end = $begin + $this->maxPages - 2; |
188
|
|
|
// if ($end >= $this->numPages) $end = $this->numPages - 1; |
|
|
|
|
189
|
|
|
|
190
|
|
|
// $tmpBegin = $this->maxPages - floor($this->maxPages / 2); |
|
|
|
|
191
|
|
|
// $tmpEnd = $tmpBegin + $this->maxPages - 1; |
192
|
|
|
// |
193
|
|
|
// if ($tmpBegin < 1) { |
194
|
|
|
// $tmpEnd += 1 - $tmpBegin; |
195
|
|
|
// $tmpBegin = 1; |
196
|
|
|
// } |
197
|
|
|
// |
198
|
|
|
// if ($tmpEnd > $this->getLastPage()) { |
199
|
|
|
// $tmpBegin -= $tmpEnd - $this->getLastPage(); |
200
|
|
|
// $tmpEnd = $this->getLastPage(); |
201
|
|
|
// } |
202
|
|
|
// |
203
|
|
|
// $begin = min($tmpBegin, 2); |
204
|
|
|
// $end = $tmpEnd; |
205
|
|
|
|
206
|
|
|
return range($begin, $end, 1); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.