35867-vm/users.php
Flatlogic Bot 6d48e88ec8 V1.0
2025-11-20 11:10:15 +00:00

119 lines
5.0 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
require_once 'db/config.php';
$success_message = $_SESSION['success_message'] ?? null;
unset($_SESSION['success_message']);
$error_message = $_SESSION['error_message'] ?? null;
unset($_SESSION['error_message']);
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, role, email, status FROM users");
$users = $stmt->fetchAll();
} catch (PDOException $e) {
if (strpos($e->getMessage(), "Unknown column 'status'") !== false) {
try {
$sql = file_get_contents('db/migrations/004_add_status_to_users.sql');
$pdo->exec($sql);
header("Location: users.php"); // Refresh the page after migration
exit;
} catch (Exception $me) {
die("Could not apply migration and connect to the database: " . $me->getMessage());
}
} else {
die("Could not connect to the database: " . $e->getMessage());
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Management - Admin Dashboard</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&family=Open+Sans&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body class="admin-body">
<div class="admin-container">
<aside class="sidebar">
<div class="sidebar-header">
<h2>School Admin</h2>
</div>
<nav class="sidebar-nav">
<a href="admin.php">Dashboard</a>
<a href="users.php" class="active">User Management</a>
<a href="#">School Settings</a>
<a href="#">Subjects & Classes</a>
<a href="#">Student Promotions</a>
<a href="#">Reports</a>
<a href="logout.php" class="logout">Logout</a>
</nav>
</aside>
<main class="main-content">
<header class="main-header">
<h1>User Management</h1>
<a href="add_user.php" class="btn btn-primary">Add New User</a>
</header>
<?php if ($success_message): ?>
<div class="alert alert-success"><?= htmlspecialchars($success_message) ?></div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
<?php endif; ?>
<div class="content-grid">
<div class="card full-width-card">
<div class="card-header">
<h3>All Users</h3>
</div>
<div class="card-body">
<table class="user-table">
<thead>
<tr>
<th>Name</th>
<th>Role</th>
<th>Email</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['name']) ?></td>
<td><?= htmlspecialchars($user['role']) ?></td>
<td><?= htmlspecialchars($user['email']) ?></td>
<td><?= htmlspecialchars(ucfirst($user['status'])) ?></td>
<td>
<a href="edit_user.php?id=<?= $user['id'] ?>" class="action-link">Edit</a>
<a href="delete_user.php?id=<?= $user['id'] ?>" class="action-link" onclick="return confirm('Are you sure you want to delete this user?')">Delete</a>
<?php if ($user['status'] === 'active'): ?>
<a href="update_user_status.php?id=<?= $user['id'] ?>&status=inactive" class="action-link" onclick="return confirm('Are you sure you want to deactivate this user?')">Deactivate</a>
<?php else: ?>
<a href="update_user_status.php?id=<?= $user['id'] ?>&status=active" class="action-link" onclick="return confirm('Are you sure you want to activate this user?')">Activate</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</main>
</div>
</body>
</html>