/
Topic.php
344 lines (303 loc) · 11.2 KB
/
Topic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @copyright Ilch 2.0
* @package ilch
*/
namespace Modules\Forum\Mappers;
use Modules\Forum\Models\ForumTopic as TopicModel;
use Modules\User\Mappers\User as UserMapper;
use Modules\Forum\Models\ForumPost as PostModel;
use Modules\Forum\Mappers\Post as PostMapper;
use Modules\Forum\Config\Config as ForumConfig;
class Topic extends \Ilch\Mapper
{
public function getTopicsByForumId($id, $pagination = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
WHERE forum_id = '.$id.'
GROUP by type, `id`, `topic_id`, `topic_prefix`, `topic_title`, `visits`, `creator_id`, `date_created`, `forum_id`, `status`
ORDER by type DESC, id DESC';
if (!empty($pagination)) {
$sql .= ' LIMIT '.implode(',',$pagination->getLimit());
$fileArray = $this->db()->queryArray($sql);
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
} else {
$fileArray = $this->db()->queryArray($sql);
}
$entry = [];
$user = null;
$dummyUser = null;
$userCache = [];
foreach ($fileArray as $entries) {
$entryModel = new TopicModel();
$userMapper = new UserMapper();
$entryModel->setId($entries['id']);
$entryModel->setTopicId($id);
$entryModel->setVisits($entries['visits']);
$entryModel->setType($entries['type']);
$entryModel->setStatus($entries['status']);
if (!array_key_exists($entries['creator_id'], $userCache)) {
$user = $userMapper->getUserById($entries['creator_id']);
if ($user) {
$userCache[$entries['creator_id']] = $user;
$entryModel->setAuthor($user);
} else {
if (!$dummyUser) {
$dummyUser = $userMapper->getDummyUser();
}
$entryModel->setAuthor($dummyUser);
}
} else {
$entryModel->setAuthor($userCache[$entries['creator_id']]);
}
$entryModel->setTopicPrefix($entries['topic_prefix']);
$entryModel->setTopicTitle($entries['topic_title']);
$entryModel->setDateCreated($entries['date_created']);
$entry[] = $entryModel;
}
return $entry;
}
public function getTopics($pagination = NULL, $limit = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
GROUP by type, `id`, `topic_id`, `topic_prefix`, `topic_title`, `visits`, `creator_id`, `date_created`, `forum_id`, `status`
ORDER by type DESC, id DESC';
if ($pagination != null) {
$sql .= ' LIMIT '.implode(',',$pagination->getLimit());
} elseif ($limit != null) {
$sql .= ' LIMIT '.$limit;
}
$fileArray = $this->db()->queryArray($sql);
if ($pagination != null) {
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
}
$entry = [];
$user = null;
$dummyUser = null;
$userCache = [];
foreach ($fileArray as $entries) {
$entryModel = new TopicModel();
$userMapper = new UserMapper();
$entryModel->setId($entries['id']);
$entryModel->setForumId($entries['forum_id']);
$entryModel->setTopicId($entries['topic_id']);
$entryModel->setVisits($entries['visits']);
$entryModel->setType($entries['type']);
$entryModel->setStatus($entries['status']);
if (!array_key_exists($entries['creator_id'], $userCache)) {
$user = $userMapper->getUserById($entries['creator_id']);
if ($user) {
$userCache[$entries['creator_id']] = $user;
$entryModel->setAuthor($user);
} else {
if (!$dummyUser) {
$dummyUser = $userMapper->getDummyUser();
}
$entryModel->setAuthor($dummyUser);
}
} else {
$entryModel->setAuthor($userCache[$entries['creator_id']]);
}
$entryModel->setTopicPrefix($entries['topic_prefix']);
$entryModel->setTopicTitle($entries['topic_title']);
$entryModel->setDateCreated($entries['date_created']);
$entry[] = $entryModel;
}
return $entry;
}
public function getPostById($id)
{
$sql = 'SELECT *
FROM `[prefix]_forum_topics`
WHERE id = '.$id;
$fileRow = $this->db()->queryRow($sql);
$entryModel = new TopicModel();
$userMapper = new UserMapper();
$entryModel->setId($fileRow['id']);
$entryModel->setTopicPrefix($fileRow['topic_prefix']);
$entryModel->setTopicTitle($fileRow['topic_title']);
$entryModel->setCreatorId($fileRow['creator_id']);
$entryModel->setVisits($fileRow['visits']);
$user = $userMapper->getUserById($fileRow['creator_id']);
if ($user) {
$entryModel->setAuthor($user);
} else {
$entryModel->setAuthor($userMapper->getDummyUser());
}
$entryModel->setDateCreated($fileRow['date_created']);
$entryModel->setStatus($fileRow['status']);
return $entryModel;
}
public function getLastPostByTopicId($id)
{
$sql = 'SELECT p.id, p.topic_id, p.date_created, p.user_id, p.read
FROM [prefix]_forum_posts as p
WHERE p.topic_id = '.$id.'
ORDER BY p.date_created DESC';
$fileRow = $this->db()->queryRow($sql);
if (empty($fileRow)) {
return null;
}
$entryModel = new PostModel();
$userMapper = new UserMapper();
$entryModel->setId($fileRow['id']);
$user = $userMapper->getUserById($fileRow['user_id']);
if ($user) {
$entryModel->setAutor($user);
} else {
$entryModel->setAutor($userMapper->getDummyUser());
}
$entryModel->setDateCreated($fileRow['date_created']);
$entryModel->setTopicId($fileRow['topic_id']);
$entryModel->setRead($fileRow['read']);
return $entryModel;
}
/**
* Inserts or updates File entry.
*
* @param TopicModel $model
*/
public function save(TopicModel $model)
{
if ($model->getId()) {
$this->db()->update('forum_topics')
->values(['topic_id' => $model->getTopicId(), 'forum_id' => $model->getForumId()])
->where(['id' => $model->getId()])
->execute();
} else {
$this->db()->insert('forum_topics')
->values([
'topic_prefix' => $model->getTopicPrefix(),
'topic_title' => $model->getTopicTitle(),
'topic_id' => $model->getTopicId(),
'forum_id' => $model->getForumId(),
'creator_id' => $model->getCreatorId(),
'type' => $model->getType(),
'date_created' => $model->getDateCreated()
])
->execute();
$this->last_insert_id = $this->db()->getLastInsertId();
}
}
/**
* Updates topic status with given id.
*
* @param integer $id
*/
public function updateStatus($id)
{
$status = (int) $this->db()->select('status')
->from('forum_topics')
->where(['id' => $id])
->execute()
->fetchCell();
$this->db()->update('forum_topics')
->values(['status' => !$status])
->where(['id' => $id])
->execute();
}
/**
* Updates topic type with given id.
*
* @param integer $id
*/
public function updateType($id)
{
$type = (int) $this->db()->select('type')
->from('forum_topics')
->where(['id' => $id])
->execute()
->fetchCell();
$this->db()->update('forum_topics')
->values(['type' => !$type])
->where(['id' => $id])
->execute();
}
/**
* Update specific column of a topic
*
* @param $id
* @param $column
* @param $value
*/
public function update($id, $column, $value) {
$this->db()->update('forum_topics')
->values([$column => $value])
->where(['id' => $id])
->execute();
}
public function getLastInsertId()
{
return $this->last_insert_id;
}
public function getPostByTopicId($id, $pagination = NULL)
{
$sql = 'SELECT SQL_CALC_FOUND_ROWS *
FROM `[prefix]_forum_topics`
WHERE topic_id = '.$id.'
LIMIT '.implode(',',$pagination->getLimit());
$fileArray = $this->db()->queryArray($sql);
$pagination->setRows($this->db()->querycell('SELECT FOUND_ROWS()'));
$entry = [];
foreach ($fileArray as $entries) {
$entryModel = new TopicModel();
$entryModel->setId($entries['id']);
$entryModel->setTopicId($id);
$entryModel->setTopicTitle($entries['topic_title']);
$entry[] = $entryModel;
}
return $entry;
}
/**
* Get x topics with latest activity where x is specified by the limit.
*
* @param null|integer $limit
* @return array[]
*/
public function getLastActiveTopics($limit = null)
{
$sql = 'SELECT *
FROM
( SELECT DISTINCT(`p`.`topic_id`),`t`.`topic_title` AS `topic_title`,`t`.`forum_id` AS `forum_id`,`p`.`date_created`
FROM `ilch_forum_posts` AS `p`
LEFT JOIN `ilch_forum_topics` AS `t` ON `p`.`topic_id` = `t`.`id`
ORDER BY `p`.`date_created` DESC
) AS `innerfrom`
GROUP BY `innerfrom`.`topic_id`
ORDER BY `innerfrom`.`date_created` DESC';
if ($limit !== null) {
$sql .= 'LIMIT '.$limit;
}
$result = $this->db()->queryArray($sql);
return $result;
}
public function deleteById($id)
{
$this->trigger(ForumConfig::EVENT_DELETETOPIC_BEFORE, ['id' => $id]);
$postMapper = new PostMapper();
$posts = $postMapper->getPostsByTopicId($id);
foreach ($posts as $post) {
$postMapper->deleteById($post->getId());
}
$this->db()->delete('forum_topics')
->where(['id' => $id])
->execute();
$this->trigger(ForumConfig::EVENT_DELETETOPIC_AFTER, ['id' => $id]);
}
/**
* Updates visits.
*
* @param TopicModel $model
*/
public function saveVisits(TopicModel $model)
{
if ($model->getVisits()) {
$this->db()->update('forum_topics')
->values(['visits' => $model->getVisits()])
->where(['id' => $model->getId()])
->execute();
}
}
}