diff --git a/admin/add_restaurant.php b/admin/add_restaurant.php index 729ddb6c..98e35076 100644 --- a/admin/add_restaurant.php +++ b/admin/add_restaurant.php @@ -8,19 +8,42 @@ if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== tru exit; } +$pdo = db(); + +// Fetch all cuisines +$cuisines_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name ASC"); +$cuisines = $cuisines_stmt->fetchAll(); + if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; - $cuisine = $_POST['cuisine'] ?? ''; + $description = $_POST['description'] ?? ''; $image_url = $_POST['image_url'] ?? ''; + $selected_cuisines = $_POST['cuisines'] ?? []; - if ($name && $cuisine) { - $pdo = db(); - $stmt = $pdo->prepare("INSERT INTO restaurants (name, cuisine, image_url) VALUES (?, ?, ?)"); - $stmt->execute([$name, $cuisine, $image_url]); - header('Location: index.php'); - exit; + if ($name && !empty($selected_cuisines)) { + try { + $pdo->beginTransaction(); + + // Insert into restaurants table + $stmt = $pdo->prepare("INSERT INTO restaurants (name, description, image_url) VALUES (?, ?, ?)"); + $stmt->execute([$name, $description, $image_url]); + $restaurant_id = $pdo->lastInsertId(); + + // Insert into restaurant_cuisines table + $cuisine_stmt = $pdo->prepare("INSERT INTO restaurant_cuisines (restaurant_id, cuisine_id) VALUES (?, ?)"); + foreach ($selected_cuisines as $cuisine_id) { + $cuisine_stmt->execute([$restaurant_id, $cuisine_id]); + } + + $pdo->commit(); + header('Location: index.php'); + exit; + } catch (Exception $e) { + $pdo->rollBack(); + $error = "Error adding restaurant: " . $e->getMessage(); + } } else { - $error = "Name and cuisine are required."; + $error = "Name and at least one cuisine are required."; } } ?> @@ -38,8 +61,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- - + + +
+
+ +
+ +
+
+ + +
+
+ +
@@ -50,4 +88,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- + \ No newline at end of file diff --git a/admin/cuisines.php b/admin/cuisines.php new file mode 100644 index 00000000..84699c12 --- /dev/null +++ b/admin/cuisines.php @@ -0,0 +1,79 @@ +prepare("INSERT INTO cuisines (name) VALUES (:name) ON CONFLICT (name) DO NOTHING"); + $stmt->bindValue(':name', $name, PDO::PARAM_STR); + $stmt->execute(); + } + } elseif (isset($_POST['delete_cuisine'])) { + $id = $_POST['id']; + $stmt = $db->prepare("DELETE FROM cuisines WHERE id = :id"); + $stmt->bindValue(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + } +} + +// Fetch all cuisines +$cuisines = $db->query("SELECT * FROM cuisines ORDER BY name ASC")->fetchAll(); + +?> + +
+

Manage Cuisines

+ + +
+

Add New Cuisine

+
+
+ + +
+
+ +
+
+
+ + +
+

Existing Cuisines

+ + + + + + + + + + + + + + + + + +
IDNameActions
+
+ + +
+
+
+
+ + diff --git a/admin/edit_restaurant.php b/admin/edit_restaurant.php index 9ca1f424..88faddca 100644 --- a/admin/edit_restaurant.php +++ b/admin/edit_restaurant.php @@ -18,20 +18,42 @@ $pdo = db(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; - $cuisine = $_POST['cuisine'] ?? ''; + $description = $_POST['description'] ?? ''; $image_url = $_POST['image_url'] ?? ''; + $selected_cuisines = $_POST['cuisines'] ?? []; $id = $_POST['id'] ?? null; - if ($name && $cuisine && $id) { - $stmt = $pdo->prepare("UPDATE restaurants SET name = ?, cuisine = ?, image_url = ? WHERE id = ?"); - $stmt->execute([$name, $cuisine, $image_url, $id]); - header('Location: index.php'); - exit; + if ($name && !empty($selected_cuisines) && $id) { + try { + $pdo->beginTransaction(); + + // Update restaurants table + $stmt = $pdo->prepare("UPDATE restaurants SET name = ?, description = ?, image_url = ? WHERE id = ?"); + $stmt->execute([$name, $description, $image_url, $id]); + + // Delete existing cuisine associations + $delete_stmt = $pdo->prepare("DELETE FROM restaurant_cuisines WHERE restaurant_id = ?"); + $delete_stmt->execute([$id]); + + // Insert new cuisine associations + $cuisine_stmt = $pdo->prepare("INSERT INTO restaurant_cuisines (restaurant_id, cuisine_id) VALUES (?, ?)"); + foreach ($selected_cuisines as $cuisine_id) { + $cuisine_stmt->execute([$id, $cuisine_id]); + } + + $pdo->commit(); + header('Location: index.php'); + exit; + } catch (Exception $e) { + $pdo->rollBack(); + $error = "Error updating restaurant: " . $e->getMessage(); + } } else { - $error = "Name and cuisine are required."; + $error = "Name and at least one cuisine are required."; } } +// Fetch restaurant details $stmt = $pdo->prepare("SELECT * FROM restaurants WHERE id = ?"); $stmt->execute([$id]); $restaurant = $stmt->fetch(); @@ -40,6 +62,16 @@ if (!$restaurant) { header('Location: index.php'); exit; } + +// Fetch all cuisines +$cuisines_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name ASC"); +$cuisines = $cuisines_stmt->fetchAll(); + +// Fetch this restaurant's cuisines +$restaurant_cuisines_stmt = $pdo->prepare("SELECT cuisine_id FROM restaurant_cuisines WHERE restaurant_id = ?"); +$restaurant_cuisines_stmt->execute([$id]); +$restaurant_cuisine_ids = $restaurant_cuisines_stmt->fetchAll(PDO::FETCH_COLUMN); + ?>
@@ -56,16 +88,31 @@ if (!$restaurant) {
- - + + +
+
+ +
+ +
+
+ > + +
+
+ +
- +
Cancel - + \ No newline at end of file diff --git a/admin/header.php b/admin/header.php index 21bc80a6..1deb2361 100644 --- a/admin/header.php +++ b/admin/header.php @@ -31,6 +31,9 @@ if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== tru +