From 5bb8b14a15daffdd2a22477f28a68b315c1b4693 Mon Sep 17 00:00:00 2001 From: Josh Brickner <15388+leftouterjoins@users.noreply.github.com> Date: Tue, 17 Jun 2025 08:32:34 -0600 Subject: [PATCH] Add pagination to episodes page --- src/Brickner/Podsumer/State.php | 18 ++++++++++++++++++ templates/episodes.html.php | 10 ++++++++++ tests/Brickner/Podsumer/StateTest.php | 16 ++++++++++++++++ www/index.php | 6 +++++- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Brickner/Podsumer/State.php b/src/Brickner/Podsumer/State.php index 70e1d71..223da98 100755 --- a/src/Brickner/Podsumer/State.php +++ b/src/Brickner/Podsumer/State.php @@ -214,6 +214,24 @@ public function getAllItems(): array return (false === $result) ? [] : $result; } + public function getAllItemsPage(int $limit, int $page = 1): array + { + $offset = ($page - 1) * $limit; + $sql = 'SELECT items.name, items.feed_id, items.id, items.guid, items.audio_url, items.audio_file, COALESCE(items.image, feeds.image) AS image, items.size, items.published, items.description, items.playback_position, feeds.name AS feed_name FROM items JOIN feeds ON feeds.id = items.feed_id ORDER BY items.published DESC LIMIT :limit OFFSET :offset'; + $params = ['limit' => $limit, 'offset' => $offset]; + $result = $this->query($sql, $params); + + return (false === $result) ? [] : $result; + } + + public function countAllItems(): int + { + $sql = 'SELECT COUNT(*) AS count FROM items'; + $result = $this->query($sql); + + return intval($result[0]['count'] ?? 0); + } + public function getFeedItemsPage(int $feed_id, int $limit, int $page = 1): array { $offset = ($page - 1) * $limit; diff --git a/templates/episodes.html.php b/templates/episodes.html.php index d07580d..6a00425 100644 --- a/templates/episodes.html.php +++ b/templates/episodes.html.php @@ -28,5 +28,15 @@ +
+ 1) { ?> + Previous + + + 1) { ?> |  + Next + +   Page of +
diff --git a/tests/Brickner/Podsumer/StateTest.php b/tests/Brickner/Podsumer/StateTest.php index c199ebd..c5b72c3 100644 --- a/tests/Brickner/Podsumer/StateTest.php +++ b/tests/Brickner/Podsumer/StateTest.php @@ -100,6 +100,22 @@ public function testGetFeedItemsPage() $this->assertEquals(2, count($items)); } + public function testGetAllItemsPage() + { + $this->feed = new Feed(self::TEST_FEED_URL); + $this->state->addFeed($this->feed); + $items = $this->state->getAllItemsPage(2, 1); + $this->assertEquals(2, count($items)); + } + + public function testCountAllItems() + { + $this->feed = new Feed(self::TEST_FEED_URL); + $this->state->addFeed($this->feed); + $count = $this->state->countAllItems(); + $this->assertEquals(4, $count); + } + public function testGetFeedByHash() { $this->feed = new Feed(self::TEST_FEED_URL); diff --git a/www/index.php b/www/index.php index 7d3077b..3c9d6eb 100755 --- a/www/index.php +++ b/www/index.php @@ -44,9 +44,13 @@ function home(array $args): void function episodes(array $args): void { global $main; + $page = isset($args['page']) ? max(1, intval($args['page'])) : 1; + $per_page = intval($main->getConf('podsumer', 'items_per_page')) ?: 10; $vars = [ - 'items' => $main->getState()->getAllItems() + 'items' => $main->getState()->getAllItemsPage($per_page, $page), + 'page' => $page, + 'page_count' => max(1, ceil($main->getState()->countAllItems() / $per_page)) ]; Template::render($main, 'episodes', $vars);