-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_management.php
More file actions
420 lines (383 loc) · 16.5 KB
/
Copy pathuser_management.php
File metadata and controls
420 lines (383 loc) · 16.5 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
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
<?php
/**
* User Management Route
* Manage employee accounts, passwords, and status
*/
require_once 'bootstrap.php';
use App\Models\Employee;
use App\Core\Auth;
// Require admin authentication
if (!Auth::check() || !Auth::isAdmin()) {
header('Location: login.php');
exit;
}
$employeeModel = new Employee();
// Handle actions
$message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
if ($_POST['action'] == 'change_status') {
$employee_no = $_POST['employee_no'];
$new_status = $_POST['new_status'];
if (!in_array($new_status, ['active', 'terminated'])) {
$message = "<div class='alert alert-danger'>
<strong>Error!</strong> Invalid status value.
</div>";
} else {
$employeeModel->update($employee_no, ['employment_status' => $new_status]);
$status_label = ucfirst($new_status);
$message = "<div class='alert alert-success'>
<strong>Success!</strong> Employee #$employee_no status changed to $status_label.
</div>";
}
} elseif ($_POST['action'] == 'reset_password') {
$employee_no = $_POST['employee_no'];
$new_password = trim($_POST['new_password']);
if (empty($new_password)) {
$message = "<div class='alert alert-danger'>
<strong>Error!</strong> Password cannot be empty.
</div>";
} else {
// Hash the password using the same method as employee creation
$hashed_password = md5(sha1($new_password));
$employeeModel->update($employee_no, ['password' => $hashed_password]);
$message = "<div class='alert alert-success'>
<strong>Success!</strong> Password updated for Employee #$employee_no.
</div>";
}
}
}
// Get filter parameter
$status_filter = isset($_GET['status']) ? $_GET['status'] : 'active';
// Get employees based on filter
if ($status_filter == 'active') {
$employees = $employeeModel->where(['employment_status' => 'active'], 'employee_no ASC');
} elseif ($status_filter == 'terminated') {
$employees = $employeeModel->where(['employment_status' => 'terminated'], 'employee_no ASC');
} else {
// All employees
$employees = $employeeModel->all('employee_no ASC');
}
// Get counts - use all() and count the results since we don't have countWhere
$all_employees = $employeeModel->all();
$count_all = count($all_employees);
$count_active = count(array_filter($all_employees, function($emp) { return $emp['employment_status'] == 'active'; }));
$count_terminated = count(array_filter($all_employees, function($emp) { return $emp['employment_status'] == 'terminated'; }));
?>
<!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">
<title>User Management - FreshServe Catering Payroll</title>
<link href="dist/css/bootstrap.min.css" rel="stylesheet">
<link href="starter-template.css" rel="stylesheet">
<link href="theme.php" rel="stylesheet" type="text/css">
<style>
.user-card {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 20px;
margin-bottom: 20px;
}
.employee-row {
border-bottom: 1px solid #e9ecef;
padding: 15px 0;
}
.employee-row:last-child {
border-bottom: none;
}
.employee-info {
display: flex;
align-items: center;
justify-content: space-between;
}
.employee-details {
flex: 1;
}
.employee-actions {
text-align: right;
}
.employee-no-badge {
display: inline-block;
background-color: #007bff;
color: white;
padding: 5px 12px;
border-radius: 4px;
font-weight: bold;
margin-right: 10px;
}
.info-section {
background-color: #f8f9fa;
border-left: 4px solid #007bff;
padding: 15px;
margin-bottom: 20px;
}
.status-badge-active {
background-color: #28a745;
color: white;
padding: 5px 12px;
border-radius: 4px;
font-weight: bold;
font-size: 12px;
}
.status-badge-terminated {
background-color: #dc3545;
color: white;
padding: 5px 12px;
border-radius: 4px;
font-weight: bold;
font-size: 12px;
}
.filter-tabs {
margin-bottom: 20px;
}
.filter-tabs .btn-group {
width: 100%;
}
.employee-row.terminated {
background-color: #f8f9fa;
opacity: 0.7;
}
</style>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include('navbar.php'); ?>
<div class="container">
<div class="starter-template">
<h2>
<span class="glyphicon glyphicon-user"></span> Employee User Management
</h2>
<p class="text-muted">Manage employee login credentials by Employee ID</p>
<?php if($message) echo $message; ?>
<div class="info-section">
<h4 style="margin-top: 0;">
<span class="glyphicon glyphicon-info-sign"></span> About Employee Logins
</h4>
<ul style="margin-bottom: 0;">
<li><strong>Username:</strong> Employee ID number</li>
<li><strong>Default Password:</strong> Employee's last name (case-sensitive)</li>
<li><strong>Login Page:</strong> Employees use <code>login.php</code> or the employee login form</li>
<li><strong>Password Security:</strong> Passwords are hashed using MD5(SHA1) encryption</li>
<li><strong>Password Reset:</strong> Admins can reset passwords to any value or back to the default (lastname)</li>
</ul>
</div>
<!-- Status Filter -->
<div class="filter-tabs">
<div class="btn-group btn-group-justified" role="group">
<a href="?status=active" class="btn btn-<?= $status_filter == 'active' ? 'primary' : 'default' ?>">
<span class="glyphicon glyphicon-user"></span> Active (<?= $count_active ?>)
</a>
<a href="?status=terminated" class="btn btn-<?= $status_filter == 'terminated' ? 'danger' : 'default' ?>">
<span class="glyphicon glyphicon-ban-circle"></span> Terminated (<?= $count_terminated ?>)
</a>
<a href="?status=all" class="btn btn-<?= $status_filter == 'all' ? 'info' : 'default' ?>">
<span class="glyphicon glyphicon-list"></span> All Employees (<?= $count_all ?>)
</a>
</div>
</div>
<div class="user-card">
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-6">
<h4 style="margin-top: 0;">
Employee Accounts
<small class="text-muted">(Showing: <?= ucfirst($status_filter) ?>)</small>
</h4>
</div>
<div class="col-md-6 text-right">
<a href="employee_setup.php" class="btn btn-primary">
<span class="glyphicon glyphicon-plus"></span> Add New Employee
</a>
</div>
</div>
<?php if (empty($employees)): ?>
<div class="alert alert-info">
No employees found. <a href="employee_setup.php">Add your first employee</a>.
</div>
<?php else: ?>
<?php foreach ($employees as $employee):
$is_active = ($employee['employment_status'] == 'active');
$row_class = $is_active ? '' : 'terminated';
?>
<div class="employee-row <?= $row_class ?>">
<div class="employee-info">
<div class="employee-details">
<div>
<span class="employee-no-badge">#<?= htmlspecialchars($employee['employee_no']) ?></span>
<strong style="font-size: 16px;">
<?= htmlspecialchars($employee['title'] . ' ' . $employee['surname'] . ' ' . $employee['lastname']) ?>
</strong>
<span class="status-badge-<?= $employee['employment_status'] ?>" style="margin-left: 10px;">
<?= $is_active ? '<span class="glyphicon glyphicon-ok-circle"></span> ACTIVE' : '<span class="glyphicon glyphicon-ban-circle"></span> TERMINATED' ?>
</span>
</div>
<div style="margin-top: 5px; color: #666;">
<span class="glyphicon glyphicon-briefcase"></span>
<?= htmlspecialchars($employee['position']) ?> - <?= htmlspecialchars($employee['dept']) ?>
<span style="margin-left: 15px;">
<span class="glyphicon glyphicon-phone"></span>
<?= htmlspecialchars($employee['phoneno']) ?>
</span>
<span style="margin-left: 15px;">
<span class="glyphicon glyphicon-calendar"></span>
Joined: <?= date('m/d/Y', strtotime($employee['joined'])) ?>
</span>
</div>
<div style="margin-top: 8px;">
<span class="label label-info">
<span class="glyphicon glyphicon-lock"></span>
Login: Employee ID #<?= htmlspecialchars($employee['employee_no']) ?>
</span>
<span class="label label-default">
Default Password: <?= htmlspecialchars($employee['lastname']) ?>
</span>
</div>
</div>
<div class="employee-actions">
<?php if ($is_active): ?>
<button type="button" class="btn btn-danger btn-sm"
onclick="changeStatus('<?= $employee['employee_no'] ?>', '<?= htmlspecialchars($employee['surname'] . ' ' . $employee['lastname']) ?>', 'terminated')">
<span class="glyphicon glyphicon-ban-circle"></span> Set as Terminated
</button>
<?php else: ?>
<button type="button" class="btn btn-success btn-sm"
onclick="changeStatus('<?= $employee['employee_no'] ?>', '<?= htmlspecialchars($employee['surname'] . ' ' . $employee['lastname']) ?>', 'active')">
<span class="glyphicon glyphicon-ok-circle"></span> Set as Active
</button>
<?php endif; ?>
<button type="button" class="btn btn-warning btn-sm"
onclick="showResetModal('<?= htmlspecialchars($employee['employee_no']) ?>', '<?= htmlspecialchars($employee['surname'] . ' ' . $employee['lastname']) ?>', '<?= htmlspecialchars($employee['lastname']) ?>')">
<span class="glyphicon glyphicon-refresh"></span> Reset Password
</button>
<a href="edit_employee.php?employee_no=<?= $employee['employee_no'] ?>"
class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit"></span> Edit
</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
<!-- Password Reset Modal -->
<div class="modal fade" id="resetPasswordModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="POST" action="user_management.php" id="resetPasswordForm">
<input type="hidden" name="action" id="reset_action" value="reset_password">
<input type="hidden" name="employee_no" id="reset_employee_no">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span class="glyphicon glyphicon-lock"></span> Reset Password
</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Employee:</strong> <span id="modal_employee_name"></span><br>
<strong>Employee ID:</strong> <span id="modal_employee_no"></span>
</div>
<div class="form-group">
<label>New Password <span class="text-danger">*</span></label>
<input type="text" name="new_password" id="new_password" class="form-control"
placeholder="Enter new password" required>
<small class="form-text text-muted">
Employee will use this password to log in (case-sensitive)
</small>
</div>
<div class="alert alert-warning">
<strong>Quick Actions:</strong>
<div style="margin-top: 10px;">
<button type="button" class="btn btn-sm btn-default" onclick="setPasswordToLastname()">
Set to Last Name
</button>
<button type="button" class="btn btn-sm btn-default" onclick="generatePassword()">
Generate Random Password
</button>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-warning">
<span class="glyphicon glyphicon-refresh"></span> Reset Password
</button>
</div>
</form>
</div>
</div>
</div>
<script src="dist/js/jquery.min.js"></script>
<script src="dist/js/bootstrap.min.js"></script>
<script>
var currentLastname = '';
function showResetModal(employeeNo, employeeName, lastname) {
currentLastname = lastname;
$('#reset_employee_no').val(employeeNo);
$('#modal_employee_no').text(employeeNo);
$('#modal_employee_name').text(employeeName);
$('#new_password').val('');
$('#resetPasswordModal').modal('show');
}
function setPasswordToLastname() {
$('#new_password').val(currentLastname);
}
function generatePassword() {
var length = 8;
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var password = "";
for (var i = 0; i < length; i++) {
password += charset.charAt(Math.floor(Math.random() * charset.length));
}
$('#new_password').val(password);
}
function changeStatus(employeeNo, employeeName, newStatus) {
var statusLabel = newStatus.charAt(0).toUpperCase() + newStatus.slice(1);
var message = 'Are you sure you want to set ' + employeeName + ' (Employee #' + employeeNo + ') as ' + statusLabel + '?';
if (newStatus === 'terminated') {
message += '\n\nThis will:\n- Mark the employee as terminated\n- The employee can still log in unless you reset their password';
}
if (confirm(message)) {
// Create a form and submit it
var form = $('<form>', {
'method': 'POST',
'action': 'user_management.php'
});
form.append($('<input>', {
'type': 'hidden',
'name': 'action',
'value': 'change_status'
}));
form.append($('<input>', {
'type': 'hidden',
'name': 'employee_no',
'value': employeeNo
}));
form.append($('<input>', {
'type': 'hidden',
'name': 'new_status',
'value': newStatus
}));
$('body').append(form);
form.submit();
}
}
$('#resetPasswordForm').submit(function(e) {
var password = $('#new_password').val().trim();
if (password === '') {
e.preventDefault();
alert('Password cannot be empty');
return false;
}
return confirm('Are you sure you want to reset the password for Employee #' + $('#reset_employee_no').val() + '?');
});
</script>
</body>
</html>