From 49c209f7cd6b628527d44a12112a3080fbf4b42c Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Wed, 1 May 2024 22:37:16 -0400 Subject: [PATCH] Add feed-level item page (#13) - Tweak header style for mobile devices --- src/public/assets/style.css | 6 ++- src/public/feed/index.php | 14 +------ src/public/feed/items.php | 79 +++++++++++++++++++++++++++++++++++++ src/public/item.php | 9 +++-- src/start.php | 10 +++++ 5 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 src/public/feed/items.php diff --git a/src/public/assets/style.css b/src/public/assets/style.css index cd7a30b..3bfae2d 100644 --- a/src/public/assets/style.css +++ b/src/public/assets/style.css @@ -22,10 +22,14 @@ header { border-bottom-right-radius: .5rem; color: white; display: flex; - flex-flow: row nowrap; + flex-flow: row wrap; justify-content: space-between; align-items: baseline; + div { + margin-bottom: .25rem; + } + .title { font-size: 1.5rem; } diff --git a/src/public/feed/index.php b/src/public/feed/index.php index f7808da..59a73e5 100644 --- a/src/public/feed/index.php +++ b/src/public/feed/index.php @@ -13,11 +13,7 @@ Security::verifyUser($db); $feedId = $_GET['id'] ?? ''; if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { - $feed = Feed::retrieveById($feedId, $db); - if (!$feed) { - http_response_code(404); - die(); - } + if (!($feed = Feed::retrieveById($feedId, $db))) not_found(); $itemDelete = $db->prepare('DELETE FROM item WHERE feed_id = :feed'); $itemDelete->bindValue(':feed', $feed['id']); if (!$itemDelete->execute()) add_error(Data::error($db)['error']); @@ -54,13 +50,7 @@ if ($feedId == 'new') { $title = 'Edit RSS Feed'; if ($feedId == 'error') { $feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? '']; - } else { - $feed = Feed::retrieveById((int) $feedId, $db); - if (!$feed) { - http_response_code(404); - die(); - } - } + } elseif (!($feed = Feed::retrieveById((int) $feedId, $db))) not_found(); } page_head($title); ?> diff --git a/src/public/feed/items.php b/src/public/feed/items.php new file mode 100644 index 0000000..1b4a362 --- /dev/null +++ b/src/public/feed/items.php @@ -0,0 +1,79 @@ + TYPE_UNREAD, + array_key_exists('bookmarked', $_GET) => TYPE_BOOKMARKED, + default => TYPE_ALL +}; + +$extraSQL = match ($type) { + TYPE_UNREAD => ' AND is_read = 0', + TYPE_BOOKMARKED => ' AND is_bookmarked = 1', + default => '' +}; +$itemQuery = $db->prepare(<<bindValue(':feed', $feed['id']); +if (!($itemResult = $itemQuery->execute())) add_error(Data::error($db)['error']); +$item = $itemResult ? $itemResult->fetchArray(SQLITE3_ASSOC) : false; + +$queryParam = match ($type) { + TYPE_UNREAD => '&unread', + TYPE_BOOKMARKED => '&bookmarked', + default => '' +}; +$thisURL = urlencode("/feed/items?id={$feed['id']}$queryParam"); + +$listType = match ($type) { + TYPE_UNREAD => 'Unread', + TYPE_BOOKMARKED => 'Bookmarked', + default => '' +}; + +page_head(($type != TYPE_ALL ? "$listType Items | " : '') . strip_tags($feed['title'])); +if ($type == TYPE_ALL) { ?> +

+

+
Items
+
+


+ New   ' : ''?> + fetchArray(SQLITE3_ASSOC); + } + } else { ?> +

There are no items +

+close(); diff --git a/src/public/item.php b/src/public/item.php index ad2f042..21659c9 100644 --- a/src/public/item.php +++ b/src/public/item.php @@ -27,9 +27,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $keepUnread->execute(); } $db->close(); - frc_redirect('/'); + frc_redirect($_POST['from']); } +$from = $_GET['from'] ?? '/'; + if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { $deleteQuery = $db->prepare(<<<'SQL' DELETE FROM item @@ -47,7 +49,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { add_error(Data::error($db)['error']); } $db->close(); - frc_redirect('/'); + frc_redirect($from); } $query = $db->prepare(<<<'SQL' @@ -83,7 +85,8 @@ page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>
> - + +
diff --git a/src/start.php b/src/start.php index 551ead6..a2e3d4e 100644 --- a/src/start.php +++ b/src/start.php @@ -81,6 +81,7 @@ function page_head(string $title): void { <?=$title?> | Feed Reader Central + @@ -155,3 +156,12 @@ function hx_get(string $url, string $text, string $extraAttrs = ''): string { $attrs = $extraAttrs != '' ? " $extraAttrs" : ''; return "$text"; } + +/** + * Return a 404 Not Found + */ +#[NoReturn] +function not_found(): void { + http_response_code(404); + die('Not Found'); +}