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();

View File

@@ -10,60 +10,28 @@ include '../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
if (array_key_exists('refresh', $_GET)) {
if (key_exists('refresh', $_GET)) {
$refreshResult = Feed::refreshAll($db);
if (array_key_exists('ok', $refreshResult)) {
if (key_exists('ok', $refreshResult)) {
add_info('All feeds refreshed successfully');
} else {
add_error(nl2br($refreshResult['error']));
}
}
if (key_exists('bookmarked', $_GET)) {
$itemCriteria = 'item.is_bookmarked = 1';
$returnURL = '&from=' . urlencode('/?bookmarked');
$type = 'Bookmarked';
} else {
$itemCriteria = 'item.is_read = 0';
$returnURL = '';
$type = 'Unread';
$list = match (true) {
key_exists('bookmarked', $_GET) => ItemList::allBookmarked($db),
default => ItemList::allUnread($db)
};
$title = "Your $list->itemType Items";
page_head($title);
echo "<h1>$title";
if ($list->itemType == 'Unread') {
echo ' &nbsp; ' . hx_get('/?refresh', '(Refresh All Feeds)', 'class=refresh hx-indicator="closest h1"')
. '<span class=loading>Refreshing&hellip;</span>';
}
$title = "Your $type Items";
$query = $db->prepare(<<<SQL
SELECT item.id, item.feed_id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of,
feed.title AS feed_title
FROM item
INNER JOIN feed ON feed.id = item.feed_id
WHERE feed.user_id = :userId
AND $itemCriteria
ORDER BY coalesce(item.updated_on, item.published_on) DESC
SQL);
$query->bindValue(':userId', $_SESSION[Key::USER_ID]);
$result = $query->execute();
$item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
page_head($title); ?>
<h1>
<?=$title?><?php
if ($type == 'Unread'): ?> &nbsp;
<?=hx_get('/?refresh', '(Refresh All Feeds)', 'class=refresh hx-indicator="closest h1"')?>
<span class=loading>Refreshing&hellip;</span><?php
endif; ?>
</h1>
<article><?php
if ($item) {
while ($item) { ?>
<p><?=hx_get("/item?id={$item['id']}$returnURL", strip_tags($item['item_title']))?><br>
<small><?=date_time($item['as_of'])?> &bull;
<?=hx_get("/feed/items?id={$item['feed_id']}&" . strtolower($type), htmlentities($item['feed_title']))?>
</small><?php
$item = $result->fetchArray(SQLITE3_ASSOC);
}
} else { ?>
<p>There are no <?=strtolower($type)?> items<?php
} ?>
</article><?php
echo '</h1>';
$list->render();
page_foot();
$db->close();

View File

@@ -5,6 +5,6 @@
include '../../start.php';
if (array_key_exists(Key::USER_ID, $_SESSION)) session_destroy();
if (key_exists(Key::USER_ID, $_SESSION)) session_destroy();
frc_redirect('/');

View File

@@ -5,7 +5,7 @@ $db = Data::getConnection();
Security::verifyUser($db, redirectIfAnonymous: false);
// Users already logged on have no need of this page
if (array_key_exists(Key::USER_ID, $_SESSION)) frc_redirect('/');
if (key_exists(Key::USER_ID, $_SESSION)) frc_redirect('/');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
Security::logOnUser($_POST['email'] ?? '', $_POST['password'], $_POST['returnTo'] ?? null, $db);