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:
49
src/public/bookmark.php
Normal file
49
src/public/bookmark.php
Normal 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
|
||||
Reference in New Issue
Block a user