Move list retrieve/render to class (#15)

- array_key_exists -> key_exists
This commit is contained in:
2024-05-25 23:03:39 -04:00
parent c4e85e6734
commit 210377b4da
9 changed files with 215 additions and 126 deletions

View File

@@ -35,7 +35,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$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)) {
if (key_exists('ok', $result)) {
add_info('Feed saved successfully');
frc_redirect('/feeds');
}

View File

@@ -12,69 +12,19 @@ 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
$list = match (true) {
key_exists('unread', $_GET) => ItemList::unreadForFeed($feed['id'], $db),
key_exists('bookmarked', $_GET) => ItemList::bookmarkedForFeed($feed['id'], $db),
default => ItemList::allForFeed($feed['id'], $db)
};
$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; ' : ''?>
<?=$item['is_bookmarked'] ? '<strong>Bookmarked</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_head(($list->itemType != '' ? "$list->itemType Items | " : '') . strip_tags($feed['title']));
if ($list->itemType == '') {
echo '<h1>' . htmlentities($feed['title']) . '</h1>';
} else {
echo '<h1 class=item_heading>' . htmlentities($feed['title']) . '</h1>';
echo "<div class=item_published>$list->itemType Items</div>";
}
$list->render();
page_foot();
$db->close();