|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Pagination; |
|
18
|
|
|
|
|
19
|
|
|
use Knp\Component\Pager\Paginator as BasePaginator; |
|
20
|
|
|
use Knp\Component\Pager\Event; |
|
21
|
|
|
|
|
22
|
|
|
class Paginator extends BasePaginator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
* |
|
27
|
|
|
* IMPORTANT: This method expects OFFSET value instead PAGE |
|
28
|
|
|
*/ |
|
29
|
|
|
public function paginate($target, $offset = 1, $limit = 10, array $options = array()) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$limit = intval(abs($limit)); |
|
32
|
|
|
if (!$limit) { |
|
33
|
|
|
throw new \LogicException('Invalid item per page number, must be a positive number'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$page = intval(round($offset / $limit), 10); |
|
37
|
|
|
if ($page < 1) { |
|
38
|
|
|
$page = 1; |
|
39
|
|
|
} |
|
40
|
|
|
$options = array_merge($this->defaultOptions, $options); |
|
41
|
|
|
|
|
42
|
|
|
// normalize default sort field |
|
43
|
|
|
if (isset($options['defaultSortFieldName']) && is_array($options['defaultSortFieldName'])) { |
|
44
|
|
|
$options['defaultSortFieldName'] = implode('+', $options['defaultSortFieldName']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// default sort field and direction are set based on options (if available) |
|
48
|
|
|
if (!isset($_GET[$options['sortFieldParameterName']]) && isset($options['defaultSortFieldName'])) { |
|
49
|
|
|
$_GET[$options['sortFieldParameterName']] = $options['defaultSortFieldName']; |
|
50
|
|
|
|
|
51
|
|
|
if (!isset($_GET[$options['sortDirectionParameterName']])) { |
|
52
|
|
|
$_GET[$options['sortDirectionParameterName']] = isset($options['defaultSortDirection']) ? $options['defaultSortDirection'] : 'asc'; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// before pagination start |
|
57
|
|
|
$beforeEvent = new Event\BeforeEvent($this->eventDispatcher); |
|
58
|
|
|
$this->eventDispatcher->dispatch('knp_pager.before', $beforeEvent); |
|
59
|
|
|
// items |
|
60
|
|
|
$itemsEvent = new Event\ItemsEvent($offset, $limit); |
|
61
|
|
|
$itemsEvent->options = &$options; |
|
62
|
|
|
$itemsEvent->target = &$target; |
|
63
|
|
|
$this->eventDispatcher->dispatch('knp_pager.items', $itemsEvent); |
|
64
|
|
|
if (!$itemsEvent->isPropagationStopped()) { |
|
65
|
|
|
throw new \RuntimeException('One of listeners must count and slice given target'); |
|
66
|
|
|
} |
|
67
|
|
|
// pagination initialization event |
|
68
|
|
|
$paginationEvent = new Event\PaginationEvent(); |
|
69
|
|
|
$paginationEvent->target = &$target; |
|
70
|
|
|
$paginationEvent->options = &$options; |
|
71
|
|
|
$this->eventDispatcher->dispatch('knp_pager.pagination', $paginationEvent); |
|
72
|
|
|
if (!$paginationEvent->isPropagationStopped()) { |
|
73
|
|
|
throw new \RuntimeException('One of listeners must create pagination view'); |
|
74
|
|
|
} |
|
75
|
|
|
// pagination class can be different, with different rendering methods |
|
76
|
|
|
$paginationView = $paginationEvent->getPagination(); |
|
77
|
|
|
$paginationView->setCustomParameters($itemsEvent->getCustomPaginationParameters()); |
|
78
|
|
|
$paginationView->setCurrentPageNumber($page); |
|
79
|
|
|
$paginationView->setItemNumberPerPage($limit); |
|
80
|
|
|
$paginationView->setTotalItemCount($itemsEvent->count); |
|
81
|
|
|
$paginationView->setPaginatorOptions($options); |
|
|
|
|
|
|
82
|
|
|
$paginationView->setItems($itemsEvent->items); |
|
83
|
|
|
|
|
84
|
|
|
// after |
|
85
|
|
|
$afterEvent = new Event\AfterEvent($paginationView); |
|
86
|
|
|
$this->eventDispatcher->dispatch('knp_pager.after', $afterEvent); |
|
87
|
|
|
|
|
88
|
|
|
return $paginationView; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: