|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Lib\Controller; |
|
6
|
|
|
|
|
7
|
|
|
class Paginate extends Controller |
|
8
|
|
|
{ |
|
9
|
|
|
public $perPage, $currentPage, $total , $maxPage , $items , $skip; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Paginate constructor. |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct() |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
parent::__construct(); |
|
17
|
|
|
$this->setCurrentPage((isset($_GET['page'])) ? $_GET['page'] : 1); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getItems() |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->items; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getSkip() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->skip; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getCurrentPage() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->currentPage; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getMaxPages() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->maxPage; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getPerPage() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->perPage; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getTotal() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->total; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $skip |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setSkip($skip) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->skip = $skip; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $items |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setItems($items) |
|
80
|
|
|
{ |
|
81
|
|
|
$count = ceil($items->count() / $this->getPerPage()); |
|
82
|
|
|
$maxPages = (!empty($this->getMaxPages()) && $count > $this->getMaxPages()) ? $this->getMaxPages() : $count; |
|
83
|
|
|
$this->setTotal($maxPages); |
|
84
|
|
|
$this->setSkip(($this->getCurrentPage() * $this->getPerPage()) - $this->getPerPage()); |
|
85
|
|
|
$this->items = $items->skip($this->getSkip())->limit($this->getPerPage())->get(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $currentPage |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setCurrentPage($currentPage) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->currentPage = $currentPage; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $maxPage |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setMaxPages($maxPage) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->maxPage = $maxPage; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param $perPage |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setPerPage($perPage) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->perPage = $perPage; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $total |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setTotal($total) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->total = $total; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function paginate() |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->getTotal() != 1) { |
|
123
|
|
|
echo /* @lang text */ '<ul class="pagination">'; |
|
124
|
|
|
if ($this->getCurrentPage() > 1) { |
|
125
|
|
|
echo /* @lang text */ '<li><a href="?page='.($this->getCurrentPage() - 1).'" aria-label="Previous"><span aria-hidden="true">Anterior</span></a></li>'; |
|
126
|
|
|
} else { |
|
127
|
|
|
echo /* @lang text */ '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">Anterior</span></a></li>'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
for ($i = 1; $i <= $this->getTotal(); $i++) { |
|
131
|
|
|
$active = ($this->getCurrentPage() == $i) ? 'class="active"' : ''; |
|
132
|
|
|
|
|
133
|
|
|
echo '<li '.$active.'><a href="?page='.$i.'">'.$i.'</a></li>'; |
|
134
|
|
|
} |
|
135
|
|
|
if ($this->getCurrentPage() < $this->getTotal()) { |
|
136
|
|
|
echo /* @lang text */ '<li><a href="?page='.($this->getCurrentPage() + 1).'" aria-label="Next"><span aria-hidden="true">Próxima</span></a></li>'; |
|
137
|
|
|
} else { |
|
138
|
|
|
echo /* @lang text */ '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">Próxima</span></a></li>'; |
|
139
|
|
|
} |
|
140
|
|
|
echo '</ul>'; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.