96 lines
4.1 KiB
PHP
96 lines
4.1 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Pager shared by the registry and the maintenance list.
|
||
*
|
||
* Both lists used to print every row at once — 101 pieces of equipment and 126
|
||
* maintenance tasks, each rendered twice (cards + table), which on a tablet
|
||
* came out as a page some 20 000px tall.
|
||
*
|
||
* Usage (before including):
|
||
* $mntPagerUrl = 'manutenzioni/index.php'; // path, relative to <base href>
|
||
* $mntPagerPage = 3; // current page, 1-based
|
||
* $mntPagerPages = 7; // number of pages
|
||
* $mntPagerTotal = 163; // rows matching the current filters
|
||
* $mntPagerPer = 25; // rows per page
|
||
*/
|
||
|
||
$mntPagerPages = max(1, (int)($mntPagerPages ?? 1));
|
||
$mntPagerPage = min(max(1, (int)($mntPagerPage ?? 1)), $mntPagerPages);
|
||
$mntPagerTotal = (int)($mntPagerTotal ?? 0);
|
||
$mntPagerPer = max(1, (int)($mntPagerPer ?? 25));
|
||
|
||
/** Keeps the active filters, drops page when it would be redundant. */
|
||
$mntPagerLink = static function (int $page, ?int $per = null) use ($mntPagerUrl) {
|
||
$query = $_GET;
|
||
unset($query['page'], $query['per_page']);
|
||
if ($per !== null) {
|
||
$query['per_page'] = $per;
|
||
} elseif (isset($_GET['per_page'])) {
|
||
$query['per_page'] = $_GET['per_page'];
|
||
}
|
||
if ($page > 1) {
|
||
$query['page'] = $page;
|
||
}
|
||
|
||
return $mntPagerUrl . ($query ? '?' . http_build_query($query) : '');
|
||
};
|
||
|
||
$mntFirstRow = $mntPagerTotal ? (($mntPagerPage - 1) * $mntPagerPer) + 1 : 0;
|
||
$mntLastRow = min($mntPagerPage * $mntPagerPer, $mntPagerTotal);
|
||
|
||
// A window around the current page: with 40 pages, 40 links are noise.
|
||
$mntWindowFrom = max(1, $mntPagerPage - 2);
|
||
$mntWindowTo = min($mntPagerPages, $mntPagerPage + 2);
|
||
?>
|
||
<div class="mnt-pager" data-page="<?= $mntPagerPage ?>" data-pages="<?= $mntPagerPages ?>" data-total="<?= $mntPagerTotal ?>">
|
||
<div class="mnt-pager-info">
|
||
<?= $mntFirstRow ?>–<?= $mntLastRow ?> di <strong><?= $mntPagerTotal ?></strong>
|
||
</div>
|
||
|
||
<div class="mnt-pager-per">
|
||
<span class="text-muted">Per pagina:</span>
|
||
<?php foreach ([25, 50, 100] as $option): ?>
|
||
<?php if ($option === $mntPagerPer): ?>
|
||
<span class="mnt-pager-link is-current"><?= $option ?></span>
|
||
<?php else: ?>
|
||
<a class="mnt-pager-link" href="<?= mnt_h($mntPagerLink(1, $option)) ?>"><?= $option ?></a>
|
||
<?php endif; ?>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
|
||
<?php if ($mntPagerPages > 1): ?>
|
||
<nav class="mnt-pager-pages" aria-label="Paginazione">
|
||
<?php if ($mntPagerPage > 1): ?>
|
||
<a class="mnt-pager-link btn-page-prev" rel="prev" href="<?= mnt_h($mntPagerLink($mntPagerPage - 1)) ?>" aria-label="Precedente">‹</a>
|
||
<?php else: ?>
|
||
<span class="mnt-pager-link is-disabled">‹</span>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($mntWindowFrom > 1): ?>
|
||
<a class="mnt-pager-link" href="<?= mnt_h($mntPagerLink(1)) ?>">1</a>
|
||
<?php if ($mntWindowFrom > 2): ?><span class="mnt-pager-gap">…</span><?php endif; ?>
|
||
<?php endif; ?>
|
||
|
||
<?php for ($p = $mntWindowFrom; $p <= $mntWindowTo; $p++): ?>
|
||
<?php if ($p === $mntPagerPage): ?>
|
||
<span class="mnt-pager-link is-current" aria-current="page"><?= $p ?></span>
|
||
<?php else: ?>
|
||
<a class="mnt-pager-link" href="<?= mnt_h($mntPagerLink($p)) ?>"><?= $p ?></a>
|
||
<?php endif; ?>
|
||
<?php endfor; ?>
|
||
|
||
<?php if ($mntWindowTo < $mntPagerPages): ?>
|
||
<?php if ($mntWindowTo < $mntPagerPages - 1): ?><span class="mnt-pager-gap">…</span><?php endif; ?>
|
||
<a class="mnt-pager-link" href="<?= mnt_h($mntPagerLink($mntPagerPages)) ?>"><?= $mntPagerPages ?></a>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($mntPagerPage < $mntPagerPages): ?>
|
||
<a class="mnt-pager-link btn-page-next" rel="next" href="<?= mnt_h($mntPagerLink($mntPagerPage + 1)) ?>" aria-label="Successiva">›</a>
|
||
<?php else: ?>
|
||
<span class="mnt-pager-link is-disabled">›</span>
|
||
<?php endif; ?>
|
||
</nav>
|
||
<?php endif; ?>
|
||
</div>
|