-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.php
232 lines (204 loc) · 6.58 KB
/
notification.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
<!doctype html>
<html>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- NOTIFICATION -->
<?php
$id = $_SESSION['id'];
if(isset($_POST['submit'])){
$stmt = $con->prepare("UPDATE `notification` SET read_status=1 WHERE receiver_id=?");
$stmt->bind_param("i",$_POST['receiver_id']);
$stmt->execute();
}
if(isset($_GET['notifID'])){
$notiID = base64_decode(urldecode($_GET['notifID']));
$stmt = $con->prepare("UPDATE `notification` SET read_status=1 WHERE id=?");
$stmt->bind_param("i",$notiID);
$stmt->execute();
}
$stmt = $con->prepare("SELECT * FROM `notification` WHERE receiver_id =? ORDER BY time DESC LIMIT 25");
$stmt->bind_param("i",$id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$count = 0;
$style=' ';
$output='<div class="drop-content">';
while($row = $result->fetch_assoc()){
$noti_id = $row['id'];
$type = $row['type'];
$content = $row['content_id'];
$readStatus = $row['read_status'];
$date = date_create($row['time']);
$postBy = '';
$link='';
$pic = '';
//display date and time
date_default_timezone_set('Asia/Kuala_Lumpur');
$time = date_format($date, 'G:i');
$dateNow = date('Y-m-d H:i:s');
$curr_date = date_create($dateNow);
$date_diff = date_diff($date, $curr_date);
$day_diff = $date_diff->format("%a");
if($day_diff>7){
$day = date_format($date, 'm M');
}else if($day_diff>=2 && $day_diff<=7){
$day = date_format($date, 'D');
}
else if($day_diff>=1 && $day_diff<2){
$day = 'Yesterday';
}
else{
$day = 'Today';
}
//check for content event
if($type == 'event'){
$stmt = $con->prepare("SELECT `title`, `photo` FROM `event` WHERE id=?");
$stmt->bind_param("i", $content);
$stmt->execute();
$eventResult = $stmt->get_result();
$stmt->close();
//if event does not exist
if(!( $event = $eventResult->fetch_assoc())){
continue;
}
$postBy = 'FSKTM Admin posted an event';
$text = $event['title'];
$pic = '../images/event/'.$event['photo'];
$link = '../alumni/eventdetail.php?eventID='.$content.'¬ifID='.urlencode(base64_encode($noti_id));
}
//check for content friend
else if($type == 'request' ||$type == 'acceptFriend' || $type == 'rejectFriend'){
$stmt = $con->prepare("SELECT `title`, `fullname`, `profile_pic` FROM `alumni_profile` WHERE alumni_id=?");
$stmt->bind_param("i", $content);
$stmt->execute();
$friendResult = $stmt->get_result();
$stmt->close();
//if user does not exist
if(!($friend = $friendResult->fetch_assoc())){
continue;
}
$title = $friend['title'];
$name = $friend['fullname'];
if ($friend['profile_pic']==NULL){
$friend['profile_pic']='default.png';
}
$pic = '../images/profileimg/'.$friend['profile_pic'];
//link to friend request tab
$link = '../alumni/friendRequest.php?notifID='.urlencode(base64_encode($noti_id));
//check if alumni has title
if(!isset($title)){
$postBy = $name;
}
else{
$postBy = $title.' '.$name;
}
//check whether accepted or requested
if($type == 'request'){
$text = 'Sent you a friend request';
}
else if($type == 'acceptFriend'){
$text = 'Accepted your friend request';
}else if($type == 'rejectFriend'){
$text = 'Rejected your friend request';
}
}else{
$text ='Notification not found';
}
//change color of noti and count number of unread status
if($readStatus==0){
$seen = 'notification-box bg-gray';
$count+=1;
}else if($readStatus==1){
$seen = 'notification-box';
}
$output .= '
<li class="'.$seen.' seen" data-id="'.$noti_id.'" onclick="location.reload();location.href = \''.$link.'\'">
<div class="row mb-3" type="submitNoti">
<div class="col-lg-3 col-sm-3 col-3 img-position">
<img src="'.$pic.'" class="image-cover">
</div>
<div class="col-lg-8 col-sm-8 col-8">
<strong class="text-info"> '.$postBy.' </strong>
<div>
'.$text.'
</div>
<small style="color: grey;"> <i>'.$day.' at '.$time.'</i></small>
</div>
</div>
</li>';
}
//for displaying notification(3)
if($count==0){
$displayCount = '';
}else{
$displayCount = '('.$count.')';
}
$header='
<li class="head text-light bg-dark">
<div class="row">
<div class="col-lg-12 col-sm-12 col-12">
<form action="" method="post">
<div class="form-inline flex-box mt-2 mb-2 ml-2 mr-2">
<label>Notification'.$displayCount.'</label>
<input type="text" name="receiver_id" value="'.$id.'" hidden/>
<button onclick="location.reload()" name="submit" type="submit" class="btn btn-secondary btn-sm ml-5" style="float: right;">Mark all as read</button>
</div>
</form>
</div>
</div>
</li>';
$overall = $header.$output.'</div>';
if ($count == 0){
$style = 'background-color: transparent; color:transparent;';
}
?>
<div style="float: right;">
<a href="#" style="border:0; text-decoration:none; outline:none;margin: 0;padding: 0;" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<div class="icon"><i style="border:0; text-decoration:none; outline:none;" class="bell fa fa-bell"></i><span class="noti badge" id="count" style="<?php echo $style ?>"><?php echo $count ?></span></div>
</a>
<div class="dropdown-menu">
<?php echo $overall ?>
</div>
</div>
<!-- NOTIFICATION ENDS -->
<script>
$(document).ready(function(){
var count = <?php echo $count ?>;
function load_unseen_notification()
{
$.ajax({
url:'../notificationFetch.php',
method:"POST",
data:{user:<?php echo $id ?>},
error: function (jqXHR, textStatus, errorThrown) {
//alert(textStatus);
},
success:function(data)
{
var json = JSON.parse(data);
var unread = json.unread;
if(count!= unread){
$('.dropdown-menu').html(json.notification);
console.log(true);
if(unread > 0)
{
if(document.getElementById("count").hasAttribute("style")){
document.getElementById("count").removeAttribute("style");
}
document.getElementById("count").innerText = unread;
}else if(unread == 0){
document.getElementById("count").setAttribute("style", "background-color: transparent; color:transparent;");
}
}
if(count== 0 && unread == 0){
document.getElementById("count").setAttribute("style", "background-color: transparent; color:transparent;");
}
}
});
}
setInterval(function(){
load_unseen_notification();
}, 5000);
});
</script>
</html>