-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposition.php
395 lines (357 loc) · 13.7 KB
/
position.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
<?php
session_start();
include 'database/db.php';
// Check if the user is not logged in or is not an administrator
if (!isset($_SESSION['username']) || $_SESSION['user_type_id'] != 1) {
// Redirect to the login page
header('Location: index.php');
// exit(); // Stop further execution
exit();
}
// Fetch user details of the current administrator from the database
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username = '$username' AND user_type_id = 1";
$result = mysqli_query($conn, $query);
// Check if the query was successful
if ($result) {
// Fetch the user data
$row = mysqli_fetch_assoc($result);
// Assign user details to variables
$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$username = $row['username'];
} else {
// Handle query error
echo "Error: " . mysqli_error($conn);
}
?>
<?php include 'includes/header.php'; ?>
<!-- DataTables -->
<link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
<link rel="stylesheet" href="plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/dataTables.jqueryui.min.css">
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
<?php
include 'includes/navbar.php';
include 'includes/sidebar.php';
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h3>Position</h3>
</div><!-- /.col -->
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12 text-sm-right">
<div class="mr-2 small"><b>Philippine Standard Time</b></div>
</div>
<div class="col-sm-12 text-sm-right">
<div id="philippine-date-time" class="small"></div>
</div>
</div>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="card">
<div class="card-header">
<marquee width="100%" direction="left"> <b>A t t e n d a n c e M o n i t o r i n g - Q R C o d e</b></marquee>
</div>
<div class="card-header m-0">
<button type="button" class="btn btn-info float-end btn-sm" data-toggle="modal" data-target="#addPositionModal">
<i class="fas fa-plus"></i> Add New
</button>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="positionTable" class="display table table-bordered" style="display: none;">
<thead>
<tr>
<th>No.</th>
<th>Position</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php include 'includes/footer.php';?>
</div>
<!-- ./wrapper -->
<!-- REQUIRED SCRIPTS -->
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<!-- DataTables & Plugins -->
<!-- Include SweetAlert JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="plugins/jszip/jszip.min.js"></script>
<script src="plugins/pdfmake/pdfmake.min.js"></script>
<script src="plugins/pdfmake/vfs_fonts.js"></script>
<script src="plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<script src="dist/js/datetime.js"></script>
<script src="dist/js/pro-pass-toggle.js"></script>
<!-- Include SweetAlert2 JS -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
$(document).ready(function() {
// Show the table after the page reloads and the DataTable is initialized
$('#positionTable').show();
// Destroy existing DataTable instance
if ($.fn.DataTable.isDataTable('#positionTable')) {
$('#positionTable').DataTable().destroy();
}
// Initialize DataTable with new settings
var table = $('#positionTable').DataTable({
"ajax": "crud_position.php",
"columns": [
{ "data": null, "render": function(data, type, row, meta) {
return meta.row + 1; // Add auto-increment number starting from 1
}
},
{ "data": "PositionName" },
{
"data": null,
"render": function(data, type, row) {
return '<div class="btn-group">' +
'<button type="button" class="btn btn-primary btn-sm edit-btn mr-1"><i class="fas fa-edit"></i></button>' +
'<button type="button" class="btn btn-danger btn-sm delete-btn"><i class="fas fa-trash-alt"></i></button>' +
'</div>';
}
}
],
"responsive": {
details: {
renderer: function(api, rowIdx, columns) {
var data = $.map(columns, function(col, i) {
return col.hidden ?
'<tr data-dt-row="' + col.rowIndex + '" data-dt-column="' + col.columnIndex + '">' +
'<td>' + col.title + ':' + '</td> ' +
'<td>' + col.data + '</td>' +
'</tr>' :
'';
}).join('');
return data ? $('<table/>').append(data) : false;
}
}
},
"lengthChange": false,
"autoWidth": false,
"dom": 'Bfrtip',
"buttons": [
{
extend: 'colvis',
text: '<i class="fas fa-eye"></i>',
className: 'btn-sm btn-light border',
}
]
});
$(document).tooltip();
// Submit form for adding new college student
$('#addPositionForm').on('submit', function(e) {
e.preventDefault(); // Prevent default form submission
// Serialize form data
var formData = $(this).serialize();
// Send AJAX request to add student
$.ajax({
url: 'crud_position.php', // PHP script to handle insertion
method: 'POST',
dataType: 'json',
data: formData,
success: function(response) {
if (response.status === 'success') {
// Reload DataTable upon successful addition
table.ajax.reload();
// Reset form fields
$('#addPositionForm')[0].reset();
// Close modal
$('#addPositionModal').modal('hide');
// Show success message
Swal.fire("Success", "New position added successfully!", "success");
} else {
// Show error message if addition failed
Swal.fire("Error", "Failed to add position", "error");
}
},
error: function(xhr, status, error) {
// Show error message if AJAX request fails
console.error(xhr.responseText);
Swal.fire("Error", "Failed to add position", "error");
}
});
});
// Edit button click event handler
$('#positionTable').on('click', '.edit-btn', function() {
var rowData = table.row($(this).parents('tr')).data();
$('#editPositionId').val(rowData['ID']);
$('#editPositionName').val(rowData['PositionName']);
$('#editPositionModal').modal('show');
});
// Submit form for editing Position
$('#editPositionForm').on('submit', function(e) {
e.preventDefault(); // Prevent default form submission
var formData = $(this).serialize(); // Serialize form data
// Send AJAX request to update student
$.ajax({
url: 'crud_position.php', // PHP script to handle update
method: 'POST',
dataType: 'json',
data: formData,
success: function(response) {
if (response.status === 'success') {
table.ajax.reload(); // Reload DataTable upon successful update
$('#editPositionModal').modal('hide'); // Close modal
Swal.fire("Success", "Position information updated successfully!", "success"); // Show success message
} else {
Swal.fire("Error", "Failed to update position information", "error"); // Show error message if update failed
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
Swal.fire("Error", "Failed to update position information", "error"); // Show error message if AJAX request fails
}
});
});
// Delete button click event handler
$('#positionTable').on('click', '.delete-btn', function() {
var rowData = table.row($(this).parents('tr')).data();
var positionId = rowData['ID'];
// Show SweetAlert confirmation dialog
Swal.fire({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this position!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#d9534f",
cancelButtonColor: "#3085d6",
confirmButtonText: "Yes, delete it!",
customClass: {
popup: 'small-swal' // Apply custom class for styling
},
}).then((result) => {
if (result.isConfirmed) {
// User confirmed deletion, proceed with AJAX request
$.ajax({
url: 'crud_position.php',
method: 'POST',
dataType: 'json',
data: { position_id: positionId },
success: function(response) {
if (response.status === 'success') {
// Reload table upon successful deletion
table.ajax.reload();
// Show success message using SweetAlert
Swal.fire("Success", response.message, "success");
} else {
// Show error message using SweetAlert
Swal.fire("Error", response.message, "error");
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
// Show error message using SweetAlert
Swal.fire("Error", "Failed to delete position", "error");
}
});
} else {
// User canceled deletion
Swal.fire({
title: "Cancelled",
text: "Position deletion cancelled.",
icon: "info",
customClass: {
popup: 'small-swal' // Apply custom class for styling
},
});
}
});
});
});
</script>
<!-- Add Position Modal -->
<div class="modal fade" id="addPositionModal" tabindex="-1" role="dialog" aria-labelledby="addPositionLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-info">
<h5 class="modal-title" id="addSeniorHighLabel">Add New Position</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="addPositionForm">
<div class="form-group">
<label for="position">Position Name</label>
<input type="text" class="form-control" id="position" name="position" required autocomplete="off">
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary btn-sm" form="addPositionForm"><i class="fas fa-paper-plane"></i> Submit</button>
</div>
</div>
</div>
</div>
<!-- Add Edit Position Modal -->
<div class="modal fade" id="editPositionModal" tabindex="-1" role="dialog" aria-labelledby="editPositionLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title" id="editPositionLabel">Edit Position</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="editPositionForm">
<input type="hidden" id="editPositionId" name="editPositionId">
<div class="form-group">
<label for="editPositionName">Position</label>
<input type="text" class="form-control" id="editPositionName" name="editPositionName" required>
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary btn-sm" form="editPositionForm"><i class="fas fa-save"></i> Save Changes</button>
</div>
</div>
</div>
</div>
</body>
</html>