-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_notifications.php
More file actions
60 lines (51 loc) · 1.77 KB
/
Copy pathget_notifications.php
File metadata and controls
60 lines (51 loc) · 1.77 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
<?php
/**
* Get Notifications API Endpoint
*
* Returns recent notifications and unread count for the logged-in user.
* Used by the notification bell dropdown in header.php.
*/
require 'session_config.php';
require 'dbcon.php';
header('Content-Type: application/json');
if (!isset($_SESSION['username'])) {
echo json_encode(['error' => 'Authentication required.']);
exit;
}
$userId = $_SESSION['user_id'];
try {
// Get unread count
$countStmt = $con->prepare("SELECT COUNT(*) as cnt FROM notifications WHERE user_id = ? AND is_read = 0");
$countStmt->bind_param("i", $userId);
$countStmt->execute();
$unreadCount = $countStmt->get_result()->fetch_assoc()['cnt'];
$countStmt->close();
// Get recent notifications (last 20)
$stmt = $con->prepare("SELECT id, title, message, link, type, is_read, created_at FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT 20");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$notifications = [];
while ($row = $result->fetch_assoc()) {
$notifications[] = $row;
}
$stmt->close();
echo json_encode([
'unread_count' => (int)$unreadCount,
'notifications' => $notifications
]);
} catch (Exception $e) {
// The `notifications` table is optional in older deployments. Only swallow
// the "missing table" case; log everything else so real DB failures don't
// silently show "no notifications" forever.
if (stripos($e->getMessage(), "doesn't exist") === false
&& stripos($e->getMessage(), 'unknown table') === false) {
error_log('get_notifications error: ' . $e->getMessage());
}
echo json_encode([
'unread_count' => 0,
'notifications' => []
]);
}
$con->close();
?>