Adds proper pagination. Paginates logs page.

main
Yasen Pramatarov 2025-01-18 17:24:30 +02:00
parent 9fd2af6538
commit e85292b58f
3 changed files with 116 additions and 10 deletions

View File

@ -15,6 +15,10 @@ include '../app/includes/messages-show.php';
$has_system_access = ($userObject->hasRight($user_id, 'superuser') || $has_system_access = ($userObject->hasRight($user_id, 'superuser') ||
$userObject->hasRight($user_id, 'view app logs')); $userObject->hasRight($user_id, 'view app logs'));
// Get current page for pagination
$currentPage = $_REQUEST['page_num'] ?? 1;
$currentPage = (int)$currentPage;
// Get selected tab // Get selected tab
$selected_tab = $_REQUEST['tab'] ?? 'user'; $selected_tab = $_REQUEST['tab'] ?? 'user';
if ($selected_tab === 'system' && !$has_system_access) { if ($selected_tab === 'system' && !$has_system_access) {
@ -44,18 +48,34 @@ if ($scope === 'system' && isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
// pagination variables // pagination variables
$items_per_page = 15; $items_per_page = 15;
$browse_page = $_REQUEST['p'] ?? 1; $offset = ($currentPage - 1) * $items_per_page;
$browse_page = (int)$browse_page;
$offset = ($browse_page -1) * $items_per_page; // Get filter from request or default to empty
$filter = $_REQUEST['filter'] ?? '';
// Build params for pagination
$params = '';
if (!empty($filter)) {
$params .= '&filter=' . urlencode($filter);
}
if (isset($_REQUEST['from'])) {
$params .= '&from=' . urlencode($_REQUEST['from']);
}
if (isset($_REQUEST['until'])) {
$params .= '&until=' . urlencode($_REQUEST['until']);
}
if (isset($_REQUEST['tab'])) {
$params .= '&tab=' . urlencode($_REQUEST['tab']);
}
// prepare the result // prepare the result
$search = $logObject->readLog($user_id, $scope, $offset, $items_per_page, $filters); $search = $logObject->readLog($user_id, $scope, $offset, $items_per_page, $filter);
$search_all = $logObject->readLog($user_id, $scope, 0, '', $filters); $search_all = $logObject->readLog($user_id, $scope, '', '', $filter);
if (!empty($search)) { if (!empty($search)) {
// we get total items and number of pages // we get total items and number of pages
$item_count = count($search_all); $item_count = count($search_all);
$page_count = ceil($item_count / $items_per_page); $totalPages = ceil($item_count / $items_per_page);
$logs = array(); $logs = array();
$logs['records'] = array(); $logs['records'] = array();

View File

@ -56,10 +56,11 @@
</table> </table>
</div> </div>
<?php <?php
if ($widget['pagination'] && $item_count > $items_per_page) { if ($widget['pagination'] === true) {
include '../app/helpers/pagination.php'; include '../app/templates/pagination.php';
} }
} else { ?> ?>
<?php } else { ?>
<div class="alert alert-info m-3"> <div class="alert alert-info m-3">
<i class="fas fa-info-circle me-2"></i>No log entries found for the specified criteria. <i class="fas fa-info-circle me-2"></i>No log entries found for the specified criteria.
</div> </div>

View File

@ -0,0 +1,85 @@
<?php
/**
* Reusable pagination view/template component
* Required variables:
* $currentPage - Current page number
* $totalPages - Total number of pages
*/
// Ensure required variables are set
if (!isset($currentPage) || !isset($totalPages)) {
return;
}
// Number of page links to show before and after current page
$range = 2;
?>
<?php if ($totalPages > 1): ?>
<nav aria-label="Page navigation" class="mt-4">
<ul class="pagination justify-content-center d-flex flex-row gap-1">
<!-- First page -->
<?php if ($currentPage > 1): ?>
<li class="page-item">
<a class="page-link" href="<?= htmlspecialchars($app_root . '?page=' . $page . $params) ?>">First</a>
</li>
<li class="page-item">
<a class="page-link" href="<?= htmlspecialchars($app_root . '?page=' . $page . ($currentPage > 1 ? '&page_num=' . ($currentPage - 1) : '') . $params) ?>">«</a>
</li>
<?php else: ?>
<li class="page-item disabled">
<span class="page-link">First</span>
</li>
<li class="page-item disabled">
<span class="page-link">«</span>
</li>
<?php endif; ?>
<!-- Page numbers -->
<?php
for ($i = 1; $i <= $totalPages; $i++) {
// Show first, last, current page, 2 pages before and after current, and step pages (10, 20, etc.)
if ($i === 1 ||
$i === $totalPages ||
$i === $currentPage ||
$i === $currentPage - 1 ||
$i === $currentPage + 1 ||
$i === $currentPage - 2 ||
$i === $currentPage + 2 ||
($i % 10 === 0 && $i > 10)
) { ?>
<li class="page-item <?= $i === (int)$currentPage ? 'active' : '' ?>">
<a class="page-link" href="<?= htmlspecialchars($app_root . '?page=' . $page . ($i > 1 ? '&page_num=' . $i : '') . $params) ?>">
<?= $i ?>
</a>
</li>
<?php
} elseif ($i === $currentPage - 3 || $i === $currentPage + 3) {
?>
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
<?php
}
} ?>
<!-- Last page -->
<?php if ($currentPage < $totalPages): ?>
<li class="page-item">
<a class="page-link" href="<?= htmlspecialchars($app_root . '?page=' . $page . '&page_num=' . ($currentPage + 1) . $params) ?>">»</a>
</li>
<li class="page-item">
<a class="page-link" href="<?= htmlspecialchars($app_root . '?page=' . $page . '&page_num=' . $totalPages . $params) ?>">Last</a>
</li>
<?php else: ?>
<li class="page-item disabled">
<span class="page-link">»</span>
</li>
<li class="page-item disabled">
<span class="page-link">Last</span>
</li>
<?php endif; ?>
</ul>
</nav>
<?php endif; ?>