/
managemessages.php
375 lines (322 loc) · 12.1 KB
/
managemessages.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<!--<link rel="stylesheet" type="text/css" href="bootstrap.css">-->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<input type="button" value = "back" onclick="back()" class="btn btn-default">
<link rel="stylesheet" href="bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
function back()
{
window.location.href = "afterlogin.php";
}
</script>
<?php
session_start();
include 'db_connect.php';
$currentuserid = $_SESSION['userid'];
$currentusername = $_SESSION['userkanaam'];
echo "<h2>Welcome ".$currentusername."</h2>";
//echo '<br>This page is to model messages';
if(isset($_POST['hiddensub']))
{
$sub = $_POST['hiddensub'];
//echo ("body ".$_POST['hiddenbody']);
$body = $_POST['hiddenbody'];
// echo $_POST['hiddencat'];
$categoryname = $_POST['hiddencat'];
//echo $_POST['hiddenvisibility'];
$access = $_POST['hiddenvisibility'];
//we need to get the category id here
$stmt = $conn->prepare("select C_Id from Category where C_Name = ?");
$stmt->bind_param("s",$categoryname);
$stmt->execute();
$stmt->bind_result($var1);
/* fetch value */
if( $stmt->fetch())
{
$catid = $var1;
}
$stmt->close();
$time = date("Y-m-d H:i:s");
//echo $currentuserid." ".$sub." ".$catid." ".$access." ".$time;
$stmt = $conn->prepare("insert into Thread (Author, Subject, C_Id, New_Member_Access, Time_of_Creation) values (?,?,?,?,?)");
$stmt->bind_param("isiis",$currentuserid,$sub,$catid,$access,$time);
if($stmt->execute())
echo 'sucessfully inserted';
else 'message send failure';
$stmt->close();
$stmt = $conn->prepare("insert into Message_content (U_Id, Time_of_Reply, Body) values (?,?,?)");
$stmt->bind_param("iss",$currentuserid,$time,$body);
if($stmt->execute())
echo 'sucessfully inserted';
else 'message send failure';
$stmt->close();
$query = "select T_Id from Thread where Time_of_Creation = '$time'";
$result= $conn->query($query);
if($result->num_rows>0)
{
$row = $result->fetch_assoc();
$threadvar = $row['T_Id'];
}
//echo("<br>thread id is".$threadvar."<br>");
$choice = $_POST['hiddenfirstoption'];
if($choice == 'Private')
{
$sub_choice = $_POST['hiddenfirstoption1'];
//echo $sub_choice;
$var = explode(",",$sub_choice);
$result = count($var);
for($i =0;$i<$result-1;$i++)
{
$sub_var = explode(";",$var[$i]);
$result1 = count($sub_var);
for($j =0;$j<$result1;$j++)
{
// echo $sub_var[$j];
}
$query4 = "select l.U_Id as UID from Login l,User_profile p where l.U_Id = p.U_Id and l.Username = '$sub_var[0]' and p.Username = '$sub_var[1]'";
$res = $conn->query($query4);
if($res->num_rows>0)
{
$row= $res->fetch_assoc();
$useridforthisthread = $row['UID'];
//echo ("User Id is ".$useridforthisthread);
}
echo "<br>";
$stmt3 = $conn->prepare("insert into Recipient values (?,?)");
$stmt3->bind_param("ii",$threadvar,$useridforthisthread);
if($stmt3->execute())
echo 'inserted sucessfully';
else echo 'insert failure';
$stmt3->close();
}
}
if($choice == 'Block Members')
{
// echo 'block members selected';
$query = "select Block_Id from User_profile where U_Id = $currentuserid and Block_Request_Accepted = 1";
$resultkiran1 = $conn->query($query);
if($resultkiran1->num_rows>0)
{
$row= $resultkiran1->fetch_assoc();
$userblockid = $row['Block_Id'];
// echo 'the user is from block '.$userblockid;
}
$query = "select U_Id from User_profile where U_Id <> $currentuserid and Block_Request_Accepted = 1 and Block_Id = $userblockid";
$resultkiran2 = $conn->query($query);
if($resultkiran2->num_rows>0)
{
while($row= $resultkiran2->fetch_assoc())
{
$stmt3 = $conn->prepare("insert into Recipient values (?,?)");
$stmt3->bind_param("ii",$threadvar,$row['U_Id']);
if($stmt3->execute())
echo 'inserted sucessfully';
else echo 'insert failure';
}
$stmt3->close();
}
}
if($choice == 'Neighbors')
{
$query = "select Block_Id from User_profile where U_Id = $currentuserid and Block_Request_Accepted = 1";
$resultkiran1 = $conn->query($query);
if($resultkiran1->num_rows>0)
{
$row= $resultkiran1->fetch_assoc();
$userblockid = $row['Block_Id'];
// echo 'the user is from block '.$userblockid;
}
$query = "select To_Id from Neighborhood where From_Id = $currentuserid";
$resultkiran2 = $conn->query($query);
if($resultkiran2->num_rows>0)
{
while($row= $resultkiran2->fetch_assoc())
{
$stmt3 = $conn->prepare("insert into Recipient values (?,?)");
$stmt3->bind_param("ii",$threadvar,$row['To_Id']);
if($stmt3->execute())
echo 'inserted sucessfully';
else echo 'insert failure';
}
$stmt3->close();
}
}
if($choice == 'Friends')
{
echo 'friends selected';
$query = "select Block_Id from User_profile where U_Id = $currentuserid and Block_Request_Accepted = 1";
$resultkiran1 = $conn->query($query);
if($resultkiran1->num_rows>0)
{
$row= $resultkiran1->fetch_assoc();
$userblockid = $row['Block_Id'];
// echo 'the user is from block '.$userblockid;
}
$query = "select Acceptor from Request_For_Friendship where Sender = $currentuserid and Status = 'accepted'" ;
$resultkiran2 = $conn->query($query);
if($resultkiran2->num_rows>0)
{
while($row= $resultkiran2->fetch_assoc())
{
$stmt3 = $conn->prepare("insert into Recipient values (?,?)");
$stmt3->bind_param("ii",$threadvar,$row['Acceptor']);
if($stmt3->execute())
echo 'inserted sucessfully';
else echo 'insert failure';
}
$stmt3->close();
}
}
else echo $choice;
}
?>
<button type="button" class="btn btn-default" id="myBtn1">Create Thread</button>
<button type="button" class="btn btn-default" id="myBtn2" onclick="viewthread()">View Thread</button>
<script>
function viewthread()
{
window.location.href = "seethreads.php";
}
</script>
<div class="modal fade" id="myModalsignup" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4><span class="glyphicon glyphicon-envelope"></span> Thread</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">
<form role="form" method="post" action="managemessages.php" id="composeform">
<div class="form-group">
<label for="usrname"><span class="glyphicon glyphicon-pencil"></span> Subject:</label>
<input type="text" class="form-control" id="s" placeholder="Enter subject" name = "msgsubject" required>
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-list-alt"></span> category:</label>
<select class="form-control" id="selectid">
<?php
$query = "select C_Name from Category";
$result= $conn->query($query);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
echo("<option>". $row['C_Name']."</option>");
}
}
?>
</select>
<br>
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-user"></span> Send to:</label>
<select class="form-control" id="selectid1" onchange="onchangefunction()">
<option>Block Members</option>
<option> Neighbors</option>
<option>Friends</option>
<option>Private</option>
</select>
<br>
</div>
<style>
#specialblock
{
display: none; !important
}
</style>
<div class="form-group" id="specialblock">
<label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Select Recepients:</label>
<select class="form-control" multiple id="multipleselect">
<option selected="selected">default</option>
<?php
$query = "select l.Username as A,p.Username as B from Login l, User_profile p where l.U_Id= p.U_Id and l.U_Id<>$currentuserid";
$result= $conn->query($query);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
// $name1= $row['l.Username'];
// $name2 =$row['p.Username'];
$name1= $row['A'];
$name2 =$row['B'];
$cont = "<option>".$name1 .";".$name2."</option>";
echo $cont;
}
}
?>
</select>
<br>
Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.
</div>
<script>
function onchangefunction()
{
var v = document.getElementById('selectid1').value;
var comp = "Private";
var n = v.localeCompare(comp);
if(n==0)
{
document.getElementById('specialblock').style.display='block';
}
else
{
document.getElementById('specialblock').style.display='none';
}
}
</script>
<div class="checkbox">
<label>
<input type="checkbox" value="" id="check">
Visible to new members
</label>
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-tag"></span> Content:</label>
<textarea rows="4" id="area" cols="50" class="form-control" placeholder="Enter message here...(upto 500 characters)" required>
</textarea>
</div>
<button type="submit" class="btn btn-success btn-block" name="submitbutton" onclick="send()"><span class="glyphicon glyphicon-ok"></span> Post</button>
<input type="hidden" id="hiddensub" name="hiddensub">
<input type="hidden" id="hiddenbody" name="hiddenbody">
<input type="hidden" id="hiddencat" name="hiddencat">
<input type="hidden" id="hiddenvisibility" name="hiddenvisibility">
<input type="hidden" id="hiddenfirstoption" name="hiddenfirstoption">
<input type="hidden" id="hiddenfirstoption1" name="hiddenfirstoption1">
</form>
</div>
</div>
</div>
</div>
<script>
function send()
{
document.getElementById('hiddensub').value = document.getElementById('s').value;
document.getElementById('hiddenbody').value = document.getElementById('area').value;
document.getElementById('hiddencat').value = document.getElementById('selectid').value;
document.getElementById('hiddenfirstoption').value = document.getElementById('selectid1').value;
if(document.getElementById('check').checked)
document.getElementById('hiddenvisibility').value = 1;
else
document.getElementById('hiddenvisibility').value = 0;
var multipleoption = document.getElementById('multipleselect');
var x = multipleoption.length;
var c = multipleoption.children;
var txt = "";
for (i = 0; i < x; i++) {
if(c[i].selected)
txt = txt + c[i].value + ",";
}
document.getElementById('hiddenfirstoption1').value =txt;
}
$(document).ready(function(){
$("#myBtn1").click(function(){
$("#myModalsignup").modal();
});
});
function myfun()
{
$("#myModal").close;
}
</script>