Geizkragen/offerAdder.php

134 lines
6.7 KiB
PHP

<?php
/**
* @file offerAdder.php
* @brief Angebot hinzufügen
*/
require_once __DIR__ . '/lib/bootstrap.php';
// Only ADMIN
if (empty($_SESSION['user_id']) || empty($_SESSION['user_roles']) || !in_array('ADMIN', $_SESSION['user_roles'], true)) {
http_response_code(403);
include 'header.php';
echo '<main class="auth"><section class="auth__grid"><div class="auth__card">';
echo '<h2 class="auth__title">Zugriff verweigert</h2>';
echo '<p>Du hast keine Berechtigung, Angebote hinzuzufügen.</p>';
echo '</div></section></main>';
include 'footer.php';
exit;
}
$conn = db_connect();
$message = '';
$messageType = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_offer') {
$productID = (int)$_POST['product_id'];
$shopID = (int)$_POST['shop_id'];
$price = (float)$_POST['price'];
$shippingCost = isset($_POST['shipping_cost']) && $_POST['shipping_cost'] !== '' ? (float)$_POST['shipping_cost'] : 0.00;
$inStock = isset($_POST['in_stock']) ? 1 : 0;
$offerURL = trim($_POST['offer_url']);
if ($productID > 0 && $shopID > 0 && $price >= 0) {
$stmt = $conn->prepare("INSERT INTO offers (productID, shopID, price, shippingCost, inStock, offerURL) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("iiddiss", $productID, $shopID, $price, $shippingCost, $inStock, $offerURL);
if ($stmt->execute()) {
$message = 'Angebot erfolgreich hinzugefügt!';
$messageType = 'success';
} else {
$message = 'Fehler beim Hinzufügen des Angebots.';
$messageType = 'error';
}
$stmt->close();
} else {
$message = 'Bitte alle Pflichtfelder korrekt ausfüllen.';
$messageType = 'error';
}
}
// Get all products for dropdown
$productsResult = $conn->query("SELECT productID, model FROM products ORDER BY model ASC");
$products = [];
if ($productsResult) {
while ($row = $productsResult->fetch_assoc()) {
$products[] = $row;
}
}
// Get all shops for dropdown
$shopsResult = $conn->query("SELECT shopID, name FROM shops ORDER BY name ASC");
$shops = [];
if ($shopsResult) {
while ($row = $shopsResult->fetch_assoc()) {
$shops[] = $row;
}
}
include 'header.php';
?>
<main class="page-main" style="padding: 2rem;">
<div class="container" style="max-width: 800px; margin: 0 auto; background: #fff; padding: 2rem; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
<h1 style="margin-bottom: 1.5rem;">Angebot hinzufügen</h1>
<?php if ($message): ?>
<div class="alert alert-<?php echo htmlspecialchars($messageType); ?>" style="padding:15px; margin-bottom: 20px; border-radius: 4px; background-color: <?php echo $messageType === 'success' ? '#d4edda' : '#f8d7da'; ?>; color: <?php echo $messageType === 'success' ? '#155724' : '#721c24'; ?>; border: 1px solid <?php echo $messageType === 'success' ? '#c3e6cb' : '#f5c6cb'; ?>;">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<form method="POST" action="offerAdder.php" style="display: flex; flex-direction: column; gap: 1.5rem;">
<input type="hidden" name="action" value="add_offer">
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
<label for="product_id" style="font-weight: 600;">Produkt auswählen *</label>
<select id="product_id" name="product_id" required style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
<option value="">-- Bitte wählen --</option>
<?php foreach ($products as $product): ?>
<option value="<?php echo htmlspecialchars($product['productID']); ?>" <?php echo (isset($_GET['productID']) && $_GET['productID'] == $product['productID']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($product['model']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
<label for="shop_id" style="font-weight: 600;">Shop auswählen *</label>
<select id="shop_id" name="shop_id" required style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
<option value="">-- Bitte wählen --</option>
<?php foreach ($shops as $shop): ?>
<option value="<?php echo htmlspecialchars($shop['shopID']); ?>">
<?php echo htmlspecialchars($shop['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div style="display: flex; gap: 1rem;">
<div style="flex: 1; display: flex; flex-direction: column; gap: 0.5rem;">
<label for="price" style="font-weight: 600;">Preis (€) *</label>
<input type="number" id="price" name="price" step="0.01" min="0" required style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
</div>
<div style="flex: 1; display: flex; flex-direction: column; gap: 0.5rem;">
<label for="shipping_cost" style="font-weight: 600;">Versandkosten (€)</label>
<input type="number" id="shipping_cost" name="shipping_cost" step="0.01" min="0" value="0.00" style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
</div>
</div>
<div style="display: flex; align-items: center; gap: 0.5rem;">
<input type="checkbox" id="in_stock" name="in_stock" value="1" checked style="width: 18px; height: 18px;">
<label for="in_stock" style="font-weight: 600; cursor: pointer;">Auf Lager</label>
</div>
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
<label for="offer_url" style="font-weight: 600;">Angebots-URL</label>
<input type="url" id="offer_url" name="offer_url" placeholder="https://" style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
</div>
<button type="submit" style="padding: 12px 20px; background: #007bff; color: white; font-weight: bold; border: none; border-radius: 4px; cursor: pointer; align-self: flex-start; text-transform: uppercase; font-size: 0.9rem;">Angebot hinzufügen</button>
</form>
</div>
</main>
<?php include 'footer.php'; ?>