35866-vm/admin/index.php
Flatlogic Bot 903cf599f0 rfresh1
2025-11-20 09:45:59 +00:00

79 lines
3.1 KiB
PHP

<?php
session_start();
require_once '../db/config.php';
// Check if user is logged in and is an admin
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== 'admin') {
header("Location: ../login.php");
exit();
}
// Fetch all recipes from the database
try {
$stmt = db()->query("SELECT * FROM recipes ORDER BY created_at DESC");
$recipes = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Handle database errors
$error = "Error fetching recipes: " . $e->getMessage();
$recipes = [];
}
include 'templates/header.php';
?>
<div class="admin-header">
<h1>Manage Recipes</h1>
<a href="create.php" class="btn btn-primary"><i class="fas fa-plus me-2"></i> Create New Recipe</a>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if (isset($_SESSION['success_message'])): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($_SESSION['success_message']); unset($_SESSION['success_message']); ?></div>
<?php endif; ?>
<div class="card admin-table">
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Servings</th>
<th>Prep Time</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($recipes)): ?>
<tr>
<td colspan="5" class="text-center">No recipes found.</td>
</tr>
<?php else: ?>
<?php foreach ($recipes as $recipe): ?>
<tr>
<td>
<img src="../<?php echo htmlspecialchars($recipe['image_path']); ?>" alt="<?php echo htmlspecialchars($recipe['title']); ?>" style="width: 100px; height: 60px; object-fit: cover; border-radius: 8px;">
</td>
<td><?php echo htmlspecialchars($recipe['title']); ?></td>
<td><?php echo htmlspecialchars($recipe['servings']); ?></td>
<td><?php echo htmlspecialchars($recipe['prep_time']); ?></td>
<td class="text-end">
<a href="edit.php?id=<?php echo $recipe['id']; ?>" class="btn btn-sm btn-outline-primary">
<i class="fas fa-edit"></i> Edit
</a>
<a href="delete.php?id=<?php echo $recipe['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this recipe?');">
<i class="fas fa-trash"></i> Delete
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php include 'templates/footer.php'; ?>