-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairlines.php
426 lines (361 loc) · 21.4 KB
/
airlines.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
<?php require_once('includes/showMessage.php') ?>
<?php
session_start();
if(!isset($_SESSION['user_type'])) {
header('location: login.php');
}
require 'includes/functions.php';
displaySessionMessage();
?>
<!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" />
<title>Adding Airlines Company</title>
<link rel="stylesheet" href="css/style.css" />
<!-- Fontawesome CDN Link -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" />
</head>
<body>
<?php include('includes/admin-nav.php') ?>;
<main>
<div class="container mt-5" style="max-width: 1038px;
margin-left: 278px;">
<h2>Airlines</h2>
<!-- Add Airline Button -->
<div class="mb-3">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#addAirlineModal">Add
Airline</button>
</div>
<table class="table table-striped" style="margin-bottom:0px;">
<thead class="table-dark">
<tr>
<th style="width: 20%;">Email</th>
<th>Password</th>
<th>Airline Name</th>
<th>Logo</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
include("connection.php");
//airline add operation
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["add_airline"])) {
$newEmail = $_POST["new_email"];
$newPassword = $_POST["new_password"];
$newAirlineName = $_POST["new_airline_name"];
// Handle uploaded logo image
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["new_logo"]["name"]);
// Check if the directory exists, if not, create it
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}
if (move_uploaded_file($_FILES["new_logo"]["tmp_name"], $targetFile)) {
$newLogoPath = $targetFile;
// Check if the email already exists in the table
$emailCheckSql = "SELECT * FROM airline WHERE email = '$newEmail'";
$emailCheckResult = $con->query($emailCheckSql);
// Check if the airline name already exists in the table
$nameCheckSql = "SELECT * FROM airline WHERE airline_name = '$newAirlineName'";
$nameCheckResult = $con->query($nameCheckSql);
if ($emailCheckResult->num_rows > 0) {
// An airline with the same email already exists
setSessionMessage("Airline with this email is already registered");
} elseif ($nameCheckResult->num_rows > 0) {
// An airline with the same name already exists
setSessionMessage("The airline name is already taken");
} else {
// No duplicate records found, proceed with the INSERT query
$insertSql = "INSERT INTO airline VALUES ('$newEmail', '$newPassword', '$newAirlineName', '$newLogoPath')";
if ($con->query($insertSql) === TRUE) {
setSessionMessage("Airline added successfully");
} else {
setSessionMessage("An error occurred while adding the record");
}
}
header('location:airlines.php');
} else {
setSessionMessage("Error uploading logo");
header('location:airlines.php');
}
}
// Update Operation
if (isset($_POST["edit_airline"])) {
$editEmail = $_POST["edit_email"];
$updateSql = "UPDATE airline SET ";
$updateValues = array(); // An array to store the fields to update
// Check if a new password was provided
if (!empty($_POST["edit_password"])) {
$editPassword = $_POST["edit_password"];
$updateValues[] = "pass='$editPassword'";
}
// Check if a new airline name was provided
$editAirlineName = $_POST["edit_airline_name"];
if (!empty($_POST["edit_airline_name"])) {
$nameCheckSql = "SELECT * FROM airline WHERE airline_name = '$editAirlineName'";
$nameCheckResult = $con->query($nameCheckSql);
if ($nameCheckResult->num_rows > 0) {
// An airline with the same name already exists
setSessionMessage("The airline name is already taken");
header('location: airlines.php');
exit();
}
$updateValues[] = "airline_name='$editAirlineName'";
}
// Check if a new logo image was uploaded
if (!empty($_FILES["edit_logo"]["name"])) {
// Handle uploaded logo image
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["edit_logo"]["name"]);
// Check if the directory exists, if not, create it
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}
if (move_uploaded_file($_FILES["edit_logo"]["tmp_name"], $targetFile)) {
$editLogoPath = $targetFile;
$updateValues[] = "logo='$editLogoPath'";
} else {
setSessionMessage("Error updating logo");
exit;
}
}
// Combine the update values into the SQL query
$updateSql .= implode(", ", $updateValues);
$updateSql .= " WHERE email='$editEmail'";
// Check if any values were provided for update
if (empty($updateValues)) {
setSessionMessage("No values provided for update");
header('location: airlines.php');
exit();
}
if ($con->query($updateSql) === TRUE) {
setSessionMessage("Record updated successfully");
header('location: airlines.php');
} else {
echo "<script>showModal('errorModal', 'Error updating record: " . $con->error . "');</script>";
header('location: airlines.php');
}
}
if ($con->connect_error) {
die("connection failed: " . $con->connect_error);
}
// Delete Operation
if (isset($_GET["delete"])) {
$deleteEmail = $_GET["delete"];
// Delete the record with the specified email
$deleteSql = "DELETE FROM airline WHERE email = '$deleteEmail'";
if ($con->query($deleteSql) === TRUE) {
setSessionMessage("Airline deleted successfully");
} else {
echo 'Error deleting record: ' . $con->error;
}
header('location: airlines.php');
}
// Displaying airlines in the table
$sqlAirlines = "SELECT * FROM airline";
$resultAirlines = $con->query($sqlAirlines);
?>
<?php
while ($rowAirline = $resultAirlines->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $rowAirline["email"] . "</td>";
echo "<td>" . $rowAirline["pass"] . "</td>";
echo "<td>" . $rowAirline["airline_name"] . "</td>";
echo "<td><img src='" . $rowAirline["logo"] . "' alt='Airline Logo' height='50'></td>";
echo "<td>";
echo "<button class='btn btn-primary btn-sm edit-record' data-id='" . $rowAirline["email"] . "' data-toggle='modal' data-target='#editAirlineModal'>Edit</button>";
echo "<button class='btn btn-danger btn-sm delete-record' data-id='" . $rowAirline["email"] . "' data-toggle='modal' data-target='#deleteAirlineModal'>Delete</button>";
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
// Close the database connection
$con->close();
?>
<!-- Add Airline Modal -->
<div class="modal fade" id="addAirlineModal" tabindex="-1" role="dialog"
aria-labelledby="addAirlineModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addAirlineModalLabel">Add Airline</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="airlines.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="new_email">Email</label>
<input type="email" class="form-control" id="new_email" name="new_email"
required>
</div>
<div class="form-group">
<label for="new_password">Password</label>
<input type="password" class="form-control" id="new_password"
name="new_password" required>
</div>
<div class="form-group">
<label for="new_airline_name">Airline Name</label>
<input type="text" class="form-control" id="new_airline_name"
name="new_airline_name" required>
</div>
<div class="form-group">
<label for="new_logo">Logo Image</label>
<input type="file" class="form-control-file" id="new_logo" name="new_logo"
accept="image/*" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary"
name="add_airline">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Edit Airline Modal -->
<div class="modal fade" id="editAirlineModal" tabindex="-1" role="dialog"
aria-labelledby="editAirlineModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editAirlineModalLabel">Edit Airline</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="airlines.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="edit_email" id="edit_email_hidden">
<div class="form-group">
<label for="edit_password">Password</label>
<input type="password" class="form-control" id="edit_password"
name="edit_password">
</div>
<div class="form-group">
<label for="edit_airline_name">Airline Name</label>
<input type="text" class="form-control" id="edit_airline_name"
name="edit_airline_name">
</div>
<div class="form-group">
<label for="edit_logo">Logo Image</label>
<input type="file" class="form-control-file" id="edit_logo" name="edit_logo"
accept="image/*">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" name="edit_airline">Save
Changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Delete Airline Modal -->
<div class="modal fade" id="deleteAirlineModal" tabindex="-1" role="dialog"
aria-labelledby="deleteAirlineModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteAirlineModalLabel">Confirm Delete</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete this airline?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" id="confirmDelete">Delete</button>
</div>
</div>
</div>
</div>
<!-- //Success Modal
<div class="modal fade" id="successModal" tabindex="-1" role="dialog" aria-labelledby="successModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="successModalLabel">Success</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="successModalBody">
Record operation was successful.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
// Error Modal
<div class="modal fade" id="errorModal" tabindex="-1" role="dialog" aria-labelledby="errorModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="errorModalLabel">Error</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="errorModalBody">
An error occurred during the operation.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div> -->
<!-- Bootstrap and jQuery Scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- JavaScript to handle modals and delete operation -->
<script>
// function showSuccessModal(message) {
// $('#successModalBody').text(message);
// $('#successModal').modal('show');
// }
// function showErrorModal(message) {
// $('#errorModalBody').text(message);
// $('#errorModal').modal('show');
// }
// Function to set the email of the record to be edited in the edit modal
function setEditEmail(editEmail) {
$('#edit_email_hidden').val(editEmail);
}
// Click event handler for edit buttons
$(document).on("click", ".edit-record", function () {
var editEmail = $(this).data('id');
setEditEmail(editEmail);
});
// Function to set the ID of the record to be deleted in the modal
$(document).on("click", ".delete-record", function () {
var email = $(this).data('id');
$("#confirmDelete").data('id', email);
});
$(document).on("click", "#confirmDelete", function () {
var email = $(this).data('id');
window.location.href = "?delete=" + email;
});
</script>
</body>
</html>