Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Brickner/Podsumer/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions templates/episodes.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@
<?= substr(strip_tags($item['description']), 0, 360); ?>
</div>
<? endforeach ?>
<div class="py-4 font-bold">
<? if ($page > 1) { ?>
<a href="/episodes?page=<?= $page - 1 ?>">Previous</a>
<? } ?>
<? if ($page < $page_count) { ?>
<? if ($page > 1) { ?>&nbsp;|&nbsp;<? } ?>
<a href="/episodes?page=<?= $page + 1 ?>">Next</a>
<? } ?>
<span>&nbsp;&nbsp;Page <?= $page ?> of <?= $page_count ?></span>
</div>
<? endif ?>
</div>
16 changes: 16 additions & 0 deletions tests/Brickner/Podsumer/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down