about summary refs log tree commit diff stats
path: root/classes/Handler/ListHandler.php
blob: fbfb2b37f6b9abe81d0efda0e4e8beac9618e700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

namespace Handler;

use AuthRequired;
use Config;
use Request;
use RequestHandlerInterface;
use Response;
use PDO;

class ListHandler implements RequestHandlerInterface, AuthRequired
{
	public function handle(Request $request): Response
	{
		$owner = $_SESSION["owner"] ?? "";
		$mode = $request->getAttribute('mode', '');
		$query = $request->getAttribute('query', '');
		$limit = (int) $request->getAttribute('limit', 60);
		$offset = (int) $request->getAttribute('offset', 0);

		$ldb = \Db::pdo();
		$caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB));

		$ids_qpart = "1";

		if ($mode == "favorites") {
			$fav_sth = $ldb->prepare("SELECT bookid FROM epube_favorites WHERE owner = ?");
			$fav_sth->execute([$owner]);

			$fav_ids = [];
			while ($line = $fav_sth->fetch()) {
				array_push($fav_ids, $line["bookid"]);
			}

			if (empty($fav_ids)) {
				return (new Response())->withBody(json_encode(["books" => [], "total" => 0]));
			}

			$ids_qpart = "books.id IN (" . implode(",", $fav_ids) . ")";
		}

		$order_by = $query ? "author_sort, series_name, series_index, title, books.id" : "books.id DESC";

		$sth = $caldb->prepare("SELECT books.*, s.name AS series_name,
			(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
				((:query = '') OR
					(
						LOWER(books.author_sort) LIKE LOWER(:query) OR
						LOWER(books.title) LIKE LOWER(:query) OR
						LOWER(series_name) LIKE LOWER(:query)
					))
				AND $ids_qpart
			ORDER BY $order_by LIMIT :limit OFFSET :offset");

		$sth->execute([':limit' => $limit, ':offset' => $offset, ':query' => '%' . $query . '%']);

		$books = [];
		$total = 0;

		while ($line = $sth->fetch()) {
			$cover_link = "";
			if ($line['has_cover']) {
				$cover_filename = Config::get(Config::BOOKS_DIR) . "/" . $line["path"] . "/" . "cover.jpg";
				if (file_exists($cover_filename)) {
					$cover_mtime = filemtime($cover_filename);
					$cover_link = "backend.php?" . http_build_query(["op" => "cover", "id" => $line["id"], "ts" => $cover_mtime]);
				}
			}

			$read_link = "";
			$in_progress = false;
			$is_read = false;

			if ($line["epub_id"]) {
				$read_link = "read.html?" . http_build_query(["id" => $line["epub_id"], "rt" => $mode, "b" => $line["id"], "ts" => time()]);

				$lastread_sth = $ldb->prepare("SELECT lastread, total_pages FROM epube_books, epube_pagination
					WHERE epube_pagination.bookid = epube_books.bookid AND
						epube_books.bookid = ? AND owner = ?");
				$lastread_sth->execute([$line['epub_id'], $owner]);

				if ($lastread_line = $lastread_sth->fetch()) {
					$lastread = $lastread_line["lastread"];
					$total_pages = $lastread_line["total_pages"];

					$is_read = $total_pages - $lastread < 5;
					$in_progress = $lastread > 1;
				}
			}

			$series_link = "";
			$series_full = "";

			if ($line["series_name"]) {
				$series_link = "?" . http_build_query(["query" => $line["series_name"]]);
				$series_full = $line["series_name"] . " [" . $line["series_index"] . "]";
			}

			$data_sth = $caldb->prepare("SELECT * FROM data WHERE book = ? LIMIT 3");
			$data_sth->execute([$line['id']]);

			$download_formats = [];
			while ($data_line = $data_sth->fetch()) {
				if ($data_line["format"] != "ORIGINAL_EPUB") {
					$download_formats[] = [
						"format" => $data_line["format"],
						"id" => $data_line["id"],
					];
				}
			}

			$is_fav = false;
			$fav_sth = $ldb->prepare("SELECT id FROM epube_favorites
				WHERE bookid = ? AND owner = ? LIMIT 1");
			$fav_sth->execute([$line['id'], $owner]);
			if ($fav_sth->fetch()) {
				$is_fav = true;
			}

			$books[] = [
				"id" => $line["id"],
				"title" => $line["title"],
				"author_sort" => $line["author_sort"],
				"series_name" => $line["series_name"] ?? "",
				"series_index" => $line["series_index"] ?? 0,
				"series_link" => $series_link,
				"series_full" => $series_full,
				"cover_link" => $cover_link,
				"read_link" => $read_link,
				"in_progress" => $in_progress,
				"is_read" => $is_read,
				"is_fav" => $is_fav,
				"epub_id" => $line["epub_id"] ?? 0,
				"download_formats" => $download_formats,
			];
		}

		// count total for pagination
		$count_sth = $caldb->prepare("SELECT COUNT(*) 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
				((:query = '%%') OR
					(
						LOWER(books.author_sort) LIKE LOWER(:query) OR
						LOWER(books.title) LIKE LOWER(:query) OR
						LOWER(s.name) LIKE LOWER(:query)
					))
				AND $ids_qpart");
		$count_sth->execute([':query' => '%' . $query . '%']);
		$total = (int) $count_sth->fetchColumn();

		return (new Response())->withBody(json_encode([
			"books" => $books,
			"total" => $total,
			"offset" => $offset,
			"limit" => $limit,
		]));
	}
}