1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Pagination\Pagerfanta\View; |
6
|
|
|
|
7
|
|
|
use Pagerfanta\PagerfantaInterface; |
8
|
|
|
use Pagerfanta\View\ViewInterface; |
9
|
|
|
use Twig\Environment; |
10
|
|
|
|
11
|
|
|
class InformationCollectionAdminView implements ViewInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Twig\Environment |
15
|
|
|
*/ |
16
|
|
|
protected $twig; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $template; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Pagerfanta\Pagerfanta |
25
|
|
|
*/ |
26
|
|
|
protected $pagerfanta; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Closure |
30
|
|
|
*/ |
31
|
|
|
protected $routeGenerator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $proximity; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $startPage; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $endPage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param \Twig\Environment $twig |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Environment $twig) |
54
|
|
|
{ |
55
|
|
|
$this->twig = $twig; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sets the default template. |
60
|
|
|
* |
61
|
|
|
* @param string $template |
62
|
|
|
*/ |
63
|
|
|
public function setDefaultTemplate($template) |
64
|
|
|
{ |
65
|
|
|
$this->template = $template; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the canonical name. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getName() |
74
|
|
|
{ |
75
|
|
|
return 'netgen_information_collection_admin'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Renders a Pagerfanta. |
80
|
|
|
* |
81
|
|
|
* The route generator can be any callable to generate |
82
|
|
|
* the routes receiving the page number as first and |
83
|
|
|
* unique argument. |
84
|
|
|
* |
85
|
|
|
* @param \Pagerfanta\PagerfantaInterface $pagerfanta A pagerfanta |
86
|
|
|
* @param \Closure $routeGenerator A callable to generate the routes |
87
|
|
|
* @param array $options An array of options (optional) |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = []) |
92
|
|
|
{ |
93
|
|
|
$this->pagerfanta = $pagerfanta; |
|
|
|
|
94
|
|
|
$this->routeGenerator = $routeGenerator; |
95
|
|
|
|
96
|
|
|
$this->initializeProximity($options); |
97
|
|
|
$this->calculateStartAndEndPage(); |
98
|
|
|
|
99
|
|
|
return $this->twig->render( |
100
|
|
|
$options['template'] ?? $this->template, |
101
|
|
|
[ |
102
|
|
|
'pager' => $pagerfanta, |
103
|
|
|
'pages' => $this->getPages(), |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Initializes the proximity. |
110
|
|
|
* |
111
|
|
|
* @param array $options |
112
|
|
|
*/ |
113
|
|
|
protected function initializeProximity($options) |
114
|
|
|
{ |
115
|
|
|
$this->proximity = isset($options['proximity']) ? |
116
|
|
|
(int) $options['proximity'] : |
117
|
|
|
2; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Calculates start and end page that will be shown in the middle of pager. |
122
|
|
|
*/ |
123
|
|
|
protected function calculateStartAndEndPage() |
124
|
|
|
{ |
125
|
|
|
$currentPage = $this->pagerfanta->getCurrentPage(); |
126
|
|
|
$nbPages = $this->pagerfanta->getNbPages(); |
127
|
|
|
|
128
|
|
|
$startPage = $currentPage - $this->proximity; |
129
|
|
|
$endPage = $currentPage + $this->proximity; |
130
|
|
|
|
131
|
|
|
if ($startPage < 1) { |
132
|
|
|
$endPage = $this->calculateEndPageForStartPageUnderflow($startPage, $endPage, $nbPages); |
133
|
|
|
$startPage = 1; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($endPage > $nbPages) { |
137
|
|
|
$startPage = $this->calculateStartPageForEndPageOverflow($startPage, $endPage, $nbPages); |
138
|
|
|
$endPage = $nbPages; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->startPage = (int) $startPage; |
142
|
|
|
$this->endPage = (int) $endPage; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Calculates the end page when start page is underflowed. |
147
|
|
|
* |
148
|
|
|
* @param int $startPage |
149
|
|
|
* @param int $endPage |
150
|
|
|
* @param int $nbPages |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
protected function calculateEndPageForStartPageUnderflow($startPage, $endPage, $nbPages) |
155
|
|
|
{ |
156
|
|
|
return min($endPage + (1 - $startPage), $nbPages); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Calculates the start page when end page is overflowed. |
161
|
|
|
* |
162
|
|
|
* @param int $startPage |
163
|
|
|
* @param int $endPage |
164
|
|
|
* @param int $nbPages |
165
|
|
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
protected function calculateStartPageForEndPageOverflow($startPage, $endPage, $nbPages) |
169
|
|
|
{ |
170
|
|
|
return max($startPage - ($endPage - $nbPages), 1); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the list of all pages that need to be displayed. |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
protected function getPages() |
179
|
|
|
{ |
180
|
|
|
$pages = []; |
181
|
|
|
|
182
|
|
|
$pages['previous_page'] = $this->pagerfanta->hasPreviousPage() ? |
183
|
|
|
$this->generateUrl($this->pagerfanta->getPreviousPage()) : |
184
|
|
|
false; |
185
|
|
|
|
186
|
|
|
$pages['first_page'] = $this->startPage > 1 ? $this->generateUrl(1) : false; |
187
|
|
|
$pages['mobile_first_page'] = $this->pagerfanta->getCurrentPage() > 2 ? $this->generateUrl(1) : false; |
188
|
|
|
|
189
|
|
|
$pages['second_page'] = $this->startPage === 3 ? $this->generateUrl(2) : false; |
190
|
|
|
|
191
|
|
|
$pages['separator_before'] = $this->startPage > 3 ? true : false; |
192
|
|
|
|
193
|
|
|
$middlePages = []; |
194
|
|
|
for ($i = $this->startPage, $end = $this->endPage; $i <= $end; ++$i) { |
195
|
|
|
$middlePages[$i] = $this->generateUrl($i); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$pages['middle_pages'] = $middlePages; |
199
|
|
|
|
200
|
|
|
$pages['separator_after'] = $this->endPage < $this->pagerfanta->getNbPages() - 2 ? true : false; |
201
|
|
|
|
202
|
|
|
$pages['second_to_last_page'] = $this->endPage === $this->pagerfanta->getNbPages() - 2 ? |
203
|
|
|
$this->generateUrl($this->pagerfanta->getNbPages() - 1) : |
204
|
|
|
false; |
205
|
|
|
|
206
|
|
|
$pages['last_page'] = $this->pagerfanta->getNbPages() > $this->endPage ? |
207
|
|
|
$this->generateUrl($this->pagerfanta->getNbPages()) : |
208
|
|
|
false; |
209
|
|
|
|
210
|
|
|
$pages['mobile_last_page'] = $this->pagerfanta->getCurrentPage() < $this->pagerfanta->getNbPages() - 1 ? |
211
|
|
|
$this->generateUrl($this->pagerfanta->getNbPages()) : |
212
|
|
|
false; |
213
|
|
|
|
214
|
|
|
$pages['next_page'] = $this->pagerfanta->hasNextPage() ? |
215
|
|
|
$this->generateUrl($this->pagerfanta->getNextPage()) : |
216
|
|
|
false; |
217
|
|
|
|
218
|
|
|
return $pages; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Generates the URL based on provided page. |
223
|
|
|
* |
224
|
|
|
* @param int $page |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
protected function generateUrl($page) |
229
|
|
|
{ |
230
|
|
|
$routeGenerator = $this->routeGenerator; |
231
|
|
|
|
232
|
|
|
// We use trim here because Pagerfanta (or Symfony?) adds an extra '?' |
233
|
|
|
// at the end of page when there are no other query params |
234
|
|
|
return trim($routeGenerator($page), '?'); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.