From 9ad6876181faaff4d1d4333d5f09c4769fd9217e Mon Sep 17 00:00:00 2001 From: Yannic Date: Thu, 25 Sep 2025 17:35:18 +0200 Subject: [PATCH] Initial Commit --- db/bootstrap.php | 12 ++ db/functions.php | 190 ++++++++++++++++++++ index.php | 449 +++++++++++++++++++++++++++++++++++++++++++++++ style.css | 9 + submission.php | 126 +++++++++++++ 5 files changed, 786 insertions(+) create mode 100644 db/bootstrap.php create mode 100644 db/functions.php create mode 100644 index.php create mode 100644 style.css create mode 100644 submission.php diff --git a/db/bootstrap.php b/db/bootstrap.php new file mode 100644 index 0000000..d96a0a5 --- /dev/null +++ b/db/bootstrap.php @@ -0,0 +1,12 @@ +getMessage()); + } +?> \ No newline at end of file diff --git a/db/functions.php b/db/functions.php new file mode 100644 index 0000000..fd25780 --- /dev/null +++ b/db/functions.php @@ -0,0 +1,190 @@ +query("SELECT bookID, title, author FROM books ORDER BY title ASC"); + $books = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $books; +} + +/////////GET BORROWERS///////// +function getBorrowers($pdo){ + $stmt = $pdo->query("SELECT * FROM borrowers ORDER BY lastName ASC"); + $borrowers = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $borrowers; +} + +/////////GET AVAILABLE COPIES OF BOOK///////// +function getAvailableCopiesOfBook($pdo, $bookID){ + $stmt = $pdo->query("SELECT * FROM copies WHERE bookID = $bookID AND isBorrowed = 0 ORDER BY copyID ASC"); + $copies = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $copies; +} + +/////////GET BORROWED COPIES OF BORROWER///////// +function getBorrowedCopiesOfBorrower($pdo, $borrowerID){ + $stmt = $pdo->query("SELECT + loans.loanID, + loans.copyID, + copies.bookCondition as bookCondition, + books.title as bookTitle, + books.author as bookAuthor + FROM loans + INNER JOIN copies ON loans.copyID = copies.copyID + INNER JOIN books ON copies.bookID = books.bookID + WHERE borrowerID = $borrowerID AND returnedDate IS NULL + ORDER BY copyID ASC"); + $copies = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $copies; +} + +/////////GET ACTIVE LOANS///////// +function getActiveLoans($pdo){ + $stmt = $pdo->query("SELECT + loans.*, + books.title as bookTitle, + books.author as bookAuthor, + borrowers.firstName as firstName, + borrowers.lastName as lastName, + borrowers.role as borrowerRole + FROM loans + INNER JOIN copies ON loans.copyID = copies.copyID + INNER JOIN books ON copies.bookID = books.bookID + INNER JOIN borrowers on loans.borrowerID = borrowers.borrowerID + WHERE loans.returnedDate IS NULL + ORDER BY dueDate ASC"); + $activeLoans = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $activeLoans; +} + +/////////GET LATE LOANS///////// +function getLateLoans($pdo){ + $currentDate = date("Y-m-d"); + + $sql = "SELECT + loans.*, + books.title as bookTitle, + books.author as bookAuthor, + borrowers.firstName as firstName, + borrowers.lastName as lastName, + borrowers.role as borrowerRole + FROM loans + INNER JOIN copies ON loans.copyID = copies.copyID + INNER JOIN books ON copies.bookID = books.bookID + INNER JOIN borrowers on loans.borrowerID = borrowers.borrowerID + WHERE loans.dueDate < ? AND loans.returnedDate IS NULL + ORDER BY dueDate ASC"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$currentDate]); + + $lateLoans = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $lateLoans; +} + +/////////ADD COPIES///////// +function addCopies($pdo, $bookID, $amount, $copyCondition){ + for($i=0; $i<$amount; $i++){ + $sql = "INSERT INTO copies (bookID, bookCondition) VALUES (:bookID, :bookCondition)"; + $stmt = $pdo->prepare($sql); + $stmt->bindParam(':bookID', $bookID); + $stmt->bindParam(':bookCondition', $copyCondition); + + try { + $stmt->execute(); + $successfullyAdded = true; + } catch (PDOException $e) { + echo "

