34968-vm/restaurant/edit_menu_item.php
Flatlogic Bot 2d8abe32bb V27
2025-10-17 06:23:25 +00:00

129 lines
4.9 KiB
PHP

<?php
include 'header.php';
require_once '../db/config.php';
// Check if the user is logged in as a restaurant owner
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'restaurant_owner') {
header('Location: ../restaurant_login.php');
exit;
}
$menu_item_id = $_GET['id'] ?? null;
if (!$menu_item_id) {
header('Location: menu.php');
exit;
}
$pdo = db();
// Get the restaurant ID associated with the logged-in user
$stmt = $pdo->prepare("SELECT id FROM restaurants WHERE user_id = ?");
$stmt->execute([$_SESSION['user_id']]);
$restaurant = $stmt->fetch();
if (!$restaurant) {
header('Location: ../index.php');
exit;
}
$restaurant_id = $restaurant['id'];
// Get the menu item and verify it belongs to the correct restaurant
$stmt = $pdo->prepare("SELECT * FROM menu_items WHERE id = ? AND restaurant_id = ?");
$stmt->execute([$menu_item_id, $restaurant_id]);
$item = $stmt->fetch();
if (!$item) {
// If the item doesn't exist or doesn't belong to this owner, redirect
header('Location: menu.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$description = $_POST['description'] ?? '';
$price = $_POST['price'] ?? '';
$promotion_id = $_POST['promotion_id'] ?? null;
$image_url = $item['image_url']; // Keep old image by default
if ($name && $price) {
try {
// Handle new image upload
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
require_once '../includes/S3Service.php';
$tmp_path = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
$key = "menu_items/{$restaurant_id}/" . uniqid() . "." . $extension;
$new_image_url = S3Service::uploadFile($tmp_path, $key);
if ($new_image_url) {
$image_url = $new_image_url; // Set new image URL
} else {
throw new Exception("Failed to upload new image to S3.");
}
}
$stmt = $pdo->prepare("UPDATE menu_items SET name = ?, description = ?, price = ?, promotion_id = ?, image_url = ? WHERE id = ? AND restaurant_id = ?");
$stmt->execute([$name, $description, $price, $promotion_id, $image_url, $menu_item_id, $restaurant_id]);
header('Location: menu.php');
exit;
} catch (Exception $e) {
$error = "Error: " . $e->getMessage();
}
} else {
$error = "Name and price are required.";
}
}
$stmt = $pdo->prepare("SELECT * FROM special_promotions");
$stmt->execute();
$promotions = $stmt->fetchAll();
?>
<div class="container mt-4">
<h2>Edit Menu Item</h2>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form action="edit_menu_item.php?id=<?php echo $item['id']; ?>" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($item['name']); ?>" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description"><?php echo htmlspecialchars($item['description']); ?></textarea>
</div>
<div class="mb-3">
<label for="price" class="form-label">Price</label>
<input type="number" step="0.01" class="form-control" id="price" name="price" value="<?php echo htmlspecialchars($item['price']); ?>" required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Image</label>
<input type="file" class="form-control" id="image" name="image">
<?php if ($item['image_url']): ?>
<div class="mt-2">
<img src="<?php echo htmlspecialchars($item['image_url']); ?>" alt="Current Image" style="max-width: 200px; height: auto;">
</div>
<?php endif; ?>
</div>
<div class="mb-3">
<label for="promotion_id" class="form-label">Promotion</label>
<select class="form-control" id="promotion_id" name="promotion_id">
<option value="">None</option>
<?php foreach ($promotions as $promotion): ?>
<option value="<?= $promotion['id'] ?>" <?= $item['promotion_id'] == $promotion['id'] ? 'selected' : '' ?>><?= htmlspecialchars($promotion['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Update Item</button>
<a href="menu.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
<?php include 'footer.php'; ?>