1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Model; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Sonata\ClassificationBundle\Model\CollectionInterface; |
19
|
|
|
use Sonata\ClassificationBundle\Model\Tag; |
20
|
|
|
use Sonata\ClassificationBundle\Model\TagInterface; |
21
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
22
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
23
|
|
|
|
24
|
|
|
abstract class Post implements PostInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $title; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $slug; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $abstract; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $content; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $rawContent; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $contentFormatter; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Collection|TagInterface[] |
58
|
|
|
*/ |
59
|
|
|
protected $tags; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Collection|CommentInterface[] |
63
|
|
|
*/ |
64
|
|
|
protected $comments; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
protected $enabled; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \DateTime|null |
73
|
|
|
*/ |
74
|
|
|
protected $publicationDateStart; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var \DateTime |
78
|
|
|
*/ |
79
|
|
|
protected $createdAt; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var \DateTime |
83
|
|
|
*/ |
84
|
|
|
protected $updatedAt; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
protected $commentsEnabled = true; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var \DateTime|null |
93
|
|
|
*/ |
94
|
|
|
protected $commentsCloseAt; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
protected $commentsDefaultStatus; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var int |
103
|
|
|
*/ |
104
|
|
|
protected $commentsCount = 0; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var UserInterface|null |
108
|
|
|
*/ |
109
|
|
|
protected $author; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var MediaInterface|null |
113
|
|
|
*/ |
114
|
|
|
protected $image; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var CollectionInterface|null |
118
|
|
|
*/ |
119
|
|
|
protected $collection; |
120
|
|
|
|
121
|
|
|
public function __construct() |
122
|
|
|
{ |
123
|
|
|
$this->setPublicationDateStart(new \DateTime()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function __toString() |
127
|
|
|
{ |
128
|
|
|
return $this->getTitle() ?: 'n/a'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setTitle($title): void |
132
|
|
|
{ |
133
|
|
|
$this->title = $title; |
134
|
|
|
|
135
|
|
|
$this->setSlug(Tag::slugify($title)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getTitle() |
139
|
|
|
{ |
140
|
|
|
return $this->title; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setAbstract($abstract): void |
144
|
|
|
{ |
145
|
|
|
$this->abstract = $abstract; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getAbstract() |
149
|
|
|
{ |
150
|
|
|
return $this->abstract; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setContent($content): void |
154
|
|
|
{ |
155
|
|
|
$this->content = $content; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getContent() |
159
|
|
|
{ |
160
|
|
|
return $this->content; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setEnabled($enabled): void |
164
|
|
|
{ |
165
|
|
|
$this->enabled = $enabled; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getEnabled() |
169
|
|
|
{ |
170
|
|
|
return $this->enabled; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setSlug($slug): void |
174
|
|
|
{ |
175
|
|
|
$this->slug = $slug; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getSlug() |
179
|
|
|
{ |
180
|
|
|
return $this->slug; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function setPublicationDateStart(?\DateTime $publicationDateStart = null): void |
184
|
|
|
{ |
185
|
|
|
$this->publicationDateStart = $publicationDateStart; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getPublicationDateStart() |
189
|
|
|
{ |
190
|
|
|
return $this->publicationDateStart; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function setCreatedAt(?\DateTime $createdAt = null): void |
194
|
|
|
{ |
195
|
|
|
$this->createdAt = $createdAt; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getCreatedAt() |
199
|
|
|
{ |
200
|
|
|
return $this->createdAt; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function setUpdatedAt(?\DateTime $updatedAt = null): void |
204
|
|
|
{ |
205
|
|
|
$this->updatedAt = $updatedAt; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getUpdatedAt() |
209
|
|
|
{ |
210
|
|
|
return $this->updatedAt; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function addComments(CommentInterface $comment): void |
214
|
|
|
{ |
215
|
|
|
$this->comments[] = $comment; |
216
|
|
|
$comment->setPost($this); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function setComments($comments): void |
220
|
|
|
{ |
221
|
|
|
$this->comments = new ArrayCollection(); |
222
|
|
|
|
223
|
|
|
foreach ($this->comments as $comment) { |
224
|
|
|
$this->addComments($comment); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getComments() |
229
|
|
|
{ |
230
|
|
|
return $this->comments; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function addTags(TagInterface $tags): void |
234
|
|
|
{ |
235
|
|
|
$this->tags[] = $tags; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function getTags() |
239
|
|
|
{ |
240
|
|
|
return $this->tags; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function setTags($tags): void |
244
|
|
|
{ |
245
|
|
|
$this->tags = $tags; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function prePersist(): void |
249
|
|
|
{ |
250
|
|
|
if (!$this->getPublicationDateStart()) { |
251
|
|
|
$this->setPublicationDateStart(new \DateTime()); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->setCreatedAt(new \DateTime()); |
255
|
|
|
$this->setUpdatedAt(new \DateTime()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function preUpdate(): void |
259
|
|
|
{ |
260
|
|
|
if (!$this->getPublicationDateStart()) { |
261
|
|
|
$this->setPublicationDateStart(new \DateTime()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$this->setUpdatedAt(new \DateTime()); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getYear() |
268
|
|
|
{ |
269
|
|
|
return $this->getPublicationDateStart()->format('Y'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function getMonth() |
273
|
|
|
{ |
274
|
|
|
return $this->getPublicationDateStart()->format('m'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function getDay() |
278
|
|
|
{ |
279
|
|
|
return $this->getPublicationDateStart()->format('d'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function setCommentsEnabled($commentsEnabled): void |
283
|
|
|
{ |
284
|
|
|
$this->commentsEnabled = $commentsEnabled; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function getCommentsEnabled() |
288
|
|
|
{ |
289
|
|
|
return $this->commentsEnabled; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function setCommentsCloseAt(?\DateTime $commentsCloseAt = null): void |
293
|
|
|
{ |
294
|
|
|
$this->commentsCloseAt = $commentsCloseAt; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function getCommentsCloseAt() |
298
|
|
|
{ |
299
|
|
|
return $this->commentsCloseAt; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function setCommentsDefaultStatus($commentsDefaultStatus): void |
303
|
|
|
{ |
304
|
|
|
$this->commentsDefaultStatus = $commentsDefaultStatus; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function getCommentsDefaultStatus() |
308
|
|
|
{ |
309
|
|
|
return $this->commentsDefaultStatus; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function setCommentsCount($commentsCount): void |
313
|
|
|
{ |
314
|
|
|
$this->commentsCount = $commentsCount; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function getCommentsCount() |
318
|
|
|
{ |
319
|
|
|
return $this->commentsCount; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function isCommentable() |
323
|
|
|
{ |
324
|
|
|
if (!$this->getCommentsEnabled() || !$this->getEnabled()) { |
325
|
|
|
return false; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if ($this->getCommentsCloseAt() instanceof \DateTime) { |
329
|
|
|
return 1 === $this->getCommentsCloseAt()->diff(new \DateTime())->invert ? true : false; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return true; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function isPublic() |
336
|
|
|
{ |
337
|
|
|
if (!$this->getEnabled()) { |
338
|
|
|
return false; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return 0 === $this->getPublicationDateStart()->diff(new \DateTime())->invert ? true : false; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
public function setAuthor($author): void |
345
|
|
|
{ |
346
|
|
|
$this->author = $author; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function getAuthor() |
350
|
|
|
{ |
351
|
|
|
return $this->author; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function setImage($image): void |
355
|
|
|
{ |
356
|
|
|
$this->image = $image; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function getImage() |
360
|
|
|
{ |
361
|
|
|
return $this->image; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function setCollection(?CollectionInterface $collection = null): void |
365
|
|
|
{ |
366
|
|
|
$this->collection = $collection; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function getCollection() |
370
|
|
|
{ |
371
|
|
|
return $this->collection; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param $contentFormatter |
376
|
|
|
*/ |
377
|
|
|
public function setContentFormatter($contentFormatter): void |
378
|
|
|
{ |
379
|
|
|
$this->contentFormatter = $contentFormatter; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function getContentFormatter() |
383
|
|
|
{ |
384
|
|
|
return $this->contentFormatter; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function setRawContent($rawContent): void |
388
|
|
|
{ |
389
|
|
|
$this->rawContent = $rawContent; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function getRawContent() |
393
|
|
|
{ |
394
|
|
|
return $this->rawContent; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|