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