First cut of feed mgt page (#13)

- Moved feed page to feed directory
- Implement feed deletion
- Add hx_get func for htmx-enabled but not dependent links
This commit is contained in:
Daniel J. Summers 2024-04-30 22:26:28 -04:00
parent b14399deb8
commit ab9537d0da
13 changed files with 159 additions and 75 deletions

View File

@ -1,7 +1,7 @@
<?php
/** The current Feed Reader Central version */
const FRC_VERSION = '1.0.0-alpha5';
const FRC_VERSION = '1.0.0-alpha6';
spl_autoload_register(function ($class) {
$file = implode(DIRECTORY_SEPARATOR, [__DIR__, 'lib', "$class.php"]);

View File

@ -79,26 +79,6 @@ class Data {
}
}
/**
* Retrieve a feed by its ID for the current user
*
* @param int $feedId The ID of the feed to retrieve
* @param ?SQLite3 $dbConn A database connection to use (optional; will use standalone if not provided)
* @return array|bool The data for the feed if found, false if not found
*/
public static function retrieveFeedById(int $feedId, ?SQLite3 $dbConn = null): array|bool {
$db = $dbConn ?? self::getConnection();
try {
$query = $db->prepare('SELECT * FROM feed WHERE id = :id AND user_id = :user');
$query->bindValue(':id', $feedId);
$query->bindValue(':user', $_SESSION[Key::USER_ID]);
$result = $query->execute();
return $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
} finally {
if (is_null($dbConn)) $db->close();
}
}
/**
* Return the last SQLite error message as a result array
*

View File

@ -514,4 +514,18 @@ class Feed {
return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];
}
/**
* Retrieve a feed by its ID for the current user
*
* @param int $feedId The ID of the feed to retrieve
* @param SQLite3 $db A database connection to use to retrieve the feed
* @return array|bool The data for the feed if found, false if not found
*/
public static function retrieveById(int $feedId, SQLite3 $db): array|bool {
$query = $db->prepare('SELECT * FROM feed WHERE id = :id AND user_id = :user');
$query->bindValue(':id', $feedId);
$query->bindValue(':user', $_SESSION[Key::USER_ID]);
return ($result = $query->execute()) ? $result->fetchArray(SQLITE3_ASSOC) : false;
}
}

View File

@ -101,6 +101,10 @@ article {
width: unset;
}
}
.meta {
font-size: .9rem;
}
}
article.docs {
line-height: 1.4rem;

View File

@ -7,10 +7,11 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Documentation'); ?>
<h1>Documentation Home</h1>
<article>
<p><a href=./the-cli>About the CLI</a> provides orientation on Feed Reader Central&rsquo;s command line interface
<p><a href=./security-modes>Configuring Security Modes</a> describes the three security modes and how to manage each
of them
<p><a href=./refresh-feeds>Refresh Feeds</a> has instructions on how feeds can be refreshed on a schedule
<p><?=hx_get('./the-cli', 'About the CLI')?> provides orientation on Feed Reader Central&rsquo;s command line
interface
<p><?=hx_get('./security-modes', 'Configuring Security Modes')?> describes the three security modes and how to
manage each of them
<p><?=hx_get('./refresh-feeds', 'Refresh Feeds')?> has instructions on how feeds can be refreshed on a schedule
</article><?php
page_foot();
$db->close();

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Refresh Feeds | Documentation'); ?>
<h1>Refresh Feeds</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<h2>Manual Feed Refresh</h2>
<p>Next to the &ldquo;Your Unread Items&rdquo; heading on the main page, there is a link labeled &ldquo;Refresh All

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Security Modes | Documentation'); ?>
<h1>Configuring Security Modes</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<h2>Security Modes</h2>
<p><strong>Single-User</strong> mode assumes that every connection to the application is the same person. It is

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('About the CLI | Documentation'); ?>
<h1>About the CLI</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<p>Feed Reader Central&rsquo;s low-friction design includes having many administrative tasks run in a terminal or
shell. &ldquo;CLI&rdquo; is short for &ldquo;Command Line Interface&rdquo;, and refers to commands that are run

View File

@ -1,32 +1,50 @@
<?php
/**
* Add/Edit Feed Page
* Add/Edit/Delete Feed Page
*
* Allows users to add or edit RSS feeds
* Allows users to add, edit, and delete feeds
*/
include '../start.php';
include '../../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
$feedId = $_GET['id'] ?? '';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$feed = Feed::retrieveById($feedId, $db);
if (!$feed) {
http_response_code(404);
die();
}
$itemDelete = $db->prepare('DELETE FROM item WHERE feed_id = :feed');
$itemDelete->bindValue(':feed', $feed['id']);
if (!$itemDelete->execute()) add_error(Data::error($db)['error']);
$feedDelete = $db->prepare('DELETE FROM feed WHERE id = :feed');
$feedDelete->bindValue(':feed', $feed['id']);
if ($feedDelete->execute()) {
add_info('Feed &ldquo;' . htmlentities($feed['title']) . '&rdquo; deleted successfully');
} else {
add_error(Data::error($db)['error']);
}
frc_redirect('/feeds');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$isNew = $_POST['id'] == 'new';
if ($isNew) {
$result = Feed::add($_POST['url'], $db);
} else {
$toEdit = Data::retrieveFeedById($_POST['id'], $db);
$toEdit = Feed::retrieveById($_POST['id'], $db);
$result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : ['error' => "Feed {$_POST['id']} not found"];
}
if (array_key_exists('ok', $result)) {
add_info('Feed saved successfully');
$feedId = $isNew ? $result['ok'] : $_POST['id'];
} else {
add_error($result['error']);
$feedId = 'error';
frc_redirect('/feeds');
}
add_error($result['error']);
$feedId = 'error';
}
if ($feedId == 'new') {
@ -37,7 +55,7 @@ if ($feedId == 'new') {
if ($feedId == 'error') {
$feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? ''];
} else {
$feed = Data::retrieveFeedById((int) $feedId, $db);
$feed = Feed::retrieveById((int) $feedId, $db);
if (!$feed) {
http_response_code(404);
die();
@ -48,7 +66,7 @@ if ($feedId == 'new') {
page_head($title); ?>
<h1><?=$title?></h1>
<article>
<form method=POST action=/feed hx-post=/feed>
<form method=POST action=/feed/ hx-post=/feed/>
<input type=hidden name=id value=<?=$feed['id']?>>
<label>
Feed URL

51
src/public/feeds.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* Feed Maintenance Page
*
* List feeds and provide links for maintenance actions
*/
include '../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
$feedQuery = $db->prepare('SELECT * FROM feed WHERE user_id = :user ORDER BY lower(title)');
$feedQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
if (!($feedResult = $feedQuery->execute())) {
add_error(Data::error($db)['error']);
}
page_head('Your Feeds'); ?>
<h1>Your Feeds</h1>
<article>
<p class=action_buttons><?=hx_get('/feed/?id=new', 'Add Feed')?></p><?php
if ($feedResult) {
while ($feed = $feedResult->fetchArray(SQLITE3_ASSOC)) {
$feedId = $feed['id'];
$countQuery = $db->prepare(<<<'SQL'
SELECT (SELECT COUNT(*) FROM item WHERE feed_id = :feed) AS total,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_read = 0) AS unread,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_bookmarked = 1) AS marked
SQL);
$countQuery->bindValue(':feed', $feed['id']);
$countResult = $countQuery->execute();
$counts = $countResult
? $countResult->fetchArray(SQLITE3_ASSOC) : ['total' => 0, 'unread' => 0, 'marked' => 0]; ?>
<p><strong><?=htmlentities($feed['title'])?></strong><br>
<span class=meta><em>Last Updated <?=date_time($feed['updated_on'])?> &bull;
As of <?=date_time($feed['checked_on'])?></em><br>
<?=hx_get("/feed/?id=$feedId", 'Edit')?> &bull; Read
<?=$counts['unread'] > 0 ? hx_get("/feed/items?id=$feedId&unread", 'Unread') : 'Unread'?>
(<?=$counts['unread']?>) |
<?=$counts['total'] > 0 ? hx_get("/feed/items?id=$feedId", 'All') : 'All'?> (<?=$counts['total']?>) |
<?=$counts['marked'] > 0 ? hx_get("/feed/items?id=$feedId&bookmarked", 'Bookmarked') : 'Bookmarked'?>
(<?=$counts['marked']?>) &bull;
<a href=/feed/?id=<?=$feedId?> hx-delete=/feed/?id=<?=$feedId?>
hx-confirm="Are you sure you want to delete &ldquo;<?=htmlentities($feed['title'], ENT_QUOTES)?>&rdquo;? This will remove the feed and all its items, including unread and bookmarked.">Delete</a>
</span><?php
}
} ?>
</article><?php
page_foot();
$db->close();

View File

@ -41,8 +41,8 @@ page_head('Your Unread Items'); ?>
<article><?php
if ($item) {
while ($item) { ?>
<p><a href=/item?id=<?=$item['id']?> hx-get=/item?id=<?=$item['id']?>><?=strip_tags($item['item_title'])?></a>
<br><?=htmlentities($item['feed_title'])?><br><small><em><?=date_time($item['as_of'])?></em></small><?php
<p><?=hx_get("/item?id={$item['id']}", strip_tags($item['item_title']))?><br>
<?=htmlentities($item['feed_title'])?><br><small><em><?=date_time($item['as_of'])?></em></small><?php
$item = $result->fetchArray(SQLITE3_ASSOC);
}
} else { ?>

View File

@ -83,7 +83,7 @@ page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>
<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>
<input type=hidden name=id value=<?=$_GET['id']?>>
<a href=/ hx-get="/">Done</a>
<?=hx_get('/', 'Done')?>
<button type=submit>Keep as New</button>
<button type=button hx-delete=/item>Delete</button>
</form>

View File

@ -42,18 +42,38 @@ function add_info(string $message): void {
$is_htmx = array_key_exists('HTTP_HX_REQUEST', $_SERVER)
&& !array_key_exists('HTTP_HX_HISTORY_RESTORE_REQUEST', $_SERVER);
/**
* Render the title bar for the page
*/
function title_bar(): void {
$version = match (true) {
str_ends_with(FRC_VERSION, '.0.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 4),
str_ends_with(FRC_VERSION, '.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 2),
default => FRC_VERSION
}; ?>
<header hx-target=#main hx-push-url=true>
<div><?=hx_get('/', 'Feed Reader Central', 'class=title')?><span class=version>v<?=$version?></span></div>
<div><?php
if (array_key_exists(Key::USER_ID, $_SESSION)) { ?>
<?=hx_get('/feeds', 'Feeds')?> | <?=hx_get('/docs/', 'Docs')?> |
<a href=/user/log-off>Log Off</a><?php
if ($_SESSION[Key::USER_EMAIL] != Security::SINGLE_USER_EMAIL) { ?>
| <?=$_SESSION[Key::USER_EMAIL]?><?php
}
} else { ?>
<?=hx_get('/user/log-on', 'Log On')?> | <?=hx_get('/docs/', 'Docs')?><?php
} ?>
</div>
</header>
<main id=main hx-target=this hx-push-url=true hx-swap="innerHTML show:window:top"><?php
}
/**
* Render the page title
* @param string $title The title of the page being displayed
*/
function page_head(string $title): void {
global $is_htmx;
$version = match (true) {
str_ends_with(FRC_VERSION, '.0.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 4),
str_ends_with(FRC_VERSION, '.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 2),
default => FRC_VERSION
};
//if ($is_htmx) header('HX-Push-Url: true');
?>
<!DOCTYPE html>
<html lang=en>
@ -65,35 +85,18 @@ function page_head(string $title): void {
} ?>
</head>
<body><?php
if (!$is_htmx) { ?>
<header hx-target=#main hx-push-url=true>
<div><a class=title href=/ hx-get="/">Feed Reader Central</a><span class=version>v<?=$version?></span></div>
<div><?php
if (array_key_exists(Key::USER_ID, $_SESSION)) { ?>
<a href=/feed?id=new hx-get=/feed?id=new>Add Feed</a> |
<a href=/docs/ hx-get=/docs/>Docs</a> |
<a href=/user/log-off hx-get=/user/log-off>Log Off</a><?php
if ($_SESSION[Key::USER_EMAIL] != Security::SINGLE_USER_EMAIL) { ?>
| <?=$_SESSION[Key::USER_EMAIL]?><?php
}
} else { ?>
<a href=/user/log-on hx-get=/user/log-on>Log On</a> | <a href=/docs/ hx-get=/docs/>Docs</a><?php
} ?>
</div>
</header>
<main id=main hx-target=this hx-push-url=true hx-swap="innerHTML show:window:top"><?php
if (!$is_htmx) title_bar();
if (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?>
<div class=user_messages><?php
array_walk($messages, function ($msg) { ?>
<div class=user_message>
<?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
<?=$msg['message']?>
</div><?php
}); ?>
</div><?php
$_SESSION[Key::USER_MSG] = [];
}
if (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?>
<div class=user_messages><?php
array_walk($messages, function ($msg) { ?>
<div class=user_message>
<?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
<?=$msg['message']?>
</div><?php
}); ?>
</div><?php
$_SESSION[Key::USER_MSG] = [];
}
}
/**
@ -139,3 +142,16 @@ function date_time(string $value): string {
return '(invalid date)';
}
}
/**
* Create an anchor tag with both `href` and `hx-get` attributes
*
* @param string $url The URL to which navigation should occur
* @param string $text The text for the link
* @param string $extraAttrs Extra attributes for the anchor tag (must be attribute-encoded)
* @return string The anchor tag with both `href` and `hx-get` attributes
*/
function hx_get(string $url, string $text, string $extraAttrs = ''): string {
$attrs = $extraAttrs != '' ? " $extraAttrs" : '';
return "<a href=\"$url\" hx-get=\"$url\"$attrs>$text</a>";
}