1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Win\DAO;
|
4
|
|
|
|
5
|
|
|
use Win\Mvc\Block;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Paginação
|
9
|
|
|
* Auxilia busca por paginação
|
10
|
|
|
*/
|
11
|
|
|
class Pagination {
|
12
|
|
|
|
13
|
|
|
private $total = 0;
|
14
|
|
|
private $totalPerPage = null;
|
15
|
|
|
private $last = null;
|
16
|
|
|
private $current;
|
17
|
|
|
|
18
|
|
|
/** Construtor */
|
19
|
|
|
public function __construct($totalPerPage = null, $current = null) {
|
20
|
|
|
$this->totalPerPage = $totalPerPage;
|
21
|
|
|
$this->setCurrent($current);
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
/** @return int */
|
25
|
|
|
public function totalPerPage() {
|
26
|
|
|
return $this->totalPerPage;
|
27
|
|
|
}
|
28
|
|
|
|
29
|
|
|
/** @return int */
|
30
|
|
|
public function current() {
|
31
|
|
|
if ($this->current > $this->last() && !is_null($this->last)) {
|
32
|
|
|
$this->current = $this->last();
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
if ($this->current < $this->first()) {
|
36
|
|
|
$this->current = $this->first();
|
37
|
|
|
}
|
38
|
|
|
return $this->current;
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
/** @return int */
|
42
|
|
|
public function prev() {
|
43
|
|
|
return ($this->current > $this->first()) ? ($this->current - 1) : $this->first();
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/** @return int */
|
47
|
|
|
public function next() {
|
48
|
|
|
return ($this->current < $this->last) ? ($this->current + 1) : $this->last();
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/** @return int */
|
52
|
|
|
public function first() {
|
53
|
|
|
return 1;
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/** @return int */
|
57
|
|
|
public function last() {
|
58
|
|
|
return $this->last;
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/** @param int $total */
|
62
|
|
|
public function setTotal($total) {
|
63
|
|
|
if ($this->totalPerPage > 0) {
|
64
|
|
|
$this->total = $total;
|
65
|
|
|
$this->last = ceil($total / $this->totalPerPage);
|
66
|
|
|
}
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/** @param int $current */
|
70
|
|
|
public function setCurrent($current) {
|
71
|
|
|
if (is_null($current) && isset($_GET['p'])) {
|
72
|
|
|
$current = $_GET['p'];
|
73
|
|
|
}
|
74
|
|
|
$this->current = $current;
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
/** @return string */
|
78
|
|
|
public function toSql() {
|
79
|
|
|
if (!is_null($this->totalPerPage)) {
|
80
|
|
|
$begin = $this->totalPerPage * ($this->current() - 1);
|
81
|
|
|
return ' LIMIT ' . $begin . ',' . $this->totalPerPage;
|
82
|
|
|
}
|
83
|
|
|
return '';
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
/** @return string */
|
87
|
|
|
public function toHtml() {
|
88
|
|
|
return (string) new Block('layout/html/pagination', ['pagination' => $this]);
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
}
|
92
|
|
|
|