-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPage.php
More file actions
104 lines (93 loc) · 3.2 KB
/
Page.php
File metadata and controls
104 lines (93 loc) · 3.2 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
98
99
100
101
102
103
<?php
namespace TypechoPlugin\Access;
use Typecho\Common;
use Typecho\Request;
use Widget\Options;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
class Page
{
private int $each_disNums;
private int $nums;
private int $current_page;
private int $sub_pages;
private int $pageNums;
private array $page_array = [];
private array $otherParams = [];
public function __construct(int $each_disNums, int $nums, int $current_page, int $sub_pages, array $otherParams)
{
$this->each_disNums = $each_disNums;
$this->nums = $nums;
if (!$current_page) {
$this->current_page = 1;
} else {
$this->current_page = $current_page;
}
$this->sub_pages = $sub_pages;
$this->pageNums = ceil($nums / $each_disNums);
$this->otherParams = $otherParams;
}
public function initArray(): array
{
for ($i = 0; $i < $this->sub_pages; $i++) {
$this->page_array[$i] = $i;
}
return $this->page_array;
}
public function construct_num_Page(): array
{
if ($this->pageNums < $this->sub_pages) {
$current_array = [];
for ($i = 0; $i < $this->pageNums; $i++) {
$current_array[$i] = $i + 1;
}
} else {
$current_array = $this->initArray();
if ($this->current_page <= 3) {
for ($i = 0; $i < count($current_array); $i++) {
$current_array[$i] = $i + 1;
}
} elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
for ($i = 0; $i < count($current_array); $i++) {
$current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
}
} else {
for ($i = 0; $i < count($current_array); $i++) {
$current_array[$i] = $this->current_page - 2 + $i;
}
}
}
return $current_array;
}
public function show(): string
{
$str = "";
if ($this->current_page > 1) {
$prevPageUrl = $this->buildUrl($this->current_page - 1);
$str .= '<li><a href="' . $prevPageUrl . '">«</a></li>';
}
$a = $this->construct_num_Page();
for ($i = 0; $i < count($a); $i++) {
$s = $a[$i];
if ($s == $this->current_page) {
$url = Request::getInstance()->getRequestUrl();
$str .= '<li class="current"><a href="' . $url . '">' . $s . '</a></li>';
} else {
$url = $this->buildUrl($s);
$str .= '<li><a href="' . $url . '">' . $s . '</a></li>';
}
}
if ($this->current_page < $this->pageNums) {
$nextPageUrl = $this->buildUrl($this->current_page + 1);
$str .= '<li><a href="' . $nextPageUrl . '">»</a></li>';
}
return $str;
}
private function buildUrl(int $page): string
{
return Common::url('extending.php?' . http_build_query(array_merge($this->otherParams, [
'page' => $page,
])), Options::alloc()->adminUrl);
}
}