Add item bookmark buttons (#14)

Implemented as a toggle button

- Move init_cap func where web can see it
- Bump version to alpha7
This commit is contained in:
Daniel J. Summers 2024-05-23 22:06:16 -04:00
parent 4fa4dcb831
commit f4273935cb
8 changed files with 95 additions and 19 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/** The current Feed Reader Central version */ /** The current Feed Reader Central version */
const FRC_VERSION = '1.0.0-alpha6'; const FRC_VERSION = '1.0.0-alpha7';
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
$file = implode(DIRECTORY_SEPARATOR, [__DIR__, 'lib', "$class.php"]); $file = implode(DIRECTORY_SEPARATOR, [__DIR__, 'lib', "$class.php"]);
@ -18,3 +18,17 @@ Data::ensureDb();
/** @var string The date the world wide web was created */ /** @var string The date the world wide web was created */
const WWW_EPOCH = '1993-04-30T00:00:00+00:00'; const WWW_EPOCH = '1993-04-30T00:00:00+00:00';
/**
* Capitalize the first letter of the given string
*
* @param string $value The string to be capitalized
* @return string The given string with the first letter capitalized
*/
function init_cap(string $value): string {
return match (strlen($value)) {
0 => "",
1 => strtoupper($value),
default => strtoupper(substr($value, 0, 1)) . substr($value, 1),
};
}

View File

@ -28,17 +28,3 @@ function cli_title(string $title): void {
printfn(' | %s | %s |', $title, $appTitle); printfn(' | %s | %s |', $title, $appTitle);
printfn($dashes . PHP_EOL); printfn($dashes . PHP_EOL);
} }
/**
* Capitalize the first letter of the given string
*
* @param string $value The string to be capitalized
* @return string The given string with the first letter capitalized
*/
function init_cap(string $value): string {
return match (strlen($value)) {
0 => "",
1 => strtoupper($value),
default => strtoupper(substr($value, 0, 1)) . substr($value, 1),
};
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -79,10 +79,6 @@ main {
margin-top: .25rem; margin-top: .25rem;
} }
.item_heading {
margin-bottom: 0;
}
.item_published { .item_published {
margin-bottom: 1rem; margin-bottom: 1rem;
line-height: 1.2; line-height: 1.2;
@ -153,3 +149,31 @@ code {
p.back-link { p.back-link {
margin-top: -1rem; margin-top: -1rem;
} }
.item_heading {
margin-bottom: 0;
.bookmark {
padding: 0;
border: solid 1px black;
border-radius: .5rem;
&.add {
background-color: lightgray;
:hover {
background: linear-gradient(lightgreen, gray);
}
}
&.remove {
background: linear-gradient(lightgreen, green);
:hover {
background: linear-gradient(gray, lightgreen);
}
}
img {
max-width: 1.5rem;
max-height: 1.5rem;
padding: .5rem;
}
}
}

49
src/public/bookmark.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/**
* Bookmark Partial Handler
*
* This will display a button which will either add or remove a bookmark for a given item.
*/
include '../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
$id = $_GET['id'];
$existsQuery = $db->prepare(
'SELECT item.id FROM item INNER JOIN feed ON feed.id = item.feed_id WHERE item.id = :id AND feed.user_id = :user');
$existsQuery->bindValue(':id', $id);
$existsQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
$existsResult = $existsQuery->execute();
$exists = $existsResult ? $existsResult->fetchArray(SQLITE3_ASSOC) : false;
if (!$exists) not_found();
if (key_exists('action', $_GET)) {
if ($_GET['action'] == 'add') {
$flag = 1;
} elseif ($_GET['action'] == 'remove') {
$flag = 0;
}
if (isset($flag)) {
$update = $db->prepare('UPDATE item SET is_bookmarked = :flag WHERE id = :id');
$update->bindValue(':id', $id);
$update->bindValue(':flag', $flag);
if (!$update->execute()) die(Data::error($db)['error']);
}
}
$bookQuery = $db->prepare('SELECT id, is_bookmarked FROM item WHERE id = :id');
$bookQuery->bindValue(':id', $id);
$bookResult = $bookQuery->execute();
$bookmark = $bookResult ? $bookResult->fetchArray(SQLITE3_ASSOC) : ['id' => $id, 'is_bookmarked' => 0];
$action = $bookmark['is_bookmarked'] ? 'remove' : 'add';
$icon = $bookmark['is_bookmarked'] ? 'added' : 'add'; ?>
<button class="bookmark <?=$action?>" type=button role=button hx-patch="/bookmark?id=<?=$id?>&action=<?=$action?>"
hx-target=this hx-swap=outerHTML hx-push-url=false title="<?=init_cap($action)?> Bookmark">
<img src=/assets/bookmark-<?=$icon?>.png alt="<?=$action?> bookmark">
</button><?php

View File

@ -67,6 +67,7 @@ if ($type == TYPE_ALL) { ?>
while ($item) { ?> while ($item) { ?>
<p><?=hx_get("/item?id={$item['id']}&from=$thisURL", strip_tags($item['title']))?><br> <p><?=hx_get("/item?id={$item['id']}&from=$thisURL", strip_tags($item['title']))?><br>
<small><?=$item['is_read'] == 0 ? '<strong>New</strong> &nbsp; ' : ''?> <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 <em><?=date_time($item['as_of'])?></em></small><?php
$item = $itemResult->fetchArray(SQLITE3_ASSOC); $item = $itemResult->fetchArray(SQLITE3_ASSOC);
} }

View File

@ -75,6 +75,8 @@ $updated = isset($item['updated_on']) ? date_time($item['updated_on']) : null;
page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?> page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>
<h1 class=item_heading> <h1 class=item_heading>
<span class=bookmark hx-get="/bookmark?id=<?=$_GET['id']?>" hx-trigger=load hx-target=this hx-swap=outerHTML
hx-push-url=false></span>
<a href="<?=$item['item_link']?>" target=_blank rel=noopener><?=strip_tags($item['item_title'])?></a><br> <a href="<?=$item['item_link']?>" target=_blank rel=noopener><?=strip_tags($item['item_title'])?></a><br>
</h1> </h1>
<div class=item_published> <div class=item_published>