source: Admin Actions/AddBook.php

main
Last change on this file was 75f74d9, checked in by Vlado 222039 <vlado.popovski@…>, 6 weeks ago

Initial commit: Adding Book Tracker code

  • Property mode set to 100644
File size: 4.7 KB
Line 
1<?php
2error_reporting(E_ALL);
3ini_set('display_errors', 1);
4
5header('Content-Type: application/json');
6
7require '../connect.php';
8
9try {
10 // Start transaction
11 $conn->beginTransaction();
12
13 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
14 throw new Exception('Invalid request method');
15 }
16
17
18 $isbn = $_POST['isbn'] ?? null;
19 $title = $_POST['title'] ?? null;
20 $genre = $_POST['genre'] ?? null;
21 $publishedYear = $_POST['publishedYear'] ?? null;
22 $description = $_POST['description'] ?? null;
23 $totalCopies = $_POST['totalCopies'] ?? 0;
24 $format = $_POST['format'] ?? null;
25 $language = $_POST['language'] ?? null;
26 $publisher = $_POST['publisher'] ?? null;
27 $pages = $_POST['pages'] ?? null;
28 $authorId = $_POST['author'] ?? null;
29
30 // file upload
31 $coverImage = null;
32 if (isset($_FILES['coverImage']) && $_FILES['coverImage']['error'] === UPLOAD_ERR_OK) {
33 $uploadDir = '../BookImages/';
34 if (!file_exists($uploadDir)) {
35 mkdir($uploadDir, 0777, true);
36 }
37
38 $fileExtension = pathinfo($_FILES['coverImage']['name'], PATHINFO_EXTENSION);
39 $fileName = uniqid() . '.' . $fileExtension;
40 $targetPath = $uploadDir . $fileName;
41
42 if (move_uploaded_file($_FILES['coverImage']['tmp_name'], $targetPath)) {
43 $coverImage = $fileName;
44 }
45 }
46
47
48 if (!$isbn || !$title || !$genre || !$publishedYear || !$format ||
49 !$language || !$publisher || !$pages || !$authorId) {
50 throw new Exception('Missing required fields');
51 }
52
53 /*
54 // Insert into Book table
55 $sql = "INSERT INTO Book (ISBN, Title, Genre, PublishedYear, Description, CoverImage, TotalCopies)
56 VALUES (:isbn, :title, :genre, :publishedYear, :description, :coverImage, :totalCopies)";
57 //echo "RUNNING!";
58 $stmt = $conn->prepare($sql);
59 $stmt->execute([
60 ':isbn' => $isbn,
61 ':title' => $title,
62 ':genre' => $genre,
63 ':publishedYear' => $publishedYear,
64 ':description' => $description,
65 ':coverImage' => $coverImage,
66 ':totalCopies' => $totalCopies
67 ]);
68
69 $bookId = $conn->lastInsertId();
70 error_log("Generated BookID: " . $bookId);
71
72 // Insert into Book_Details table
73 $sql = "INSERT INTO Book_Details (BookID, Format, Language, Publisher, Pages)
74 VALUES (:bookId, :format, :language, :publisher, :pages)";
75
76 $stmt = $conn->prepare($sql);
77 $stmt->execute([
78 ':bookId' => $bookId,
79 ':format' => $format,
80 ':language' => $language,
81 ':publisher' => $publisher,
82 ':pages' => $pages
83 ]);
84 */
85
86 $sql = "CALL add_book(:ISBN, :Title, :Genre, :PublishedYear, :Description, :TotalCopies, :Format, :Language, :Publisher, :Pages, :AuthorID, :CoverImage)";
87 $stmt = $conn->prepare($sql);
88 $stmt->execute([
89 ':ISBN' => $isbn,
90 ':Title' => $title,
91 ':Genre' => $genre,
92 ':PublishedYear' => $publishedYear,
93 ':Description' => $description,
94 ':CoverImage' => $coverImage,
95 ':TotalCopies' => $totalCopies,
96 ':Format' => $format,
97 ':Language' => $language,
98 ':Publisher' => $publisher,
99 ':Pages' => $pages,
100 ':AuthorID' => $authorId
101 ]);
102
103 $bookId = $conn->lastInsertId();
104
105 $allowedConditions = ['New', 'Good', 'Damaged'];
106 foreach ($_POST['condition'] as $index => $condition) {
107 if (!in_array($condition, $allowedConditions)) {
108 throw new Exception('Invalid condition value: ' . $condition);
109 }
110 }
111
112 if (isset($_POST['condition']) && isset($_POST['quantity']) &&
113 is_array($_POST['condition']) && is_array($_POST['quantity'])) {
114
115 $sql = "INSERT INTO Book_Copies (BookID, Condition)
116 VALUES (:bookId, :condition)";
117 $stmt = $conn->prepare($sql);
118
119 foreach ($_POST['condition'] as $index => $condition) {
120 $quantity = $_POST['quantity'][$index] ?? 0;
121 if ($condition && $quantity > 0) {
122 // Loop through the quantity to insert individual rows
123 for ($i = 1; $i <= $quantity; $i++) {
124 $stmt->execute([
125 ':bookId' => $bookId,
126 ':condition' => $condition
127 ]);
128 }
129 }
130 }
131 }
132 // Commit the transaction
133 $conn->commit();
134
135 echo json_encode(['success' => true, 'message' => 'Book added successfully']);
136} catch (Exception $e) {
137 if ($conn->inTransaction()) {
138 $conn->rollBack();
139 }
140
141 if (isset($targetPath) && file_exists($targetPath)) {
142 unlink($targetPath);
143 }
144
145 error_log("Error in AddBook.php: " . $e->getMessage());
146 echo json_encode(['success' => false, 'message' => $e->getMessage()]);
147}
Note: See TracBrowser for help on using the repository browser.