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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
<?php
/**
* Pagination abstraction
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
* @since Stud.IP 4.4
*/
class Pagination
{
/**
* Creates a pagination object.
*
* @param int $total Total number of entries
* @param int $current_page Current page
* @param int $per_page Displayed entries per page
* @return Pagination object
*/
public static function create($total, $current_page = 0, $per_page = null)
{
if ($per_page === null) {
$per_page = Config::get()->ENTRIES_PER_PAGE;
}
return new self($total, $current_page, $per_page);
}
private $total;
private $current_page;
private $per_page;
private $dialog = null;
private $show_page = false;
/**
* Checks current page ranges for validity.
*
* @param int $total Total number of entries
* @param int $current_page Current page
* @param int $per_page Displayed entries per page
*/
private function __construct($total, $current_page, $per_page)
{
$this->total = $total;
$this->current_page = $current_page;
$this->per_page = $per_page;
$this->current_page = min(
$this->current_page,
$this->getPageCount() - 1
);
if ($this->current_page < 0) {
$this->current_page = 0;
}
}
/**
* Add dialog options
* @param string $content Parameters for data-dialog attribute
* @return Pagination instance to allow chaining
*/
public function asDialog($content = '')
{
$this->dialog = $content;
return $this;
}
/**
* Returns a list of all pages to display.
* @return array of pages
*/
private function getPages()
{
$page_count = $this->getPageCount();
$pages = array_unique([
0,
$this->current_page - 2,
$this->current_page - 1,
$this->current_page,
$this->current_page + 1,
$this->current_page + 2,
$page_count - 1,
]);
$pages = array_filter($pages, function ($page) use ($page_count) {
return $page >= 0 && $page < $page_count;
});
sort($pages);
return $pages;
}
/**
* Returns the total number of entries.
* @return int
*/
public function getTotal()
{
return $this->total;
}
/**
* Returns the current page
* @return int
*/
public function getCurrentPage()
{
return $this->current_page;
}
/**
* Returns the current offset or skipped entries before the current page.
* @return int
*/
public function getOffset()
{
return $this->current_page * $this->per_page;
}
/**
* Returns the maximum number of entries per page.
* @return int
*/
public function getPerPage()
{
return $this->per_page;
}
/**
* Get total number of pages.
* @return int
*/
public function getPageCount()
{
return ceil($this->total / $this->per_page);
}
/**
* Renders the paginations with <button> elements.
*
* @param string $name Value of the button's name attribute
* @return string html
*/
public function asButtons($name = 'page')
{
if ($this->getPageCount() <= 1) {
return ' ';
}
return $this->render('pagination/buttons.php', compact('name'));
}
/**
* Renders the paginations with <a> link elements.
*
* @param Closure $link_for Optional generator for page links (defaults to
* links to the current page with ?page=
* parameters)
* @return string html
*/
public function asLinks(Closure $link_for = null)
{
if ($this->getPageCount() <= 1) {
return ' ';
}
if ($link_for === null) {
$link_for = function ($page) {
return URLHelper::getLink('', compact('page'));
};
}
return $this->render('pagination/links.php', compact('link_for'));
}
/**
* Renders a template for the pagination with the provided variables.
*
* @param string $template Name of the template file
* @param array $variables Additional variables
* @return string html
*/
private function render($template, array $variables = [])
{
return $GLOBALS['template_factory']->render($template, $variables + [
'random_id' => md5(uniqid('pagination', true)),
'pages' => $this->getPages(),
'count' => $this->getPageCount(),
'current' => $this->current_page,
'dialog' => $this->dialog,
'show_page' => $this->show_page,
]);
}
/**
* Loads the slice of a sorm collection defined by this object.
*
* @param string $sorm_class Name of the SORM class
* @param string $condition Condition to load objects by
* @param array $parameters Additional parameters for the condition
* @return SimpleORMapCollection
* @throws
*/
public function loadSORMCollection($sorm_class, $condition = '1', array $parameters = [])
{
if (!class_exists($sorm_class) || !is_a($sorm_class, 'SimpleORMap', true)) {
throw new RuntimeException('No valid SORM class given');
}
$sql = "{$condition} LIMIT {$this->getOffset()}, {$this->getPerPage()}";
return SimpleORMapCollection::createFromArray(
$sorm_class::findBySQL($sql, $parameters)
);
}
}
|