-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_handler.php
More file actions
118 lines (100 loc) · 4.08 KB
/
Copy pathnotification_handler.php
File metadata and controls
118 lines (100 loc) · 4.08 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
<?php
// Handle AJAX requests for notifications
include 'config.php';
if (isset($_POST['action'])) {
header('Content-Type: application/json');
if ($_POST['action'] === 'get_notifications') {
$user_id = $_SESSION['user_id'];
$query = "SELECT id, title, message, created_at, is_read FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT 20";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
$notifications = [];
while ($row = $result->fetch_assoc()) {
$notifications[] = [
'id' => (int)$row['id'],
'title' => $row['title'],
'message' => $row['message'],
'created_at' => $row['created_at'],
'is_read' => (bool)$row['is_read']
];
}
$stmt->close();
echo json_encode(['success' => true, 'notifications' => $notifications]);
exit();
}
if ($_POST['action'] === 'get_unread_count') {
$user_id = $_SESSION['user_id'];
$query = "SELECT COUNT(*) as count FROM notifications WHERE user_id = ? AND is_read = 0";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
echo json_encode(['success' => true, 'count' => (int)$row['count']]);
exit();
}
if ($_POST['action'] === 'mark_notification_read') {
$notification_id = (int)$_POST['notification_id'];
$user_id = $_SESSION['user_id'];
$query = "UPDATE notifications SET is_read = 1 WHERE id = ? AND user_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ii', $notification_id, $user_id);
$success = $stmt->execute();
$stmt->close();
echo json_encode(['success' => $success]);
exit();
}
if ($_POST['action'] === 'mark_all_notifications_read') {
$user_id = $_SESSION['user_id'];
$query = "UPDATE notifications SET is_read = 1 WHERE user_id = ? AND is_read = 0";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $user_id);
$success = $stmt->execute();
$stmt->close();
echo json_encode(['success' => $success]);
exit();
}
if ($_POST['action'] === 'clear_all_notifications') {
$user_id = $_SESSION['user_id'];
$query = "DELETE FROM notifications WHERE user_id = ?";
$stmt = $conn->prepare($query);
if (!$stmt) {
echo json_encode(['success' => false, 'message' => 'Database prepare failed: ' . $conn->error]);
exit();
}
$stmt->bind_param('i', $user_id);
$success = $stmt->execute();
if (!$success) {
echo json_encode(['success' => false, 'message' => 'Database execute failed: ' . $stmt->error]);
exit();
}
$affected_rows = $stmt->affected_rows;
$stmt->close();
echo json_encode(['success' => true, 'message' => 'Cleared ' . $affected_rows . ' notifications']);
exit();
}
if ($_POST['action'] === 'delete_single_notification') {
$notification_id = (int)$_POST['notification_id'];
$user_id = $_SESSION['user_id'];
$query = "DELETE FROM notifications WHERE id = ? AND user_id = ?";
$stmt = $conn->prepare($query);
if (!$stmt) {
echo json_encode(['success' => false, 'message' => 'Database prepare failed: ' . $conn->error]);
exit();
}
$stmt->bind_param('ii', $notification_id, $user_id);
$success = $stmt->execute();
if (!$success) {
echo json_encode(['success' => false, 'message' => 'Database execute failed: ' . $stmt->error]);
exit();
}
$affected_rows = $stmt->affected_rows;
$stmt->close();
echo json_encode(['success' => $affected_rows > 0, 'message' => $affected_rows > 0 ? 'Notification deleted' : 'Notification not found']);
exit();
}
}
?>