Browse Source

add basic favorites via a horrible hack

Andrew Dolgov 2 years ago
parent
commit
55f2b0090a
4 changed files with 103 additions and 5 deletions
  1. 23 0
      backend.php
  2. 50 5
      index.php
  3. 24 0
      js/index.js
  4. 6 0
      schema.sql

+ 23 - 0
backend.php

@@ -76,6 +76,29 @@
 
 		break;
 
+	case "togglefav":
+		$id = (int) $_REQUEST["id"];
+
+		$result = $ldb->query("SELECT id FROM epube_favorites WHERE bookid = '$id'
+			AND owner = '$owner' LIMIT 1");
+
+		$found_id = false;
+		$status = -1;
+
+		while ($line = $result->fetchArray(SQLITE3_ASSOC)) {
+			$found_id = $line["id"];
+		}
+
+		if ($found_id) {
+			$ldb->query("DELETE FROM epube_favorites WHERE id = " . $found_id);
+			$status = 0;
+		} else {
+			$ldb->query("INSERT INTO epube_favorites (bookid, owner) VALUES ($id, '$owner')");
+			$status = 1;
+		}
+
+		print json_encode(["id" => $id, "status" => $status]);
+
 	case "download":
 		$id = (int) $_REQUEST["id"];
 

+ 50 - 5
index.php

@@ -46,6 +46,8 @@
 		die(dirname(SCRATCH_DB) . " directory is not writable");
 	}
 
+	@$mode = htmlspecialchars($_REQUEST["mode"]);
+
 	$ldb = Db::get();
 ?>
 <!DOCTYPE html>
@@ -90,16 +92,23 @@
 
 	</div>
 
+	<?php
+		$fav_active = $mode == "favorites" ? "active" : "";
+		$index_active = $mode != "favorites" ? "active" : "";
+	?>
+
 	<div class="collapse navbar-collapse" id="nav-collapse">
 
 		<ul class="nav navbar-nav">
-			<li class="active"><a href="index.php">All</a></li>
+			<li class="<?php echo $index_active ?>"><a href="index.php">All</a></li>
+			<li class="<?php echo $fav_active ?>"><a href="index.php?mode=favorites">Favorites</a></li>
 			<li><a href="offline.html">Local</a></li>
 		</ul>
 
 		<form class="navbar-form navbar-right">
 			<input type="text" name="query" class="form-control"
 				value="<?php echo htmlspecialchars($query) ?>">
+			<input type="hidden" name="mode" value="<?php echo $mode ?>">
 			<button type="submit" class="btn btn-default">Search</button>
 		</form>
 
@@ -113,8 +122,9 @@
 </div>
 
 <script type="text/javascript">
-	$(document).ready(function() {
+	var index_mode = "<?php echo $mode ?>";
 
+	$(document).ready(function() {
 		if ('serviceWorker' in navigator) {
  			 navigator.serviceWorker
            .register('worker.js')
@@ -168,6 +178,19 @@
 		$search_qpart = "1";
 	}
 
+	$ids_qpart = "1";
+
+	if ($mode == "favorites") {
+		$fav_result = $ldb->query("SELECT bookid FROM epube_favorites WHERE owner = '$owner'");
+		$fav_ids = [];
+
+		while ($line = $fav_result->fetchArray(SQLITE3_ASSOC)) {
+			array_push($fav_ids, $line["bookid"]);
+		}
+
+		$ids_qpart = "books.id IN (" . implode(",", $fav_ids) . ")";
+	}
+
 	$limit = 60;
 	@$offset = (int) $_REQUEST["offset"];
 
@@ -177,7 +200,7 @@
 		(SELECT id FROM data WHERE book = books.id AND format = 'EPUB' LIMIT 1) AS epub_id FROM books
 		LEFT JOIN books_series_link AS bsl ON (bsl.book = books.id)
 		LEFT JOIN series AS s ON (bsl.series = s.id)
-		WHERE $search_qpart ORDER BY $order_by LIMIT $limit OFFSET $offset");
+		WHERE $search_qpart AND $ids_qpart ORDER BY $order_by LIMIT $limit OFFSET $offset");
 
 	print "<div class='row'>";
 
@@ -278,9 +301,31 @@
 				}
 				?> -->
 
+				<?php
+
+					$fav_result = $ldb->query("SELECT id FROM epube_favorites WHERE bookid = ".
+						$line['id'] . " AND owner = '$owner' LIMIT 1");
+
+					$found_id = false;
+
+					while ($fav_line = $fav_result->fetchArray(SQLITE3_ASSOC)) {
+						$found_id = $fav_line["id"];
+					}
+
+					if ($found_id) {
+						$toggle_fav_prompt = "Remove from favorites";
+					} else {
+						$toggle_fav_prompt = "Add to favorites";
+					}
+				?>
+
 				<li><a href="#" onclick="return show_summary(this)"
 					data-book-id="<?php echo $line["id"] ?>">Summary</a></li>
 
+				<li><a href="#" onclick="return toggle_fav(this)"
+					class="fav_item" data-book-id="<?php echo $line["id"] ?>">
+					<?php echo $toggle_fav_prompt ?></a></li>
+
 				<?php if ($line["epub_id"]) { ?>
 				<li><a href="#" onclick=""
 					data-book-id="<?php echo $line["id"] ?>" class="offline_dropitem"></a></li>
@@ -312,8 +357,8 @@
 	</div>
 
 	<?php
-		$prev_link = http_build_query(["query" => $query, "offset" => $offset - $limit]);
-		$next_link = http_build_query(["query" => $query, "offset" => $offset + $limit]);
+		$prev_link = http_build_query(["mode" => $mode, "query" => $query, "offset" => $offset - $limit]);
+		$next_link = http_build_query(["mode" => $mode, "query" => $query, "offset" => $offset + $limit]);
 	?>
 
 	<ul class="pager">

+ 24 - 0
js/index.js

@@ -13,6 +13,30 @@ function cache_refresh() {
 	}
 }
 
+function toggle_fav(elem) {
+	var bookId = elem.getAttribute("data-book-id");
+
+	$.post("backend.php", {op: "togglefav", id: bookId}, function(data) {
+		if (data) {
+			var msg = "[Error]";
+
+			if (data.status == 0) {
+				msg = "Add to favorites";
+			} else if (data.status == 1) {
+				msg = "Remove from favorites";
+			}
+
+			$(elem).html(msg);
+
+			if (index_mode == "favorites" && data.status == 0) {
+				$("#cell-" + bookId).remove();
+			}
+		}
+	});
+
+	return false;
+}
+
 function mark_offline(elem) {
 
 	var bookId = elem.getAttribute("data-book-id");

+ 6 - 0
schema.sql

@@ -28,4 +28,10 @@ create table epube_sessions (
 	data text,
 	expire integer not null);
 
+create table epube_favorites(
+	id integer not null primary key autoincrement,
+	bookid integer not null,
+	owner varchar(200) not null);
+
+
 create index epube_sessions_expire on epube_sessions(expire);