feat(ext-blog): Add pagination and search to blog post listing

The PostsRepository now includes methods for fetching paginated and searched posts. The admin and public views include search forms, pagination controls, and display total counts instead of just post counts. Tests verify the new functionality including special character search handling and pagination limits.
This commit is contained in:
2026-12-02 10:20:36 +00:00
parent f35645132e
commit 32582e9279
4 changed files with 258 additions and 7 deletions
@@ -1,10 +1,32 @@
<?php
$posts = (new \Plugi\Extensions\Blog\PostsRepository())->getPublishedForListing();
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
$search = is_string($_GET['q'] ?? null) ? trim($_GET['q']) : '';
$pageValue = $_GET['page'] ?? null;
$page = is_string($pageValue) ? filter_var($pageValue, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
$page = $page === false ? 1 : $page;
$listingPath = isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']) ? phpb_full_url(phpb_current_relative_url(true)) : '/';
$pageResult = $postsRepository->getPageForListing($search, $page);
$totalPages = max(1, (int) ceil($pageResult['total'] / \Plugi\Extensions\Blog\PostsRepository::POSTS_PER_PAGE));
if ($page > $totalPages) {
$page = $totalPages;
}
$posts = $pageResult['posts'];
$searchParameters = $search === '' ? [] : ['q' => $search];
?>
<section class="space-y-6" aria-label="Blog posts">
<?php if (empty($posts)): ?>
<div class="alert alert-info" role="status">No published posts yet.</div>
<form class="flex flex-col gap-3 sm:flex-row sm:items-end" method="get" action="<?= phpb_e($listingPath) ?>">
<label class="flex w-full max-w-md flex-col gap-2">
<span class="text-sm font-medium">Search posts</span>
<input class="input w-full" type="search" name="q" value="<?= phpb_e($search) ?>" />
</label>
<button class="btn" type="submit">Search</button>
</form>
<?php if ($pageResult['total'] === 0): ?>
<div class="alert alert-info" role="status">
<?= $search === '' ? 'No published posts yet.' : 'No published posts match &quot;' . phpb_e($search) . '&quot;.' ?>
</div>
<?php else: ?>
<div class="grid gap-6 md:grid-cols-2">
<?php foreach ($posts as $post): ?>
@@ -38,4 +60,22 @@ $posts = (new \Plugi\Extensions\Blog\PostsRepository())->getPublishedForListing(
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($totalPages > 1): ?>
<nav class="flex justify-center" aria-label="Blog post pages">
<div class="join">
<?php if ($page > 1): ?>
<a class="join-item btn btn-sm" rel="prev" href="<?= phpb_e($listingPath . '?' . http_build_query($searchParameters + ['page' => $page - 1])) ?>">Previous</a>
<?php else: ?>
<span class="join-item btn btn-sm btn-disabled" aria-disabled="true">Previous</span>
<?php endif; ?>
<span class="join-item btn btn-sm btn-disabled" aria-current="page">Page <?= phpb_e($page) ?> of <?= phpb_e($totalPages) ?></span>
<?php if ($page < $totalPages): ?>
<a class="join-item btn btn-sm" rel="next" href="<?= phpb_e($listingPath . '?' . http_build_query($searchParameters + ['page' => $page + 1])) ?>">Next</a>
<?php else: ?>
<span class="join-item btn btn-sm btn-disabled" aria-disabled="true">Next</span>
<?php endif; ?>
</div>
</nav>
<?php endif; ?>
</section>