1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\widgets\datatable; |
3
|
|
|
|
4
|
|
|
use Ajax\semantic\html\collections\menus\HtmlPaginationMenu; |
5
|
|
|
|
6
|
|
|
class Pagination { |
7
|
|
|
private $items_per_page; |
8
|
|
|
private $page; |
9
|
|
|
private $visible; |
10
|
|
|
private $page_count; |
11
|
|
|
private $pages_visibles; |
12
|
|
|
private $row_count; |
13
|
|
|
private $menu; |
14
|
|
|
|
15
|
|
|
public function __construct($items_per_page=10,$pages_visibles=null,$page=1,$row_count=null){ |
16
|
|
|
$this->items_per_page=$items_per_page; |
17
|
|
|
$this->row_count=$row_count; |
18
|
|
|
$this->page=$page; |
19
|
|
|
$this->setPagesVisibles($pages_visibles); |
20
|
|
|
$this->visible=true; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getObjects($objects){ |
24
|
|
|
$auto=(!isset($this->row_count)); |
25
|
|
|
$os=$objects; |
26
|
|
|
if(!\is_array($os)){ |
27
|
|
|
$os=[]; |
28
|
|
|
foreach ($objects as $o){ |
29
|
|
|
$os[]=$o; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
$this->page_count = 0; |
33
|
|
|
$row_count=($auto)?\sizeof($os):$this->row_count; |
34
|
|
|
if (0 === $row_count) { |
35
|
|
|
$this->visible=false; |
36
|
|
|
} else { |
37
|
|
|
|
38
|
|
|
$this->page_count = (int)ceil($row_count / $this->items_per_page); |
39
|
|
|
$this->visible=$this->page_count>1; |
40
|
|
|
if($this->page > $this->page_count+1) { |
41
|
|
|
$this->page = 1; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
if($auto){ |
45
|
|
|
$offset = ($this->page - 1) * $this->items_per_page; |
46
|
|
|
return array_slice($os, $offset,$this->items_per_page); |
47
|
|
|
} |
48
|
|
|
return $os; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getItemsPerPage() { |
52
|
|
|
return $this->items_per_page; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setItemsPerPage($items_per_page) { |
56
|
|
|
$this->items_per_page=$items_per_page; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getPage() { |
61
|
|
|
return $this->page; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setPage($page) { |
65
|
|
|
$this->page=$page; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getVisible() { |
70
|
|
|
return $this->visible; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setVisible($visible) { |
74
|
|
|
$this->visible=$visible; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPageCount() { |
79
|
|
|
return $this->page_count; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getPagesNumbers(){ |
83
|
|
|
$middle= (int)ceil(($this->pages_visibles-1)/ 2); |
84
|
|
|
$first=$this->page-$middle; |
85
|
|
|
if($first<1){ |
86
|
|
|
$first=1; |
87
|
|
|
} |
88
|
|
|
$last=$first+$this->pages_visibles-1; |
89
|
|
|
if($last>$this->page_count){ |
90
|
|
|
$last=$this->page_count; |
91
|
|
|
} |
92
|
|
|
return \range($first, $last); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setPagesVisibles($pages_visibles) { |
96
|
|
|
if(!isset($pages_visibles)) |
97
|
|
|
$pages_visibles=(int)ceil($this->row_count / $this->items_per_page)+1; |
98
|
|
|
$this->pages_visibles=$pages_visibles; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function generateMenu($identifier){ |
103
|
|
|
$menu=new HtmlPaginationMenu("pagination-".$identifier,$this->getPagesNumbers()); |
104
|
|
|
$menu->setMax($this->page_count); |
105
|
|
|
$menu->floatRight(); |
106
|
|
|
$menu->setActivePage($this->getPage()); |
107
|
|
|
return $this->menu=$menu; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getMenu() { |
114
|
|
|
return $this->menu; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function getPageOfRow($rownum,$itemsPerPage=10){ |
118
|
|
|
$pageNum=0;$activeRow=0; |
119
|
|
|
while($activeRow<$rownum){ |
120
|
|
|
$activeRow+=$itemsPerPage; |
121
|
|
|
$pageNum++; |
122
|
|
|
} |
123
|
|
|
return $pageNum; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|