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