1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality relating to the news articles admins can post |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A news article |
11
|
|
|
* @package BZiON\Models |
12
|
|
|
*/ |
13
|
|
|
class News extends UrlModel implements NamedModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The category of the article |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $category; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The subject of the news article |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $subject; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The content of the news article |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $content; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The creation date of the news article |
35
|
|
|
* @var TimeDate |
36
|
|
|
*/ |
37
|
|
|
protected $created; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The date the news article was last updated |
41
|
|
|
* @var TimeDate |
42
|
|
|
*/ |
43
|
|
|
protected $updated; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The ID of the author of the news article |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $author; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The ID of the last person to edit the news article |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
protected $editor; |
56
|
|
|
|
57
|
|
|
/** @var bool Whether or not the News item is a draft */ |
58
|
|
|
protected $is_draft; |
59
|
|
|
|
60
|
|
|
const DELETED_COLUMN = 'is_deleted'; |
61
|
|
|
const TABLE = "news"; |
62
|
|
|
|
63
|
|
|
const CREATE_PERMISSION = Permission::CREATE_NEWS; |
64
|
|
|
const EDIT_PERMISSION = Permission::EDIT_NEWS; |
65
|
|
|
const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_NEWS; |
66
|
|
|
const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_NEWS; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
protected function assignResult($news) |
72
|
|
|
{ |
73
|
|
|
$this->category = $news['category']; |
74
|
|
|
$this->subject = $news['subject']; |
75
|
|
|
$this->content = $news['content']; |
76
|
2 |
|
$this->created = TimeDate::fromMysql($news['created']); |
77
|
|
|
$this->updated = TimeDate::fromMysql($news['updated']); |
78
|
2 |
|
$this->author = $news['author']; |
79
|
2 |
|
$this->editor = $news['editor']; |
80
|
2 |
|
$this->is_draft = $news['is_draft']; |
81
|
2 |
|
$this->is_deleted = $news['is_deleted']; |
82
|
2 |
|
} |
83
|
2 |
|
|
84
|
2 |
|
/** |
85
|
2 |
|
* Get the author of the news article |
86
|
2 |
|
* @return Player The author of the post |
87
|
|
|
*/ |
88
|
|
|
public function getAuthor() |
89
|
|
|
{ |
90
|
|
|
return Player::get($this->author); |
91
|
|
|
} |
92
|
1 |
|
|
93
|
|
|
/** |
94
|
1 |
|
* Get the user ID of the author who wrote this article |
95
|
|
|
* @return int The author ID |
96
|
|
|
*/ |
97
|
|
|
public function getAuthorID() |
98
|
|
|
{ |
99
|
|
|
return $this->author; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the category of the news article |
104
|
|
|
* @return NewsCategory The category of the post |
105
|
|
|
*/ |
106
|
|
|
public function getCategory() |
107
|
|
|
{ |
108
|
|
|
return NewsCategory::get($this->category); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the database ID from the category the article belongs into |
113
|
|
|
* @return int The category ID |
114
|
|
|
*/ |
115
|
|
|
public function getCategoryID() |
116
|
|
|
{ |
117
|
|
|
return $this->category; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the content of the article |
122
|
|
|
* @return string The raw content of the article |
123
|
|
|
*/ |
124
|
|
|
public function getContent() |
125
|
|
|
{ |
126
|
|
|
return $this->content; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the time when the article was submitted |
131
|
|
|
* |
132
|
|
|
* @return TimeDate The article's creation time |
133
|
|
|
*/ |
134
|
|
|
public function getCreated() |
135
|
|
|
{ |
136
|
|
|
return $this->created->copy(); |
137
|
|
|
} |
138
|
1 |
|
|
139
|
|
|
/** |
140
|
1 |
|
* Get the time when the article was last updated |
141
|
|
|
* |
142
|
|
|
* @return TimeDate The article's last update time |
143
|
|
|
*/ |
144
|
|
|
public function getLastEdit() |
145
|
|
|
{ |
146
|
|
|
return $this->updated->copy(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the last editor of the post |
151
|
|
|
* @return Player A Player object of the last editor |
152
|
|
|
*/ |
153
|
|
|
public function getLastEditor() |
154
|
|
|
{ |
155
|
|
|
return Player::get($this->editor); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the ID of the person who last edited the article |
160
|
|
|
* @return int The ID of the last editor |
161
|
|
|
*/ |
162
|
|
|
public function getLastEditorID() |
163
|
|
|
{ |
164
|
|
|
return $this->editor; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the subject of the news article |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getSubject() |
172
|
|
|
{ |
173
|
|
|
return $this->subject; |
174
|
|
|
} |
175
|
1 |
|
|
176
|
|
|
/** |
177
|
1 |
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function getName() |
180
|
|
|
{ |
181
|
|
|
return $this->getSubject(); |
182
|
|
|
} |
183
|
1 |
|
|
184
|
|
|
/** |
185
|
1 |
|
* Get whether or not this news article is |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function isDraft() |
190
|
|
|
{ |
191
|
1 |
|
return $this->is_draft; |
192
|
|
|
} |
193
|
1 |
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public static function getParamName() |
198
|
|
|
{ |
199
|
1 |
|
return "article"; |
200
|
|
|
} |
201
|
1 |
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
|
|
public static function getRouteName($action = 'show') |
206
|
|
|
{ |
207
|
|
|
return "news_$action"; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Update the "draft" status of a post |
212
|
|
|
* |
213
|
|
|
* @param bool $draft |
214
|
|
|
* |
215
|
|
|
* @return static |
216
|
|
|
*/ |
217
|
|
|
public function setDraft($draft) |
218
|
|
|
{ |
219
|
|
|
return $this->updateProperty($this->is_draft, 'is_draft', $draft); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Update the content of a post |
224
|
|
|
* |
225
|
|
|
* @param string $content The new content of the post |
226
|
|
|
* |
227
|
|
|
* @return self |
228
|
|
|
*/ |
229
|
|
|
public function updateContent($content) |
230
|
|
|
{ |
231
|
|
|
return $this->updateProperty($this->content, 'content', $content); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Update the last edit timestamp |
236
|
|
|
* @return self |
237
|
|
|
*/ |
238
|
|
|
public function updateEditTimestamp() |
239
|
|
|
{ |
240
|
|
|
return $this->updateProperty($this->updated, 'updated', TimeDate::now()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Update the editor of the post |
245
|
|
|
* |
246
|
|
|
* @param int $editorID The ID of the editor |
247
|
|
|
* @return self |
248
|
|
|
*/ |
249
|
|
|
public function updateLastEditor($editorID) |
250
|
|
|
{ |
251
|
|
|
return $this->updateProperty($this->editor, 'editor', $editorID); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Update the category of the post |
256
|
|
|
* |
257
|
|
|
* @param int $categoryID The ID of the category |
258
|
|
|
* @return self |
259
|
|
|
*/ |
260
|
|
|
public function updateCategory($categoryID) |
261
|
|
|
{ |
262
|
|
|
return $this->updateProperty($this->category, 'category', $categoryID); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Update the status of a post |
267
|
|
|
* |
268
|
|
|
* @param string $status The new status of a post |
269
|
|
|
* @return self |
270
|
|
|
*/ |
271
|
|
|
public function updateStatus($status = 'published') |
272
|
1 |
|
{ |
273
|
|
|
@trigger_error('The `status` column of the News article has been deprecated. Use `is_draft` or `is_deleted`', E_USER_DEPRECATED); |
|
|
|
|
274
|
1 |
|
|
275
|
|
|
return $this->updateProperty($this->status, 'status', $status); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Update the subject of a post |
280
|
|
|
* |
281
|
|
|
* @param string $subject The new subject of a post |
282
|
|
|
* @return self |
283
|
|
|
*/ |
284
|
|
|
public function updateSubject($subject) |
285
|
|
|
{ |
286
|
|
|
return $this->updateProperty($this->subject, 'subject', $subject); |
287
|
|
|
} |
288
|
2 |
|
|
289
|
|
|
/** |
290
|
2 |
|
* Add a new news article |
291
|
2 |
|
* |
292
|
2 |
|
* @param string $subject The subject of the article |
293
|
2 |
|
* @param string $content The content of the article |
294
|
2 |
|
* @param int $authorID The ID of the author |
295
|
2 |
|
* @param int $categoryId The ID of the category this article will be published under |
296
|
2 |
|
* @param bool $is_draft Whether or not the added news item should be stored as a draft |
297
|
2 |
|
* |
298
|
|
|
* @return News An object representing the article that was just created or false if the article was not created |
299
|
|
|
*/ |
300
|
|
|
public static function addNews($subject, $content, $authorID, $categoryId = 1, $is_draft = false) |
301
|
|
|
{ |
302
|
|
|
return self::create([ |
303
|
|
|
'category' => $categoryId, |
304
|
|
|
'subject' => $subject, |
305
|
|
|
'content' => $content, |
306
|
|
|
'author' => $authorID, |
307
|
|
|
'editor' => $authorID, |
308
|
|
|
'is_draft' => $is_draft, |
309
|
1 |
|
], ['created', 'updated']); |
310
|
|
|
} |
311
|
1 |
|
|
312
|
|
|
/** |
313
|
1 |
|
* Get all the news entries in the database that aren't disabled or deleted |
314
|
1 |
|
* |
315
|
|
|
* @param int $start The offset used when fetching matches, i.e. the starting point |
316
|
|
|
* @param int $limit The amount of matches to be retrieved |
317
|
1 |
|
* @param bool $getDrafts Whether or not to fetch drafts |
318
|
1 |
|
* |
319
|
1 |
|
* @throws Exception When a database is not configured in BZiON |
320
|
1 |
|
* |
321
|
|
|
* @return News[] An array of news objects |
322
|
|
|
*/ |
323
|
|
View Code Duplication |
public static function getNews($start = 0, $limit = 5, $getDrafts = false) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
$qb = self::getQueryBuilder() |
326
|
|
|
->limit($limit) |
327
|
|
|
->offset($start) |
328
|
|
|
->orderBy('created', 'DESC') |
329
|
|
|
->active() |
330
|
|
|
; |
331
|
|
|
|
332
|
|
|
if ($getDrafts) { |
333
|
|
|
$qb->orWhere('is_draft', '=', true); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $qb->getModels(true); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Get a query builder for news |
341
|
|
|
* |
342
|
|
|
* @throws Exception |
343
|
|
|
* |
344
|
|
|
* @return QueryBuilderFlex |
345
|
|
|
*/ |
346
|
|
|
public static function getQueryBuilder() |
347
|
|
|
{ |
348
|
|
|
return QueryBuilderFlex::createForModel(News::class) |
|
|
|
|
349
|
|
|
->setNameColumn('subject') |
350
|
|
|
; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: