pizzeria.it/ingredient_types_management.php

98 lines
3.8 KiB
PHP

<?php
session_start();
require 'db_con.php'; // Inclure la connexion à la base de données
// Vérifiez si l'utilisateur est connecté
if (!isset($_SESSION['user_id'])) {
header("Location: index.php");
exit;
}
// Vérifiez le rôle de l'utilisateur
$role = $_SESSION['role'];
if ($role !== 'admin' && $role !== 'chef') {
header("Location: main.php");
exit;
}
// Ajouter un type d'ingrédient
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_ingredient_type'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$stmt = $pdo->prepare("INSERT INTO ingredients_types (name, description) VALUES (:name, :description)");
$stmt->execute(['name' => $name, 'description' => $description]);
}
// Modifier un type d'ingrédient
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_ingredient_type'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$description = $_POST['description'];
$stmt = $pdo->prepare("UPDATE ingredients_types SET name = :name, description = :description WHERE id = :id");
$stmt->execute(['name' => $name, 'description' => $description, 'id' => $id]);
}
// Supprimer un type d'ingrédient
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_ingredient_type'])) {
$id = $_POST['id'];
$stmt = $pdo->prepare("DELETE FROM ingredients_types WHERE id = :id");
$stmt->execute(['id' => $id]);
}
// Récupérer tous les types d'ingrédients
$stmt = $pdo->query("SELECT * FROM ingredients_types");
$ingredient_types = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Ingredient Types</title>
</head>
<body>
<h1>Manage Ingredient Types</h1>
<!-- Formulaire pour ajouter un type d'ingrédient -->
<h2>Add Ingredient Type</h2>
<form method="POST">
<input type="text" name="name" required placeholder="Ingredient Type Name">
<input type="text" name="description" placeholder="Description">
<button type="submit" name="add_ingredient_type">Add</button>
</form>
<h2>Existing Ingredient Types</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
<?php foreach ($ingredient_types as $type): ?>
<tr>
<td><?php echo htmlspecialchars($type['id']); ?></td>
<td><?php echo htmlspecialchars($type['name']); ?></td>
<td><?php echo htmlspecialchars($type['description']); ?></td>
<td>
<!-- Formulaire pour modifier un type d'ingrédient -->
<form method="POST" style="display:inline;">
<input type="hidden" name="id" value="<?php echo $type['id']; ?>">
<input type="text" name="name" value="<?php echo htmlspecialchars($type['name']); ?>" required>
<input type="text" name="description" value="<?php echo htmlspecialchars($type['description']); ?>">
<button type="submit" name="edit_ingredient_type">Edit</button>
</form>
<!-- Formulaire pour supprimer un type d'ingrédient -->
<form method="POST" style="display:inline;">
<input type="hidden" name="id" value="<?php echo $type['id']; ?>">
<button type="submit" name="delete_ingredient_type" onclick="return confirm('Are you sure you want to delete this ingredient type?');">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<a href="main.php">Retour à la page principale</a>
</body>
</html>