-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.php
213 lines (184 loc) · 7.52 KB
/
conversation.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
<?php
session_start();
$base_url = 'http://' . $_SERVER['HTTP_HOST'] . '/Hacettepe-KDSE-BPYS';
if (isset($_SESSION['userlogin'])) {
}
$myUser = $_SESSION['userlogin']['id'];
require_once("config-messages.php");
if (isset($_GET)) {
$senderId = $_GET['senderID'];
$recieverId = $_GET['recieverID'];
$senderName = $_GET['name'];
$senderSurname = $_GET['surname'];
$userName = $_SESSION["userlogin"]['name'];
$userID = $_SESSION["userlogin"]['id'];
$sql = "SELECT * FROM messages WHERE sender_id =" . $senderId . " AND recipient_Id =" . $recieverId . " OR sender_id =" . $recieverId . " AND recipient_Id =" . $senderId . " ORDER by sent_at";
$smtmselect = $db->prepare($sql);
$messages = $smtmselect->execute();
if ($messages) {
$values = $smtmselect->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<title>KDSE-BPYS</title>
</head>
<body>
<div style=" position: absolute; margin-left:10%; margin-bottom: 30px; height:90%; "
class="conversation-box w-75 shadow border mt-5">
<div class="title-box w-100 border shadow">
<?php
$name = ucfirst($senderName);
$surname = ucfirst($senderSurname);
echo "<h3 class='p-4 ml-5'>$name $surname</h3>";
?>
</div>
<div class="d-flex" style="height: 80%">
<div class="p-5" id="scrollable" style="overflow-y: scroll; height:100%; width:100%">
</div>
</div>
<form action="" method="post" class="d-flex m-3"
style="position: absolute; bottom: 0; width:90%; margin-top:30px">
<input type="text" class="form-control" name="message" id="messageInput" aria-describedby="emailHelp"
placeholder="Mesaj Yazınız...">
<button type="submit" class="btn btn-primary" id="sendMessage">Gönder</button>
</form>
</div>
<div>
<div class="mt-3"></div>
<p id="notEmpty" style="color:red"></p>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
window.onload = function() {
var sender_id = <?php echo $senderId ?>;
var recipient_Id = <?php echo $recieverId ?>;
var senderName = <?php echo json_encode(ucfirst($senderName)) ?>;
var senderSurname = <?php echo json_encode(ucfirst($senderSurname)) ?>;
var userName = <?php echo json_encode(ucfirst($userName)) ?>;
var userID = <?php echo $userID ?>;
$.ajax({
type: 'POST',
url: '<?php echo $base_url; ?>/updateConvo.php/',
data: {
sender_id: sender_id,
recipient_id: recipient_Id,
userID: userID,
senderName: senderName,
senderSurname: senderSurname,
userName: userName
},
success: function(data) {
$('#scrollable').html(data);
var element = document.getElementById("scrollable");
element.scrollTop = element.scrollHeight;
},
error: function(data) {
Swal.fire({
'title': 'Errors',
'text': 'There were errors',
'type': 'error'
})
}
})
}
window.setInterval(() => {
var sender_id = <?php echo $senderId ?>;
var recipient_Id = <?php echo $recieverId ?>;
var senderName = <?php echo json_encode(ucfirst($senderName)) ?>;
var senderSurname = <?php echo json_encode(ucfirst($senderSurname)) ?>;
var userName = <?php echo json_encode(ucfirst($userName)) ?>;
var userID = <?php echo $userID ?>;
$.ajax({
type: 'POST',
url: '<?php echo $base_url; ?>/updateConvo.php/',
data: {
sender_id: sender_id,
recipient_id: recipient_Id,
userID: userID,
senderName: senderName,
senderSurname: senderSurname,
userName: userName
},
success: function(data) {
$('#scrollable').html(data);
},
error: function(data) {
Swal.fire({
'title': 'Errors',
'text': 'There were errors',
'type': 'error'
})
}
})
}, 3000);
function pad(n) {
return n < 10 ? '0' + n : n
};
$(document).ready(function() {
$('#sendMessage').click(function(e) {
e.preventDefault()
var valid = this.form.checkValidity();
if (valid) {
var date = new Date();
var current_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date
.getDate();
var current_time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
var dateAndTime = current_date + " " + current_time;
var message = $('#messageInput').val();
var sender_id = <?php echo $senderId ?>;
var recipient_Id = <?php echo $recieverId ?>;
var senderName = <?php echo json_encode(ucfirst($senderName)) ?>;
var senderSurname = <?php echo json_encode(ucfirst($senderSurname)) ?>;
var userName = <?php echo json_encode(ucfirst($userName)) ?>;
var userID = <?php echo $userID ?>;
if (message === '') {
document.getElementById("notEmpty").innerText = "Message cannot be empty!"
} else {
document.getElementById("notEmpty").innerText = "";
$.ajax({
type: 'POST',
url: '<?php echo $base_url; ?>/process-messages.php/?',
data: {
sender_id: sender_id,
recipient_id: recipient_Id,
message: message,
sent_at: dateAndTime,
senderName: senderName,
senderSurname: senderSurname,
userName: userName,
userID: userID,
},
success: function(data) {
$('#messageInput').val("");
$('#scrollable').append(data);
var element = document.getElementById("scrollable");
element.scrollTop = element.scrollHeight;
},
error: function(data) {
Swal.fire({
'title': 'Errors',
'text': 'There were errors',
'type': 'error'
})
}
})
}
} else {
}
})
})
</script>
<script>
</script>
</body>
</html>