Daniel J. Summers d06249aecd beta4 changes (#26)
These changes are mostly in underlying libraries; however, this now uses the [inspired by F#](https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp) library to handle the feed parsing pipeline and optional return values

Reviewed-on: #26
2024-08-06 23:20:17 +00:00

83 lines
2.4 KiB
PHP

<?php
/**
* Add/Edit/Delete Feed Page
*
* Allows users to add, edit, and delete feeds
*
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\InspiredByFSharp\Result;
use BitBadger\PDODocument\{Delete, DocumentException, Field};
use FeedReaderCentral\{Feed, Security, Table};
include '../../start.php';
Security::verifyUser();
$feedId = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
switch ($_SERVER['REQUEST_METHOD']) {
case 'DELETE':
try {
$feed = Feed::retrieveById($feedId)->getOrCall(not_found(...));
Delete::byFields(Table::Item, [Field::EQ('feed_id', $feed->id)]);
Delete::byId(Table::Feed, $feed->id);
add_info('Feed &ldquo;' . htmlentities($feed->title) . '&rdquo; deleted successfully');
frc_redirect('/feeds');
} catch (DocumentException $ex) {
add_error("$ex");
}
break;
case 'POST':
try {
if ((int)$_POST['id'] === -1) {
$result = Feed::add($_POST['url']);
} else {
$feedId = (int)$_POST['id'];
$toEdit = Feed::retrieveById($feedId);
$result = $toEdit->isSome()
? Feed::update($toEdit->get(), $_POST['url'])
: Result::Error("Feed $feedId not found");
}
$result->iter(function () {
add_info('Feed saved successfully');
frc_redirect('/feeds');
});
add_error($result->getError());
$feedId = 'error';
} catch (DocumentException $ex) {
add_error("$ex");
}
break;
}
if ($feedId == -1) {
$title = 'Add RSS Feed';
$feed = new Feed(id: -1);
} else {
$title = 'Edit RSS Feed';
$feed = $feedId == 'error'
? new Feed(id: (int)$_POST['id'], url: $_POST['url'] ?? '')
: Feed::retrieveById((int)$feedId)->getOrCall(not_found(...));
}
page_head($title); ?>
<h1><?=$title?></h1>
<article>
<form method=POST action=/feed/ hx-post=/feed/>
<input type=hidden name=id value=<?=$feed->id?>>
<label>
Feed URL
<input type=url name=url required autofocus value="<?=$feed->url?>">
</label>
<span class=break></span>
<button type=submit>Save</button>
</form>
</article><?php
page_foot();