1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Twig; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Borut Balažek <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class PaginatorExtension extends \Twig_Extension |
11
|
|
|
{ |
12
|
|
|
private $app; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Application $app |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Application $app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getName() |
26
|
|
|
{ |
27
|
|
|
return 'application/paginator'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return \Twig_SimpleFunction[] |
32
|
|
|
*/ |
33
|
|
|
public function getFunctions() |
34
|
|
|
{ |
35
|
|
|
return array( |
36
|
|
|
new \Twig_SimpleFunction( |
37
|
|
|
'paginator_limit_per_page_render', |
38
|
|
|
array( |
39
|
|
|
$this, |
40
|
|
|
'paginatorLimitPerPageRender', |
41
|
|
|
), |
42
|
|
|
array( |
43
|
|
|
'is_safe' => array('html'), |
44
|
|
|
) |
45
|
|
|
), |
46
|
|
|
new \Twig_SimpleFunction( |
47
|
|
|
'paginator_search_render', |
48
|
|
|
array( |
49
|
|
|
$this, |
50
|
|
|
'paginatorSearchRender', |
51
|
|
|
), |
52
|
|
|
array( |
53
|
|
|
'is_safe' => array('html'), |
54
|
|
|
) |
55
|
|
|
), |
56
|
|
|
new \Twig_SimpleFunction( |
57
|
|
|
'paginator_top_render', |
58
|
|
|
array( |
59
|
|
|
$this, |
60
|
|
|
'paginatorTopRender', |
61
|
|
|
), |
62
|
|
|
array( |
63
|
|
|
'is_safe' => array('html'), |
64
|
|
|
) |
65
|
|
|
), |
66
|
|
|
new \Twig_SimpleFunction( |
67
|
|
|
'paginator_pagination_render', |
68
|
|
|
array( |
69
|
|
|
$this, |
70
|
|
|
'paginatorPaginationRender', |
71
|
|
|
), |
72
|
|
|
array( |
73
|
|
|
'is_safe' => array('html'), |
74
|
|
|
) |
75
|
|
|
), |
76
|
|
|
new \Twig_SimpleFunction( |
77
|
|
|
'paginator_results_text_render', |
78
|
|
|
array( |
79
|
|
|
$this, |
80
|
|
|
'paginatorResultsTextRender', |
81
|
|
|
), |
82
|
|
|
array( |
83
|
|
|
'is_safe' => array('html'), |
84
|
|
|
) |
85
|
|
|
), |
86
|
|
|
new \Twig_SimpleFunction( |
87
|
|
|
'paginator_bottom_render', |
88
|
|
|
array( |
89
|
|
|
$this, |
90
|
|
|
'paginatorBottomRender', |
91
|
|
|
), |
92
|
|
|
array( |
93
|
|
|
'is_safe' => array('html'), |
94
|
|
|
) |
95
|
|
|
), |
96
|
|
|
new \Twig_SimpleFunction( |
97
|
|
|
'paginator_sortable', |
98
|
|
|
array( |
99
|
|
|
$this, |
100
|
|
|
'paginatorSortable', |
101
|
|
|
), |
102
|
|
|
array( |
103
|
|
|
'is_safe' => array('html'), |
104
|
|
|
) |
105
|
|
|
), |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $pagination |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function paginatorPaginationRender($pagination) |
115
|
|
|
{ |
116
|
|
|
if (!$pagination) { |
117
|
|
|
return ''; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$output = ''; |
121
|
|
|
|
122
|
|
|
$paginationData = $pagination->getPaginationData(); |
123
|
|
|
$maxPageRange = isset($paginationData['pageRangeLimit']) |
124
|
|
|
? intval($paginationData['pageRangeLimit']) |
125
|
|
|
: 10 |
126
|
|
|
; |
127
|
|
|
$route = $paginationData['route']; |
128
|
|
|
$routeParameters = $this->app['request']->query->all(); |
129
|
|
|
if (isset($paginationData['routeParameters'])) { |
130
|
|
|
$routeParameters = array_merge( |
131
|
|
|
$routeParameters, |
132
|
|
|
$paginationData['routeParameters'] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
$pageCount = ceil( |
136
|
|
|
intval($paginationData['totalCount']) / |
137
|
|
|
intval($paginationData['numItemsPerPage']) |
138
|
|
|
); |
139
|
|
|
$currentPage = intval($paginationData['current']); |
140
|
|
|
|
141
|
|
|
if ($pageCount > 1) { |
142
|
|
|
$pageRange = range(1, $pageCount); |
143
|
|
|
|
144
|
|
|
// Page range by max page numbers |
145
|
|
|
$pageRangeTmp = array(); |
146
|
|
|
$rangeFrom = $currentPage - ceil($maxPageRange / 2); |
147
|
|
|
$rangeTo = $currentPage + ceil($maxPageRange / 2); |
148
|
|
|
|
149
|
|
|
foreach (range($rangeFrom, $rangeTo) as $singleRangePage) { |
150
|
|
|
if (in_array($singleRangePage, $pageRange)) { |
151
|
|
|
$pageRangeTmp[] = $singleRangePage; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$pageRange = $pageRangeTmp; |
156
|
|
|
// Page range by max page numbers /END |
157
|
|
|
|
158
|
|
|
// Prev |
159
|
|
View Code Duplication |
if ($currentPage > 1) { |
|
|
|
|
160
|
|
|
$routeParameters = array_merge( |
161
|
|
|
$routeParameters, |
162
|
|
|
array( |
163
|
|
|
$pagination->getPaginatorOption('pageParameterName') => $currentPage - 1, |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$prevUrl = $this->app['url_generator']->generate( |
168
|
|
|
$route, |
169
|
|
|
$routeParameters |
170
|
|
|
); |
171
|
|
|
} else { |
172
|
|
|
$prevUrl = '#'; |
173
|
|
|
} |
174
|
|
|
// Prev /END |
175
|
|
|
|
176
|
|
|
// Next |
177
|
|
View Code Duplication |
if ($currentPage < $pageCount) { |
|
|
|
|
178
|
|
|
$routeParameters = array_merge( |
179
|
|
|
$routeParameters, |
180
|
|
|
array( |
181
|
|
|
$pagination->getPaginatorOption('pageParameterName') => $currentPage + 1, |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$nextUrl = $this->app['url_generator']->generate( |
186
|
|
|
$route, |
187
|
|
|
$routeParameters |
188
|
|
|
); |
189
|
|
|
} else { |
190
|
|
|
$nextUrl = '#'; |
191
|
|
|
} |
192
|
|
|
// Next /END |
193
|
|
|
|
194
|
|
|
$output = $this->app['twig']->render( |
195
|
|
|
'twig/paginator/pagination.html.twig', |
196
|
|
|
array( |
197
|
|
|
'app' => $this->app, |
198
|
|
|
'prevUrl' => $prevUrl, |
199
|
|
|
'nextUrl' => $nextUrl, |
200
|
|
|
'pageRange' => $pageRange, |
201
|
|
|
'routeParameters' => $routeParameters, |
202
|
|
|
'pagination' => $pagination, |
203
|
|
|
'route' => $route, |
204
|
|
|
'currentPage' => $currentPage, |
205
|
|
|
'pageCount' => $pageCount, |
206
|
|
|
) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $output; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $pagination |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function paginatorResultsTextRender($pagination) |
219
|
|
|
{ |
220
|
|
|
if (!$pagination) { |
221
|
|
|
return ''; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$output = ''; |
225
|
|
|
|
226
|
|
|
$paginationData = $pagination->getPaginationData(); |
227
|
|
|
$pageCount = ceil( |
228
|
|
|
intval($paginationData['totalCount']) / |
229
|
|
|
intval($paginationData['numItemsPerPage']) |
230
|
|
|
); |
231
|
|
|
$currentPage = intval($paginationData['current']); |
232
|
|
|
$total = $paginationData['totalCount']; |
233
|
|
|
|
234
|
|
|
if ($total > 0 && $currentPage <= $pageCount) { |
235
|
|
|
$from = (($currentPage - 1) * $paginationData['numItemsPerPage']) + 1; |
236
|
|
|
$to = $currentPage * $paginationData['numItemsPerPage'] > $total |
237
|
|
|
? $total |
238
|
|
|
: $currentPage * $paginationData['numItemsPerPage'] |
239
|
|
|
; |
240
|
|
|
|
241
|
|
|
$output = $this->app['twig']->render( |
242
|
|
|
'twig/paginator/results-text.html.twig', |
243
|
|
|
array( |
244
|
|
|
'app' => $this->app, |
245
|
|
|
'from' => $from, |
246
|
|
|
'to' => $to, |
247
|
|
|
'total' => $total, |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $output; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param $pagination |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
public function paginatorBottomRender($pagination) |
261
|
|
|
{ |
262
|
|
|
return $this->app['twig']->render( |
263
|
|
|
'twig/paginator/_bottom.html.twig', |
264
|
|
|
array( |
265
|
|
|
'pagination' => $pagination, |
266
|
|
|
) |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param $pagination |
272
|
|
|
* |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
|
public function paginatorLimitPerPageRender($pagination) |
276
|
|
|
{ |
277
|
|
|
if (!$pagination) { |
278
|
|
|
return ''; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$output = ''; |
282
|
|
|
|
283
|
|
|
$paginationData = $pagination->getPaginationData(); |
284
|
|
|
|
285
|
|
|
if ($paginationData['totalCount'] > 0) { |
286
|
|
|
$output = $this->app['twig']->render( |
287
|
|
|
'twig/paginator/limit-per-page.html.twig', |
288
|
|
|
array( |
289
|
|
|
'app' => $this->app, |
290
|
|
|
'pagination' => $pagination, |
291
|
|
|
) |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $output; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param $pagination |
300
|
|
|
* |
301
|
|
|
* @return string |
302
|
|
|
*/ |
303
|
|
|
public function paginatorSearchRender($pagination) |
304
|
|
|
{ |
305
|
|
|
return $this->app['twig']->render( |
306
|
|
|
'twig/paginator/search.html.twig', |
307
|
|
|
array( |
308
|
|
|
'app' => $this->app, |
309
|
|
|
'pagination' => $pagination, |
310
|
|
|
) |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param $pagination |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
|
|
public function paginatorTopRender($pagination) |
320
|
|
|
{ |
321
|
|
|
return $this->app['twig']->render( |
322
|
|
|
'twig/paginator/_top.html.twig', |
323
|
|
|
array( |
324
|
|
|
'pagination' => $pagination, |
325
|
|
|
) |
326
|
|
|
); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param $pagination |
331
|
|
|
* @param $text |
332
|
|
|
* @param $key |
333
|
|
|
* |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function paginatorSortable($pagination, $text = '', $key = '') |
337
|
|
|
{ |
338
|
|
|
if (!$pagination) { |
339
|
|
|
return ''; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$text = $this->app['translator']->trans($text); |
343
|
|
|
$sortDirectionParameterName = $pagination->getPaginatorOption('sortDirectionParameterName'); |
344
|
|
|
$direction = isset($sortDirectionParameterName) |
345
|
|
|
? $this->app['request']->query->get($sortDirectionParameterName) |
346
|
|
|
: 'asc' |
347
|
|
|
; |
348
|
|
|
|
349
|
|
|
$direction = $direction == 'asc' |
350
|
|
|
? 'desc' |
351
|
|
|
: 'asc' |
352
|
|
|
; |
353
|
|
|
|
354
|
|
|
$paginationData = $pagination->getPaginationData(); |
355
|
|
|
$route = $paginationData['route']; |
356
|
|
|
$routeParameters = $this->app['request']->query->all(); |
357
|
|
|
if (isset($paginationData['routeParameters'])) { |
358
|
|
|
$routeParameters = array_merge( |
359
|
|
|
$routeParameters, |
360
|
|
|
$paginationData['routeParameters'] |
361
|
|
|
); |
362
|
|
|
} |
363
|
|
|
$routeParameters = array_merge( |
364
|
|
|
$routeParameters, |
365
|
|
|
array( |
366
|
|
|
$pagination->getPaginatorOption('pageParameterName') => 1, |
367
|
|
|
$pagination->getPaginatorOption('sortFieldParameterName') => $key, |
368
|
|
|
$pagination->getPaginatorOption('sortDirectionParameterName') => $direction, |
369
|
|
|
) |
370
|
|
|
); |
371
|
|
|
|
372
|
|
|
$url = $this->app['url_generator']->generate( |
373
|
|
|
$route, |
374
|
|
|
$routeParameters |
375
|
|
|
); |
376
|
|
|
|
377
|
|
|
$icon = $direction == 'asc' |
378
|
|
|
? 'down' |
379
|
|
|
: 'up' |
380
|
|
|
; |
381
|
|
|
|
382
|
|
|
$showIcon = $this->app['request']->query->get( |
383
|
|
|
$pagination->getPaginatorOption('sortFieldParameterName'), |
384
|
|
|
$paginationData['defaultSortFieldName'] |
385
|
|
|
) == $key |
386
|
|
|
? true |
387
|
|
|
: false |
388
|
|
|
; |
389
|
|
|
|
390
|
|
|
return '<a href="'.$url.'">'. |
391
|
|
|
$text. |
392
|
|
|
($showIcon ? ' <i class="fa fa-chevron-'.$icon.'"></i>' : ''). |
393
|
|
|
'</a>'; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.