Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/view/control/AphrontCursorPagerView.php
12241 views
1
<?php
2
3
final class AphrontCursorPagerView extends AphrontView {
4
5
private $afterID;
6
private $beforeID;
7
8
private $pageSize = 100;
9
10
private $nextPageID;
11
private $prevPageID;
12
private $moreResults;
13
14
private $uri;
15
16
public function setPageSize($page_size) {
17
$this->pageSize = max(1, $page_size);
18
return $this;
19
}
20
21
public function getPageSize() {
22
return $this->pageSize;
23
}
24
25
public function setURI(PhutilURI $uri) {
26
$this->uri = $uri;
27
return $this;
28
}
29
30
public function readFromRequest(AphrontRequest $request) {
31
$this->uri = $request->getRequestURI();
32
$this->afterID = $request->getStr('after');
33
$this->beforeID = $request->getStr('before');
34
return $this;
35
}
36
37
public function setAfterID($after_id) {
38
$this->afterID = $after_id;
39
return $this;
40
}
41
42
public function getAfterID() {
43
return $this->afterID;
44
}
45
46
public function setBeforeID($before_id) {
47
$this->beforeID = $before_id;
48
return $this;
49
}
50
51
public function getBeforeID() {
52
return $this->beforeID;
53
}
54
55
public function setNextPageID($next_page_id) {
56
$this->nextPageID = $next_page_id;
57
return $this;
58
}
59
60
public function getNextPageID() {
61
return $this->nextPageID;
62
}
63
64
public function setPrevPageID($prev_page_id) {
65
$this->prevPageID = $prev_page_id;
66
return $this;
67
}
68
69
public function getPrevPageID() {
70
return $this->prevPageID;
71
}
72
73
public function sliceResults(array $results) {
74
if (count($results) > $this->getPageSize()) {
75
$offset = ($this->beforeID ? count($results) - $this->getPageSize() : 0);
76
$results = array_slice($results, $offset, $this->getPageSize(), true);
77
$this->moreResults = true;
78
}
79
return $results;
80
}
81
82
public function getHasMoreResults() {
83
return $this->moreResults;
84
}
85
86
public function willShowPagingControls() {
87
return $this->prevPageID ||
88
$this->nextPageID ||
89
$this->afterID ||
90
($this->beforeID && $this->moreResults);
91
}
92
93
public function getFirstPageURI() {
94
if (!$this->uri) {
95
throw new PhutilInvalidStateException('setURI');
96
}
97
98
if (!$this->afterID && !($this->beforeID && $this->moreResults)) {
99
return null;
100
}
101
102
return id(clone $this->uri)
103
->removeQueryParam('after')
104
->removeQueryParam('before');
105
}
106
107
public function getPrevPageURI() {
108
if (!$this->uri) {
109
throw new PhutilInvalidStateException('getPrevPageURI');
110
}
111
112
if (!$this->prevPageID) {
113
return null;
114
}
115
116
return id(clone $this->uri)
117
->removeQueryParam('after')
118
->replaceQueryParam('before', $this->prevPageID);
119
}
120
121
public function getNextPageURI() {
122
if (!$this->uri) {
123
throw new PhutilInvalidStateException('setURI');
124
}
125
126
if (!$this->nextPageID) {
127
return null;
128
}
129
130
return id(clone $this->uri)
131
->replaceQueryParam('after', $this->nextPageID)
132
->removeQueryParam('before');
133
}
134
135
public function render() {
136
if (!$this->uri) {
137
throw new PhutilInvalidStateException('setURI');
138
}
139
140
$links = array();
141
142
$first_uri = $this->getFirstPageURI();
143
if ($first_uri) {
144
$icon = id(new PHUIIconView())
145
->setIcon('fa-fast-backward');
146
$links[] = id(new PHUIButtonView())
147
->setTag('a')
148
->setHref($first_uri)
149
->setIcon($icon)
150
->addClass('mml')
151
->setColor(PHUIButtonView::GREY)
152
->setText(pht('First'));
153
}
154
155
$prev_uri = $this->getPrevPageURI();
156
if ($prev_uri) {
157
$icon = id(new PHUIIconView())
158
->setIcon('fa-backward');
159
$links[] = id(new PHUIButtonView())
160
->setTag('a')
161
->setHref($prev_uri)
162
->setIcon($icon)
163
->addClass('mml')
164
->setColor(PHUIButtonView::GREY)
165
->setText(pht('Prev'));
166
}
167
168
$next_uri = $this->getNextPageURI();
169
if ($next_uri) {
170
$icon = id(new PHUIIconView())
171
->setIcon('fa-forward');
172
$links[] = id(new PHUIButtonView())
173
->setTag('a')
174
->setHref($next_uri)
175
->setIcon($icon, false)
176
->addClass('mml')
177
->setColor(PHUIButtonView::GREY)
178
->setText(pht('Next'));
179
}
180
181
return phutil_tag(
182
'div',
183
array(
184
'class' => 'phui-pager-view',
185
),
186
$links);
187
}
188
189
}
190
191