-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminUsers.php
More file actions
134 lines (118 loc) · 5.25 KB
/
adminUsers.php
File metadata and controls
134 lines (118 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
session_start();
require_once('config/config.php');
require_once('includes/nav.php');
require_once('util/dbUtil.php');
require_once('util/userUtil.php');
if(!isset($_SESSION['UserType']) || $_SESSION['UserType'] !== UserType::admin->value){
$_SESSION['UserType'] = UserType::visitor->value;
header("Location: index.php");
exit();
}
$pageTitle = $APP_PROPERTIES['app_name'].' - Admin';
$users = getAllUsersFromDb();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once('includes/head-includes.php') ?>
<title><?php echo $pageTitle;?></title>
</head>
<body class="d-flex flex-column min-vh-100">
<?php
$navbar = new Navbar(UserType::getEnumFromValue($_SESSION['UserType']));
echo $navbar->getNavBar();
?>
<main class="container flex-grow-1 mt-3 mt-md-5 pt-md-4">
<div class="d-flex flex-column flex-sm-row justify-content-between align-items-sm-center mb-3">
<h1>Admin: Users</h1>
<span class="text-muted">Count: <?php $count = count($users); echo "$count ".($count === 1 ? 'user' : 'users'); ?></span>
</div>
<!-- RESPONSIVE TABLE -->
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead class="table-dark">
<tr>
<th class="d-none d-md-table-cell">ID</th>
<th>User</th>
<th class="d-none d-md-table-cell">Email</th>
<th class="d-none d-md-table-cell text-center">Active</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user)
{
echo '
<tr>
<td class="d-none d-md-table-cell fw-semibold">'.$user->getId().'</td>
<td>
<div class="fw-semibold text-break">'.$user->getUsername().'</div>
<div class="d-md-none small text-muted">ID: '.$user->getId().'</div>
<small class="text-muted text-break">'.strtoupper($user->getLastname()).' '.$user->getFirstname().'</small>
<div class="d-md-none small text-muted">'.$user->getEmail().'</div>';
if($user->getActive())
echo '
<div class="d-md-none small text-success">active</div>';
else
echo '
<div class="d-md-none small text-danger">not active</div>';
echo '
</td>
<td class="d-none d-md-table-cell">'.$user->getEmail().'</td>
<td class="d-none d-md-table-cell text-center">
<input type="checkbox" class="form-check-input pe-none" '.($user->getActive() == 1 ? ' checked' : '').' disabled>
</td>
<td class="text-center">
<div class="d-flex justify-content-center flex-nowrap gap-1">
<a href="adminUsersManageUser.php?userId='.$user->getId().'"
class="btn btn-warning btn-sm" title="Manage">
<i class="bi bi-pencil"></i>
</a>
<a href="adminUsersDeleteUser.php?userId='.$user->getId().'"
class="btn btn-danger btn-sm" title="Delete">
<i class="bi bi-trash"></i>
</a>
</div>
</td>
</tr>
';
}
?>
</tbody>
</table>
</div>
</main>
<?php require_once('./includes/footer.php'); ?>
</body>
</html>
<?php
/**
* Fetches all users (except admins) from the database as User objects.
*
* @return User[] Array of User instances
*/
function getAllUsersFromDb(): array
{
$dbObj = getDbObj();
$sql = "SELECT `id` FROM `users`
WHERE `role_id` != 1
ORDER BY `active` DESC, `username`, `lastname`, `firstname`, `id`";
$stmt = $dbObj->prepare($sql);
if (!$stmt) {
return []; // Query failed
}
$stmt->execute();
$result = $stmt->get_result();
if (!$result) {
return [];
}
$users = [];
while ($row = $result->fetch_assoc()) {
$user = new User($row['id']);
$users[] = $user;
}
$stmt->close();
return $users;
}