287 lines
13 KiB
PHP
287 lines
13 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$notification = null;
|
|
|
|
// Handle Add, Edit, Delete actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$db = db();
|
|
// Add Role
|
|
if (isset($_POST['add_role'])) {
|
|
$name = trim($_POST['name']);
|
|
$description = trim($_POST['description']);
|
|
|
|
if (!empty($name)) {
|
|
try {
|
|
$stmt = $db->prepare("INSERT INTO roles (name, description) VALUES (?, ?)");
|
|
$stmt->execute([$name, $description]);
|
|
$_SESSION['notification'] = ['text' => 'Role added successfully!', 'type' => 'success'];
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error adding role: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
} else {
|
|
$_SESSION['notification'] = ['text' => 'Role name is required.', 'type' => 'warning'];
|
|
}
|
|
}
|
|
// Edit Role
|
|
elseif (isset($_POST['edit_role'])) {
|
|
$id = $_POST['role_id'];
|
|
$name = trim($_POST['name']);
|
|
$description = trim($_POST['description']);
|
|
|
|
if (!empty($name) && !empty($id)) {
|
|
try {
|
|
$stmt = $db->prepare("UPDATE roles SET name = ?, description = ? WHERE id = ?");
|
|
$stmt->execute([$name, $description, $id]);
|
|
$_SESSION['notification'] = ['text' => 'Role updated successfully!', 'type' => 'success'];
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error updating role: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
} else {
|
|
$_SESSION['notification'] = ['text' => 'Role name and ID are required.', 'type' => 'warning'];
|
|
}
|
|
}
|
|
header("Location: roles.php");
|
|
exit;
|
|
}
|
|
|
|
// Delete Role
|
|
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
|
|
try {
|
|
$db = db();
|
|
// Check if any user is assigned this role
|
|
$stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE role_id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
if ($stmt->fetchColumn() > 0) {
|
|
$_SESSION['notification'] = ['text' => 'Cannot delete role. It is currently assigned to one or more users.', 'type' => 'warning'];
|
|
} else {
|
|
$stmt = $db->prepare("DELETE FROM roles WHERE id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
$_SESSION['notification'] = ['text' => 'Role deleted successfully!', 'type' => 'success'];
|
|
}
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error deleting role: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
header("Location: roles.php");
|
|
exit;
|
|
}
|
|
|
|
|
|
if (isset($_SESSION['notification'])) {
|
|
$notification = $_SESSION['notification'];
|
|
unset($_SESSION['notification']);
|
|
}
|
|
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->query("SELECT id, name, description, created_at FROM roles ORDER BY name ASC");
|
|
$roles = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
$roles = [];
|
|
$notification = ['text' => 'Error fetching roles: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
|
|
$page_title = "Role Management";
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> - Petrol Pump Management</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/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">
|
|
</head>
|
|
<body>
|
|
<?php include 'includes/header.php'; ?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h1><i class="bi bi-person-badge"></i> <?= htmlspecialchars($page_title) ?></h1>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addRoleModal">
|
|
<i class="bi bi-plus-circle"></i> Add New Role
|
|
</button>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Description</th>
|
|
<th>Created At</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($roles)): ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center">No roles found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($roles as $role): ?>
|
|
<tr>
|
|
<td><span class="badge bg-primary"><?= htmlspecialchars($role['name']) ?></span></td>
|
|
<td><?= htmlspecialchars($role['description']) ?></td>
|
|
<td><?= date('d M, Y', strtotime($role['created_at'])) ?></td>
|
|
<td>
|
|
<button type="button" class="btn btn-sm btn-outline-secondary edit-btn"
|
|
data-bs-toggle="modal" data-bs-target="#editRoleModal"
|
|
data-id="<?= $role['id'] ?>"
|
|
data-name="<?= htmlspecialchars($role['name']) ?>"
|
|
data-description="<?= htmlspecialchars($role['description']) ?>">
|
|
<i class="bi bi-pencil"></i>
|
|
</button>
|
|
<button type="button" class="btn btn-sm btn-outline-danger delete-btn"
|
|
data-bs-toggle="modal" data-bs-target="#deleteConfirmModal"
|
|
data-id="<?= $role['id'] ?>">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Role Modal -->
|
|
<div class="modal fade" id="addRoleModal" tabindex="-1" aria-labelledby="addRoleModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form action="roles.php" method="POST">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="addRoleModalLabel">Add New Role</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Role Name*</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" name="add_role" class="btn btn-primary">Save Role</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Role Modal -->
|
|
<div class="modal fade" id="editRoleModal" tabindex="-1" aria-labelledby="editRoleModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form action="roles.php" method="POST">
|
|
<input type="hidden" name="role_id" id="edit_role_id">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="editRoleModalLabel">Edit Role</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label for="edit_name" class="form-label">Role Name*</label>
|
|
<input type="text" class="form-control" id="edit_name" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="edit_description" name="description" rows="3"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" name="edit_role" class="btn btn-primary">Save Changes</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Delete Confirmation Modal -->
|
|
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-labelledby="deleteConfirmModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="deleteConfirmModalLabel">Confirm Deletion</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
Are you sure you want to delete this role? This action cannot be undone.
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<a href="#" id="delete-confirm-btn" class="btn btn-danger">Delete</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Toast Notification -->
|
|
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
|
|
<div id="notificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header">
|
|
<strong class="me-auto">Notification</strong>
|
|
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
<div class="toast-body">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
// Toast notification
|
|
<?php if ($notification): ?>
|
|
const toastEl = document.getElementById('notificationToast');
|
|
const toastBody = toastEl.querySelector('.toast-body');
|
|
|
|
toastEl.classList.remove('bg-success', 'bg-danger', 'bg-warning');
|
|
toastEl.classList.add('bg-<?= $notification['type'] ?>', 'text-white');
|
|
toastBody.textContent = '<?= addslashes(htmlspecialchars($notification['text'])) ?>';
|
|
|
|
const toast = new bootstrap.Toast(toastEl);
|
|
toast.show();
|
|
<?php endif; ?>
|
|
|
|
// Edit modal handler
|
|
const editRoleModal = document.getElementById('editRoleModal');
|
|
editRoleModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const id = button.getAttribute('data-id');
|
|
const name = button.getAttribute('data-name');
|
|
const description = button.getAttribute('data-description');
|
|
|
|
const modalTitle = editRoleModal.querySelector('.modal-title');
|
|
const modalBodyInputId = editRoleModal.querySelector('#edit_role_id');
|
|
const modalBodyInputName = editRoleModal.querySelector('#edit_name');
|
|
const modalBodyInputDescription = editRoleModal.querySelector('#edit_description');
|
|
|
|
modalTitle.textContent = 'Edit Role: ' + name;
|
|
modalBodyInputId.value = id;
|
|
modalBodyInputName.value = name;
|
|
modalBodyInputDescription.value = description;
|
|
});
|
|
|
|
// Delete confirmation handler
|
|
const deleteConfirmModal = document.getElementById('deleteConfirmModal');
|
|
deleteConfirmModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const id = button.getAttribute('data-id');
|
|
const deleteBtn = deleteConfirmModal.querySelector('#delete-confirm-btn');
|
|
deleteBtn.href = 'roles.php?action=delete&id=' + id;
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|