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:
@@ -8,6 +8,8 @@ use Plugi\Repositories\BaseRepository;
|
||||
|
||||
class PostsRepository extends BaseRepository
|
||||
{
|
||||
public const POSTS_PER_PAGE = 10;
|
||||
|
||||
protected $table = 'ext__blog_posts';
|
||||
|
||||
public function create(array $data)
|
||||
@@ -39,6 +41,51 @@ class PostsRepository extends BaseRepository
|
||||
);
|
||||
}
|
||||
|
||||
public function getPageForAdmin(string $search, int $page): array
|
||||
{
|
||||
return $this->getPostPage($search, $page, false);
|
||||
}
|
||||
|
||||
public function getPageForListing(string $search, int $page): array
|
||||
{
|
||||
return $this->getPostPage($search, $page, true);
|
||||
}
|
||||
|
||||
private function getPostPage(string $search, int $page, bool $publishedOnly): array
|
||||
{
|
||||
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
|
||||
$from = $publishedOnly
|
||||
? "FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id"
|
||||
: "FROM {$this->table} posts";
|
||||
$filters = [];
|
||||
$parameters = [];
|
||||
|
||||
if ($publishedOnly) {
|
||||
$filters[] = 'posts.status = ?';
|
||||
$parameters[] = 'published';
|
||||
}
|
||||
if (trim($search) !== '') {
|
||||
$filters[] = '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\' OR posts.body LIKE ? ESCAPE \'!\')';
|
||||
$pattern = '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($search)) . '%';
|
||||
array_push($parameters, $pattern, $pattern, $pattern, $pattern);
|
||||
}
|
||||
|
||||
$where = $filters === [] ? '' : ' WHERE ' . implode(' AND ', $filters);
|
||||
$total = $this->db->select("SELECT COUNT(*) AS total {$from}{$where}", $parameters)[0]['total'] ?? 0;
|
||||
$page = min(max(1, $page), max(1, (int) ceil((int) $total / self::POSTS_PER_PAGE)));
|
||||
$offset = ($page - 1) * self::POSTS_PER_PAGE;
|
||||
$select = $publishedOnly
|
||||
? 'SELECT posts.*, categories.name AS category_name'
|
||||
: 'SELECT posts.*';
|
||||
$order = $publishedOnly ? 'posts.published_at DESC, posts.id DESC' : 'posts.updated_at DESC, posts.id DESC';
|
||||
$posts = $this->db->select(
|
||||
"{$select} {$from}{$where} ORDER BY {$order} LIMIT " . self::POSTS_PER_PAGE . " OFFSET {$offset}",
|
||||
$parameters
|
||||
);
|
||||
|
||||
return ['posts' => $posts, 'total' => (int) $total];
|
||||
}
|
||||
|
||||
public function findPublishedBySlug(string $slug): ?array
|
||||
{
|
||||
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
|
||||
|
||||
@@ -3,7 +3,20 @@
|
||||
use Plugi\Extensions\Blog\CategoriesRepository;
|
||||
use Plugi\Extensions\Blog\PostsRepository;
|
||||
|
||||
$posts = (new PostsRepository())->getAllForAdmin();
|
||||
$postsRepository = new PostsRepository();
|
||||
$search = is_string($_GET['q'] ?? null) ? trim($_GET['q']) : '';
|
||||
$pageValue = $_GET['posts_page'] ?? null;
|
||||
$page = is_string($pageValue) ? filter_var($pageValue, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
||||
$page = $page === false ? 1 : $page;
|
||||
$pageResult = $postsRepository->getPageForAdmin($search, $page);
|
||||
$totalPages = max(1, (int) ceil($pageResult['total'] / PostsRepository::POSTS_PER_PAGE));
|
||||
if ($page > $totalPages) {
|
||||
$page = $totalPages;
|
||||
}
|
||||
$posts = $pageResult['posts'];
|
||||
$searchParameters = $search === '' ? [] : ['q' => $search];
|
||||
$adminPostsUrl = phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']);
|
||||
$websiteManagerUrl = phpb_url('website_manager');
|
||||
$categories = (new CategoriesRepository())->getAllForAdmin();
|
||||
$categoryNames = [];
|
||||
foreach ($categories as $category) {
|
||||
@@ -15,7 +28,7 @@ foreach ($categories as $category) {
|
||||
<div class="space-y-1">
|
||||
<p class="text-xs font-semibold uppercase tracking-[0.18em] text-primary">Blog extension</p>
|
||||
<h2 class="text-xl font-bold tracking-tight text-base-content">Posts</h2>
|
||||
<p class="text-sm text-base-content/60"><?= count($posts) ?> post<?= count($posts) !== 1 ? 's' : '' ?></p>
|
||||
<p class="text-sm text-base-content/60"><?= phpb_e((string) $pageResult['total']) ?> post<?= $pageResult['total'] !== 1 ? 's' : '' ?></p>
|
||||
</div>
|
||||
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'editPost']) ?>" class="btn btn-primary">
|
||||
<i class="ph-duotone ph-plus" aria-hidden="true"></i>
|
||||
@@ -23,7 +36,22 @@ foreach ($categories as $category) {
|
||||
</a>
|
||||
</header>
|
||||
|
||||
<?php if (empty($posts)): ?>
|
||||
<form class="flex flex-col gap-3 sm:flex-row sm:items-end" method="get" action="<?= phpb_e($websiteManagerUrl) ?>">
|
||||
<input type="hidden" name="tab" value="ext--blog">
|
||||
<input type="hidden" name="action" value="posts">
|
||||
<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 && $search !== ''): ?>
|
||||
<div class="alert alert-info" role="status">
|
||||
No posts found for "<?= phpb_e($search) ?>".
|
||||
<a class="link link-hover" href="<?= phpb_e($adminPostsUrl) ?>">Clear search</a>
|
||||
</div>
|
||||
<?php elseif ($pageResult['total'] === 0): ?>
|
||||
<div class="card border border-dashed border-base-300 bg-base-100 shadow-sm" role="status">
|
||||
<div class="card-body items-center gap-3 px-5 py-12 text-center sm:py-16">
|
||||
<span class="grid size-14 place-items-center rounded-2xl bg-primary/10 text-primary">
|
||||
@@ -56,7 +84,7 @@ foreach ($categories as $category) {
|
||||
<div class="font-medium"><?= phpb_e($post['title']) ?></div>
|
||||
<div class="text-xs text-base-content/60">/<?= phpb_e($post['slug']) ?></div>
|
||||
</td>
|
||||
<td><?= phpb_e($categoryNames[$post['category_id']] ?? '—') ?></td>
|
||||
<td><?= phpb_e($post['category_id'] === null ? '—' : ($categoryNames[$post['category_id']] ?? '—')) ?></td>
|
||||
<td><span class="badge <?= $post['status'] === 'published' ? 'badge-success' : 'badge-warning' ?>"><?= phpb_e(ucfirst($post['status'])) ?></span></td>
|
||||
<td><?= phpb_e($post['published_at'] ?? '—') ?></td>
|
||||
<td>
|
||||
@@ -79,4 +107,22 @@ foreach ($categories as $category) {
|
||||
</table>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($totalPages > 1): ?>
|
||||
<nav class="flex justify-center" aria-label="Admin post pages">
|
||||
<div class="join">
|
||||
<?php if ($page > 1): ?>
|
||||
<a class="join-item btn btn-sm" rel="prev" href="<?= phpb_e($adminPostsUrl . '&' . http_build_query($searchParameters + ['posts_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($adminPostsUrl . '&' . http_build_query($searchParameters + ['posts_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>
|
||||
Reference in New Issue
Block a user