prepare("SELECT id FROM restaurants WHERE user_id = ?"); $stmt->execute([$_SESSION['user_id']]); $restaurant = $stmt->fetch(); if (!$restaurant) { // If for some reason the user is a restaurant owner but has no restaurant, redirect them header('Location: ../index.php'); exit; } $restaurant_id = $restaurant['id']; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $description = $_POST['description'] ?? ''; $price = $_POST['price'] ?? ''; $promotion_id = $_POST['promotion_id'] ?? null; $image_url = null; if ($name && $price) { try { 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']; $file_type = $_FILES['image']['type']; $extension = pathinfo($file_name, PATHINFO_EXTENSION); $key = "menu_items/{$restaurant_id}/" . uniqid() . "." . $extension; $image_url = S3Service::uploadFile($tmp_path, $key); if (!$image_url) { throw new Exception("Failed to upload image to S3."); } } $stmt = $pdo->prepare("INSERT INTO menu_items (restaurant_id, name, description, price, promotion_id, image_url) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$restaurant_id, $name, $description, $price, $promotion_id, $image_url]); 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(); ?>