php namespace Evilworlds View Widgets use Evilworlds Text Url class Pa

 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
<?php
namespace Evilworlds\View\Widgets;
use \Evilworlds\Text\Url;
class Pagination extends \Evilworlds\View\Widget {
private $page = 1;
public function __construct($args = NULL) {
$this -> set([
'total' => 0,
'arg_name' => 'p',
'per_page' => 10,
'url' => '',
'neighbors' => 10,
]);
parent::__construct($args);
$this -> page = array_var($_GET, $this -> fields['arg_name'], 0);
}
public function getPage() {
return max(1, min($this -> getTotalPages(), $this -> page));
}
public function getLimit() {
return $this -> fields['per_page'];
}
public function getTotalPages() {
return ceil($this -> fields['total'] / $this -> fields['per_page']);
}
public function getTotal() {
return $this -> fields['total'];
}
public function getOffset() {
return ($this -> getPage() - 1) * $this -> fields['per_page'];
}
public function render() {
$page = $this -> getPage();
$prev_link = new Url($this -> fields['url']);
$prev_link[$this -> fields['arg_name']] = $page - 1;
$page_link = new Url($this -> fields['url']);
$pages = [];
// Ссылка на первую страницу
if ($page > 1) {
$page_link[$this -> fields['arg_name']] = 1;
$pages[] = ['name' => 1, 'link' => $page_link -> getUrl()];
}
// Страницы слева активной
if ($page - 1 > $this -> fields['neighbors'] + 1)
$pages[] = ['name' => '...'];
$min_page = max(2, $page - $this -> fields['neighbors']);
for ($i = $min_page; $i < $page; $i++) {
$page_link[$this -> fields['arg_name']] = $i;
$pages[] = ['name' => $i, 'link' => $page_link -> getUrl()];
}
// Текущая страничка
$pages[] = ['name' => $page, 'curr' => true];
// Страницы справа активной
$max_page = min($this -> getTotalPages() - 1, $page + $this -> fields['neighbors']);
for ($i = $page + 1; $i <= $max_page; $i++) {
$page_link[$this -> fields['arg_name']] = $i;
$pages[] = ['name' => $i, 'link' => $page_link -> getUrl()];
}
if ($page < $this -> getTotalPages() - $this -> fields['neighbors'] - 1)
$pages[] = ['name' => '...'];
// Ссылка на последнюю страницу
if ($page < $this -> getTotalPages()) {
$page_link[$this -> fields['arg_name']] = $this -> getTotalPages();
$pages[] = ['name' => $this -> getTotalPages(), 'link' => $page_link -> getUrl()];
}
$next_link = new Url($this -> fields['url']);
$next_link[$this -> fields['arg_name']] = $page + 1;
$tpl = \Evilworlds\View\Template::getInstance();
$data = $tpl -> render("widgets/pagination.xhtml", [
'total' => $this -> getTotalPages(),
'curr_page' => $page,
'pages' => $pages,
'next_link' => $next_link -> getUrl(),
'prev_link' => $prev_link -> getUrl(),
]);
unset($pages);
return $data;
}
}