35005-vm/users.php
Flatlogic Bot d9ae8f552d v3
2025-10-16 12:45:17 +00:00

330 lines
17 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 User
if (isset($_POST['add_user'])) {
$username = trim($_POST['username']);
$password = $_POST['password'];
$role_id = $_POST['role_id'];
$bunk_id = !empty($_POST['bunk_id']) ? $_POST['bunk_id'] : null;
if (!empty($username) && !empty($password) && !empty($role_id)) {
try {
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
$stmt = $db->prepare("INSERT INTO users (username, password, role_id, bunk_id) VALUES (?, ?, ?, ?)");
$stmt->execute([$username, $hashed_password, $role_id, $bunk_id]);
$_SESSION['notification'] = ['text' => 'User added successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error adding user: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Username, password, and role are required.', 'type' => 'warning'];
}
}
// Edit User
elseif (isset($_POST['edit_user'])) {
$id = $_POST['user_id'];
$username = trim($_POST['username']);
$role_id = $_POST['role_id'];
$bunk_id = !empty($_POST['bunk_id']) ? $_POST['bunk_id'] : null;
$password = $_POST['password'];
if (!empty($username) && !empty($role_id) && !empty($id)) {
try {
if (!empty($password)) {
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
$stmt = $db->prepare("UPDATE users SET username = ?, role_id = ?, bunk_id = ?, password = ? WHERE id = ?");
$stmt->execute([$username, $role_id, $bunk_id, $hashed_password, $id]);
} else {
$stmt = $db->prepare("UPDATE users SET username = ?, role_id = ?, bunk_id = ? WHERE id = ?");
$stmt->execute([$username, $role_id, $bunk_id, $id]);
}
$_SESSION['notification'] = ['text' => 'User updated successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error updating user: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Username, role, and ID are required.', 'type' => 'warning'];
}
}
header("Location: users.php");
exit;
}
// Delete User (Soft Delete)
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$db = db();
$stmt = $db->prepare("UPDATE users SET deleted_at = NOW() WHERE id = ?");
$stmt->execute([$_GET['id']]);
$_SESSION['notification'] = ['text' => 'User deleted successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error deleting user: ' . $e->getMessage(), 'type' => 'danger'];
}
header("Location: users.php");
exit;
}
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
// Fetch roles and bunks for dropdowns
$roles = db()->query("SELECT * FROM roles")->fetchAll(PDO::FETCH_ASSOC);
$bunks = db()->query("SELECT * FROM bunks WHERE deleted_at IS NULL")->fetchAll(PDO::FETCH_ASSOC);
$users = db()->query("SELECT u.*, r.name as role_name, b.name as bunk_name FROM users u JOIN roles r ON u.role_id = r.id LEFT JOIN bunks b ON u.bunk_id = b.id WHERE u.deleted_at IS NULL ORDER BY u.created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
$page_title = "User 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-people-fill"></i> <?= htmlspecialchars($page_title) ?></h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">
<i class="bi bi-plus-circle"></i> Add New User
</button>
</div>
<div class="card">
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Role</th>
<th>Assigned Bunk</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)): ?>
<tr>
<td colspan="6" class="text-center">No users found.</td>
</tr>
<?php else: ?>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['id']) ?></td>
<td><?= htmlspecialchars($user['username']) ?></td>
<td><span class="badge bg-secondary"><?= htmlspecialchars($user['role_name']) ?></span></td>
<td><?= htmlspecialchars($user['bunk_name'] ?? 'N/A') ?></td>
<td><?= htmlspecialchars(date('Y-m-d H:i', strtotime($user['created_at']))) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary edit-btn"
data-bs-toggle="modal" data-bs-target="#editUserModal"
data-id="<?= $user['id'] ?>"
data-username="<?= htmlspecialchars($user['username']) ?>"
data-role-id="<?= $user['role_id'] ?>"
data-bunk-id="<?= $user['bunk_id'] ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button type="button" class="btn btn-sm btn-outline-danger delete-btn"
data-bs-toggle="modal" data-bs-target="#deleteConfirmModal"
data-id="<?= $user['id'] ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addUserModalLabel">Add New User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST">
<div class="modal-body">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="role_id" class="form-label">Role</label>
<select class="form-select" id="role_id" name="role_id" required>
<option value="">Select Role</option>
<?php foreach ($roles as $role): ?>
<option value="<?= $role['id'] ?>"><?= htmlspecialchars($role['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="bunk_id" class="form-label">Assign to Bunk (Optional)</label>
<select class="form-select" id="bunk_id" name="bunk_id">
<option value="">Select Bunk</option>
<?php foreach ($bunks as $bunk): ?>
<option value="<?= $bunk['id'] ?>"><?= htmlspecialchars($bunk['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="add_user" class="btn btn-primary">Save User</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit User Modal -->
<div class="modal fade" id="editUserModal" tabindex="-1" aria-labelledby="editUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="user_id" id="edit_user_id">
<div class="modal-header">
<h5 class="modal-title" id="editUserModalLabel">Edit User</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_username" class="form-label">Username</label>
<input type="text" class="form-control" id="edit_username" name="username" required>
</div>
<div class="mb-3">
<label for="edit_password" class="form-label">New Password (optional)</label>
<input type="password" class="form-control" id="edit_password" name="password">
</div>
<div class="mb-3">
<label for="edit_role_id" class="form-label">Role</label>
<select class="form-select" id="edit_role_id" name="role_id" required>
<option value="">Select Role</option>
<?php foreach ($roles as $role): ?>
<option value="<?= $role['id'] ?>"><?= htmlspecialchars($role['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="edit_bunk_id" class="form-label">Assign to Bunk (Optional)</label>
<select class="form-select" id="edit_bunk_id" name="bunk_id">
<option value="">Select Bunk</option>
<?php foreach ($bunks as $bunk): ?>
<option value="<?= $bunk['id'] ?>"><?= htmlspecialchars($bunk['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="edit_user" 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 user? 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 toast-container">
<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 editUserModal = document.getElementById('editUserModal');
editUserModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
const username = button.getAttribute('data-username');
const roleId = button.getAttribute('data-role-id');
const bunkId = button.getAttribute('data-bunk-id');
const modalTitle = editUserModal.querySelector('.modal-title');
const modalBodyInputId = editUserModal.querySelector('#edit_user_id');
const modalBodyInputUsername = editUserModal.querySelector('#edit_username');
const modalBodyInputRoleId = editUserModal.querySelector('#edit_role_id');
const modalBodyInputBunkId = editUserModal.querySelector('#edit_bunk_id');
modalTitle.textContent = 'Edit User: ' + username;
modalBodyInputId.value = id;
modalBodyInputUsername.value = username;
modalBodyInputRoleId.value = roleId;
modalBodyInputBunkId.value = bunkId;
});
// 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 = 'users.php?action=delete&id=' + id;
});
});
</script>
</body>
</html>