Repository navigation
Expand file tree
/
Copy pathThreadController.php
More file actions
275 lines (211 loc) · 9.24 KB
/
Copy pathThreadController.php
File metadata and controls
275 lines (211 loc) · 9.24 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
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
<?php
include_once("model/Thread.php");
include_once("model/User.php");
include_once("model/Comment.php");
class ThreadController {
private static function jsonResponse($data, $statusCode = 200) {
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
}
private static function requireLoginOrJsonError() {
if (!isset($_SESSION["user"])) {
self::jsonResponse(["ok" => false, "error" => "Login required."], 401);
return false;
}
return true;
}
public static function showThread($thread_id, $additional = []) {
$thread = Thread::getThreadById($thread_id);
if (!$thread) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Thread Not Found", "message" => "The thread you are looking for does not exist."]);
return;
}
$comments = Comment::getCommentsByThreadId($thread_id);
$threadVote = 0;
if (isset($_SESSION["user"])) {
$userId = $_SESSION["user"]["id"];
$threadVote = Thread::getThreadVoteByUser($thread_id, $userId);
foreach ($comments as &$comment) {
$comment["user_vote"] = Comment::getCommentVoteByUser($comment["id"], $userId);
}
unset($comment);
}
ViewHelper::render("view/thread/threadShow.php", array_merge([
"thread" => $thread,
"comments" => $comments,
"thread_vote" => $threadVote
], $additional));
}
public static function ajaxCreateComment() {
if (!self::requireLoginOrJsonError()) return;
$thread_id = intval($_POST['thread_id'] ?? 0);
$content = trim($_POST['content'] ?? '');
if ($thread_id <= 0 || $content === '') {
self::jsonResponse(["ok" => false, "error" => "Comment content cannot be empty."], 422);
return;
}
Comment::createComment($content, $_SESSION['user']['id'], $thread_id);
$comments = Comment::getCommentsByThreadId($thread_id);
$latest = null;
foreach ($comments as $c) {
if (intval($c['user_id']) === intval($_SESSION['user']['id'])) {
$latest = $c;
}
}
if (!$latest && !empty($comments)) {
$latest = end($comments);
}
if (!$latest) {
self::jsonResponse(["ok" => false, "error" => "Failed to render comment."], 500);
return;
}
ob_start();
$comment = $latest;
$comment['user_vote'] = 0;
include("view/partial/commentCard.php");
$commentHtml = ob_get_clean();
self::jsonResponse(["ok" => true, "commentHtml" => $commentHtml]);
}
public static function createComment() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$thread_id = intval($_POST['thread_id'] ?? 0);
$content = trim($_POST['content'] ?? '');
if ($thread_id <= 0 || $content === '') {
self::showThread($thread_id, [
"error" => "Comment content cannot be empty.",
"commentContent" => $content
]);
return;
}
Comment::createComment($content, $_SESSION['user']['id'], $thread_id);
ViewHelper::redirect(BASE_URL . "thread/show?id=" . $thread_id);
}
public static function deleteComment() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$comment_id = intval($_POST['comment_id'] ?? 0);
$thread_id = intval($_POST['thread_id'] ?? 0);
$comment = Comment::getCommentById($comment_id);
if (!$comment) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Comment Not Found", "message" => "The comment you are trying to delete does not exist."]);
return;
}
if ($comment['user_id'] !== $_SESSION['user']['id']) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Unauthorized", "message" => "You can only delete your own comments."]);
return;
}
Comment::deleteComment($comment_id);
ViewHelper::redirect(BASE_URL . "thread/show?id=" . $thread_id);
}
public static function deleteThread() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$thread_id = intval($_POST['thread_id'] ?? 0);
$thread = Thread::getThreadById($thread_id);
if (!$thread) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Thread Not Found", "message" => "The thread you are trying to delete does not exist."]);
return;
}
if ($thread['user_id'] !== $_SESSION['user']['id']) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Unauthorized", "message" => "You can only delete your own threads."]);
return;
}
Thread::deleteThread($thread_id);
ViewHelper::redirect(BASE_URL . "main");
}
public static function ajaxVoteThread() {
if (!self::requireLoginOrJsonError()) return;
$thread_id = intval($_POST['thread_id'] ?? 0);
$vote = intval($_POST['vote'] ?? 0);
if ($thread_id <= 0 || ($vote !== 1 && $vote !== -1)) {
self::jsonResponse(["ok" => false, "error" => "The vote value is invalid."], 422);
return;
}
Thread::voteThread($thread_id, $_SESSION['user']['id'], $vote);
$userId = $_SESSION['user']['id'];
$userVote = Thread::getThreadVoteByUser($thread_id, $userId);
$thread = Thread::getThreadById($thread_id);
$threadVotePlus = (int)($thread['vote_plus'] ?? 0);
$threadVoteMinus = (int)($thread['vote_minus'] ?? 0);
self::jsonResponse([
"ok" => true,
"threadVotePlus" => $threadVotePlus,
"threadVoteMinus" => $threadVoteMinus,
"userVote" => (int)$userVote
]);
}
public static function voteThread() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$thread_id = intval($_POST['thread_id'] ?? 0);
$vote = intval($_POST['vote'] ?? 0);
if ($thread_id <= 0 || ($vote !== 1 && $vote !== -1)) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Invalid Vote", "message" => "The vote value is invalid."]);
return;
}
Thread::voteThread($thread_id, $_SESSION['user']['id'], $vote);
ViewHelper::redirect(BASE_URL . "thread/show?id=" . $thread_id);
}
public static function ajaxVoteComment() {
if (!self::requireLoginOrJsonError()) return;
$comment_id = intval($_POST['comment_id'] ?? 0);
$thread_id = intval($_POST['thread_id'] ?? 0);
$vote = intval($_POST['vote'] ?? 0);
if ($comment_id <= 0 || $thread_id <= 0 || ($vote !== 1 && $vote !== -1)) {
self::jsonResponse(["ok" => false, "error" => "The vote value is invalid."], 422);
return;
}
Comment::voteComment($comment_id, $_SESSION['user']['id'], $vote);
$userId = $_SESSION['user']['id'];
$userVote = Comment::getCommentVoteByUser($comment_id, $userId);
$comment = Comment::getCommentById($comment_id);
$commentVotePlus = (int)($comment['vote_plus'] ?? 0);
$commentVoteMinus = (int)($comment['vote_minus'] ?? 0);
self::jsonResponse([
"ok" => true,
"commentVotePlus" => $commentVotePlus,
"commentVoteMinus" => $commentVoteMinus,
"userVote" => (int)$userVote
]);
}
public static function voteComment() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$comment_id = intval($_POST['comment_id'] ?? 0);
$thread_id = intval($_POST['thread_id'] ?? 0);
$vote = intval($_POST['vote'] ?? 0);
if ($comment_id <= 0 || $thread_id <= 0 || ($vote !== 1 && $vote !== -1)) {
ViewHelper::render("view/alert.php", ["type" => "danger", "heading" => "Invalid Vote", "message" => "The vote value is invalid."]);
return;
}
Comment::voteComment($comment_id, $_SESSION['user']['id'], $vote);
ViewHelper::redirect(BASE_URL . "thread/show?id=" . $thread_id);
}
public static function create() {
if (!isset($_SESSION["user"])) {
ViewHelper::redirect(BASE_URL . "user/login");
return;
}
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
if ($title === '' || $content === '') {
ViewHelper::render("view/thread/threadAdd.php", ["error" => "Title and content are required.", "title" => $title, "content" => $content]);
return;
}
$user_id = $_SESSION['user']['id'];
$thread = Thread::createThread($title, $content, $user_id);
ViewHelper::redirect(BASE_URL . "thread/show?id=" . $thread['id']);
}
}