Compare commits

..

No commits in common. "7e53308ae78ff2bee914cff0ef454f7dfea3431c" and "9f2dbb29d0a78148e9ac8c309ec07b27957d6a6e" have entirely different histories.

View File

@ -1,26 +1,11 @@
<?php <?php
/** /**
* @file offerAdder.php * @file offerAdder.php
* @brief Angebot hinzufügen und verwalten. * @brief Angebot hinzufügen
*
* Dieses Skript stellt eine Benutzeroberfläche und die zugrundeliegende Logik bereit,
* um Angebote für Produkte in verschiedenen Shops hinzuzufügen, anzuzeigen und zu löschen.
* Der Zugriff ist streng auf Administratoren beschränkt.
*
* @author Geizkragen-Team
* @date 2026-04-06
*/ */
require_once __DIR__ . '/lib/bootstrap.php'; require_once __DIR__ . '/lib/bootstrap.php';
/**
* @brief Zugriffskontrolle für Administratoren.
*
* Überprüft, ob der aktuelle Benutzer angemeldet ist, Rollen zugewiesen hat
* und ob die Rolle 'ADMIN' in seinen Benutzerrollen enthalten ist.
* Falls nicht, wird ein HTTP 403 (Forbidden) Fehler gesendet und eine
* Fehlermeldung auf der Seite angezeigt, bevor die Ausführung beendet wird.
*/
// Only ADMIN // Only ADMIN
if (empty($_SESSION['user_id']) || empty($_SESSION['user_roles']) || !in_array('ADMIN', $_SESSION['user_roles'], true)) { if (empty($_SESSION['user_id']) || empty($_SESSION['user_roles']) || !in_array('ADMIN', $_SESSION['user_roles'], true)) {
http_response_code(403); http_response_code(403);
@ -33,30 +18,11 @@ if (empty($_SESSION['user_id']) || empty($_SESSION['user_roles']) || !in_array('
exit; exit;
} }
/**
* @brief Datenbankverbindung herstellen und Initialisierung von Statusvariablen.
*
* @var mysqli $conn Die aktive Datenbankverbindung.
* @var string $message Eine Nachricht an den Benutzer (Erfolg oder Fehler).
* @var string $messageType Der Typ der Nachricht ('success' oder 'error').
*/
$conn = db_connect(); $conn = db_connect();
$message = ''; $message = '';
$messageType = ''; $messageType = '';
/**
* @brief Verarbeitung von POST-Anfragen zur Angebotsverwaltung.
*
* Überprüft, ob das Formular abgesendet wurde und eine spezifische 'action' gesetzt ist.
* Unterstützte Aktionen sind 'add_offer' (Angebot hinzufügen) und 'delete_offer' (Angebot löschen).
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
/**
* @brief Aktion: Neues Angebot hinzufügen.
*
* Liest die übermittelten Formulardaten aus, validiert diese (Produkt-ID, Shop-ID und
* gültiger Preis) und fügt sie durch ein Prepared Statement in die Tabelle `offers` ein.
*/
if ($_POST['action'] === 'add_offer') { if ($_POST['action'] === 'add_offer') {
$productID = (int)$_POST['product_id']; $productID = (int)$_POST['product_id'];
$shopID = (int)$_POST['shop_id']; $shopID = (int)$_POST['shop_id'];
@ -66,10 +32,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$offerURL = trim($_POST['offer_url']); $offerURL = trim($_POST['offer_url']);
if ($productID > 0 && $shopID > 0 && $price >= 0) { if ($productID > 0 && $shopID > 0 && $price >= 0) {
/**
* @brief Einfügen in die Datenbank.
* Bereitet das SQL-Statement vor und bindet die Parameter sicher, um SQL-Injection zu verhindern.
*/
$stmt = $conn->prepare("INSERT INTO offers (productID, shopID, price, shippingCost, inStock, offerURL) VALUES (?, ?, ?, ?, ?, ?)"); $stmt = $conn->prepare("INSERT INTO offers (productID, shopID, price, shippingCost, inStock, offerURL) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("iiddis", $productID, $shopID, $price, $shippingCost, $inStock, $offerURL); $stmt->bind_param("iiddis", $productID, $shopID, $price, $shippingCost, $inStock, $offerURL);
if ($stmt->execute()) { if ($stmt->execute()) {
@ -81,19 +43,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
} }
$stmt->close(); $stmt->close();
} else { } else {
/**
* @brief Fehlerbehandlung bei ungültigen Eingaben.
*/
$message = 'Bitte alle Pflichtfelder korrekt ausfüllen.'; $message = 'Bitte alle Pflichtfelder korrekt ausfüllen.';
$messageType = 'error'; $messageType = 'error';
} }
} elseif ($_POST['action'] === 'delete_offer') { } elseif ($_POST['action'] === 'delete_offer') {
/**
* @brief Aktion: Existierendes Angebot löschen.
*
* Überprüft die übermittelte Angebot-ID und löscht den entsprechenden Eintrag
* sicher per Prepared Statement aus der Datenbank.
*/
$offerID = (int)$_POST['offer_id']; $offerID = (int)$_POST['offer_id'];
if ($offerID > 0) { if ($offerID > 0) {
$stmt = $conn->prepare("DELETE FROM offers WHERE offerID = ?"); $stmt = $conn->prepare("DELETE FROM offers WHERE offerID = ?");
@ -110,14 +63,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
} }
} }
/**
* @brief Abrufen aller Produkte für das Dropdown-Feld.
*
* Führt eine Query aus, um alle verfügbaren Produkte (IDs und Modelle)
* alphabetisch geordnet aus der Datenbank zu laden.
*
* @var array $products Enthält die geladenen Produkte.
*/
// Get all products for dropdown // Get all products for dropdown
$productsResult = $conn->query("SELECT productID, model FROM products ORDER BY model ASC"); $productsResult = $conn->query("SELECT productID, model FROM products ORDER BY model ASC");
$products = []; $products = [];
@ -127,14 +72,6 @@ if ($productsResult) {
} }
} }
/**
* @brief Abrufen aller Shops für das Dropdown-Feld.
*
* Führt eine Query aus, um alle verfügbaren Shops (IDs und Namen)
* alphabetisch geordnet aus der Datenbank zu laden.
*
* @var array $shops Enthält die geladenen Shops.
*/
// Get all shops for dropdown // Get all shops for dropdown
$shopsResult = $conn->query("SELECT shopID, name FROM shops ORDER BY name ASC"); $shopsResult = $conn->query("SELECT shopID, name FROM shops ORDER BY name ASC");
$shops = []; $shops = [];
@ -144,15 +81,6 @@ if ($shopsResult) {
} }
} }
/**
* @brief Filterlogik für die Anzeige existierender Angebote.
*
* Überprüft, ob über GET ein Filter für eine bestimmte Produkt-ID gesetzt wurde.
* Ist dies der Fall, werden nur Angebote für dieses Produkt angezeigt.
*
* @var int $filterProductID Die ID des zu filternden Produkts (0, wenn kein Filter gesetzt ist).
* @var string $offersQuery Die dynamisch generierte SQL-Abfrage für die Angebote.
*/
$filterProductID = isset($_GET['filter_product_id']) ? (int)$_GET['filter_product_id'] : 0; $filterProductID = isset($_GET['filter_product_id']) ? (int)$_GET['filter_product_id'] : 0;
$offersQuery = " $offersQuery = "
@ -163,20 +91,11 @@ $offersQuery = "
"; ";
if ($filterProductID > 0) { if ($filterProductID > 0) {
/**
* @brief Anwenden des Filters auf die SQL-Query, falls notwendig.
*/
$offersQuery .= " WHERE o.productID = " . $filterProductID; $offersQuery .= " WHERE o.productID = " . $filterProductID;
} }
$offersQuery .= " ORDER BY o.offerID DESC"; $offersQuery .= " ORDER BY o.offerID DESC";
/**
* @brief Ausführen der Angebot-Abfrage und Speichern in ein Array.
*
* @var array $existingOffers Enthält die geladenen Angebote inkl. Shop- und Produkt-Namen,
* die zur Anzeige in der Tabelle verwendet werden.
*/
// Get existing offers to manage // Get existing offers to manage
$offersResult = $conn->query($offersQuery); $offersResult = $conn->query($offersQuery);
$existingOffers = []; $existingOffers = [];
@ -186,9 +105,6 @@ if ($offersResult) {
} }
} }
/**
* @brief Einbinden des HTML-Headers, der Navigation und Stile.
*/
include 'header.php'; include 'header.php';
?> ?>
<main class="auth"> <main class="auth">
@ -301,10 +217,6 @@ include 'header.php';
<form method="POST" action="offerAdder.php<?php echo $filterProductID > 0 ? '?filter_product_id=' . $filterProductID : ''; ?>" onsubmit="return confirm('Möchtest du dieses Angebot wirklich löschen?');" style="display: inline-block; margin: 0;"> <form method="POST" action="offerAdder.php<?php echo $filterProductID > 0 ? '?filter_product_id=' . $filterProductID : ''; ?>" onsubmit="return confirm('Möchtest du dieses Angebot wirklich löschen?');" style="display: inline-block; margin: 0;">
<input type="hidden" name="action" value="delete_offer"> <input type="hidden" name="action" value="delete_offer">
<input type="hidden" name="offer_id" value="<?php echo $offer['offerID']; ?>"> <input type="hidden" name="offer_id" value="<?php echo $offer['offerID']; ?>">
<!--
@brief Button für das Löschen eines Angebotes.
Enthält ein SVG-Icon als Mülleimer. Ein Klick löst einen Bestätigungsdialog (JavaScript) aus.
-->
<button type="submit" style="background: none; border: none; color: #ef4444; cursor: pointer; display: flex; align-items: center; justify-content: flex-end; padding: 0.5rem; border-radius: 4px;" title="Angebot löschen" onmouseover="this.style.backgroundColor='#fee2e2';" onmouseout="this.style.backgroundColor='transparent';"> <button type="submit" style="background: none; border: none; color: #ef4444; cursor: pointer; display: flex; align-items: center; justify-content: flex-end; padding: 0.5rem; border-radius: 4px;" title="Angebot löschen" onmouseover="this.style.backgroundColor='#fee2e2';" onmouseout="this.style.backgroundColor='transparent';">
<svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24"> <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
<polyline points="3 6 5 6 21 6"></polyline> <polyline points="3 6 5 6 21 6"></polyline>
@ -324,9 +236,5 @@ include 'header.php';
</div> </div>
</section> </section>
</main> </main>
<?php <?php include 'footer.php'; ?>
/**
* @brief Einbinden des HTML-Footers am Ende der Seite.
*/
include 'footer.php';
?>