mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
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
This commit is contained in:
1 parent
47667e56ab
commit
2a6f27c3c4
5 files changed
+252
-3
No files matched your search
@@ -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<int, string> $pages
|
||||
* @param array<string, mixed> $query_params
|
||||
* @param array{first?: string, previous?: string, next?: string} $aria_labels
|
||||
* @return array<int, array<string, bool|int|string>>
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
||||
+39
-3
@@ -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');
|
||||
|
||||
@@ -34,6 +34,11 @@
|
||||
{/if}
|
||||
|
||||
<div class="card-body">
|
||||
{if isset($pagination) && $pagination}
|
||||
<div class="mb-3">
|
||||
{include file="_pagination.tpl" pagination=$pagination pagination_label=$pagination_label}
|
||||
</div>
|
||||
{/if}
|
||||
{if $table == 'domain'}
|
||||
<div class="domain-list-scroll">
|
||||
{elseif $table == 'alias' || $table == 'aliasdomain'}
|
||||
@@ -182,6 +187,11 @@
|
||||
</tbody>
|
||||
</table>
|
||||
{if $table == 'domain' || $table == 'alias' || $table == 'aliasdomain'}</div>{/if}
|
||||
{if isset($pagination) && $pagination}
|
||||
<div class="mt-3">
|
||||
{include file="_pagination.tpl" pagination=$pagination pagination_label=$pagination_label}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="card-footer">
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class ListTemplatePaginationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testRendersPaginationAboveAndBelowGenericList(): void
|
||||
{
|
||||
$html = $this->renderList([
|
||||
[
|
||||
'label' => 'aa-cz',
|
||||
'url' => '?table=domain&limit=0#main_div',
|
||||
'active' => true,
|
||||
],
|
||||
[
|
||||
'label' => 'de-zz',
|
||||
'url' => '?table=domain&limit=10#main_div',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(2, substr_count($html, '<nav aria-label="Domains">'));
|
||||
$this->assertSame(2, substr_count($html, 'aria-current="page">aa-cz'));
|
||||
$this->assertSame(2, substr_count($html, 'href="?table=domain&limit=10#main_div"'));
|
||||
}
|
||||
|
||||
public function testOmitsPaginationForSinglePageList(): void
|
||||
{
|
||||
$html = $this->renderList([]);
|
||||
|
||||
$this->assertStringNotContainsString('<nav ', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, bool|string>> $pagination
|
||||
*/
|
||||
private function renderList(array $pagination): string
|
||||
{
|
||||
$original_session = $_SESSION;
|
||||
$original_language = Config::read_array('__LANG');
|
||||
|
||||
try {
|
||||
$_SESSION = [
|
||||
'sessid' => [
|
||||
'roles' => ['global-admin'],
|
||||
'username' => 'admin',
|
||||
],
|
||||
];
|
||||
Config::write('__LANG', ['download_csv' => 'Download CSV']);
|
||||
|
||||
$smarty = PFASmarty::getInstance();
|
||||
$smarty->assign('id_div', 'main_div');
|
||||
$smarty->assign('admin_list', []);
|
||||
$smarty->assign('msg', [
|
||||
'show_simple_search' => false,
|
||||
'list_header' => '',
|
||||
'can_create' => false,
|
||||
]);
|
||||
$smarty->assign('table', 'domain');
|
||||
$smarty->assign('struct', []);
|
||||
$smarty->assign('items', []);
|
||||
$smarty->assign('id_field', 'domain');
|
||||
$smarty->assign('formconf', ['create_button' => '']);
|
||||
$smarty->assign('search', []);
|
||||
$smarty->assign('searchmode', []);
|
||||
$smarty->assign('pagination', $pagination);
|
||||
$smarty->assign('pagination_label', 'Domains');
|
||||
$smarty->assign('domain_selected', '');
|
||||
|
||||
ob_start();
|
||||
$smarty->display('list.tpl');
|
||||
return (string)ob_get_clean();
|
||||
} finally {
|
||||
$_SESSION = $original_session;
|
||||
Config::write('__LANG', $original_language);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
class PageBrowserPaginationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testEmptyPageBrowserDoesNotRenderPagination(): void
|
||||
{
|
||||
$this->assertSame([], page_browser_pagination([], 0, 10, [], 'main_div'));
|
||||
}
|
||||
|
||||
public function testFirstPagePreservesGenericListState(): void
|
||||
{
|
||||
$pagination = page_browser_pagination(
|
||||
['aa-cz', 'de-pf', 'pg-zz'],
|
||||
0,
|
||||
10,
|
||||
[
|
||||
'table' => 'alias',
|
||||
'username' => 'admin@example.com',
|
||||
'search' => ['_' => 'sales & support'],
|
||||
'searchmode' => ['_' => '='],
|
||||
],
|
||||
'main_div',
|
||||
[
|
||||
'first' => 'First page',
|
||||
'previous' => 'Previous page',
|
||||
'next' => 'Next page',
|
||||
]
|
||||
);
|
||||
|
||||
$this->assertTrue($pagination[0]['disabled']);
|
||||
$this->assertTrue($pagination[1]['disabled']);
|
||||
$this->assertTrue($pagination[2]['active']);
|
||||
$this->assertSame(
|
||||
'?table=alias&username=admin%40example.com&search%5B_%5D=sales%20%26%20support&searchmode%5B_%5D=%3D&limit=10#main_div',
|
||||
$pagination[3]['url']
|
||||
);
|
||||
$this->assertSame('Next page', $pagination[5]['aria']);
|
||||
}
|
||||
|
||||
public function testMiddlePageBuildsFirstPreviousAndNextLinks(): void
|
||||
{
|
||||
$pagination = page_browser_pagination(
|
||||
['aa-cz', 'de-pf', 'pg-zz'],
|
||||
10,
|
||||
10,
|
||||
['table' => 'domain'],
|
||||
'main_div'
|
||||
);
|
||||
|
||||
$this->assertFalse($pagination[0]['disabled']);
|
||||
$this->assertSame('?table=domain&limit=0#main_div', $pagination[0]['url']);
|
||||
$this->assertSame('?table=domain&limit=0#main_div', $pagination[1]['url']);
|
||||
$this->assertTrue($pagination[3]['active']);
|
||||
$this->assertSame('?table=domain&limit=20#main_div', $pagination[5]['url']);
|
||||
$this->assertFalse($pagination[5]['disabled']);
|
||||
}
|
||||
|
||||
public function testRejectsInvalidPageSize(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
page_browser_pagination(['aa-zz'], 0, 0, [], 'main_div');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user