-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin_DB.php
411 lines (358 loc) · 13.8 KB
/
admin_DB.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
<?php
//Marasingha M A M N IT23539990
session_start();
//check if the user is logged in
if(isset($_SESSION['username']))
{
//check if the user is admin
if($_SESSION['user_type']=='Admin')
{
require_once './db_Config/config.php';
}
else if($_SESSION['user_type']=='Manager')
{
header('location: ./manager_DB.php');
}
else
{
header('location: ./index.php');
}
}
else
{
header('location: ./index.php');
}
//check user status functionality
$UserStatus = '-----';
$inputValue = '';
if(isset($_POST['Check']))
{
$username = $_POST['UserInput'];
$inputValue = $username;
//check username is empty or not
if(empty($username))
{
$UserStatus = 'Empty';
}
else
{
$sql = "SELECT acc_status FROM User_info WHERE user_name='$username' ";
$result = mysqli_query($Connection, $sql);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$UserStatus = $row['acc_status'];
}
else
{
$UserStatus = 'Invalid UN';
}
}
}
//user activate, deactivate and delete functionality
if(isset($_POST['activate']))
{
$username = $_POST['UserInput'];
if(empty($username))
{
$UserStatus = 'Empty';
}
else
{
$sql = "UPDATE User_info SET acc_status='Active' WHERE user_name='$username' AND user_type='Customer' ";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
$UserStatus = 'Activated';
}
else
{
$UserStatus = 'Failed';
}
}
}
if(isset($_POST['deactivate']))
{
$username = $_POST['UserInput'];
if(empty($username))
{
$UserStatus = 'Empty';
}
else
{
$sql = "UPDATE User_info SET acc_status='Inactive' WHERE user_name='$username' AND user_type='Customer' ";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
$UserStatus = 'Deactivated';
}
else
{
$UserStatus = 'Failed';
}
}
}
if(isset($_POST['delete']))
{
$username = $_POST['UserInput'];
if(empty($username))
{
$UserStatus = 'Empty';
}
else
{
$sql = "DELETE FROM User_info WHERE user_name='$username' AND user_type='Customer'";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
$UserStatus = 'Deleted';
}
else
{
$UserStatus = 'Failed';
}
}
}
//User Analytics
//count all users and active users and deactivated users
$sql = "SELECT
COUNT(user_name) AS TotalUsers,
COUNT(CASE WHEN acc_status = 'Active' THEN 1 END) AS ActiveUsers,
COUNT(CASE WHEN acc_status = 'Inactive' THEN 1 END) AS DeactivatedUsers
FROM
User_info
WHERE
user_type = 'Customer';
";
$result = mysqli_query($Connection, $sql);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$TotalUsers = $row['TotalUsers'];
$ActiveUsers = $row['ActiveUsers'];
$DeactivatedUsers = $row['DeactivatedUsers'];
}
else
{
$TotalUsers = 0;
$ActiveUsers = 0;
$DeactivatedUsers = 0;
}
//Order Requests section mark as shipped functionality
if(isset($_POST['Shipped']))
{
$orderID = $_POST['order_id'];
$sql = "UPDATE Orders SET order_status= 'Shipped' WHERE order_id='$orderID' ";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
header("Location: ".$_SERVER['PHP_SELF']);
exit();
}
}
//Message Reply and Delete functionality
//submit reply
if(isset($_POST['submit_rply']))
{
$messageID = $_POST['message_id'];
$reply = $_POST['Reply_in'];
$sql = "UPDATE Messages SET response_text='$reply' WHERE message_id='$messageID' ";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
header("Location: ".$_SERVER['PHP_SELF']);
}
}
//delete message
if(isset($_POST['Delete_msg']))
{
$messageID = $_POST['message_id'];
$sql = "DELETE FROM Messages WHERE message_id='$messageID' ";
$result = mysqli_query($Connection, $sql);
if($result && mysqli_affected_rows($Connection) > 0)
{
header("Location: ".$_SERVER['PHP_SELF']);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" sizes="32x32" href="./Images/Pharmacy X Icon.png">
<link rel="icon" type="image/png" sizes="16x16" href="./Images/Pharmacy X Icon.png">
<link rel="stylesheet" href="./CSS/admin_DB.css">
<script src="./JS/admin_DB.js"></script>
</head>
<body>
<?php include ("./header.php"); ?>
<div class="Container1">
<div class="user_box">
<h2 class="user-heading">Manage Users</h2>
<div class="user_content">
<form action="admin_DB.php" method="POST">
<div class="check-users">
<input type="text" class="check-user-input" placeholder="Username" name="UserInput" value="<?php echo $inputValue; ?>" >
<button type="submit" class="check-user-button" name="Check">Check</button>
</div>
<div class="user-satus-container">
<h3 class="status-heading">Status : </h3>
<div class="user-status"><?php echo $UserStatus; ?></div>
</div>
<div class="user-control-buttons">
<button class="action_btn bg" name="activate" >Activate</button>
<button class="action_btn bg" name="deactivate" >Deactivate</button>
<button class="delete" name="delete" >Delete</button>
</div>
</form>
</div>
</div>
<div class="stat_box">
<h2 class="user-heading">User Statistics Overview</h2>
<div class="stat_content">
<h3>Total Registered Users: <span class="stat_value"><?php echo $TotalUsers; ?></span></h3>
<h3>Active Users: <span class="stat_value"><?php echo $ActiveUsers; ?></span></h3>
<h3>Deactivated Users: <span class="stat_value"><?php echo $DeactivatedUsers; ?></span></h3>
</div>
</div>
</div>
<div class="Container2">
<h2>Order Requests</h2>
<div class="order_content">
<table>
<?php
$sql = "SELECT o.*,
CASE
WHEN p.payment_id IS NOT NULL THEN 'Paid'
ELSE 'Not Paid'
END AS payment_status
FROM Orders o
LEFT JOIN Payment p ON o.order_id = p.order_id
WHERE o.order_status IN ('Pending', 'Shipped');";
$result = mysqli_query($Connection, $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = $result->fetch_assoc())
{
$ID = $row['order_id'];
$customer = $row['user_name'];
$date = $row['order_date'];
$status = $row['order_status'];
$total = $row['Order_total'];
$orderStat = $row['order_status'];
$orderType = $row['order_type'];
$paymentStatus = $row['payment_status'];
$PrescriptionLink = "./Images/PrescriptionOrders/{$row['prescription_url']}";
if($orderType == 'Prescription')
{
$orderType = "<a href='$PrescriptionLink' target='_blank'>Prescription</a>";
}
if($orderStat=='Shipped')
{
echo "<tr>
<td><span class='ordertb_title'>ID:</span> $ID </td>
<td><span class='ordertb_title'>Customer:</span> $customer</td>
<td><span class='ordertb_title'>Date:</span> $date</td>
<td><span class='ordertb_title'>Status:</span> <span class='order_stat_row'>$status</span> </td>
<td><span class='ordertb_title'>Total:</span> $total </td>
<td><span class='order_stat_row'>$paymentStatus</span></td>
<td>$orderType</td>
<td class='disable_box'>
<input type='submit' value='Mark as Shipped' disabled>
</td>
</tr>";
}
else
{
echo "<tr>
<td><span class='ordertb_title'>ID:</span> $ID </td>
<td><span class='ordertb_title'>Customer:</span> $customer</td>
<td><span class='ordertb_title'>Date:</span> $date</td>
<td><span class='ordertb_title'>Status:</span> <span class='order_stat_row'>$status</span> </td>
<td><span class='ordertb_title'>Total:</span> $total </td>
<td><span class='order_stat_row'>$paymentStatus</span></td>
<td>$orderType</td>
<td class='shipped_box'>
<form action='admin_DB.php' method='POST'>
<input type='hidden' value=\"$ID\" name='order_id'>
<input type='submit' name='Shipped' value='Mark as Shipped'>
</form>
</td>
</tr>";
}
}
}
else
{
echo "<tr><td colspan='8'>No Orders Found</td></tr>";
}
?>
</table>
</div>
</div>
<div class="Container2">
<h2>Inbox</h2>
<div class="message_content">
<table>
<tr>
<th>Message ID</th>
<th>Sender Username</th>
<th>Message</th>
<th>Attachments</th>
<th>Reply</th>
<th>Submit</th>
<th>Delete</th>
</tr>
<?php
$sql = "SELECT * FROM Messages WHERE response_text IS NULL;";
$result = mysqli_query($Connection, $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = $result->fetch_assoc())
{
$messageID = $row['message_id'];
$senderusername = $row['user_name'];
$message = $row['message_text'];
$attachment = $row['Uploads_url'];
if($attachment===NULL)
{
$uploads_Url = "----";
}
else
{
$uploads_Url = "<a href='./Images/PrescriptionMessage/$attachment' target='_blank'>Link</a>";
}
echo "<tr>
<td>$messageID</td>
<td>$senderusername</td>
<td>$message</td>
<td>$uploads_Url</td>
<form action='admin_DB.php' method='POST'>
<input type='hidden' value='$messageID' name='message_id'>
<td><input class=\"Reply_in\" type=\"text\" placeholder=\"Reply\" name='Reply_in'></td>
<td><button type=\"submit\" class=\"Submit_rply\" name='submit_rply'>Submit</button></td>
<td><button type=\"submit\" class=\"Delete_rply\" name='Delete_msg'>Delete</button></td>
</form>
</tr>";
}
}
else
{
echo "<tr><td colspan='7'>No Messages Found</td></tr>";
}
?>
</table>
</div>
</div>
<hr class="order_hr">
<div class="manage_product">
<button class="action_btn bg" onclick="manage_Product()" >Manage Products</button>
</div>
<?php include ("./footer.php"); ?>
</body>
</html>