Add feed-level item page (#13)

- Tweak header style for mobile devices
This commit is contained in:
Daniel J. Summers 2024-05-01 22:37:16 -04:00
parent ab9537d0da
commit 49c209f7cd
5 changed files with 102 additions and 16 deletions

View File

@ -22,10 +22,14 @@ header {
border-bottom-right-radius: .5rem; border-bottom-right-radius: .5rem;
color: white; color: white;
display: flex; display: flex;
flex-flow: row nowrap; flex-flow: row wrap;
justify-content: space-between; justify-content: space-between;
align-items: baseline; align-items: baseline;
div {
margin-bottom: .25rem;
}
.title { .title {
font-size: 1.5rem; font-size: 1.5rem;
} }

View File

@ -13,11 +13,7 @@ Security::verifyUser($db);
$feedId = $_GET['id'] ?? ''; $feedId = $_GET['id'] ?? '';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$feed = Feed::retrieveById($feedId, $db); if (!($feed = Feed::retrieveById($feedId, $db))) not_found();
if (!$feed) {
http_response_code(404);
die();
}
$itemDelete = $db->prepare('DELETE FROM item WHERE feed_id = :feed'); $itemDelete = $db->prepare('DELETE FROM item WHERE feed_id = :feed');
$itemDelete->bindValue(':feed', $feed['id']); $itemDelete->bindValue(':feed', $feed['id']);
if (!$itemDelete->execute()) add_error(Data::error($db)['error']); if (!$itemDelete->execute()) add_error(Data::error($db)['error']);
@ -54,13 +50,7 @@ if ($feedId == 'new') {
$title = 'Edit RSS Feed'; $title = 'Edit RSS Feed';
if ($feedId == 'error') { if ($feedId == 'error') {
$feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? '']; $feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? ''];
} else { } elseif (!($feed = Feed::retrieveById((int) $feedId, $db))) not_found();
$feed = Feed::retrieveById((int) $feedId, $db);
if (!$feed) {
http_response_code(404);
die();
}
}
} }
page_head($title); ?> page_head($title); ?>

79
src/public/feed/items.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/**
* Feed Item List Page
*
* Lists items in a given feed (all, unread, or bookmarked)
*/
include '../../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
if (!($feed = Feed::retrieveById($_GET['id'], $db))) not_found();
/** Display a list of unread items for this feed */
const TYPE_UNREAD = 0;
/** Display a list of bookmarked items for this feed */
const TYPE_BOOKMARKED = 1;
/** Display all items for this feed */
const TYPE_ALL = 2;
$type = match (true) {
array_key_exists('unread', $_GET) => 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(<<<SQL
SELECT id, title, coalesce(updated_on, published_on) AS as_of, is_read, is_bookmarked
FROM item
WHERE feed_id = :feed$extraSQL
ORDER BY date(coalesce(updated_on, published_on)) DESC
SQL);
$itemQuery->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) { ?>
<h1><?=htmlentities($feed['title'])?></h1><?php
} else { ?>
<h1 class=item_heading><?=htmlentities($feed['title'])?></h1>
<div class=item_published><?=$listType?> Items</div><?php
} ?>
<article><?php
if ($item) {
while ($item) { ?>
<p><?=hx_get("/item?id={$item['id']}&from=$thisURL", strip_tags($item['title']))?><br>
<small><?=$item['is_read'] == 0 ? '<strong>New</strong> &nbsp; ' : ''?>
<em><?=date_time($item['as_of'])?></em></small><?php
$item = $itemResult->fetchArray(SQLITE3_ASSOC);
}
} else { ?>
<p><em>There are no <?=strtolower($listType)?> items</em><?php
} ?>
</article>
<?php
page_foot();
$db->close();

View File

@ -27,9 +27,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$keepUnread->execute(); $keepUnread->execute();
} }
$db->close(); $db->close();
frc_redirect('/'); frc_redirect($_POST['from']);
} }
$from = $_GET['from'] ?? '/';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$deleteQuery = $db->prepare(<<<'SQL' $deleteQuery = $db->prepare(<<<'SQL'
DELETE FROM item DELETE FROM item
@ -47,7 +49,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
add_error(Data::error($db)['error']); add_error(Data::error($db)['error']);
} }
$db->close(); $db->close();
frc_redirect('/'); frc_redirect($from);
} }
$query = $db->prepare(<<<'SQL' $query = $db->prepare(<<<'SQL'
@ -83,7 +85,8 @@ page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>
<div class=item_content><?=str_replace('<a ', '<a target=_blank rel=noopener ', $item['content'])?></div> <div class=item_content><?=str_replace('<a ', '<a target=_blank rel=noopener ', $item['content'])?></div>
<form class=action_buttons action=/item method=POST hx-post=/item> <form class=action_buttons action=/item method=POST hx-post=/item>
<input type=hidden name=id value=<?=$_GET['id']?>> <input type=hidden name=id value=<?=$_GET['id']?>>
<?=hx_get('/', 'Done')?> <input type=hidden name=from value="<?=$from?>">
<?=hx_get($from, 'Done')?>
<button type=submit>Keep as New</button> <button type=submit>Keep as New</button>
<button type=button hx-delete=/item>Delete</button> <button type=button hx-delete=/item>Delete</button>
</form> </form>

View File

@ -81,6 +81,7 @@ function page_head(string $title): void {
<title><?=$title?> | Feed Reader Central</title><?php <title><?=$title?> | Feed Reader Central</title><?php
if (!$is_htmx) { ?> if (!$is_htmx) { ?>
<meta name=viewport content="width=device-width, initial-scale=1"> <meta name=viewport content="width=device-width, initial-scale=1">
<meta name=htmx-config content='{"historyCacheSize":0}'>
<link href=/assets/style.css rel=stylesheet><?php <link href=/assets/style.css rel=stylesheet><?php
} ?> } ?>
</head> </head>
@ -155,3 +156,12 @@ function hx_get(string $url, string $text, string $extraAttrs = ''): string {
$attrs = $extraAttrs != '' ? " $extraAttrs" : ''; $attrs = $extraAttrs != '' ? " $extraAttrs" : '';
return "<a href=\"$url\" hx-get=\"$url\"$attrs>$text</a>"; return "<a href=\"$url\" hx-get=\"$url\"$attrs>$text</a>";
} }
/**
* Return a 404 Not Found
*/
#[NoReturn]
function not_found(): void {
http_response_code(404);
die('Not Found');
}