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"; ?> <?= htmlspecialchars($page_title) ?> - Petrol Pump Management

Name Description Created At Actions
No roles found.