|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Services\HTML; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Services\Globals\Globals; |
|
6
|
|
|
|
|
7
|
|
|
class Paging |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Paging of the past SQL statement |
|
11
|
|
|
* @param int $limit number of elements returned per page |
|
12
|
|
|
* @param string $queryString ex:'post.index' |
|
13
|
|
|
* @param object $table table involved |
|
14
|
|
|
* @param string $statement |
|
15
|
|
|
* @param null|array $attributes |
|
16
|
|
|
* @return array with the \compat function | !! use \extract to retrieve variables |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function generate(int $limit, string $queryString, object $table, string $statement, ?array $attributes = \null): array |
|
19
|
|
|
{ |
|
20
|
|
|
$globals = new Globals; |
|
21
|
|
|
|
|
22
|
|
|
// retrieves the page number |
|
23
|
|
|
preg_match('/\d*$/', $globals->getSERVER('QUERY_STRING'), $page); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
$rows = \lcfirst($table->table . 's'); |
|
26
|
|
|
|
|
27
|
|
|
$nbRows = $table->countRows($statement, $attributes); |
|
28
|
|
|
$nbPages = \ceil($nbRows / $limit); |
|
29
|
|
|
$page = !empty($page[0]) && $page[0] <= $nbPages ? $page[0] : 1; |
|
30
|
|
|
|
|
31
|
|
|
// add the offest to the base SQL statement |
|
32
|
|
|
$$rows = $table->offset($statement, $attributes, $limit, $page); |
|
33
|
|
|
// manages the disabling of the previous and following btn |
|
34
|
|
|
$previous = $page <= 1 ? ' disabled' : \null; |
|
35
|
|
|
$next = $page >= $nbPages ? ' disabled' : \null; |
|
36
|
|
|
|
|
37
|
|
|
\ob_start(); |
|
38
|
|
|
?> |
|
39
|
|
|
<nav class="paging" aria-label="Page navigation"> |
|
40
|
|
|
<ul class="pagination"> |
|
41
|
|
|
<?php if (empty($previous)) : ?> |
|
42
|
|
|
<li class="page-item<?= $previous ?>"> |
|
43
|
|
|
<a class="page-link" href="?p=<?= $queryString; ?><?= '.' . ($page - 1); ?>">«</a> |
|
44
|
|
|
</li> |
|
45
|
|
|
<?php endif; ?> |
|
46
|
|
|
<?php for ($i = 1; $i <= $nbPages; $i++) : ?> |
|
47
|
|
|
<li class="page-item <?= $i == $page ? ' active' : null; ?>" <?= $i == $page ? ' arria-current="page"' : null ?>> |
|
48
|
|
|
<a class="page-link" href="?p=<?= $queryString; ?><?= '.' . $i; ?>"><?= $i; ?></a> |
|
49
|
|
|
</li> |
|
50
|
|
|
<?php endfor; ?> |
|
51
|
|
|
<?php if (empty($next)) : ?> |
|
52
|
|
|
<li class="page-item<?= $next ?>"> |
|
53
|
|
|
<a class="page-link" href="?p=<?= $queryString; ?><?= '.' . ($page + 1); ?>">»</a> |
|
54
|
|
|
</li> |
|
55
|
|
|
<?php endif; ?> |
|
56
|
|
|
</ul> |
|
57
|
|
|
</nav> |
|
58
|
|
|
<?php |
|
59
|
|
|
$paging = \ob_get_clean(); |
|
60
|
|
|
|
|
61
|
|
|
return compact($rows, 'paging'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|