1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Utils; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
13
|
|
|
use Slick\Filter\StaticFilter; |
14
|
|
|
use Slick\Http\PhpEnvironment\Request; |
15
|
|
|
use Slick\Validator\StaticValidator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Pagination utility |
19
|
|
|
* |
20
|
|
|
* @package Slick\Mvc\Utils |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @property Request $request |
24
|
|
|
* @property integer $offset |
25
|
|
|
* @property integer $rowsPerPage |
26
|
|
|
* @property integer $pages |
27
|
|
|
* @property integer $total |
28
|
|
|
* @property integer $current |
29
|
|
|
*/ |
30
|
|
|
class Pagination extends Base |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @readwrite |
35
|
|
|
* @var int Total pages |
36
|
|
|
*/ |
37
|
|
|
protected $pages = 0; |
38
|
|
|
/** |
39
|
|
|
* @readwrite |
40
|
|
|
* @var int Total records |
41
|
|
|
*/ |
42
|
|
|
protected $total; |
43
|
|
|
/** |
44
|
|
|
* @readwrite |
45
|
|
|
* @var int current page index |
46
|
|
|
*/ |
47
|
|
|
protected $current = 1; |
48
|
|
|
/** |
49
|
|
|
* @readwrite |
50
|
|
|
* @var int total rows per page |
51
|
|
|
*/ |
52
|
|
|
protected $rowsPerPage = 12; |
53
|
|
|
/** |
54
|
|
|
* @readwrite |
55
|
|
|
* @var int First row to return |
56
|
|
|
*/ |
57
|
|
|
protected $offset = 0; |
58
|
|
|
/** |
59
|
|
|
* @readwrite |
60
|
|
|
* @var Request |
61
|
|
|
*/ |
62
|
|
|
protected $request; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Overrides the constructor to calculate the properties for current |
66
|
|
|
* pagination state. |
67
|
|
|
* |
68
|
|
|
* @param array $options |
69
|
|
|
*/ |
70
|
18 |
|
public function __construct($options = array()) |
71
|
|
|
{ |
72
|
18 |
|
parent::__construct($options); |
73
|
18 |
|
if ($this->getRequest()->getQuery('rows')) { |
74
|
10 |
|
$this->setRowsPerPage($this->getRequest()->getQuery('rows')); |
75
|
5 |
|
} |
76
|
18 |
|
if ($this->getRequest()->getQuery('page')) { |
77
|
10 |
|
$this->setCurrent($this->getRequest()->getQuery('page')); |
78
|
5 |
|
} |
79
|
18 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Lazy loads request object |
83
|
|
|
* |
84
|
|
|
* @return Request |
85
|
|
|
*/ |
86
|
18 |
|
public function getRequest() |
87
|
|
|
{ |
88
|
18 |
|
if (is_null($this->request)) { |
89
|
6 |
|
$this->request = new Request(); |
90
|
3 |
|
} |
91
|
18 |
|
return $this->request; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets current page and calculates offset |
96
|
|
|
* |
97
|
|
|
* @param integer $value The number of the current page. |
98
|
|
|
* |
99
|
|
|
* @return Pagination A self instance for method chain calls |
100
|
|
|
*/ |
101
|
10 |
View Code Duplication |
public function setCurrent($value) |
|
|
|
|
102
|
|
|
{ |
103
|
10 |
|
if (StaticValidator::validates('number', $value)) { |
104
|
10 |
|
$this->current = StaticFilter::filter('number',$value); |
105
|
10 |
|
$this->offset = $this->rowsPerPage * ($this->current - 1); |
|
|
|
|
106
|
5 |
|
} |
107
|
10 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the total rows to paginate. |
112
|
|
|
* |
113
|
|
|
* @param integer $value The rows total to set. |
114
|
|
|
* |
115
|
|
|
* @return Pagination A self instance for method chain calls |
116
|
|
|
*/ |
117
|
4 |
View Code Duplication |
public function setTotal($value) |
|
|
|
|
118
|
|
|
{ |
119
|
4 |
|
if (StaticValidator::validates('number', $value)) { |
120
|
4 |
|
$this->total = StaticFilter::filter('number',$value); |
121
|
4 |
|
$this->pages = ceil($this->total / $this->rowsPerPage); |
|
|
|
|
122
|
2 |
|
} |
123
|
4 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the total rows per page. |
128
|
|
|
* |
129
|
|
|
* @param integer $value The total rows per page to set. |
130
|
|
|
* |
131
|
|
|
* @return Pagination A self instance for method chain calls |
132
|
|
|
*/ |
133
|
14 |
View Code Duplication |
public function setRowsPerPage($value) |
|
|
|
|
134
|
|
|
{ |
135
|
14 |
|
if (StaticValidator::validates('number', $value)) { |
136
|
14 |
|
$this->rowsPerPage = StaticFilter::filter('number',$value); |
137
|
14 |
|
$this->pages = ceil($this->total / $this->rowsPerPage); |
|
|
|
|
138
|
7 |
|
} |
139
|
14 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Creates a request query for the provided page. |
144
|
|
|
* |
145
|
|
|
* This method check the current request query in order to maintain the |
146
|
|
|
* other parameters unchanged and sets the 'page' parameter to the |
147
|
|
|
* provided page number. |
148
|
|
|
* |
149
|
|
|
* @param integer $page The page number to build the query on. |
150
|
|
|
* |
151
|
|
|
* @return string The query string to use in the pagination links. |
152
|
|
|
*/ |
153
|
2 |
|
public function pageUrl($page) |
154
|
|
|
{ |
155
|
2 |
|
$params = $this->getRequest()->getQuery(); |
156
|
2 |
|
if (isset($params['url'])) |
157
|
1 |
|
unset($params['url']); |
158
|
2 |
|
$params['page'] = $page; |
159
|
2 |
|
return '?' . http_build_query($params); |
160
|
|
|
} |
161
|
|
|
} |
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.