241 lines
12 KiB
PHP
241 lines
12 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'])) {
|
|
if ($_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("iiddis", $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';
|
|
}
|
|
} elseif ($_POST['action'] === 'delete_offer') {
|
|
$offerID = (int)$_POST['offer_id'];
|
|
if ($offerID > 0) {
|
|
$stmt = $conn->prepare("DELETE FROM offers WHERE offerID = ?");
|
|
$stmt->bind_param("i", $offerID);
|
|
if ($stmt->execute()) {
|
|
$message = 'Angebot erfolgreich gelöscht!';
|
|
$messageType = 'success';
|
|
} else {
|
|
$message = 'Fehler beim Löschen des Angebots.';
|
|
$messageType = 'error';
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
$filterProductID = isset($_GET['filter_product_id']) ? (int)$_GET['filter_product_id'] : 0;
|
|
|
|
$offersQuery = "
|
|
SELECT o.offerID, p.model AS productName, s.name AS shopName, o.price
|
|
FROM offers o
|
|
JOIN products p ON o.productID = p.productID
|
|
JOIN shops s ON o.shopID = s.shopID
|
|
";
|
|
|
|
if ($filterProductID > 0) {
|
|
$offersQuery .= " WHERE o.productID = " . $filterProductID;
|
|
}
|
|
|
|
$offersQuery .= " ORDER BY o.offerID DESC";
|
|
|
|
// Get existing offers to manage
|
|
$offersResult = $conn->query($offersQuery);
|
|
$existingOffers = [];
|
|
if ($offersResult) {
|
|
while ($row = $offersResult->fetch_assoc()) {
|
|
$existingOffers[] = $row;
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
<main class="auth">
|
|
<section class="auth__grid" style="grid-template-columns: 1fr;">
|
|
<div class="auth__card">
|
|
<header class="auth__header">
|
|
<h2 class="auth__title">Angebot verwalten</h2>
|
|
</header>
|
|
|
|
<?php if ($message): ?>
|
|
<p class="<?php echo $messageType === 'success' ? 'auth__alert__sucess' : 'auth__alert__error'; ?>" style="margin-bottom: 1rem;">
|
|
<?php echo htmlspecialchars($message); ?>
|
|
</p>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="offerAdder.php" class="auth__form">
|
|
<input type="hidden" name="action" value="add_offer">
|
|
|
|
<div class="auth__select__wrap">
|
|
<label class="auth__select__label" for="product_id">Produkt auswählen *</label>
|
|
<select id="product_id" name="product_id" class="auth__select" required>
|
|
<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 class="auth__select__wrap" style="margin-top: 1rem;">
|
|
<label class="auth__select__label" for="shop_id">Shop auswählen *</label>
|
|
<select id="shop_id" name="shop_id" class="auth__select" required>
|
|
<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; margin-top: 1rem;">
|
|
<div style="flex: 1;">
|
|
<label for="price" style="display: block; margin-bottom: 0.5rem; font-weight: 500; color: var(--text-muted);">Preis (€) *</label>
|
|
<input type="number" id="price" name="price" class="auth__input" step="0.01" min="0" required>
|
|
</div>
|
|
<div style="flex: 1;">
|
|
<label for="shipping_cost" style="display: block; margin-bottom: 0.5rem; font-weight: 500; color: var(--text-muted);">Versandkosten (€)</label>
|
|
<input type="number" id="shipping_cost" name="shipping_cost" class="auth__input" step="0.01" min="0" value="0.00">
|
|
</div>
|
|
</div>
|
|
|
|
<div style="margin-top: 1rem; 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; accent-color: var(--primary-color);">
|
|
<label for="in_stock" style="font-weight: 500; color: var(--text-muted); cursor: pointer;">Auf Lager</label>
|
|
</div>
|
|
|
|
<div style="margin-top: 1rem;">
|
|
<label for="offer_url" style="display: block; margin-bottom: 0.5rem; font-weight: 500; color: var(--text-muted);">Angebots-URL</label>
|
|
<input type="url" id="offer_url" name="offer_url" class="auth__input" placeholder="https://">
|
|
</div>
|
|
|
|
<div class="auth__actions" style="margin-top: 1.5rem;">
|
|
<button type="submit" class="auth__submit">Angebot hinzufügen</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="auth__card" style="margin-top: 2rem;">
|
|
<header class="auth__header">
|
|
<h2 class="auth__title">Bestehende Angebote verwalten</h2>
|
|
</header>
|
|
|
|
<form method="GET" action="offerAdder.php" style="margin-bottom: 1.5rem; display: flex; gap: 1rem; align-items: flex-end;">
|
|
<div class="auth__select__wrap" style="flex: 1;">
|
|
<label class="auth__select__label" for="filter_product_id">Nach Produkt filtern</label>
|
|
<select id="filter_product_id" name="filter_product_id" class="auth__select" onchange="this.form.submit()">
|
|
<option value="">-- Alle Produkte anzeigen --</option>
|
|
<?php foreach ($products as $product): ?>
|
|
<option value="<?php echo htmlspecialchars($product['productID']); ?>" <?php echo ($filterProductID == $product['productID']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($product['model']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<?php if ($filterProductID > 0): ?>
|
|
<a href="offerAdder.php" style="color: var(--primary-color); text-decoration: none; font-weight: 500; font-size: 0.9rem; padding-bottom: 0.75rem;">Filter zurücksetzen</a>
|
|
<?php endif; ?>
|
|
</form>
|
|
|
|
<?php if (count($existingOffers) > 0): ?>
|
|
<div style="overflow-x: auto; margin-top: 1rem;">
|
|
<table style="width: 100%; border-collapse: collapse; min-width: 500px;">
|
|
<thead>
|
|
<tr style="border-bottom: 2px solid var(--border-color); text-align: left;">
|
|
<th style="padding: 0.75rem 0.5rem; color: var(--text-muted); font-weight: 600;">Produkt</th>
|
|
<th style="padding: 0.75rem 0.5rem; color: var(--text-muted); font-weight: 600;">Shop</th>
|
|
<th style="padding: 0.75rem 0.5rem; color: var(--text-muted); font-weight: 600;">Preis</th>
|
|
<th style="padding: 0.75rem 0.5rem; text-align: right; color: var(--text-muted); font-weight: 600;">Aktion</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($existingOffers as $offer): ?>
|
|
<tr style="border-bottom: 1px solid var(--border-light);">
|
|
<td style="padding: 1rem 0.5rem; vertical-align: middle;"><?php echo htmlspecialchars($offer['productName']); ?></td>
|
|
<td style="padding: 1rem 0.5rem; vertical-align: middle;"><?php echo htmlspecialchars($offer['shopName']); ?></td>
|
|
<td style="padding: 1rem 0.5rem; vertical-align: middle; font-weight: 600;">€<?php echo number_format($offer['price'], 2, ',', '.'); ?></td>
|
|
<td style="padding: 1rem 0.5rem; text-align: right; vertical-align: middle;">
|
|
<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="offer_id" value="<?php echo $offer['offerID']; ?>">
|
|
<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">
|
|
<polyline points="3 6 5 6 21 6"></polyline>
|
|
<path d="M19 6V20a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
|
</svg>
|
|
</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php else: ?>
|
|
<p style="margin-top: 1rem; color: var(--text-muted); text-align: center; padding: 2rem 0;">Keine Angebote vorhanden.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
<?php include 'footer.php'; ?>
|
|
|