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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php
namespace App\Twig\Extension;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
/**
* twig 模板分页扩展
* Class Paginate
* @package App\Twig\Extension
*/
class Paginate extends AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('links', [$this, 'links'], ['needs_environment' => true]),
new \Twig\TwigFunction('links2', [$this, 'links2'], ['needs_environment' => true]),
];
}
public function links(Environment $env, $data, $showTotal = true, $extraClass='', $eachSideCount = 2, $pageName = 'page', $template = 'paginate/links.html')
{
$links = $this->generateLinkItem($data['currentPage'], $data['totalPage'], $data['path'], $eachSideCount, $pageName);
$env->load($template)->display(compact('data', 'links', 'showTotal', 'extraClass'));
}
public function links2(Environment $env, $data, $showTotal = true, $extraClass='', $eachSideCount = 3, $pageName = 'page', $template = 'paginate/links.html')
{
$links = $this->generateLinkItem2($data['currentPage'], $data['totalPage'], $data['path'], $eachSideCount, $pageName);
$env->load($template)->display(compact('data', 'links', 'showTotal', 'extraClass'));
}
private function generateLinkItem($currentPage, $totalPage, $path, $eachSideCount = 2, $pageName = 'page')
{
$totalCount = $eachSideCount * 2 + 1;
$start = $currentPage - $eachSideCount;
$end = $currentPage + $eachSideCount;
if ($totalPage <= $totalCount) {
$start = 1;
$end = $totalPage;
} else {
if ($currentPage <= $eachSideCount) {
$start = $currentPage - abs($eachSideCount - $currentPage - 1);
$end = $currentPage + $eachSideCount + ($eachSideCount - $currentPage + 1);
}
if ($currentPage + $eachSideCount > $totalPage) {
$start = $currentPage - $eachSideCount - ($currentPage + $eachSideCount - $totalPage);
$end = $currentPage + $eachSideCount - ($currentPage + $eachSideCount - $totalPage);
}
}
$links = [];
if ($totalPage > $totalCount) {
if ($currentPage > $eachSideCount + 1) {
$links[] = $this->generateLinkData('首页', $path, 1, $currentPage, false, null, $pageName);
}
$links[] = $this->generateLinkData("«", $path, ($currentPage == 1 ? 1 : ($currentPage - 1)), $currentPage, ($currentPage == 1 ? true : false), false, $pageName);
}
for ($i = $start; $i <= $end; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
if ($totalPage > $totalCount) {
$links[] = $this->generateLinkData("»", $path, ($currentPage == $totalPage ? $totalPage : ($currentPage + 1)), $currentPage, ($currentPage == $totalPage ? true : false), false, $pageName);
if ($totalPage - $currentPage > 2) {
$links[] = $this->generateLinkData('末页', $path, $totalPage, $currentPage, false, null, $pageName);
}
}
return $links;
}
private function generateLinkItem2($currentPage, $totalPage, $path, $eachSideCount = 3, $pageName = 'page')
{
$totalCount = $eachSideCount * 2 + 6;
$start = ($currentPage - $eachSideCount < 1) ? 1 : $currentPage - $eachSideCount;
$end = ($currentPage + $eachSideCount > $totalPage) ? $totalPage : $currentPage + $eachSideCount;
$links = [];
$links[] = $this->generateLinkData("«", $path, ($currentPage == 1 ? 1 : ($currentPage - 1)), $currentPage, ($currentPage == 1 ? true : false), false, $pageName);
if ($totalPage <= $totalCount) {
$start = 1;
$end = $totalPage;
for ($i = $start; $i <= $end; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
} else {
if ($currentPage < $eachSideCount * 2 + 2) {
for ($i = 1; $i <= $eachSideCount * 2 + 2; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
$links[] = $this->generateLinkData('...', false, '', $currentPage, true, null, $pageName);
for ($i = $totalPage - 1; $i <= $totalPage; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
} else if ($currentPage > $totalPage - ($eachSideCount * 2 + 2)) {
for ($i = 1; $i <= 2; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
$links[] = $this->generateLinkData('...', false, '', $currentPage, true, null, $pageName);
for ($i = $totalPage - ($eachSideCount * 2 + 2); $i <= $totalPage; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
} else {
for ($i = 1; $i <= 2; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
$links[] = $this->generateLinkData('...', false, '', $currentPage, true, null, $pageName);
for ($i = $start; $i <= $end; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
$links[] = $this->generateLinkData('...', false, '', $currentPage, true, null, $pageName);
for ($i = $totalPage - 1; $i <= $totalPage; $i++) {
$links[] = $this->generateLinkData($i, $path, $i, $currentPage, false, null, $pageName);
}
}
}
$links[] = $this->generateLinkData("»", $path, ($currentPage == $totalPage ? $totalPage : ($currentPage + 1)), $currentPage, ($currentPage == $totalPage ? true : false), false, $pageName);
return $links;
}
private function generateLinkData($name, $path, $index, $currentPage, $isDisable = false, $isActive = null, $pageName = 'page')
{
return [
'name' => $name,
'url' => $path . (stripos('?', $path) ? '&' : '?') . $pageName . '=' . $index,
'isDisabled' => $isDisable,
'isActive' => $isActive === null ? (($index == $currentPage) ? true : false) : $isActive,
'isLink' => $path === false ? false : true,
];
}
}
|