Candle/warenkorb.php

385 lines
17 KiB
PHP
Raw Permalink Normal View History

<!--
TODO:
- Seite ist noch nicht erreichbar
- Löschbestätigung -> removeBookFromCart verschieben
TODO (optional, geringe Priorität):
- 80 Zeichen
- JS Funktionen größtmöglich auslagern
- Mengenfeld bearbeitbar machen
- Layout verbessern (z.B. resize)
-->
2023-05-26 16:39:10 +02:00
<!DOCTYPE html>
<html>
<head>
<meta lang="de"/>
<meta charset="utf-8"/>
<meta name="viewport" content="device=device-width, initial-scaling=1"/>
<link rel="Stylesheet" type="text/css" href="style.css"/>
<link rel="Stylesheet" type="text/css" href="warenkorb.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Candle Bibliothek | Warenkorb</title>
<link rel="icon" href="pictures/candle.png">
<script src="notwendiges_laden.js" defer></script>
</head>
2023-06-14 18:23:07 +02:00
<body onload="on_load()">
2023-06-14 11:44:33 +02:00
<header id="header">
<h1 class="kopf" >
<br><br>
Bibliothek
2023-06-14 18:26:05 +02:00
<a href="index.html" id="bildID">
2023-06-14 11:44:33 +02:00
<img id="CandleID" src="pictures/candle.png"
alt="Bild von einer Kerze" width="200" height="192"/></a>
<br><br>
Candle
</h1>
<div>
<a id="uberuns" href="uberuns.html">Über Uns</a>
<a id="buecher" href="buecher.php">Bücher</a>
<a id="meinebuecher" href="anmelden_meinebuecher.php">
Meine Bücher</a>
<?php
session_start();
if(isset($_SESSION["eingeloggt"]) && $_SESSION["eingeloggt"] == 1 ){
echo "<a id='abmelden' href='abmelden.php'>Abmelden</a>";}
?>
<form action="suchergebnisse.php">
<input type="hidden" name="searchType" value="simple">
<input name="searchFor" class="suchleiste" type="search"
placeholder="Suche...">
<button type="submit" id="buttonID" class="searchButton">
<i class="fa fa-search"></i>
</button>
</form>
<a id="erweitertesuche" href="erweitertesuche.html">
Erweiterte Suche</a>
2023-06-14 20:31:11 +02:00
<a href="warenkorb.php" id="warenKorbID">
2023-06-14 11:44:33 +02:00
<img id="shoppingCard" src="pictures/einkaufswagen.png"
alt="Bild von einem Wagen" width="50" height="50"/>
</a>
</div>
</header>
<!------------------------------------------------------------------------->
<div class="hauptcontainer">
<h1 id="shoppingCart_heading">Warenkorb</h1>
<div id="secondary_container">
<div id='cartItems_container'>
<?php
session_start();
// Counter für Divider
$counter = 0;
// Überprüfen, ob Bücher im Warenkorb liegen
if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
// Es liegen Bücher im Warenkorb
// Alle Elemente im Warenkorb durchgehen
foreach ($_SESSION["cart"] as $key=>$item) {
$quantity = $item['quantity'];
$sql = '';
2023-05-26 16:39:10 +02:00
$servername = "localhost";
$username = "web_b-3";
$password = "een7Ao6s";
$dbname = "bibliothek_candle";
$connection =
mysqli_connect($servername, $username, $password, $dbname);
if (!$connection) {
die("Verbindung fehlgeschlagen: " . mysqli_connect_error());
}
/* SQL-Befehl vorbereiten */
$sql = $connection->prepare("SELECT b.BuchID, b.Titel,
b.Bild,
GROUP_CONCAT(DISTINCT CONCAT(a.VorName, ' ', a.NachName)
SEPARATOR ', ') AS 'Autor',
IF(b.Anzahl!=0, 'Verfügbar', CONCAT('Verfügbar ab: ',
(SELECT DATE_FORMAT(DATE_ADD(ab.Enddatum, INTERVAL 1 DAY)
,'%d.%m.%Y') FROM `ausgeliehenes_buch` AS ab
WHERE ab.BuchID = b.BuchID
ORDER BY ab.Reserviert ASC, ab.Enddatum ASC LIMIT 1)))
AS 'Verfügbarkeit' FROM `buch` AS b
INNER JOIN `buch_verfasst_von_autor` AS bvva
ON b.BuchID = bvva.BuchID
INNER JOIN `autor` AS a ON bvva.AutorNr = a.AutorNr
WHERE b.BuchID = ?
GROUP BY b.BuchID;");
// Parameter binden
$sql->bind_param("i", $ID);
// ID festlegen
$ID = $key;
// SQL-Befehl ausführen
$sql->execute();
// Ergebnis speichern
$result = $sql->get_result();
if ($counter != 0) {
echo "<div class='divider'></div>";
}
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$bookID = $row['BuchID'];
$bookCover = $row['Bild'];
$bookTitle = $row['Titel'];
$bookAuthor = $row['Autor'];
$bookAvailability = $row['Verfügbarkeit'];
// TODO: OnClick auslagern.
echo "<div id='book_container$bookID' class='book_container'
onclick='open_bookDetails(this.id)'>
<div class='bookCover_container'>
<img class='bookCover'
src='book_covers/$bookCover'
alt='Kein Bild vorhanden'>
</div>
<div class='bookDetails_container'>
<h2 class='bookTitle'>$bookTitle</h2>
<div class='bookDetails'>
<p class='author'>von $bookAuthor</p>
</div>
<div class='available_container'>
<p id='available_$bookID' class='available'>
$bookAvailability</p>
<!--TODO: Auslagern-->
<script type='text/javascript'
language='JavaScript'>
(function() {
var currentElement = document.
getElementById('available_$bookID');
var parentDiv =
currentElement.parentElement;
if (currentElement.innerHTML.trim()
== 'Verfügbar') {
parentDiv.style.backgroundColor
= '#70AD47';
}
else {
parentDiv.style.backgroundColor
= '#F34343';
}
})();
</script>
</div>
</div>
<!--TODO: OnClick Event auslagern-->
<div class='quantity_container'>
<div class='reduce_container'
id='reduce$bookID'
onclick='decreaseQuantity(this.id)'>
<div class='reduce'></div>
</div>
<div class='quantityText_container'>
<p class='quantityText'
id='quantityText$bookID'>$quantity</p>
</div>
<div class='add_container'
id='add$bookID'
onclick='increaseQuantity(this.id)'>
<div class='add'></div>
</div>
</div>
<div class='trashCan_container'
id='trashCan$bookID'
onclick='removeBookFromCart(this.id)'>
<div class='trashCan'></div>
</div>
</div>";
$counter++;
}
} else {
echo "Keine Ergebnisse.";
}
if (!$result) {
die("Ungültige SQL-Abfrage: " . mysqli_connect_error());
}
mysqli_close($connection);
$counter++;
}
}
else {
// Es liegen keine Bücher im Warenkorb
echo
"Der Warenkorb ist leer,
bitte fügen Sie Bücher hinzu.";
}
?>
</div>
<div id='totalQuantity_container'>
<div id='totalQuantityText_container'>
<p id='totalQuantityText'>? Artikel</p>
</div>
<input type='button' id='reserveNow'
onclick='open_reservation()' value='Jetzt reservieren'>
</div>
</div>
</div>
<!------------------------------------------------------------------------->
<!-- TODO: Auslagern (falls möglich) -->
<script>
2023-06-14 18:23:07 +02:00
/*
* Überprüft, ob Bücher im Warenkorb existieren.
* Ist die nicht der Fall, wird der "Jetzt reservieren" Button
* deaktiviert.
*/
function on_load() {
var book_container =
document.getElementsByClassName("book_container");
if (book_container.length > 0) {
// Bücher existieren, Button aktivieren
document.getElementById("reserveNow").disabled = false;
getTotalQuantity();
}
else {
// Bücher existieren nicht, Button deaktivieren
document.getElementById("reserveNow").disabled = true;
document.getElementById("totalQuantityText").innerHTML
= "0 Artikel";
}
}
function getTotalQuantity() {
const collection = document.getElementsByClassName("quantityText");
var totalAmount = 0;
for (var i = 0; i < collection.length; i++) {
totalAmount = totalAmount + parseInt(collection[i].innerHTML);
}
document.getElementById("totalQuantityText").innerHTML
= `${totalAmount} Artikel`;
}
function open_bookDetails(ID) {
var data = new URLSearchParams();
data.append("bookID", ID);
var url = "http://141.19.142.11/buch_details.php?" +
data.toString();
location.href = url;
}
function decreaseQuantity(ID) {
// OnClick Event für open_bookDetails stoppen
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
// ID abtrennen
ID = ID.replace('reduce', '');
var request = new XMLHttpRequest();
request.open('POST',
'http://141.19.142.11/warenkorb_funktionen.php', true);
// TODO: Handler erstellen
request.onload = function() {
console.log(this.responseText);
// Überprüfen, ob die Menge im Warenkorb verringert wurde
if (this.responseText.includes('Erfolgreich verringert')) {
toChange = document.getElementById(`quantityText${ID}`);
toChange.innerHTML = parseInt(toChange.innerHTML) - 1;
2023-06-14 18:23:07 +02:00
getTotalQuantity();
}
}
request.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
request.send(`function=decreaseQuantity&id=${ID}`);
}
function increaseQuantity(ID) {
// OnClick Event für open_bookDetails stoppen
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
// ID abtrennen
ID = ID.replace('add', '');
var request = new XMLHttpRequest();
request.open('POST',
'http://141.19.142.11/warenkorb_funktionen.php', true);
// TODO: Handler erstellen
request.onload = function() {
console.log(this.responseText);
// Überprüfen, ob die Menge im Warenkorb erhöht wurde
if (this.responseText.includes('Erfolgreich erhöht')) {
toChange = document.getElementById(`quantityText${ID}`);
toChange.innerHTML = parseInt(toChange.innerHTML) + 1;
2023-06-14 18:23:07 +02:00
getTotalQuantity();
}
}
request.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
request.send(`function=increaseQuantity&id=${ID}`);
}
function removeBookFromCart(ID) {
// OnClick Event für open_bookDetails stoppen
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
// ID abtrennen
ID = ID.replace('trashCan', '');
var request = new XMLHttpRequest();
request.open('POST',
'http://141.19.142.11/warenkorb_funktionen.php', true);
// TODO: Handler erstellen
request.onload = function() {
console.log(this.responseText);
// Überprüfen, ob das Buch aus dem Warenkorb entfernt wurde
if (this.responseText.includes('Erfolgreich entfernt')) {
window.location.reload(true);
}
}
request.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
request.send(`function=removeBookFromCart&id=${ID}`);
}
function open_reservation() {
2023-06-14 20:01:56 +02:00
var url = "http://141.19.142.11/anmelden.php";
location.href = url;
}
</script>
<!------------------------------------------------------------------------->
<!--Link JS file-->
<script src="burgerIcon.js"></script>
<!--If it was small and got big,
it calls the function only one time and doesn't do it all the time-->
2023-06-14 18:26:05 +02:00
<script>
var widthissue = false;
window.addEventListener("resize", function(event) {
if(window.innerWidth > 800 && widthissue) {
widthissue = false
unset()
}
else if(window.innerWidth < 800) widthissue = true;
})
</script>
2023-05-26 16:39:10 +02:00
<nav id="activeid" class="active"></nav>
2023-05-26 16:39:10 +02:00
<footer id="footer" class="fuss" ></footer>
</body>
</html>