Error: " . $e->getMessage() . "

"; + } + } + if($successfullyAdded){ + if($amount > 1) + { + echo $amount . " Copies added successfully!"; + }else{ + echo "Copy added successfully!"; + } + } +} + +/////////ADD BOOK///////// +function addBook($pdo, $title, $author, $area){ + $sql = "INSERT INTO books (title, author, areaOfStudy) VALUES (:title, :author, :area)"; + $stmt = $pdo->prepare($sql); + $stmt->bindParam(':title', $title); + $stmt->bindParam(':author', $author); + $stmt->bindParam(':area', $area); + + try { + $stmt->execute(); + echo "

Book added successfully!

"; + } catch (PDOException $e) { + echo "

Error: " . $e->getMessage() . "

"; + } +} + +/////////ADD BORROWER///////// +function addBorrower($pdo, $firstname, $lastname, $role){ + $sql = "INSERT INTO borrowers (firstname, lastname, role) VALUES (:firstname, :lastname, :role)"; + $stmt = $pdo->prepare($sql); + $stmt->bindParam(":firstname", $firstname); + $stmt->bindParam(":lastname", $lastname); + $stmt->bindParam(":role", $role); + + try{ + $stmt->execute(); + echo "

Borrowers added!

"; + }catch(PDOException $e){ + echo "

Error: " . $e->getMessage() . "

"; + } +} + +/////////ADD LOAN///////// +function addLoan($pdo, $copyID, $borrowerID, $borrowedDate, $dueDate){ + $sql = "INSERT INTO loans (copyID, borrowerID, borrowedDate, dueDate) VALUES (:copyID, :borrowerID, :borrowedDate, :dueDate)"; + $stmt = $pdo->prepare($sql); + $stmt->bindParam(":copyID", $copyID); + $stmt->bindParam("borrowerID", $borrowerID); + $stmt->bindParam("borrowedDate", $borrowedDate); + $stmt->bindParam(":dueDate", $dueDate); + + try{ + $stmt->execute(); + echo "

Loan added!

"; + }catch(PDOException $e){ + echo "

Error: " . $e->getMessage() . "

"; + } + + $sql = "UPDATE copies SET isBorrowed=? WHERE copyID=?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([1, $copyID]); +} + +/////////REMOVE LOAN///////// +function removeLoan($pdo, $copyID, $loanID, $dateReturned){ + $sql = "UPDATE loans SET returnedDate = ? WHERE copyID = ? AND loanID = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$dateReturned, $copyID, $loanID]); + + $sql = "UPDATE copies SET isBorrowed=? WHERE copyID=?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([0, $copyID]); +} + +/////////SELECT REQUEST///////// +function selectRequest($pdo, $selectRequest){ + $stmt = $pdo -> query("SELECT " . $selectRequest); + + $results = $stmt -> fetchAll(PDO::FETCH_ASSOC); + + foreach($results as $result){ + echo '
'; print_r($result); echo '
'; + } +} +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..cb78cf6 --- /dev/null +++ b/index.php @@ -0,0 +1,449 @@ + + + + + + + Bücherei Verwaltung + + +

Bücherei Verwaltung

+
+ + + + + + +
+

Neues Buch hinzufügen

+
+ +
+
+ +
+
+ + + +
+
+ +
+
+

Neue Exemplare hinzufügen

+
+ +
+
+ +
+
+ + + +
+
+ +
+
+

Ausleiher hinzufügen

+
+ +
+
+ +
+
+ + + +
+
+ +
+
+

Buch verleihen

+
+ +
+
+ +
+
+ +
+
+ +
+
+ + + +
+
+ +
+
+

