|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace litemerafrukt\Comments; |
|
4
|
|
|
|
|
5
|
|
|
use jakezatecky\array_group_by; |
|
6
|
|
|
|
|
7
|
|
|
class CommentHandler |
|
8
|
|
|
{ |
|
9
|
|
|
private $db; |
|
10
|
|
|
private $table; |
|
11
|
|
|
|
|
12
|
10 |
|
public function __construct($db, $table = 'r1_comments') |
|
13
|
|
|
{ |
|
14
|
10 |
|
$this->db = $db; |
|
15
|
10 |
|
$this->table = $table; |
|
16
|
10 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a comment |
|
20
|
|
|
* |
|
21
|
|
|
* @param $postId |
|
22
|
|
|
* @param $parentId - parent comment |
|
23
|
|
|
* @param $authorId - author/user id |
|
24
|
|
|
* @param $authorName - author/user name |
|
25
|
|
|
* @param $text - comment text |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
6 |
|
public function new($postId, $parentId, $authorId, $authorName, $text) |
|
30
|
|
|
{ |
|
31
|
6 |
|
$sql = "INSERT INTO $this->table (postId, parentId, authorId, authorName, `text`) VALUES (?, ?, ?, ?, ?)"; |
|
32
|
6 |
|
$this->db->query($sql, [$postId, $parentId, $authorId, $authorName, $text]); |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get a comment |
|
37
|
|
|
* |
|
38
|
|
|
* @param $id - comment id |
|
39
|
|
|
* |
|
40
|
|
|
* @return array - the comment |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function fetch($id) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$sql = "SELECT * FROM $this->table WHERE id=?"; |
|
45
|
4 |
|
return $this->db->query($sql, [$id])->fetch(\PDO::FETCH_ASSOC); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Update a comments text. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $id - comment id |
|
52
|
|
|
* @param $text - new text |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function upsert($id, $text) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$updated = \date("Y-m-d H:i:s"); |
|
59
|
2 |
|
$sql = "UPDATE $this->table SET `text`=?, updated=? WHERE id=?"; |
|
60
|
2 |
|
$this->db->query($sql, [$text, $updated, $id]); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Delete all comments associated with a post. |
|
65
|
|
|
* |
|
66
|
|
|
* @param $postId |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
4 |
|
public function deleteComments($postId) |
|
71
|
|
|
{ |
|
72
|
4 |
|
$sql = "DELETE FROM $this->table WHERE postId=?"; |
|
73
|
4 |
|
$this->db->query($sql, [$postId]); |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get comments for a post. |
|
78
|
|
|
* |
|
79
|
|
|
* @param $id - post id |
|
80
|
|
|
* |
|
81
|
|
|
* @return array - comments grouped by parent comment |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function commentsForPost($id) |
|
84
|
|
|
{ |
|
85
|
6 |
|
$sql = "SELECT * from $this->table WHERE postId=?"; |
|
86
|
|
|
|
|
87
|
6 |
|
$comments = $this->db->query($sql, [$id]) |
|
88
|
6 |
|
->fetchAll(\PDO::FETCH_ASSOC); |
|
89
|
|
|
|
|
90
|
6 |
|
$groupedComments = array_group_by($comments, 'parentId'); |
|
91
|
|
|
|
|
92
|
6 |
|
return $groupedComments; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|