|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Model; |
|
6
|
|
|
use Core\Container; |
|
7
|
|
|
use Core\Constant; |
|
8
|
|
|
use HTMLPurifier; |
|
9
|
|
|
use HTMLPurifier_Config; |
|
10
|
|
|
|
|
11
|
|
|
class CommentModel extends Model{ |
|
12
|
|
|
|
|
13
|
|
|
private $commentTbl; |
|
14
|
|
|
private $userTbl; |
|
15
|
|
|
private $postTbl; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Container $container) |
|
18
|
|
|
{ |
|
19
|
|
|
parent::__construct($container); |
|
20
|
|
|
|
|
21
|
|
|
$this->commentTbl = $this->getTablePrefix("comments"); |
|
22
|
|
|
$this->userTbl = $this->getTablePrefix("users"); |
|
23
|
|
|
$this->postTbl = $this->getTablePrefix("posts"); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* the base Select SQl |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
private function baseSql() |
|
31
|
|
|
{ |
|
32
|
|
|
$sql = " |
|
33
|
|
|
SELECT idcomments, users_idusers, posts_idposts, comment, approved, idposts, title, posts_slug, idusers, username, avatar |
|
34
|
|
|
FROM $this->commentTbl |
|
35
|
|
|
LEFT JOIN $this->postTbl ON $this->commentTbl.posts_idposts = $this->postTbl.idposts |
|
36
|
|
|
LEFT JOIN $this->userTbl ON $this->commentTbl.users_idusers = $this->userTbl.idusers |
|
37
|
|
|
"; |
|
38
|
|
|
return $sql; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* secure the HTML thanks to HTML Purifier |
|
43
|
|
|
* @param $dirtyHtml |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
private function purifyHtml($dirtyHtml) |
|
47
|
|
|
{ |
|
48
|
|
|
$config = HTMLPurifier_Config::createDefault(); |
|
49
|
|
|
$purifier = new HTMLPurifier($config); |
|
50
|
|
|
return $purifier->purify($dirtyHtml); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Count the number of comments on a post |
|
55
|
|
|
* @param int $postId |
|
56
|
|
|
* @return int |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
public function countCommentsOnPost(int $postId): int |
|
60
|
|
|
{ |
|
61
|
|
|
$sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId"; |
|
62
|
|
|
$this->query($sql); |
|
63
|
|
|
$this->bind(":postId", $postId); |
|
64
|
|
|
$this->execute(); |
|
65
|
|
|
return $this->stmt->fetchColumn(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* the list of comments on a post with limit and offset |
|
70
|
|
|
* @param int $postId |
|
71
|
|
|
* @param int $offset |
|
72
|
|
|
* @param int $limit |
|
73
|
|
|
* @return bool |
|
74
|
|
|
* @throws \Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE) |
|
77
|
|
|
{ |
|
78
|
|
|
$sql = $this->baseSql(); |
|
79
|
|
|
$sql .= " |
|
80
|
|
|
WHERE approved = 1 |
|
81
|
|
|
AND posts_idposts = :postId |
|
82
|
|
|
LIMIT :limit OFFSET :offset |
|
83
|
|
|
"; |
|
84
|
|
|
|
|
85
|
|
|
$this->query($sql); |
|
86
|
|
|
$this->bind(":limit", $limit); |
|
87
|
|
|
$this->bind(":offset", $offset); |
|
88
|
|
|
$this->bind(":postId", $postId); |
|
89
|
|
|
$this->execute(); |
|
90
|
|
|
return $this->fetchAll(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* count the number of pending comments |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
* @throws \Exception |
|
97
|
|
|
*/ |
|
98
|
|
|
public function countPendingComments() |
|
99
|
|
|
{ |
|
100
|
|
|
$sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0"; |
|
101
|
|
|
$this->query($sql); |
|
102
|
|
|
$this->execute(); |
|
103
|
|
|
return $this->stmt->fetchColumn(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* get the list of pending comments with limit and offset |
|
108
|
|
|
* @param int $offset |
|
109
|
|
|
* @param int $limit |
|
110
|
|
|
* @return bool |
|
111
|
|
|
* @throws \Exception |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE) |
|
114
|
|
|
{ |
|
115
|
|
|
$sql = $this->baseSql(); |
|
116
|
|
|
$sql .= " |
|
117
|
|
|
WHERE approved = 0 |
|
118
|
|
|
LIMIT :limit OFFSET :offset |
|
119
|
|
|
"; |
|
120
|
|
|
$this->query($sql); |
|
121
|
|
|
$this->bind(":limit", $limit); |
|
122
|
|
|
$this->bind(":offset", $offset); |
|
123
|
|
|
$this->execute(); |
|
124
|
|
|
|
|
125
|
|
|
return $this->fetchAll(); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function countComments(): int |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->count($this->commentTbl); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
public function getCommentsList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE) |
|
135
|
|
|
{ |
|
136
|
|
|
$sql = $this->baseSql(); |
|
137
|
|
|
$sql .= " |
|
138
|
|
|
LIMIT :limit OFFSET :offset |
|
139
|
|
|
"; |
|
140
|
|
|
$this->query($sql); |
|
141
|
|
|
$this->bind(":limit", $limit); |
|
142
|
|
|
$this->bind(":offset", $offset); |
|
143
|
|
|
$this->execute(); |
|
144
|
|
|
|
|
145
|
|
|
return $this->fetchAll(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Add a comment to a post |
|
150
|
|
|
* @param int $postId |
|
151
|
|
|
* @param int $userId |
|
152
|
|
|
* @param string $comment |
|
153
|
|
|
* @param bool $admin |
|
154
|
|
|
* @return int |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
*/ |
|
157
|
|
|
public function addComment(int $postId, int $userId, string $comment, bool $admin=false):int |
|
158
|
|
|
{ |
|
159
|
|
|
$comment = $this->purifyHtml($comment); |
|
160
|
|
|
$sql=" |
|
161
|
|
|
INSERT INTO $this->commentTbl (users_idusers, posts_idposts, comment, approved) |
|
162
|
|
|
VALUES (:userId, :postId, :comment, :approved) |
|
163
|
|
|
"; |
|
164
|
|
|
$this->query($sql); |
|
165
|
|
|
$this->bind(':userId', $userId); |
|
166
|
|
|
$this->bind(':postId', $postId); |
|
167
|
|
|
$this->bind(':comment', $comment); |
|
168
|
|
|
$this->bind(':approved', $admin); |
|
169
|
|
|
|
|
170
|
|
|
$this->execute(); |
|
171
|
|
|
return (int)$this->dbh->lastInsertId(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* delete a comment by it's ID |
|
176
|
|
|
* @param int $commentId |
|
177
|
|
|
* @return bool |
|
178
|
|
|
* @throws \Exception |
|
179
|
|
|
*/ |
|
180
|
|
|
public function delete(int $commentId) |
|
181
|
|
|
{ |
|
182
|
|
|
$sql = " |
|
183
|
|
|
DELETE FROM $this->commentTbl |
|
184
|
|
|
WHERE idcomments = :commentId |
|
185
|
|
|
"; |
|
186
|
|
|
$this->query($sql); |
|
187
|
|
|
$this->bind(":commentId", $commentId); |
|
188
|
|
|
return $this->execute(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Update an existing comment |
|
193
|
|
|
* @param int $commentId |
|
194
|
|
|
* @param string $comment |
|
195
|
|
|
* @param bool $approved |
|
196
|
|
|
* @return bool |
|
197
|
|
|
* @throws \Exception |
|
198
|
|
|
*/ |
|
199
|
|
|
public function update(int $commentId, string $comment, bool $approved) |
|
200
|
|
|
{ |
|
201
|
|
|
|
|
202
|
|
|
$comment = $this->purifyHtml($comment); |
|
203
|
|
|
|
|
204
|
|
|
$sql=" |
|
205
|
|
|
UPDATE $this->commentTbl |
|
206
|
|
|
SET |
|
207
|
|
|
comment = :comment, |
|
208
|
|
|
approved = :state |
|
209
|
|
|
WHERE |
|
210
|
|
|
idcomments = :commentId |
|
211
|
|
|
"; |
|
212
|
|
|
|
|
213
|
|
|
$this->query($sql); |
|
214
|
|
|
$this->bind(":commentId", $commentId); |
|
215
|
|
|
$this->bind(":comment", $comment); |
|
216
|
|
|
$this->bind(":state", $approved); |
|
217
|
|
|
return $this->execute(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* get a comment from it's ID |
|
222
|
|
|
* @param int $commentId |
|
223
|
|
|
* @return mixed |
|
224
|
|
|
* @throws \Exception |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getCommentById(int $commentId) |
|
227
|
|
|
{ |
|
228
|
|
|
$sql = $this->baseSql(); |
|
229
|
|
|
$sql .= " |
|
230
|
|
|
WHERE idcomments = :commentId |
|
231
|
|
|
"; |
|
232
|
|
|
$this->query($sql); |
|
233
|
|
|
$this->bind(':commentId', $commentId); |
|
234
|
|
|
$this->execute(); |
|
235
|
|
|
|
|
236
|
|
|
return $this->fetch(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Set the approved state |
|
241
|
|
|
* @param bool $state |
|
242
|
|
|
* @param int $commentId |
|
243
|
|
|
* @return bool |
|
244
|
|
|
* @throws \Exception |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setApproved(bool $state, int $commentId):bool |
|
247
|
|
|
{ |
|
248
|
|
|
$sql = " |
|
249
|
|
|
UPDATE $this->commentTbl |
|
250
|
|
|
SET |
|
251
|
|
|
approved = :state |
|
252
|
|
|
WHERE |
|
253
|
|
|
idcomments = :commentId |
|
254
|
|
|
"; |
|
255
|
|
|
$this->query($sql); |
|
256
|
|
|
$this->bind(":commentId", $commentId); |
|
257
|
|
|
$this->bind(":state", $state); |
|
258
|
|
|
return $this->execute(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
} |