-
Notifications
You must be signed in to change notification settings - Fork 4
/
discussiontopic.php
218 lines (174 loc) · 7.56 KB
/
discussiontopic.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
<?php
/*
This file is part of Church Rota.
Copyright (C) 2011 David Bunce
Church Rota is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Church Rota is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Church Rota. If not, see <http://www.gnu.org/licenses/>.
*/
// Include files, including the database connection
include('includes/dbConfig.php');
include('includes/functions.php');
// Start the session. This checks whether someone is logged in and if not redirects them
session_start();
if (isset($_SESSION['is_logged_in']) || $_SESSION['db_is_logged_in'] == true) {
// Just continue the code
} else {
header('Location: login.php');
exit;
}
// Handle details from the header
$id = $_GET['id'];
$postid = $_GET['postid'];
$parentid = $_GET['parentid'];
$userID = $_SESSION['userid'];
$subscribe = $_GET['subscribe'];
$subscribestatus = $_GET['subscribestatus'];
$postremove = $_GET['postremove'];
$subscription = $_GET['subscription'];
$redirect = $_GET['redirect'];
// Method to remove someone from the band
if($postremove == "true") {
removePost($postid);
if($redirect == "true") {
header ( "Location: discussion.php?categoryid=" . $parentid);
} else {
header ( "Location: discussiontopic.php?id=" . $id . "&parentid=" . $parentid );
}
}
if($subscribe == "true") {
subscribeto($userID, 0, $id);
header ( "Location: discussiontopic.php?id=" . $id . "&parentid=" . $parentid );
} else if($subscribe == "false") {
unsubscribefrom($subscription);
header ( "Location: discussiontopic.php?id=" . $id . "&parentid=" . $parentid );
}
// If the form has been sent, we need to handle the data.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$discussiontopic = $_POST['discussiontopic'];
$discussiontopic = mysql_real_escape_string($discussiontopic);
$discussion = $_POST['discussion'];
$discussion = cleanUpTextarea($discussion);
if($id == "") {
// If we are starting a new discussion
$sql = ("INSERT INTO cr_discussion (CategoryParent, userID, topic, topicName, date) VALUES ('$parentid', '$userID', '$discussion', '$discussiontopic', NOW())");
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
$type = "category";
notifySubscribers($parentid, $type, $userID);
header( 'Location: discussion.php?categoryid=' . $parentid);
exit;
} else {
// Otherwise we reply in thread
$sql = ("INSERT INTO cr_discussion (topicParent, CategoryParent, userID, topic, date)
VALUES ('$id', '$parentid', '$userID', '$discussion', NOW())");
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
if($subscribestatus == "false") {
subscribeto($userID, 0, $id);
}
$type = "post";
notifySubscribers($id, $type, $userID);
}
}
$formatting = "true";
include('includes/header.php');
if($id == "") {
// If there is no topic, we want to create one
?>
<div class="elementBackground">
<h2>New post</h2>
<p>You are commenting as <strong><?php echo $_SESSION['name']; ?></strong></p>
<form id="addTopic" method="post" action="discussiontopic.php?parentid=<?php echo $parentid; ?>">
<fieldset>
<label for="discussiontopic">Title:</label>
<input id="discussiontopic" type="text" name="discussiontopic" placeholder="Enter discussion topic:" />
<label for="discussion">Your comment:</label>
<textarea id="discussion" type="text" name="discussion"></textarea>
<input type="submit" value="Add topic" />
</fieldset>
</form>
</div>
<?php
} else {
// Otherwise we want to display the requested topic
// First we get the heading post for the topic . . .
$sql = "SELECT *, (SELECT CONCAT(`firstname`, ' ', `lastname`) FROM cr_users WHERE `cr_users`.id = `cr_discussion`.`userID` ) AS `name`,
DATE_FORMAT(date,'%W, %M %e @ %h:%i %p') AS dateFormatted
FROM cr_discussion WHERE id = '$id'";
$result = mysql_query($sql) or die();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$topic = $row['topic'];
?>
<div class="elementBackground highlight">
<h2>Topic: <?php echo $row['topicName']; ?></h2>
<p>You are commenting as <strong><?php echo $_SESSION['name']; ?></strong></p>
</div>
<div class="elementBackground">
<h2><a name="addcategory"><?php echo $row['name']; ?></a> - <span class="postdate"><em><?php echo $row['dateFormatted']; ?></em></span>
<?php if(isAdmin()) { echo "<a href='discussiontopic.php?redirect=true&postremove=true&postid=" . $row['id'] . "&parentid=" . $parentid . "'><img src='graphics/close.png' /></a><br />"; }?>
</h2>
<p><?php echo stripslashes($topic); ?></p>
</div>
<?php
}
// Then we get all the posts that go in the topic
$sql = "SELECT *, (SELECT CONCAT(`firstname`, ' ', `lastname`) FROM cr_users WHERE `cr_users`.id = `cr_discussion`.`userID` ) AS `name`,
DATE_FORMAT(date,'%W, %M %e @ %h:%i %p') AS dateFormatted
FROM cr_discussion WHERE topicParent = '$id' ORDER BY id";
$result = mysql_query($sql) or die();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$topic = $row['topic'];
?>
<div class="elementBackground">
<h2><a name="addcategory"><?php echo $row['name']; ?></a> - <span class="postdate"><em><?php echo $row['dateFormatted']; ?></em></span>
<?php if(isAdmin()) { echo "<a href='discussiontopic.php?postremove=true&postid=" . $row['id'] . "&parentid=" . $parentid . "&id=" . $id . "'><img src='graphics/close.png' /></a>"; }?></h2>
<p><?php echo stripslashes($topic); ?></p>
</div>
<?php
// At the end of the stream of posts, we want to have the possibility of replying
$sql2 = "SELECT id FROM cr_subscriptions WHERE `cr_subscriptions`.userid = $userID AND `cr_subscriptions`.`topicid` = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
// Check to see if we're subscribed or not. If yes, give the option to unsubscribe. If not, allow them to subscribe
$isSubscribed = $row2['id'];
}
if($isSubscribed != "") {
$subscribe = '<div class="item"><a href="discussiontopic.php?subscribe=false&subscription=' . $isSubscribed. '&id=' .
$id . '&parentid=' . $parentid . '">Unsubscribe from this post</a></div>';
$subscribestatus = "true";
} else {
$subscribe = '<div class="item"><a href="discussiontopic.php?subscribe=true&id=' . $id . '&parentid=' . $parentid . '">
Subscribe to this post</a></div>';
$subscribestatus = "false";
}
}
?>
<div class="elementBackground">
<h2><a name="addcategory">Reply:</a></h2>
<form id="addTopic" method="post" action="discussiontopic.php?id=<?php echo $id; ?>&subscribestatus=<?php echo $subscribestatus; ?>&parentid=<?php echo $parentid; ?>">
<fieldset>
<label for="discussion">Your comment:</label>
<textarea id="discussion" type="text" name="discussion"></textarea>
<input type="submit" value="Add reply" />
</fieldset>
</form>
</div>
<?php
}
?>
<div id="right">
<?php echo $subscribe; ?>
</div>
<?php include('includes/footer.php'); ?>