|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Navigator\Pagination; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Database\Query\Select as SelectQuery; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AbstractPaginator |
|
9
|
|
|
* @package Nip\Records\Navigator\Pagination |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractPaginator |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var SelectQuery |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $query = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $page = 1; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $itemsPerPage = 20; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var null|int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $count = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $pages; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param SelectQuery $query |
|
41
|
|
|
* @return SelectQuery |
|
42
|
|
|
*/ |
|
43
|
|
|
public function paginate($query) |
|
44
|
|
|
{ |
|
45
|
|
|
$query->limit($this->getLimitStart(), $this->getItemsPerPage()); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->setQuery($query); |
|
48
|
|
|
|
|
49
|
|
|
return $query; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return SelectQuery |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getQuery() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->query; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param SelectQuery $query |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setQuery($query) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->query = $query; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @deprecated Method is called automatically on getCount |
|
70
|
|
|
*/ |
|
71
|
|
|
public function count() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->getCount(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Does the count for all records |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function doCount() |
|
80
|
|
|
{ |
|
81
|
|
|
$query = $this->getCountQuery(); |
|
82
|
|
|
$result = $query->execute()->fetchResult(); |
|
83
|
|
|
|
|
84
|
|
|
$this->count = intval($result['count']); |
|
85
|
|
|
$this->pages = intval($this->count / $this->itemsPerPage); |
|
86
|
|
|
|
|
87
|
|
|
if ($this->count % $this->itemsPerPage != 0) { |
|
88
|
|
|
$this->pages++; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($this->pages == 0) { |
|
92
|
|
|
$this->pages = 1; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return SelectQuery |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getCountQuery() |
|
100
|
|
|
{ |
|
101
|
|
|
$query = clone $this->getQuery(); |
|
102
|
|
|
$query->setCols(); |
|
|
|
|
|
|
103
|
|
|
$query->count('*', 'count'); |
|
104
|
|
|
$query->limit(1); |
|
105
|
|
|
return $query; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param bool $page |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setPage($page = false) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($page) { |
|
115
|
|
|
$this->page = $page; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getPage() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->page; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getPages() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->pages; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param $items |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setItemsPerPage($items) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($items > 0) { |
|
143
|
|
|
$this->itemsPerPage = $items; |
|
144
|
|
|
} |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return int |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getItemsPerPage() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->itemsPerPage; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return float|int |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getLimitStart() |
|
160
|
|
|
{ |
|
161
|
|
|
return ($this->getPage() - 1) * $this->getItemsPerPage(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return int |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getCount() |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->count === null) { |
|
170
|
|
|
$this->doCount(); |
|
171
|
|
|
} |
|
172
|
|
|
return $this->count; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|