-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQAM_Account.php
551 lines (512 loc) · 31.8 KB
/
QAM_Account.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
include("connection.php");
session_start(); // Start the session
if (!isset($_SESSION['login'])) {
header("Location: index.php");
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- fontawesome -->
<link rel="icon" type="image/png" href="./img/favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
<!-- main css -->
<link rel="stylesheet" type="text/css" href="./css/style.css">
<title>Quality Assurance Manager</title>
</head>
<body class="bg-gray">
<!--============ Header =========-->
<?php
include 'header.php';
?>
<!-- ================= Main ================= -->
<div class="container-fluid" style="margin-bottom: 100px;">
<div class="row justify-content-evenly">
<!-- ================= Sidebar ================= -->
<div class="col-12 col-lg-3">
<div class="d-none d-xxl-block h-100 fixed-top overflow-hidden scrollbar"
style="max-width: 360px; width: 100%; z-index: 4">
<ul class="navbar-nav mt-4 ms-3 d-flex flex-column pb-5 mb-5" style="padding-top: 56px">
<!-- top -->
<!-- avatar -->
<li class="dropdown-item p-1 rounded">
<a href="Settings.php" class="d-flex align-items-center text-decoration-none text-dark">
<div class="p-2">
<img src="https://source.unsplash.com/collection/happy-people" alt="avatar"
class="rounded-circle me-2"
style="width: 38px; height: 38px; object-fit: cover"/>
</div>
<div>
<p class="m-0"><?php echo $_SESSION['full_name']; ?></p>
</div>
</a>
</li>
<!-- top 1 -->
<li class="dropdown-item p-1 rounded">
<a href="QAM_Topics.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">Manage Topics</p>
</div>
</a>
</li>
<!-- top 2 -->
<li class="dropdown-item p-1 rounded">
<a href="QAM_Department.php"
class="d-flex align-items-center text-decoration-none text-dark" id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">Manage
Departments</p>
</div>
</a>
</li>
<!-- top 3 -->
<li class="dropdown-item p-1 rounded">
<a href="QAM_Account.php"
class="d-flex align-items-center text-decoration-none text-dark" id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon active" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;" id="manage-topics-text">Manage Account
</p>
</div>
</a>
</li>
<!-- top 4 -->
<li class="dropdown-item p-1 rounded">
<a href="QAM_Role.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px; ">Manage Role</p>
</div>
</a>
</li>
<hr class="m-0"/>
</ul>
</div>
</div>
<!-- ================= Timeline ================= -->
<div class="col-12">
<div class="d-flex flex-column justify-content-center mx-auto main-container">
<h1>Manage Accounts</h1>
<!-- Create Department -->
<div class="d-flex justify-content-end align-items-center my-4">
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createAccountModal">
<i class="fas fa-plus-circle"></i> Create New Account
</button>
</div>
<?php
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
include_once("connection.php");
// Retrieve the list of roles
$sql = "SELECT * FROM Role";
$role_result = mysqli_query($conn, $sql);
// Retrieve the list of departments
$sql = "SELECT * FROM Department";
$dept_result = mysqli_query($conn, $sql);
if (isset($_POST['add'])) {
$full_name = $_POST['fullName'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$role_id = $_POST['role'];
$dept_id = $_POST['department'];
$sql = "INSERT INTO Staff (FullName, Email, Password, RoleID, DepartmentID) VALUES ('$full_name','$email', '$password', '$role_id', '$dept_id')";
$result = mysqli_query($conn, $sql);
if ($result) {
// Send email to the new staff member about login details
$to = $email;
$subject = 'Login details for new staff member account';
$message = 'Dear ' . $full_name . ',<br><br>';
$message .= 'Your account has been created at https://greenwichideas.cleverapps.io/ Please use the following details to log in:<br><br>';
$message .= 'Email: ' . $email . '<br>';
$message .= 'Password: ' . $_POST['password'] . '<br><br>';
$message .= 'Thank you,<br>Greenwich Quality Assurance Manager';
// Set up the email headers
$mail = new PHPMailer\PHPMailer\PHPMailer();
try {
// Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'rwdibyoqadzmaxbw';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Recipients
$mail->setFrom('[email protected]', 'Greenwich QA');
$mail->addAddress($to, $full_name);
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
echo "<script>alert('Staff member added successfully')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
} catch (Exception $e) {
echo "<script>alert('Error sending email.')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
}
} else {
echo "<script>alert('Error adding staff member')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
}
}
?>
<!-- Create Account Modal -->
<div class="modal fade" id="createAccountModal" tabindex="-1"
aria-labelledby="createAccountModalLabel" aria-hidden="true" data-bs-backdrop="false">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="createAccountModalLabel">Create New Account</h5>
<button type="button" class="btn-close text-white" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="QAM_Account.php">
<div class="mb-3">
<label for="fullName" class="form-label text-primary">Full Name:</label>
<input type="text" class="form-control" id="fullName" name="fullName"
placeholder="Full Name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label text-primary">Email:</label>
<input type="email" class="form-control" id="email" name="email"
placeholder="Email" required>
</div>
<div class="mb-3">
<label for="password" class="form-label text-primary">Password:</label>
<div class="input-group">
<input type="password" class="form-control" id="password"
name="password"
placeholder="Password" required>
<button class="btn btn-outline-secondary" type="button"
id="togglePasswordBtn">
<i class="fas fa-eye"></i>
</button>
</div>
</div>
<div class="mb-3">
<label for="role" class="form-label text-primary">Role:</label>
<select class="form-select" id="role" name="role" required>
<option value="" disabled selected>Select Role</option>
<?php foreach ($role_result as $role) : ?>
<option
value="<?= $role['RoleID']; ?>"><?= $role['RoleName']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="department" class="form-label text-primary">Department:</label>
<select class="form-select" id="department" name="department" required>
<option value="" disabled selected>Select Department</option>
<?php foreach ($dept_result as $dept) : ?>
<option
value="<?= $dept['DepartmentID']; ?>"><?= $dept['DepartmentName']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary w-100" name="add">Create
Account
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Script to toggle password visibility -->
<script>
const togglePasswordBtn = document.getElementById('togglePasswordBtn');
const passwordInput = document.getElementById('password');
togglePasswordBtn.addEventListener('click', function () {
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
passwordInput.setAttribute('type', type);
this.querySelector('i').classList.toggle('fa-eye-slash');
});
</script>
<?php
include_once("connection.php");
$sql = "SELECT Staff.*, Role.RoleName, Department.DepartmentName FROM Staff JOIN Role ON Staff.RoleID = Role.RoleID JOIN Department ON Staff.DepartmentID = Department.DepartmentID";
$result = mysqli_query($conn, $sql);
?>
<!-- Account Table -->
<div class="bg-light p-3 table-responsive">
<?php if (mysqli_num_rows($result) > 0) { ?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Role</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
<tbody>
<tr>
<td><?= $row['FullName']; ?></td>
<td><?= $row['Email']; ?></td>
<?php if ($row['RoleID'] == '1') { ?>
<td><span
class="badge bg-danger rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } else if ($row['RoleID'] == '2') { ?>
<td><span
class="badge bg-warning rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } else if ($row['RoleID'] == '3') { ?>
<td><span
class="badge bg-success rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } ?>
<td><?= $row['DepartmentName']; ?></td>
<td>
<button class="btn btn-outline-primary me-2" id="editStaff"
data-id="<?php echo $row['StaffID']; ?>"
data-fullname="<?php echo $row['FullName']; ?>"
data-email="<?php echo $row['Email']; ?>"
data-role="<?php echo $row['RoleID']; ?>"
data-department="<?php echo $row['DepartmentID']; ?>"
data-toggle="modal" data-target="#editAccountModal">
<i class="fas fa-edit"></i> Edit
</button>
<button class="btn btn-outline-danger" data-bs-toggle="modal"
id="deleteStaff"
data-bs-target="#deleteAccountModal"
data-id="<?php echo $row['StaffID']; ?>"
><i class="fas fa-trash"></i>
Delete
</button>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<?php } else { ?>
<p>No staff found.</p>
<?php } ?>
</div>
<?php
include("connection.php");
if (isset($_POST["edit"])) {
$staff_id = $_POST["staffID"];
$full_name = $_POST["fullName"];
$email = $_POST["email"];
if ($_POST["password"] == "") {
$sql = "SELECT Password FROM Staff WHERE StaffID = '$staff_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$password = $row["Password"];
} else {
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
}
$role_id = $_POST["role"];
$department_id = $_POST["department"];
$sql = "UPDATE Staff SET FullName = '$full_name', Email = '$email', Password = '$password', RoleID = '$role_id', DepartmentID = '$department_id' WHERE StaffID = '$staff_id'";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<script>alert('Account updated successfully')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
} else {
echo "<script>alert('Failed to update account')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
}
}
// Delete Account
if (isset($_POST["delete"])) {
$staff_id = $_POST["staffID"];
$sql = "DELETE FROM Staff WHERE StaffID = '$staff_id'";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<script>alert('Account deleted successfully')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
} else {
echo "<script>alert('Failed to delete account')</script>";
echo "<script>window.location.href = 'QAM_Account.php'</script>";
}
}
?>
<!-- Edit Account Modal -->
<div class="modal fade" id="editAccountModal" tabindex="-1" aria-labelledby="editAccountModalLabel"
aria-hidden="true" data-bs-backdrop="false">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="editAccountModalLabel">Edit Account</h5>
<button type="button" class="btn-close text-white" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="QAM_Account.php">
<input type="hidden" name="staffID" id="edit_staff_id">
<!-- Hidden input to get staff ID -->
<div class="mb-3">
<label for="fullName" class="form-label text-primary">Full Name:</label>
<input type="text" class="form-control" id="edit_fullname" name="fullName"
placeholder="Full Name"
required>
</div>
<div class="mb-3">
<label for="email" class="form-label text-primary">Email:</label>
<input type="email" class="form-control" id="edit_email" name="email"
required
placeholder="Email">
</div>
<div class="mb-3">
<label for="password" class="form-label text-primary">New Password:</label>
<div class="input-group">
<input type="password" class="form-control" id="passwordEdit"
name="password" placeholder="New password">
<button class="btn btn-outline-secondary" type="button"
id="togglePasswordBtnEdit">
<i class="fas fa-eye"></i>
</button>
</div>
</div>
<div class="mb-3">
<label for="role" class="form-label text-primary">Role:</label>
<select class="form-select" id="edit_role" name="role" required>
<option value="" disabled selected>Select Role</option>
<?php foreach ($role_result as $role) : ?>
<option
value="<?= $role['RoleID']; ?>"><?= $role['RoleName']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="department" class="form-label text-primary">Department:</label>
<select class="form-select" id="edit_department" name="department" required>
<option value="" disabled selected>Select Department</option>
<?php foreach ($dept_result as $dept) : ?>
<option
value="<?= $dept['DepartmentID']; ?>"><?= $dept['DepartmentName']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary w-100" name="edit">Update
Account
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
const togglePasswordBtnEdit = document.getElementById('togglePasswordBtnEdit');
const passwordInputEdit = document.getElementById('passwordEdit');
togglePasswordBtnEdit.addEventListener('click', function () {
const type = passwordInputEdit.getAttribute('type') === 'password' ? 'text' : 'password';
passwordInputEdit.setAttribute('type', type);
this.querySelector('i').classList.toggle('fa-eye-slash');
});
</script>
<!-- Delete Account Modal -->
<div class="modal fade" id="deleteAccountModal" tabindex="-1"
aria-labelledby="deleteAccountModalLabel" aria-hidden="true" data-bs-backdrop="false">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title" id="deleteAccountModalLabel">Delete Account</h5>
<button type="button" class="btn-close text-white" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this account?</p>
</div>
<div class="modal-footer">
<form method="post" action="QAM_Account.php">
<input type="hidden" name="staffID" id="delete_staff_id">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">Cancel
</button>
<button type="submit" class="btn btn-danger" name="delete">Delete</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!--============ Footer =========-->
<?php
include 'footer.php';
?>
<!-- bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
<!-- main js -->
<script src="./js/main.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
//NOTE: passing value to update house modal
var editStaff = document.querySelectorAll('#editStaff');
var deleteStaff = document.querySelectorAll('#deleteStaff');
editStaff.forEach(function (e) {
e.addEventListener('click', function () {
// Get the values
var StaffID = e.getAttribute('data-id');
var StaffName = e.getAttribute('data-fullname');
var StaffEmail = e.getAttribute('data-email');
var StaffRole = e.getAttribute('data-role');
var StaffDepartment = e.getAttribute('data-department');
// Set the values
document.getElementById('edit_staff_id').value = StaffID;
document.getElementById('edit_fullname').value = StaffName;
document.getElementById('edit_email').value = StaffEmail;
document.getElementById('edit_role').value = StaffRole;
document.getElementById('edit_department').value = StaffDepartment;
// Show the modal
var editAccountModal = new bootstrap.Modal(document.getElementById('editAccountModal'));
editAccountModal.show();
});
});
deleteStaff.forEach(function (e) {
e.addEventListener('click', function () {
// Get the values
var StaffID = e.getAttribute('data-id');
// Set the values
document.getElementById('delete_staff_id').value = StaffID;
// Show the modal
var deleteAccountModal = new bootstrap.Modal(document.getElementById('deleteAccountModal'));
deleteAccountModal.show();
});
});
});
</script>
</body>
</html>
<?php
}
?>