-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginator.php
526 lines (489 loc) · 16.9 KB
/
Paginator.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
/**
* User: junius
* Date: 2017/04/15
* Time: 3:46 PM
*/
class Paginator
{
private $_db;
private $_table = null;
private $_currentPageClass = '';
private $_itemLimitPerPage;
private $_rowOffset = 0;
private $_urlPattern = '/';
private $_urlParam = false;
private $_lastPage = null;
private $_paramName = 'page';
/**
* @return string url pattern
*/
public function getUrlPattern()
{
if ($this->_urlParam) {
return $this->_urlPattern . '?' . $this->_paramName . '=';
}
return $this->_urlPattern;
}
/**
* @param string $urlPattern
*/
public function setUrlPattern($urlPattern)
{
$this->_urlPattern = $urlPattern;
}
/**
* @param string $paramName
*/
public function setParamName($paramName)
{
$this->_paramName = $paramName;
}
/**
* @return int value of itemLimitPerPage
*/
public function getItemLimitPerPage()
{
return $this->_itemLimitPerPage;
}
/**
* @param int $limitItems number of items per page
*/
public function setItemLimitPerPage($limitItems)
{
$this->_itemLimitPerPage = $limitItems;
}
/**
* @return bool
*/
public function isUrlParam()
{
return $this->_urlParam;
}
/**
* @param bool $urlParam
*/
public function setUrlParam($urlParam)
{
$this->_urlParam = $urlParam;
}
/**
* @return int value of rowOffset
*/
public function getRowOffset()
{
return $this->_rowOffset;
}
/**
* @param int $rowOffset number of row offset
*/
public function setRowOffset($rowOffset)
{
$this->_rowOffset = $rowOffset;
}
/**
* Paginator constructor.
* @param string $dsn database host and database name
* @param string $username database username
* @param string $password user password
*/
public function __construct($dsn, $username, $password)
{
try {
$this->_db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
/**
* Get the name of the table
* @return string the name of the table
*/
public function getTable()
{
return $this->_table;
}
/**
* Set the name of the table
* @param string $table the name of the table to be used
*/
public function setTable($table)
{
$this->_table = $table;
}
/**
* Get the class to be used on the current item/page
* @return string the current page class
*/
public function getCurrentPageClass()
{
return $this->_currentPageClass;
}
/**
* Set the class to be used on the current item/page
* @param string $currentPageClass set the class to be used for the current page
*/
public function setCurrentPageClass($currentPageClass)
{
$this->_currentPageClass = $currentPageClass;
}
/**
* @return mixed
*/
private function getLastPage()
{
if ($this->_lastPage == null) {
$this->_lastPage = round(($this->getRowCount() / $this->getItemLimitPerPage()) - 1);
}
return $this->_lastPage;
}
/**
* Get the number of rows available
* @param null $table optional table name
* @return int the number of row count
* @throws Exception when table is not set or provided
*/
public function getRowCount($table = null)
{
if ($this->_table === null && $table === null) {
throw new Exception("Table was not set");
} else {
if ($table !== null) {
$stmt = $this->_db->prepare("SELECT * FROM $table");
$stmt->execute();
return $stmt->rowCount();
} elseif ($this->_table !== null) {
$stmt = $this->_db->prepare("SELECT * FROM $this->_table");
$stmt->execute();
return $stmt->rowCount();
}
}
}
/**
* Get the number of rows left from the database
* @param null $table optional table name
* @return int number of rows left
* @throws Exception when table is not set or provided
*/
public function getRowsLeft($table = null)
{
if ($this->getCurrentPage() !== 'index.php') {
$this->_rowOffset = ($this->_itemLimitPerPage * $this->getPageNumber());
}
if ($this->_table === null && $table === null) {
throw new Exception("Table was not set");
} else {
if ($table !== null) {
$stmt = $this->_db->prepare("SELECT * FROM $table LIMIT " . $this->getRowOffset() . "," . $this->getItemLimitPerPage());
$stmt->execute();
return $stmt->rowCount();
} elseif ($this->_table !== null) {
$stmt = $this->_db->prepare("SELECT * FROM $this->_table LIMIT " . $this->getRowOffset() . "," . $this->getItemLimitPerPage());
$stmt->execute();
return $stmt->rowCount();
}
}
}
/**
* Get data to be used on the current page
* @param string $colId column id
* @param array $params optional parameters for ['table' => 'tableName', 'sort' => 'ASC', 'columns' => 'colId, name, etc']
* @return array columns from database
* @throws Exception when table is not set or provided
*/
public function getPageData($colId, $params = [])
{
if ($this->_table === null && !isset($params['table'])) {
throw new Exception("Table was not set");
}
$columns = isset($params['columns']) ? $params['columns'] : '*';
$sort = isset($params['sort']) ? $params['sort'] : 'DESC';
if (isset($params['table'])) {
$table = $params['table'];
$rowsLeft = $this->getRowsLeft($this->_table);
$limit = $this->_itemLimitPerPage;
if ($rowsLeft < $this->_itemLimitPerPage) {
$limit = $rowsLeft;
}
$select = "SELECT $columns FROM " . $table . " ORDER BY $colId $sort LIMIT ?,?";
$prepare = $this->_db->prepare($select);
$prepare->bindParam(1, $this->_rowOffset, PDO::PARAM_INT);
$prepare->bindParam(2, $limit, PDO::PARAM_INT);
$prepare->execute();
$results = $prepare->fetchAll();
return $results;
} elseif ($this->_table !== null) {
$rowsLeft = $this->getRowsLeft($this->_table);
$limit = $this->_itemLimitPerPage;
if ($rowsLeft < $this->_itemLimitPerPage) {
$limit = $rowsLeft;
}
$prepare = $this->_db->prepare("SELECT * FROM $this->_table ORDER BY $colId $sort LIMIT " . $this->getRowOffset() . "," . $limit);
$prepare->execute();
$results = $prepare->fetchAll();
return $results;
}
}
/**
* Create pages that will appear before the current page
* @param int $pageNumber the current page number
* @param int $numPrevPages the number of pages to appear before the current page
* @param $cssClass class set to the li list
* @param $attr attribtes for li list
* @return string list of pagination links
*/
function prevPages($pageNumber, $numPrevPages, $cssClass, $attr)
{
$listItems = ''; // to save all list items.
while ($numPrevPages >= 1) {
$pageNumber -= 1;
if ($pageNumber >= 1) {
$listItems = $this->createListItem($cssClass, $attr, $pageNumber) . $listItems;
}
$numPrevPages -= 1;
}
return $listItems;
}
function createListItem($cssClass, $attr, $pageNumber)
{
$lastPage = $this->getLastPage();
if ($this->_urlParam && $pageNumber <= $lastPage && $lastPage > 0) {
return '<li class="' . $cssClass . '" ' . $attr . '><a href="' . $this->getUrlPattern() . $pageNumber . '">' . $pageNumber . '</a></li>';
} elseif (!$this->_urlParam) {
$page = $pageNumber . '.php';
if (file_exists($page)) {
return '<li class="' . $cssClass . '" ' . $attr . '><a href="' . $this->getUrlPattern() . $pageNumber . '.php">' . $pageNumber . '</a></li>';
}
}
}
/**
* Create pages that will appear after the current page
* @param $pageNumber the current page number
* @param $numNextPages the number of pages to appear after the current page
* @param $cssClass class set to the li list
* @param $attr attribtes for li list
* @return string list of pagination links
*/
function nextPages($pageNumber, $numNextPages, $cssClass, $attr)
{
$listItems = ''; // to save list items.
$count = 1;
while ($count <= $numNextPages) {
$pageNumber += 1;
$listItems .= $this->createListItem($cssClass, $attr, $pageNumber);
$count += 1;
}
return $listItems;
}
/**
* Create the pagination links
* @param $pageNumber the current page number
* @param $numPrevPages the number of pages to appear before the current page
* @param $numNextPages the number of pages to appear after the current page
* @param array $attributes optional list of list attributes.
* ['ul-class': => 'space separated list of classes', 'ul-attr': 'id="someId" data-pre="pre"', 'li-class': 'space separated list of classes', 'li-attr': 'id="someid"']
*/
function pagination($pageNumber, $numPrevPages, $numNextPages, $attributes = [])
{
$ulCssClass = isset($attributes['ul-class']) ? $attributes['ul-class'] : '';
$ulAttr = isset($attributes['ul-attr']) ? $attributes['ul-attr'] : '';
$liCssClass = isset($attributes['li-class']) ? $attributes['li-class'] : '';
$liAttr = isset($attributes['li-attr']) ? $attributes['li-attr'] : '';
$prevPagesList = '<ul class="' . $ulCssClass . '" ' . $ulAttr . '>' . $this->prevButton($pageNumber) . $this->prevPages($pageNumber, $numPrevPages, $liCssClass, $liAttr);
$nextPageList = $this->nextPages($pageNumber, $numNextPages, $liCssClass, $liAttr) . $this->nextButton($pageNumber) . '</ul>';
if ($pageNumber == 'index') {
$listItems = $prevPagesList . $nextPageList;
} else {
$listItems = $prevPagesList . '<li class="' . $this->getCurrentPageClass() . '"><a href="">' . $pageNumber . '</a> </li>' . $nextPageList;
}
echo $listItems;
}
/**
* Create a link for previous button
* @param $pageNumber the current page number
* @return string the previous link item list
*/
function prevButton($pageNumber)
{
$prev = '';
if ($this->_urlParam && $pageNumber >= 1) {
return '<li><a href="' . $this->getUrlPattern() . ($pageNumber - 1) . '">« Previous</a></li>';
} elseif (!$this->_urlParam) {
if ($pageNumber == 1) {
$prev = '<li><a href="index.php">« Previous</a></li>';
} elseif ($pageNumber > 1) {
$prev = '<li><a href="' . $this->getUrlPattern() . ($pageNumber - 1) . '.php' . '">« Previous</a></li>';
}
return $prev;
}
}
/**
* Create a link for next button
* @param $pageNumber the current page number
* @return string the next link item list
*/
function nextButton($pageNumber)
{
if ($this->_urlParam && $pageNumber < $this->getLastPage()) {
return '<li><a href="' . $this->getUrlPattern() . ($pageNumber + 1) . '">Next » </a></li>';
} elseif (!$this->_urlParam) {
if ($pageNumber == 'index') {
$page = '1.php';
} else {
$page = ($pageNumber + 1) . '.php';
}
if (file_exists($page)) {
return '<li><a href="' . $this->getUrlPattern() . ($pageNumber + 1) . '.php">Next » </a></li>';
}
}
return '';
}
/**
* Get the current page number
* @return int the current page number
*/
function getPageNumber()
{
if ($this->_urlParam && isset($_GET[$this->_paramName]) && $_GET[$this->_paramName] != 0) {
return $_GET[$this->_paramName];
}
$currentPage = basename($_SERVER['SCRIPT_FILENAME']);
$pageNumber = rtrim($currentPage, '.php');
return $pageNumber;
}
/**
* Get the current page
* @return string return the current page
*/
function getCurrentPage()
{
if ($this->_urlParam && isset($_GET[$this->_paramName]) && $_GET[$this->_paramName] != 0) {
return $_GET[$this->_paramName];
}
$currentPage = basename($_SERVER['SCRIPT_FILENAME']);
return $currentPage;
}
/**
* create the required pages
*/
function createPages()
{
if (!$this->_urlParam) {
$lastPage = $this->getLastPage();
for ($counter = 1; $counter <= $lastPage; $counter++) {
$page = $counter . '.php';
if (!file_exists($page)) {
copy('index.php', $page);
}
}
}
}
/**
* Force column names to a specific case
* @param string $case the case attribute to be set
* @throws Exception when unknown case was provided
*/
public function setColumnCaseAttribute($case)
{
switch ($case) {
case 'lower':
$this->_db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
break;
case 'upper':
$this->_db->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
break;
case 'natural':
$this->_db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
break;
default:
throw new Exception("Unknown column case.");
}
}
/**
* Set error reporting
* @param string $error type of error mode to be set
* @throws Exception when unknown error mode was provided
*/
public function setErrorModeAttribute($error)
{
switch ($error) {
case 'silent':
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
break;
case 'warning':
$this->_db->setAttribute(PDO::ATTR_CASE, PDO::ERRMODE_WARNING);
break;
case 'exception':
$this->_db->setAttribute(PDO::ATTR_CASE, PDO::ERRMODE_EXCEPTION);
break;
default:
throw new Exception("Unknown error mode.");
}
}
/**
* Conversion of NULL and empty strings
* @param string $error type of null attribute to be set
* @throws Exception when unknown type is provided
*/
public function setOracleNullsAttribute($error)
{
switch ($error) {
case 'null natural':
$this->_db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
break;
case 'null empty string':
$this->_db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
break;
case 'null to string':
$this->_db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
break;
default:
throw new Exception("Unknown Oracle null attribute.");
}
}
/**
* Convert numeric values to strings when fetching
* @param boolean $bool set boolean value for stringify fetches
* @throws Exception when non boolean value if provided
*/
public function setStringifyFetchesAttribute($bool)
{
if (is_bool($bool)) {
$this->_db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, $bool);
} else {
throw new Exception("Function requires boolean value, unknown type provided.");
}
}
/**
* Set user-supplied statement class derived from PDOStatement
* @param string $custom the name of the custom class
*/
public function setStatementClassAttribute($custom)
{
$this->_db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($custom, array($this->_db)));
}
/**
* Specifies the timeout duration in seconds
* @param int $seconds number of timeout seconds
*/
public function setTimeoutAttribute($seconds)
{
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, $seconds);
}
/**
* Whether to autocommit every single statement
* @param boolean $bool set db to autcommit
*/
public function setAutoCommitAttribute($bool)
{
$this->_db->setAttribute(PDO::ATTR_AUTOCOMMIT, $bool);
}
/**
* Enables or disables emulation of prepared statements
* @param boolean $bool
*/
public function setEmulatePreparesAttribute($bool)
{
$this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, $bool);
}
}