1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Pagination;
15
16 use \Slimore\Debug as Debug;
17
18 19 20 21 22 23 24
25
26 class Paginator
27 {
28 public $sql;
29 public $num;
30 public $page;
31 public $prev;
32 public $next;
33 public $last;
34 public $first;
35 public $total;
36 public $range;
37 public $ranges;
38 public $offset;
39 public $pageTotal;
40 public $baseUrl = '';
41 public $select = '*';
42 public $where = [];
43 public $orderBy = 'id';
44 public $sortBy = 'ASC';
45 public $model;
46 public $query;
47 private $params;
48 private $data = [];
49
50 51 52 53 54 55 56 57 58 59
60
61 public function __construct(\Slimore\Mvc\Model $model, $page, $total, $num = 10, $range = 6)
62 {
63 $this->num = $num;
64 $this->page = $page;
65 $this->total = $total;
66 $this->range = $range;
67 $this->model = $model;
68 $this->first = 1;
69
70 $this->make();
71 }
72
73 74 75 76 77
78
79 public function make()
80 {
81 $this->pageTotal = ceil($this->total / $this->num);
82 $this->page = ($this->page > $this->pageTotal) ? $this->pageTotal : $this->page;
83 $this->offset = ($this->page == 1) ? 0 : (($this->page - 1) * $this->num);
84 $this->prev = ($this->page == 1) ? 1 : $this->page - 1;
85 $this->next = ($this->page == $this->pageTotal) ? $this->pageTotal : $this->page + 1;
86 $this->last = $this->pageTotal;
87
88 $this->range();
89 }
90
91 92 93 94 95
96
97 public function range()
98 {
99 $start = $this->page - $this->range;
100 $start = ($start < 1) ? 1 : $start;
101 $end = $this->page + $this->range;
102 $end = ($end > $this->pageTotal) ? $this->pageTotal : $end;
103
104 $this->ranges = [
105 'start' => $start,
106 'end' => $end
107 ];
108
109 return $this->ranges;
110 }
111
112 113 114 115 116 117
118
119 public function model(\Slimore\Mvc\Model $model)
120 {
121 $this->model = $model;
122
123 return $model;
124 }
125
126 127 128 129 130 131
132
133 public function query()
134 {
135 $model = $this->model;
136
137 $this->query = $model->select($this->select)
138 ->where($this->where)
139 ->orderBy($this->orderBy, $this->sortBy)
140 ->take($this->num)
141 ->offset($this->offset);
142
143 $this->sql = $this->query->getQuery()->toSql();
144
145 $this->data = $this->query->get()->toArray();
146
147 return $this->data;
148 }
149
150 151 152
153
154 public function getQuery()
155 {
156 return $this->query;
157 }
158
159 160 161
162
163 public function getSql()
164 {
165 return $this->sql;
166 }
167
168 169 170 171
172
173 public function params($sql = false)
174 {
175 $this->params = [
176 'sql' => ($sql) ? $this->sql : '',
177 'num' => $this->num,
178 'data' => $this->data,
179 'page' => $this->page,
180 'prev' => $this->prev,
181 'next' => $this->next,
182 'last' => $this->last,
183 'first' => $this->first,
184 'range' => $this->range,
185 'total' => $this->total,
186 'ranges' => $this->ranges,
187 'offset' => $this->offset,
188 'baseUrl' => $this->baseUrl,
189 'pageTotal' => $this->pageTotal
190 ];
191
192 return $this->params;
193 }
194
195 196 197
198
199 public function debug($sql = true)
200 {
201 Debug::printr($this->params($sql));
202 }
203
204 205 206 207
208
209 public function json($sql = true, $return = false)
210 {
211 Debug::json($this->params($sql), $return);
212 }
213 }