35570-vm/add_book.php
Flatlogic Bot 9671081d9c 1
2025-11-08 12:20:22 +00:00

154 lines
7.1 KiB
PHP

<?php
session_start();
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit();
}
require_once 'db/config.php';
require_once 'header.php';
$user_id = $_SESSION["user_id"];
$message = '';
$search_results = [];
$search_query = '';
// Handle Google Books Search
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['search'])) {
$search_query = trim($_GET['search']);
if (!empty($search_query) && defined('GOOGLE_BOOKS_API_KEY') && GOOGLE_BOOKS_API_KEY != 'YOUR_GOOGLE_BOOKS_API_KEY') {
$api_url = "https://www.googleapis.com/books/v1/volumes?q=" . urlencode($search_query) . "&key=" . GOOGLE_BOOKS_API_KEY;
$response = @file_get_contents($api_url);
if ($response) {
$data = json_decode($response, true);
if (isset($data['items'])) {
$search_results = $data['items'];
}
} else {
$message = '<div class="alert alert-danger">Could not connect to Google Books API.</div>';
}
} elseif (empty($search_query)) {
$message = '<div class="alert alert-warning">Please enter a search term.</div>';
} else {
$message = '<div class="alert alert-danger">Google Books API key is not configured. Please ask the administrator to set it up.</div>';
}
}
// Handle Add Book from Google Books
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_google_book'])) {
$title = trim($_POST['title']);
$author_name = trim($_POST['author']);
$google_books_id = trim($_POST['google_books_id']);
$description = trim($_POST['description']);
$cover_image_url = trim($_POST['cover_image_url']);
if (!empty($title) && !empty($author_name)) {
try {
// Check if book with this Google ID already exists
$stmt = db()->prepare("SELECT id FROM books WHERE google_books_id = :google_books_id");
$stmt->execute(['google_books_id' => $google_books_id]);
$book = $stmt->fetch();
if ($book) {
$book_id = $book['id'];
} else {
// Insert new book
$stmt = db()->prepare(
"INSERT INTO books (title, author_name, description, cover_image_url, google_books_id, added_by_user_id)
VALUES (:title, :author_name, :description, :cover_image_url, :google_books_id, :user_id)"
);
$stmt->execute([
'title' => $title,
'author_name' => $author_name,
'description' => $description,
'cover_image_url' => $cover_image_url,
'google_books_id' => $google_books_id,
'user_id' => $user_id
]);
$book_id = db()->lastInsertId();
}
// Check if the book is already in the user's library
$stmt = db()->prepare("SELECT * FROM user_libraries WHERE user_id = :user_id AND book_id = :book_id");
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
if ($stmt->fetch()) {
$message = '<div class="alert alert-warning">This book is already in your library.</div>';
} else {
// Add book to user's library
$stmt = db()->prepare("INSERT INTO user_libraries (user_id, book_id) VALUES (:user_id, :book_id)");
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
$message = '<div class="alert alert-success">Book added to your library! <a href="library.php">View your library</a>.</div>';
}
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Database Error: ' . $e->getMessage() . '</div>';
}
} else {
$message = '<div class="alert alert-danger">Title and Author are required.</div>';
}
}
?>
<div class="container mt-5">
<h2>Add Book from Google</h2>
<p>Search for a book to add it to your library automatically.</p>
<?php echo $message; ?>
<div class="card mb-4">
<div class="card-header">Search Google Books</div>
<div class="card-body">
<form action="add_book.php" method="get">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Search by title, author, or ISBN..." name="search" value="<?php echo htmlspecialchars($search_query); ?>">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
</div>
</div>
<?php if (!empty($search_results)): ?>
<div class="card">
<div class="card-header">Search Results</div>
<div class="card-body">
<div class="list-group">
<?php foreach ($search_results as $item): ?>
<?php
$volumeInfo = $item['volumeInfo'];
$title = $volumeInfo['title'] ?? 'N/A';
$authors = isset($volumeInfo['authors']) ? implode(', ', $volumeInfo['authors']) : 'N/A';
$description = $volumeInfo['description'] ?? '';
$cover_image = $volumeInfo['imageLinks']['thumbnail'] ?? 'https://via.placeholder.com/128x192.png?text=No+Cover';
$google_id = $item['id'];
?>
<div class="list-group-item">
<div class="row g-3">
<div class="col-md-2">
<img src="<?php echo htmlspecialchars($cover_image); ?>" class="img-fluid rounded" alt="Cover for <?php echo htmlspecialchars($title); ?>">
</div>
<div class="col-md-10">
<h5 class="mb-1"><?php echo htmlspecialchars($title); ?></h5>
<p class="mb-1">by <?php echo htmlspecialchars($authors); ?></p>
<p class="mb-1 text-muted small"><?php echo htmlspecialchars(substr($description, 0, 200)); ?>...</p>
<form action="add_book.php" method="post" class="mt-2">
<input type="hidden" name="title" value="<?php echo htmlspecialchars($title); ?>">
<input type="hidden" name="author" value="<?php echo htmlspecialchars($authors); ?>">
<input type="hidden" name="google_books_id" value="<?php echo htmlspecialchars($google_id); ?>">
<input type="hidden" name="description" value="<?php echo htmlspecialchars($description); ?>">
<input type="hidden" name="cover_image_url" value="<?php echo htmlspecialchars($cover_image); ?>">
<button type="submit" name="add_google_book" class="btn btn-sm btn-success">Add to Library</button>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php require_once 'footer.php'; ?>