-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.php
More file actions
141 lines (135 loc) · 6.84 KB
/
notifications.php
File metadata and controls
141 lines (135 loc) · 6.84 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Notification helper — include after session and user setup.
* Requires: $user['email'], $isAdmin, h() function.
*/
$_notificationsPath = __DIR__ . '/storage/notifications.json';
$_userNotifications = [];
$_unreadCount = 0;
if (!$isAdmin && !empty($user['email'])) {
if (is_file($_notificationsPath)) {
$allNotifs = json_decode(file_get_contents($_notificationsPath), true);
if (is_array($allNotifs)) {
foreach ($allNotifs as $n) {
if (strtolower(trim($n['to_email'] ?? '')) === strtolower(trim($user['email']))) {
$_userNotifications[] = $n;
if (empty($n['read'])) {
$_unreadCount++;
}
}
}
// Most recent first
usort($_userNotifications, function ($a, $b) {
return strcmp(($b['created_at'] ?? ''), ($a['created_at'] ?? ''));
});
}
}
// Handle mark-as-read via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'mark_notifications_read') {
$allNotifs = json_decode(file_get_contents($_notificationsPath), true);
if (is_array($allNotifs)) {
foreach ($allNotifs as &$n) {
if (strtolower(trim($n['to_email'] ?? '')) === strtolower(trim($user['email']))) {
$n['read'] = true;
}
}
unset($n);
file_put_contents($_notificationsPath, json_encode($allNotifs, JSON_PRETTY_PRINT), LOCK_EX);
$_unreadCount = 0;
// Refresh user notifications
foreach ($_userNotifications as &$un) {
$un['read'] = true;
}
unset($un);
}
}
// Handle clear all notifications via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'clear_notifications') {
$allNotifs = json_decode(file_get_contents($_notificationsPath), true);
if (is_array($allNotifs)) {
$allNotifs = array_values(array_filter($allNotifs, function ($n) use ($user) {
return strtolower(trim($n['to_email'] ?? '')) !== strtolower(trim($user['email']));
}));
file_put_contents($_notificationsPath, json_encode($allNotifs, JSON_PRETTY_PRINT), LOCK_EX);
$_userNotifications = [];
$_unreadCount = 0;
}
}
}
function render_notification_bell() {
global $_userNotifications, $_unreadCount, $isAdmin;
if ($isAdmin) return;
$typeLabels = [
'networking' => 'Networking Request',
'job_application' => 'Job Application',
'event_registration' => 'Event Registration'
];
?>
<div class="notif-wrapper" id="notif-wrapper">
<button class="notif-bell" type="button" id="notif-bell" aria-label="Notifications">
<svg viewBox="0 0 24 24" width="22" height="22">
<path d="M12 22c1.1 0 2-.9 2-2h-4a2 2 0 0 0 2 2Zm6-6V11c0-3.07-1.63-5.64-4.5-6.32V4a1.5 1.5 0 0 0-3 0v.68C7.64 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2Z"/>
</svg>
<?php if ($_unreadCount > 0) { ?>
<span class="notif-badge"><?php echo $_unreadCount; ?></span>
<?php } ?>
</button>
<div class="notif-dropdown" id="notif-dropdown" style="display:none;">
<div class="notif-dropdown-header">
<span class="notif-dropdown-title">Notifications</span>
<div class="notif-header-actions">
<?php if ($_unreadCount > 0) { ?>
<form method="post" action="" style="display:inline;">
<input type="hidden" name="action" value="mark_notifications_read">
<button class="notif-mark-read" type="submit">Mark all read</button>
</form>
<?php } ?>
<?php if (!empty($_userNotifications)) { ?>
<form method="post" action="" style="display:inline;">
<input type="hidden" name="action" value="clear_notifications">
<button class="notif-clear-all" type="submit" onclick="return confirm('Clear all notifications?');">Clear all</button>
</form>
<?php } ?>
</div>
</div>
<div class="notif-dropdown-body">
<?php if (empty($_userNotifications)) { ?>
<div class="notif-empty">No notifications yet.</div>
<?php } else { ?>
<?php foreach ($_userNotifications as $notif) { ?>
<?php
$nType = $typeLabels[$notif['type'] ?? ''] ?? 'Request';
$nDecision = $notif['decision'] ?? '';
$nDetail = $notif['detail'] ?? '';
$nDate = !empty($notif['created_at']) ? date('M d, Y', strtotime($notif['created_at'])) : '';
$nRead = !empty($notif['read']);
$decisionClass = $nDecision === 'accepted' ? 'accepted' : 'rejected';
$decisionLabel = ucfirst($nDecision);
?>
<div class="notif-item <?php echo $nRead ? '' : 'unread'; ?>">
<div class="notif-item-icon <?php echo $decisionClass; ?>">
<?php if ($nDecision === 'accepted') { ?>
<svg viewBox="0 0 24 24" width="16" height="16"><path d="M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17Z"/></svg>
<?php } else { ?>
<svg viewBox="0 0 24 24" width="16" height="16"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
<?php } ?>
</div>
<div class="notif-item-content">
<div class="notif-item-text">
Your <strong><?php echo htmlspecialchars($nType); ?></strong>
<?php if ($nDetail) { ?> for <strong><?php echo htmlspecialchars($nDetail); ?></strong><?php } ?>
was <span class="notif-decision <?php echo $decisionClass; ?>"><?php echo $decisionLabel; ?></span>
</div>
<?php if ($nDate) { ?>
<div class="notif-item-date"><?php echo $nDate; ?></div>
<?php } ?>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</div>
</div>
<?php
}
?>