37392-vm/categories.php
Flatlogic Bot b400910e2f gastos02
2026-01-13 02:34:53 +00:00

146 lines
5.8 KiB
PHP

<?php
require_once 'db/config.php';
$pdo = db();
$message = '';
$error = '';
$edit_category = null;
// Handle form submissions for adding/editing a category
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name']);
$id = $_POST['id'] ?? null;
if (empty($name)) {
$error = 'Category name is required.';
} else {
try {
if ($id) {
// Update existing category
$stmt = $pdo->prepare('UPDATE categories SET name = ? WHERE id = ?');
$stmt->execute([$name, $id]);
$message = 'Category updated successfully!';
} else {
// Insert new category
$stmt = $pdo->prepare('INSERT INTO categories (name) VALUES (?)');
$stmt->execute([$name]);
$message = 'Category added successfully!';
}
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$error = 'Category name already exists.';
} else {
$error = 'Database error: ' . $e->getMessage();
}
}
}
}
// Handle deleting a category
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
try {
$stmt = $pdo->prepare('DELETE FROM categories WHERE id = ?');
$stmt->execute([$id]);
$message = 'Category deleted successfully!';
} catch (PDOException $e) {
$error = 'Error deleting category. It might be in use.';
}
}
// Handle fetching a category for editing
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$stmt = $pdo->prepare('SELECT * FROM categories WHERE id = ?');
$stmt->execute([$id]);
$edit_category = $stmt->fetch();
}
// Fetch all categories to display
$stmt = $pdo->query('SELECT * FROM categories ORDER BY name');
$categories = $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 Categories</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="container mt-5">
<header class="d-flex justify-content-between align-items-center mb-4">
<h1>Manage Categories</h1>
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
</header>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<!-- Add/Edit Category Form -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0"><?php echo $edit_category ? 'Edit Category' : 'Add New Category'; ?></h5>
</div>
<div class="card-body">
<form action="categories.php" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_category['id'] ?? ''); ?>">
<div class="mb-3">
<label for="name" class="form-label">Category Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_category['name'] ?? ''); ?>" required>
</div>
<button type="submit" class="btn btn-primary"><?php echo $edit_category ? 'Update Category' : 'Add Category'; ?></button>
<?php if ($edit_category): ?>
<a href="categories.php" class="btn btn-secondary">Cancel Edit</a>
<?php endif; ?>
</form>
</div>
</div>
<!-- Categories List -->
<div class="card">
<div class="card-header">
<h5 class="mb-0">Existing Categories</h5>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($categories)): ?>
<tr>
<td colspan="2" class="text-center">No categories found.</td>
</tr>
<?php else: ?>
<?php foreach ($categories as $category): ?>
<tr>
<td><?php echo htmlspecialchars($category['name']); ?></td>
<td class="text-end">
<a href="categories.php?edit=<?php echo $category['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i> Edit</a>
<a href="categories.php?delete=<?php echo $category['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this category?');"><i class="bi bi-trash"></i> Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>