These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\AdminBundle\Datagrid; |
||
15 | |||
16 | use Doctrine\Common\Collections\ArrayCollection; |
||
17 | |||
18 | /** |
||
19 | * @final since sonata-project/admin-bundle 3.52 |
||
20 | * |
||
21 | * @author Lukas Kahwe Smith <[email protected]> |
||
22 | * @author Sjoerd Peters <[email protected]> |
||
23 | */ |
||
24 | class SimplePager extends Pager |
||
25 | { |
||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $haveToPaginate; |
||
30 | |||
31 | /** |
||
32 | * How many pages to look forward to create links to next pages. |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $threshold; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $thresholdCount; |
||
42 | |||
43 | /** |
||
44 | * The threshold parameter can be used to determine how far ahead the pager |
||
45 | * should fetch results. |
||
46 | * |
||
47 | * If set to 1 which is the minimal value the pager will generate a link to the next page |
||
48 | * If set to 2 the pager will generate links to the next two pages |
||
49 | * If set to 3 the pager will generate links to the next three pages |
||
50 | * etc. |
||
51 | * |
||
52 | * @param int $maxPerPage Number of records to display per page |
||
53 | * @param int $threshold |
||
54 | */ |
||
55 | public function __construct($maxPerPage = 10, $threshold = 1) |
||
56 | { |
||
57 | parent::__construct($maxPerPage); |
||
58 | $this->setThreshold($threshold); |
||
59 | } |
||
60 | |||
61 | public function getNbResults() |
||
62 | { |
||
63 | $n = ($this->getLastPage() - 1) * $this->getMaxPerPage(); |
||
64 | if ($this->getLastPage() === $this->getPage()) { |
||
65 | return $n + $this->thresholdCount; |
||
66 | } |
||
67 | |||
68 | return $n; |
||
69 | } |
||
70 | |||
71 | public function getResults($hydrationMode = null) |
||
72 | { |
||
73 | if ($this->results) { |
||
74 | return $this->results; |
||
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
75 | } |
||
76 | |||
77 | $this->results = $this->getQuery()->execute([], $hydrationMode); |
||
78 | $this->thresholdCount = \count($this->results); |
||
79 | if (\count($this->results) > $this->getMaxPerPage()) { |
||
80 | $this->haveToPaginate = true; |
||
81 | // doctrine/phpcr-odm returns ArrayCollection |
||
82 | if ($this->results instanceof ArrayCollection) { |
||
83 | $this->results = $this->results->toArray(); |
||
84 | } |
||
85 | $this->results = \array_slice($this->results, 0, $this->getMaxPerPage()); |
||
86 | } else { |
||
87 | $this->haveToPaginate = false; |
||
88 | } |
||
89 | |||
90 | return $this->results; |
||
91 | } |
||
92 | |||
93 | public function haveToPaginate() |
||
94 | { |
||
95 | return $this->haveToPaginate || $this->getPage() > 1; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | * |
||
101 | * @throws \RuntimeException the QueryBuilder is uninitialized |
||
102 | */ |
||
103 | public function init(): void |
||
104 | { |
||
105 | if (!$this->getQuery()) { |
||
106 | throw new \RuntimeException('Uninitialized QueryBuilder'); |
||
107 | } |
||
108 | $this->resetIterator(); |
||
109 | |||
110 | if (0 === $this->getPage() || 0 === $this->getMaxPerPage()) { |
||
111 | $this->setLastPage(0); |
||
112 | $this->getQuery()->setFirstResult(0); |
||
113 | $this->getQuery()->setMaxResults(0); |
||
114 | } else { |
||
115 | $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); |
||
116 | $this->getQuery()->setFirstResult($offset); |
||
117 | |||
118 | $maxOffset = $this->getThreshold() > 0 |
||
119 | ? $this->getMaxPerPage() * $this->threshold + 1 : $this->getMaxPerPage() + 1; |
||
120 | |||
121 | $this->getQuery()->setMaxResults($maxOffset); |
||
122 | $this->initializeIterator(); |
||
123 | |||
124 | $t = (int) ceil($this->thresholdCount / $this->getMaxPerPage()) + $this->getPage() - 1; |
||
125 | $this->setLastPage(max(1, $t)); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Set how many pages to look forward to create links to next pages. |
||
131 | * |
||
132 | * @param int $threshold |
||
133 | */ |
||
134 | public function setThreshold($threshold): void |
||
135 | { |
||
136 | $this->threshold = (int) $threshold; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function getThreshold() |
||
143 | { |
||
144 | return $this->threshold; |
||
145 | } |
||
146 | |||
147 | protected function resetIterator(): void |
||
148 | { |
||
149 | parent::resetIterator(); |
||
150 | $this->haveToPaginate = false; |
||
151 | } |
||
152 | } |
||
153 |