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
|
|
|
|
13
|
|
|
public function __construct(Container $container) |
14
|
|
|
{ |
15
|
|
|
parent::__construct($container); |
16
|
|
|
|
17
|
|
|
$this->commentTbl = $this->getTablePrefix("comments"); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Count the number of comments on a post |
22
|
|
|
* @param int $postId |
23
|
|
|
* @return int |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function countCommentsOnPost(int $postId): int |
27
|
|
|
{ |
28
|
|
|
$sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId"; |
29
|
|
|
$this->query($sql); |
30
|
|
|
$this->bind(":postId", $postId); |
31
|
|
|
$this->execute(); |
32
|
|
|
return $this->stmt->fetchColumn(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* the list of comments on a post with limit and offset |
37
|
|
|
* @param int $postId |
38
|
|
|
* @param int $offset |
39
|
|
|
* @param int $limit |
40
|
|
|
* @return bool |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE) |
44
|
|
|
{ |
45
|
|
|
$sql = " |
46
|
|
|
SELECT * FROM $this->commentTbl |
47
|
|
|
WHERE approved = 1 |
48
|
|
|
AND posts_idposts = :postId |
49
|
|
|
LIMIT :limit OFFSET :offset |
50
|
|
|
"; |
51
|
|
|
|
52
|
|
|
$this->query($sql); |
53
|
|
|
$this->bind(":limit", $limit); |
54
|
|
|
$this->bind(":offset", $offset); |
55
|
|
|
$this->bind(":postId", $postId); |
56
|
|
|
return $this->execute(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* count the number of pending comments |
61
|
|
|
* @return mixed |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public function countPendingComments() |
65
|
|
|
{ |
66
|
|
|
$sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0"; |
67
|
|
|
$this->query($sql); |
68
|
|
|
$this->execute(); |
69
|
|
|
return $this->stmt->fetchColumn(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* get the list of pending comments with limit and offset |
74
|
|
|
* @param int $offset |
75
|
|
|
* @param int $limit |
76
|
|
|
* @return bool |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE) |
80
|
|
|
{ |
81
|
|
|
$sql = " |
82
|
|
|
SELECT * FROM $this->commentTbl |
83
|
|
|
WHERE approved = 0 |
84
|
|
|
LIMIT :limit OFFSET :offset |
85
|
|
|
"; |
86
|
|
|
$this->query($sql); |
87
|
|
|
$this->bind(":limit", $limit); |
88
|
|
|
$this->bind(":offset", $offset); |
89
|
|
|
return $this->execute(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |