forked from jorgelive/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplePager.php
174 lines (151 loc) · 4.5 KB
/
SimplePager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Datagrid;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Class SimplePager
*
* @package Sonata\AdminBundle\Datagrid
* @author Lukas Kahwe Smith <[email protected]>
* @author Sjoerd Peters <[email protected]>
*/
class SimplePager extends Pager
{
/**
* @var boolean
*/
protected $haveToPaginate;
/**
* How many pages to look forward to create links to next pages.
*
* @var int
*/
protected $threshold;
/**
* @var int
*/
protected $thresholdCount;
/**
* The threshold parameter can be used to determine how far ahead the pager
* should fetch results.
*
* If set to 1 which is the minimal value the pager will generate a link to the next page
* If set to 2 the pager will generate links to the next two pages
* If set to 3 the pager will generate links to the next three pages
* etc.
*
* @param integer $maxPerPage Number of records to display per page
* @param int $threshold
*/
public function __construct($maxPerPage = 10, $threshold = 1)
{
parent::__construct($maxPerPage);
$this->setThreshold($threshold);
}
/**
* Returns the exact count when there is only one page or when the current
* equals the last page.
*
* In all other cases an estimate of the total count is returned.
*
* @return integer
*/
public function getNbResults()
{
$n = ceil(($this->getLastPage() -1) * $this->getMaxPerPage());
if ($this->getLastPage() == $this->getPage()) {
return $n + $this->thresholdCount;
}
return $n;
}
/**
* Get all the results for the pager instance
*
* @param mixed $hydrationMode A hydration mode identifier
*
* @return array
*/
public function getResults($hydrationMode = null)
{
if ($this->results) {
return $this->results;
}
$this->results = $this->getQuery()->execute(array(), $hydrationMode);
$this->thresholdCount = count($this->results);
if (count($this->results) > $this->getMaxPerPage()) {
$this->haveToPaginate = true;
if ($this->results instanceof ArrayCollection) {
$this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
} else {
$this->results = new ArrayCollection(array_slice($this->results, 0, $this->getMaxPerPage()));
}
} else {
$this->haveToPaginate = false;
}
return $this->results;
}
/**
* {@inheritDoc}
*/
public function haveToPaginate()
{
return $this->haveToPaginate || $this->getPage() > 1;
}
/**
* {@inheritDoc}
*/
protected function resetIterator()
{
parent::resetIterator();
$this->haveToPaginate = false;
}
/**
* {@inheritDoc}
*
* @throws \RuntimeException the QueryBuilder is uninitialized.
*/
public function init()
{
if (!$this->getQuery()) {
throw new \RuntimeException('Uninitialized QueryBuilder');
}
$this->resetIterator();
if (0 == $this->getPage() || 0 == $this->getMaxPerPage()) {
$this->setLastPage(0);
$this->getQuery()->setFirstResult(0);
$this->getQuery()->setMaxResults(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$this->getQuery()->setFirstResult($offset);
$maxOffset = $this->getThreshold() > 0
? $this->getMaxPerPage() * $this->threshold + 1 : $this->getMaxPerPage() + 1;
$this->getQuery()->setMaxResults($maxOffset);
$this->initializeIterator();
$t = (int) ceil($this->thresholdCount / $this->getMaxPerPage()) + $this->getPage() - 1;
$this->setLastPage($t);
}
}
/**
* Set how many pages to look forward to create links to next pages.
*
* @param int $threshold
*/
public function setThreshold($threshold)
{
$this->threshold = (int) $threshold;
}
/**
* @return int
*/
public function getThreshold()
{
return $this->threshold;
}
}