|
1
|
|
|
<?php |
|
2
|
|
|
namespace RbComment\Model; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\Db\Sql\Select; |
|
5
|
|
|
use Zend\Db\Sql\Expression; |
|
6
|
|
|
use Zend\Db\TableGateway\TableGateway; |
|
7
|
|
|
|
|
8
|
|
|
class CommentTable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var TableGateway |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $tableGateway; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(TableGateway $tableGateway) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->tableGateway = $tableGateway; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns all the comments. |
|
22
|
|
|
* |
|
23
|
|
|
* @return ResultSet |
|
24
|
|
|
*/ |
|
25
|
|
|
public function fetchAll() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->tableGateway->select(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns all the comments of a thread. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $thread |
|
34
|
|
|
* |
|
35
|
|
|
* @return ResultSet |
|
36
|
|
|
*/ |
|
37
|
|
|
public function fetchAllForThread($thread) |
|
38
|
|
|
{ |
|
39
|
|
|
$columns = [ |
|
40
|
|
|
'id', |
|
41
|
|
|
'author', |
|
42
|
|
|
'content', |
|
43
|
|
|
'contact', |
|
44
|
|
|
'published_on_raw' => 'published_on', |
|
45
|
|
|
'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$select = new Select($this->tableGateway->getTable()); |
|
49
|
|
|
$select->columns($columns) |
|
50
|
|
|
->where(['thread' => $thread, 'visible' => 1]) |
|
51
|
|
|
->order('published_on_raw ASC'); |
|
52
|
|
|
|
|
53
|
|
|
$resultSet = $this->tableGateway->selectWith($select); |
|
54
|
|
|
|
|
55
|
|
|
return $resultSet; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns all the comments pending approval for a thread. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $thread |
|
62
|
|
|
* |
|
63
|
|
|
* @return ResultSet |
|
64
|
|
|
*/ |
|
65
|
|
|
public function fetchAllPendingForThread($thread) |
|
66
|
|
|
{ |
|
67
|
|
|
$columns = [ |
|
68
|
|
|
'id', |
|
69
|
|
|
'author', |
|
70
|
|
|
'content', |
|
71
|
|
|
'contact', |
|
72
|
|
|
'published_on_raw' => 'published_on', |
|
73
|
|
|
'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$select = new Select($this->tableGateway->getTable()); |
|
77
|
|
|
$select->columns($columns) |
|
78
|
|
|
->where(['thread' => $thread, 'visible' => 0]) |
|
79
|
|
|
->order('published_on_raw ASC'); |
|
80
|
|
|
|
|
81
|
|
|
$resultSet = $this->tableGateway->selectWith($select); |
|
82
|
|
|
|
|
83
|
|
|
return $resultSet; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Allow custom comments selection. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Select $select |
|
90
|
|
|
* |
|
91
|
|
|
* @return ResultSet |
|
92
|
|
|
*/ |
|
93
|
|
|
public function fetchAllUsingSelect(Select $select) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->tableGateway->selectWith($select); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns a comment by id. |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $id |
|
102
|
|
|
* |
|
103
|
|
|
* @return Comment |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getComment($id) |
|
106
|
|
|
{ |
|
107
|
|
|
$id = (int)$id; |
|
108
|
|
|
$rowset = $this->tableGateway->select(['id' => $id]); |
|
109
|
|
|
$row = $rowset->current(); |
|
110
|
|
|
|
|
111
|
|
|
return $row; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Saves a comment into the database. |
|
116
|
|
|
* |
|
117
|
|
|
* @param Comment $comment |
|
118
|
|
|
* |
|
119
|
|
|
* @return int The id of the inserted/updated comment |
|
120
|
|
|
*/ |
|
121
|
|
|
public function saveComment(Comment $comment) |
|
122
|
|
|
{ |
|
123
|
|
|
$data = [ |
|
124
|
|
|
'thread' => $comment->thread, |
|
125
|
|
|
'uri' => $comment->uri, |
|
126
|
|
|
'author' => $comment->author, |
|
127
|
|
|
'contact' => $comment->contact, |
|
128
|
|
|
'content' => $comment->content, |
|
129
|
|
|
'visible' => $comment->visible, |
|
130
|
|
|
'spam' => $comment->spam, |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
$id = (int)$comment->id; |
|
134
|
|
|
if ($id === 0) { |
|
135
|
|
|
$this->tableGateway->insert($data); |
|
136
|
|
|
$id = $this->tableGateway->lastInsertValue; |
|
137
|
|
|
} else { |
|
138
|
|
|
if ($this->getComment($id)) { |
|
139
|
|
|
$this->tableGateway->update($data, ['id' => $id]); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $id; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Removes a comment from the database. |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $id |
|
150
|
|
|
*/ |
|
151
|
|
|
public function deleteComment($id) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->tableGateway->delete(['id' => $id]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Deletes all comments maked as spam from the database. |
|
159
|
|
|
* |
|
160
|
|
|
* @return int - number of rows affected. |
|
161
|
|
|
*/ |
|
162
|
|
|
public function deleteSpam() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->tableGateway->delete(['spam' => 1]); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|