|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Cocur\Slugify\Slugify; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Post Entity |
|
12
|
|
|
* |
|
13
|
|
|
* @ORM\Table(name="posts") |
|
14
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\PostRepository") |
|
15
|
|
|
* @ORM\HasLifecycleCallbacks() |
|
16
|
|
|
* |
|
17
|
|
|
* @author Borut Balažek <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PostEntity |
|
20
|
|
|
extends AbstractImageUpload |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var integer |
|
24
|
|
|
* |
|
25
|
|
|
* @ORM\Column(name="id", type="integer") |
|
26
|
|
|
* @ORM\Id |
|
27
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
* |
|
34
|
|
|
* @ORM\Column(name="title", type="string", length=255) |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $title; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
* |
|
41
|
|
|
* @ORM\Column(name="content", type="text", nullable=true) |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $content; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
* |
|
48
|
|
|
* @ORM\Column(name="image_url", type="text", nullable=true) |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $imageUrl; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \DateTime |
|
54
|
|
|
* |
|
55
|
|
|
* @ORM\Column(name="time_created", type="datetime") |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $timeCreated; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var \DateTime |
|
61
|
|
|
* |
|
62
|
|
|
* @ORM\Column(name="time_updated", type="datetime") |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $timeUpdated; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @ORM\ManyToOne(targetEntity="Application\Entity\UserEntity", inversedBy="posts") |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $user; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var Doctrine\Common\Collections\ArrayCollection |
|
73
|
|
|
* |
|
74
|
|
|
* @ORM\OneToMany(targetEntity="Application\Entity\PostMetaEntity", mappedBy="post", cascade={"all"}) |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $postMetas; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Helper for metas |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $metas; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Constructor |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->postMetas = new ArrayCollection(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/*** Id ***/ |
|
92
|
|
|
/** |
|
93
|
|
|
* @return integer |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getId() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->id; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param integer $id |
|
102
|
|
|
* |
|
103
|
|
|
* @return PostEntity |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setId($id) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->id = $id; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/*** Title ***/ |
|
113
|
|
|
/** |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getTitle() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->title; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $title |
|
123
|
|
|
* |
|
124
|
|
|
* @return PostEntity |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setTitle($title) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->title = $title; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/*** Content ***/ |
|
134
|
|
|
/** |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getContent() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->content; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $content |
|
144
|
|
|
* |
|
145
|
|
|
* @return PostEntity |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setContent($content) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->content = $content; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/*** Time created ***/ |
|
155
|
|
|
/** |
|
156
|
|
|
* @return \DateTime |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getTimeCreated() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->timeCreated; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param \DateTime $timeCreated |
|
165
|
|
|
* |
|
166
|
|
|
* @return PostEntity |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setTimeCreated(\DateTime $timeCreated) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->timeCreated = $timeCreated; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/*** Time updated ***/ |
|
176
|
|
|
/** |
|
177
|
|
|
* @return \DateTime |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getTimeUpdated() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->timeUpdated; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param \DateTime $timeUpdated |
|
186
|
|
|
* |
|
187
|
|
|
* @return PostEntity |
|
188
|
|
|
*/ |
|
189
|
|
|
public function setTimeUpdated(\DateTime $timeUpdated) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->timeUpdated = $timeUpdated; |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/*** User ***/ |
|
197
|
|
|
/** |
|
198
|
|
|
* @return UserEntity |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getUser() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->user; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param UserEntity $user |
|
207
|
|
|
* |
|
208
|
|
|
* @return PostEntity |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setUser(UserEntity $user = null) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->user = $user; |
|
213
|
|
|
|
|
214
|
|
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/*** Post Metas ***/ |
|
218
|
|
|
/** |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getPostMetas() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->postMetas->toArray(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param array $postMetas |
|
228
|
|
|
* |
|
229
|
|
|
* @return PostEntity |
|
230
|
|
|
*/ |
|
231
|
|
|
public function setPostMetas($postMetas) |
|
232
|
|
|
{ |
|
233
|
|
|
if ($postMetas) { |
|
|
|
|
|
|
234
|
|
|
foreach ($postMetas as $postMeta) { |
|
235
|
|
|
$postMeta->setPost($this); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$this->postMetas = $postMetas; |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param PostMetaEntity $postMeta |
|
246
|
|
|
* |
|
247
|
|
|
* @return PostEntity |
|
248
|
|
|
*/ |
|
249
|
|
|
public function addPostMeta(PostMetaEntity $postMeta) |
|
250
|
|
|
{ |
|
251
|
|
|
if (!$this->postMetas->contains($postMeta)) { |
|
252
|
|
|
$postMeta->setPost($this); |
|
253
|
|
|
$this->postMetas->add($postMeta); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param PostMetaEntity $postMeta |
|
261
|
|
|
* |
|
262
|
|
|
* @return PostEntity |
|
263
|
|
|
*/ |
|
264
|
|
|
public function removePostMeta(PostMetaEntity $postMeta) |
|
265
|
|
|
{ |
|
266
|
|
|
$postMeta->setPost(null); |
|
|
|
|
|
|
267
|
|
|
$this->postMetas->removeElement($postMeta); |
|
268
|
|
|
|
|
269
|
|
|
return $this; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/*** Metas ***/ |
|
273
|
|
|
/** |
|
274
|
|
|
* @param $key |
|
275
|
|
|
* |
|
276
|
|
|
* @return array|null |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getMetas($key = null) |
|
279
|
|
|
{ |
|
280
|
|
|
return $key |
|
281
|
|
|
? (isset($this->metas[$key]) |
|
282
|
|
|
? $this->metas[$key] |
|
283
|
|
|
: null) |
|
284
|
|
|
: $this->metas |
|
285
|
|
|
; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return PostEntity |
|
290
|
|
|
*/ |
|
291
|
|
|
public function setMetas($metas) |
|
292
|
|
|
{ |
|
293
|
|
|
$this->metas = $metas; |
|
294
|
|
|
|
|
295
|
|
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @return void |
|
300
|
|
|
*/ |
|
301
|
|
|
public function hydratePostMetas() |
|
302
|
|
|
{ |
|
303
|
|
|
$postMetas = $this->getPostMetas(); |
|
304
|
|
|
|
|
305
|
|
|
if (count($postMetas)) { |
|
306
|
|
|
$metas = array(); |
|
307
|
|
|
|
|
308
|
|
|
foreach ($postMetas as $postMeta) { |
|
309
|
|
|
$metas[$postMeta->getKey()] = $postMeta->getValue(); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$this->setMetas($metas); |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @return void |
|
318
|
|
|
*/ |
|
319
|
|
|
public function convertMetasToPostMetas($uploadPath, $uploadDir) |
|
320
|
|
|
{ |
|
321
|
|
|
$slugify = new Slugify(); |
|
322
|
|
|
$metas = $this->getMetas(); |
|
323
|
|
|
|
|
324
|
|
|
if (!empty($metas)) { |
|
325
|
|
|
foreach ($metas as $metaKey => $metaValue) { |
|
326
|
|
|
$metaEntity = new PostMetaEntity(); |
|
327
|
|
|
|
|
328
|
|
|
// Check if it's a file! |
|
329
|
|
|
if ($metaValue instanceof UploadedFile) { |
|
330
|
|
|
$filename = $slugify->slugify( |
|
331
|
|
|
$metaValue->getClientOriginalName() |
|
332
|
|
|
); |
|
333
|
|
|
$filename .= '_'.sha1(uniqid(mt_rand(), true)).'.'. |
|
334
|
|
|
$metaValue->guessExtension() |
|
335
|
|
|
; |
|
336
|
|
|
$metaValue->move( |
|
337
|
|
|
$uploadDir, |
|
338
|
|
|
$filename |
|
339
|
|
|
); |
|
340
|
|
|
$metaValue = $uploadPath.$filename; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$metaEntity |
|
344
|
|
|
->setKey($metaKey) |
|
345
|
|
|
->setValue($metaValue) |
|
346
|
|
|
; |
|
347
|
|
|
$this |
|
348
|
|
|
->addPostMeta($metaEntity) |
|
349
|
|
|
; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @return array |
|
356
|
|
|
*/ |
|
357
|
|
|
public function toArray() |
|
358
|
|
|
{ |
|
359
|
|
|
return array( |
|
360
|
|
|
'id' => $this->getId(), |
|
361
|
|
|
'title' => $this->getTitle(), |
|
362
|
|
|
'content' => $this->getContent(), |
|
363
|
|
|
'image_url' => $this->getImageUrl(), |
|
364
|
|
|
'metas' => $this->getMetas(), |
|
365
|
|
|
'time_created' => $this->getTimeCreated()->format(DATE_ATOM), |
|
366
|
|
|
); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @return string |
|
371
|
|
|
*/ |
|
372
|
|
|
public function __toString() |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->getTitle(); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* @ORM\PostLoad |
|
379
|
|
|
*/ |
|
380
|
|
|
public function postLoad() |
|
381
|
|
|
{ |
|
382
|
|
|
$this->hydratePostMetas(); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* @ORM\PreUpdate |
|
387
|
|
|
*/ |
|
388
|
|
|
public function preUpdate() |
|
389
|
|
|
{ |
|
390
|
|
|
$this->setTimeUpdated(new \DateTime('now')); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* @ORM\PrePersist |
|
395
|
|
|
*/ |
|
396
|
|
|
public function prePersist() |
|
397
|
|
|
{ |
|
398
|
|
|
$this->setTimeUpdated(new \DateTime('now')); |
|
399
|
|
|
$this->setTimeCreated(new \DateTime('now')); |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
|