-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaginator.php
More file actions
97 lines (89 loc) · 2.1 KB
/
Paginator.php
File metadata and controls
97 lines (89 loc) · 2.1 KB
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
<?php
/**
* Paginator
* @author Shilov Vasiliy
*/
class Paginator {
private $page;
private $limit;
private $max;
private $total;
public function init($page = 0, $limit = 10, $max = 10, $total = 0) {
$o = new self();
$o->page = $page;
$o->limit = $limit;
$o->max = $max-1;
$o->total = $total;
return $o->get();
}
public function get() {
if ($this->limit >= $this->total)
return NULL;
elseif ($this->total / $this->limit < $this->max)
return $this->offFirstLast();
else
return $this->onFirstLast();
}
private function offFirstLast() {
$return = array();
if ($this->page > 0)
$return['prev'] = array(
'page' => $this->page - 1,
'current' => false,
);
for ($p = 0; $p < ceil($this->total / $this->limit); $p++) {
$return[$p + 1] = array(
'page' => $p,
'current' => ($p == $this->page) ? true : false,
);
}
if ($this->page < ceil($this->total / $this->limit)-1)
$return['next'] = array(
'page' => $this->page + 1,
'current' => false,
);
return $return;
}
private function onFirstLast() {
$return = array();
$amp = $this->max / 2;
$start = 0;
if ($this->page <= $amp) {
$start = 0;
} elseif ($this->page >= ceil($this->total / $this->limit) - $amp) {
$start = ceil($this->total / $this->limit) - $this->max;
} else {
$start = $this->page - $amp;
}
if ($this->page > 0) {
$return['first'] = array(
'page' => 0,
'current' => false,
);
$return['prev'] = array(
'page' => $this->page - 1,
'current' => false,
);
}
for ($p = $start; $p < $start + $this->max; $p++) {
$return[$p + 1] = array(
'page' => $p,
'current' => ($p == $this->page) ? true : false,
);
}
if ($this->page < ceil($this->total / $this->limit) - 1) {
$return['next'] = array(
'page' => $this->page + 1,
'current' => false,
);
$return['last'] = array(
'page' => ceil($this->total / $this->limit),
'current' => false,
);
}
return $return;
}
static function translate($value, $options = array()) {
return (is_array($options) && isset($options[$value])) ? $options[$value] : $value;
}
}