Buch einsammeln

+
+ +
+
+ +
+
+ +
+
+ + + > +
+
+ + +
+

Verliehene Bücher

+ + + + + + + + + + + + + +
VornameNachnameKlasse/FunktionBuchExemplarAusleihdatumRückgabe fällig
#format("d.m.Y")?>format("d.m.Y")?>
+
+

Versäumte Rückgaben

+ + + + + + + + + + + + + +
VornameNachnameKlasse/FunktionBuchExemplarAusleihdatumRückgabe fällig
#format("d.m.Y")?>format("d.m.Y")?>
+
+ + +
+

Datenbank durchsuchen

+
+ + + +
+
+ +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..ffe95ab --- /dev/null +++ b/style.css @@ -0,0 +1,9 @@ +*{ + font-family: arial; +} + +td, th{ + border: 1px solid black; + vertical-align: top; + padding: 10px; +} \ No newline at end of file diff --git a/submission.php b/submission.php new file mode 100644 index 0000000..60c012b --- /dev/null +++ b/submission.php @@ -0,0 +1,126 @@ +Please fill in all fields.

"; + } + } + + //Copy Submission + if($_POST['submissionType'] == "copy"){ + $bookID = $_POST['book'] ?? ''; + $amount = $_POST['amount'] ?? ''; + $copyCondition = $_POST['condition'] ?? ''; + + if ($bookID && $amount && $copyCondition) { + addCopies($pdo, $bookID, $amount, $copyCondition); + } else { + echo "

Please fill in all fields.

"; + } + } + + //Borrower Submission + if($_POST['submissionType'] == "borrower"){ + $firstname = $_POST['firstname'] ?? ''; + $lastname = $_POST['lastname'] ?? ''; + $role = $_POST['role'] ?? ''; + + if($firstname && $lastname && $role){ + addBorrower($pdo, $firstname, $lastname, $role); + }else{ + echo "

Please fill in all fields.

"; + } + } + + if($_POST['submissionType'] == "loan"){ + $copyID = $_POST['copyID'] ?? ''; + $borrowerID = $_POST['borrowerID'] ?? ''; + $borrowedDate = $_POST['borrowedDate'] ?? ''; + $dueDate = $_POST['dueDate']; + + if($copyID && $borrowerID && $borrowedDate && $dueDate){ + addLoan($pdo, $copyID, $borrowerID, $borrowedDate, $dueDate); + }else{ + echo "

Please fill in all fields.

"; + } + } + + if($_POST['submissionType'] == "return"){ + $copyIDLoanID = explode("-", $_POST['copyID-loanID'], 2); + + $copyID = $copyIDLoanID[0]; + $loanID = $copyIDLoanID[1]; + $returnedDate = $_POST['returnedDate']; + + if($copyID && $loanID){ + removeLoan($pdo, $copyID, $loanID, $returnedDate); + }else{ + echo "

Please fill in all fields

"; + } + } + + if($_POST['submissionType'] == "selectRequest"){ + $selectRequest = $_POST['sqlSelectTextarea']; + + if($selectRequest){ + selectRequest($pdo, $selectRequest); + }else{ + echo "

Please fill in all fields

"; + } + } +} + +if($_SERVER['REQUEST_METHOD'] == 'GET'){ + + if(!hash_equals($sessionToken, $getToken)){ + die("Invalid request: Token mismatch."); + } + + if($_GET['submissionType'] == "getCopies"){ + $bookID = $_GET['bookID']; + if($bookID){ + $copies = getAvailableCopiesOfBook($pdo, $bookID); + + echo json_encode($copies); + }else{ + echo json_encode([]); + } + } + + if($_GET['submissionType'] == "getReturnCopies"){ + $borrowerID = $_GET['borrowerID']; + if($borrowerID){ + $copies = getBorrowedCopiesOfBorrower($pdo, $borrowerID); + + echo json_encode($copies); + }else{ + echo json_encode([]); + } + } +} +?> \ No newline at end of file