-
Notifications
You must be signed in to change notification settings - Fork 10
/
EDTPagination.php
154 lines (138 loc) · 4.38 KB
/
EDTPagination.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
<?php
/**
* EDTPagination class file.
*
* @author Jan Was <[email protected]>
*
* @link http://www.yiiframework.com/
*
* @copyright Copyright © 2011-2012 Jan Was
* @license http://www.yiiframework.com/license/
*/
/**
* EDTPagination represents information relevant to pagination.
*
* @see CPagination
*/
class EDTPagination extends CPagination
{
/**
* The default page size.
*/
const DEFAULT_PAGE_SIZE = 25;
/**
* @var string name of the GET variable storing the current page index. Defaults to 'page'.
*/
public $pageVar = 'iDisplayStart';
/**
* @var string
*/
public $lengthVar = 'iDisplayLength';
private $_pageSize = self::DEFAULT_PAGE_SIZE;
private $_itemCount = 0;
private $_currentPage;
/**
* @return int number of items in each page. Defaults to 10.
*/
public function getPageSize($recalculate = true)
{
if ($this->_pageSize === self::DEFAULT_PAGE_SIZE || $recalculate) {
if (isset($_GET[$this->lengthVar])) {
if (($this->_pageSize = (int) $_GET[$this->lengthVar]) <= 0) {
$this->_pageSize = self::DEFAULT_PAGE_SIZE;
}
} else {
$this->_pageSize = self::DEFAULT_PAGE_SIZE;
}
}
return $this->_pageSize;
}
/**
* @param int $value number of items in each page
*/
public function setPageSize($value)
{
if (($this->_pageSize = $value) <= 0) {
$this->_pageSize = self::DEFAULT_PAGE_SIZE;
}
$_GET[$this->lengthVar] = $this->_pageSize;
}
/**
* @return int total number of items. Defaults to 0.
*/
public function getItemCount()
{
return $this->_itemCount;
}
/**
* @param int $value total number of items.
*/
public function setItemCount($value)
{
if (($this->_itemCount = $value) < 0) {
$this->_itemCount = 0;
}
}
/**
* @param bool $recalculate whether to recalculate the current page based on the page size and item count.
*
* @return int the zero-based index of the current page. Defaults to 0.
*/
public function getCurrentPage($recalculate = true)
{
if ($this->_currentPage === null || $recalculate) {
if (isset($_GET[$this->pageVar])) {
$this->_currentPage = floor(intval($_GET[$this->pageVar]) / $this->getPageSize());
if ($this->validateCurrentPage) {
$pageCount = $this->getPageCount();
if ($this->_currentPage >= $pageCount) {
$this->_currentPage = $pageCount - 1;
}
}
if ($this->_currentPage < 0) {
$this->_currentPage = 0;
}
} else {
$this->_currentPage = 0;
}
}
return $this->_currentPage;
}
/**
* @param int $value the zero-based index of the current page.
*/
public function setCurrentPage($value)
{
$this->_currentPage = $value;
$_GET[$this->pageVar] = $value + 1;
}
/**
* @return int number of pages
*/
public function getPageCount()
{
return (int) (($this->_itemCount + $this->_pageSize - 1) / $this->_pageSize);
}
/**
* Creates the URL suitable for pagination.
* This method is mainly called by pagers when creating URLs used to
* perform pagination. The default implementation is to call
* the controller's createUrl method with the page information.
* You may override this method if your URL scheme is not the same as
* the one supported by the controller's createUrl method.
*
* @param CController $controller the controller that will create the actual URL
* @param int $page the page that the URL should point to. This is a zero-based index.
*
* @return string the created URL
*/
public function createPageUrl($controller, $page)
{
$params = $this->params === null ? $_GET : $this->params;
$params[$this->pageVar] = $page * $this->getPageSize(true);
if (isset($params[$this->lengthVar]) && $params[$this->lengthVar] === self::DEFAULT_PAGE_SIZE) {
unset($params[$this->lengthVar]);
}
return $controller->createUrl($this->route, $params);
}
}