From 2a6f27c3c41611d995f0abaca323de1f2d673d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Cort=C3=A9s=20Cano?= <38475681+TrapoSAMA@users.noreply.github.com> Date: Sat, 29 Aug 2026 10:09:23 -0400 Subject: [PATCH] Use shared pagination in generic lists (#1127) Extend bootstrap pagination to list.php #1090, #1093, and this PR cover all three pagination surfaces tracked in #1064. thanks @TrapoSAMA --- functions.inc.php | 65 ++++++++++++++++++++++++ public/list.php | 42 ++++++++++++++-- templates/list.tpl | 10 ++++ tests/ListTemplatePaginationTest.php | 75 ++++++++++++++++++++++++++++ tests/PageBrowserPaginationTest.php | 63 +++++++++++++++++++++++ 5 files changed, 252 insertions(+), 3 deletions(-) create mode 100644 tests/ListTemplatePaginationTest.php create mode 100644 tests/PageBrowserPaginationTest.php diff --git a/functions.inc.php b/functions.inc.php index 413277b5..b9e26155 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -2085,6 +2085,71 @@ function viewlog_domain_condition(bool $show_all, bool $is_global_admin, string return ''; } +/** + * Build normalized pagination items for an alphabetical page browser. + * + * @param array $pages + * @param array $query_params + * @param array{first?: string, previous?: string, next?: string} $aria_labels + * @return array> + */ +function page_browser_pagination( + array $pages, + int $offset, + int $page_size, + array $query_params, + string $anchor, + array $aria_labels = [] +): array { + if (count($pages) === 0) { + return []; + } + if ($page_size < 1) { + throw new InvalidArgumentException('Page size must be greater than zero'); + } + + $current_page = intdiv(max(0, $offset), $page_size); + $last_page = count($pages) - 1; + + $url_for_page = static function (int $page) use ($query_params, $page_size, $anchor): string { + $query_params['limit'] = $page * $page_size; + $query = http_build_query($query_params, '', '&', PHP_QUERY_RFC3986); + return '?' . htmlspecialchars($query, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '#' . rawurlencode($anchor); + }; + + $pagination = [ + [ + 'label' => '«', + 'url' => $url_for_page(0), + 'disabled' => $current_page === 0, + 'aria' => $aria_labels['first'] ?? '', + ], + [ + 'label' => '‹', + 'url' => $url_for_page(max(0, $current_page - 1)), + 'disabled' => $current_page === 0, + 'aria' => $aria_labels['previous'] ?? '', + ], + ]; + + foreach ($pages as $page => $label) { + $pagination[] = [ + 'label' => $label, + 'url' => $url_for_page($page), + 'active' => $page === $current_page, + ]; + } + + $pagination[] = [ + 'label' => '›', + 'url' => $url_for_page(min($last_page, $current_page + 1)), + 'disabled' => $current_page >= $last_page, + 'aria' => $aria_labels['next'] ?? '', + ]; + + return $pagination; +} + /** * Compute the page numbers to display in a windowed pager. * diff --git a/public/list.php b/public/list.php index 18f2be72..a344bfa4 100644 --- a/public/list.php +++ b/public/list.php @@ -89,10 +89,44 @@ if (array_key_exists('reset_search', $_GET)) { $_SESSION["search_$table"] = $search; $_SESSION["searchmode_$table"] = $searchmode; -if (count($search)) { - $handler->getList($search, $searchmode); +$condition = count($search) ? $search : ''; +$pagination = []; + +if (safeget('output') == 'csv') { + // CSV exports keep the historical behavior and contain the complete list. + $handler->getList($condition, $searchmode); } else { - $handler->getList(''); + $page_size = (int)$CONF['page_size']; + $pagebrowser = $handler->getPagebrowser($condition, $searchmode); + $last_offset = max(0, (count($pagebrowser) - 1) * $page_size); + $offset = max(0, (int)safeget('limit', '0')); + $offset = min($last_offset, intdiv($offset, $page_size) * $page_size); + + $handler->getList($condition, $searchmode, $page_size, $offset); + + $pagination_params = ['table' => $table]; + if ($is_superadmin && $formconf['required_role'] != 'global-admin') { + $pagination_params['username'] = $username; + } + if (count($search)) { + $pagination_params['search'] = $search; + } + if (count($searchmode)) { + $pagination_params['searchmode'] = $searchmode; + } + + $pagination = page_browser_pagination( + $pagebrowser, + $offset, + $page_size, + $pagination_params, + 'main_div', + [ + 'first' => $PALANG['pOverview_up_arrow'], + 'previous' => $PALANG['pOverview_left_arrow'], + 'next' => $PALANG['pOverview_right_arrow'], + ] + ); } $items = $handler->result(); @@ -163,6 +197,8 @@ $smarty->assign('id_field', $handler->getId_field()); $smarty->assign('formconf', $formconf); $smarty->assign('search', $search); $smarty->assign('searchmode', $searchmode); +$smarty->assign('pagination', $pagination); +$smarty->assign('pagination_label', $PALANG[$handler->getMsg()['list_header'] ?? ''] ?? 'Pagination'); $smarty->assign('domain_selected', ''); /* stop list-virtual.tpl triggering a PHP notice */ $smarty->display('index.tpl'); diff --git a/templates/list.tpl b/templates/list.tpl index be4087de..9b844a5e 100644 --- a/templates/list.tpl +++ b/templates/list.tpl @@ -34,6 +34,11 @@ {/if}
+ {if isset($pagination) && $pagination} +
+ {include file="_pagination.tpl" pagination=$pagination pagination_label=$pagination_label} +
+ {/if} {if $table == 'domain'}
{elseif $table == 'alias' || $table == 'aliasdomain'} @@ -182,6 +187,11 @@ {if $table == 'domain' || $table == 'alias' || $table == 'aliasdomain'}
{/if} + {if isset($pagination) && $pagination} +
+ {include file="_pagination.tpl" pagination=$pagination pagination_label=$pagination_label} +
+ {/if}