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';
|
||||
|
||||
Reference in New Issue
Block a user