]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Entity; | |
4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | |
6 | use Doctrine\ORM\Mapping as ORM; | |
7 | use Hateoas\Configuration\Annotation as Hateoas; | |
8 | use JMS\Serializer\Annotation\Groups; | |
9 | use JMS\Serializer\Annotation\XmlRoot; | |
10 | use JMS\Serializer\Annotation\Exclude; | |
11 | use JMS\Serializer\Annotation\VirtualProperty; | |
12 | use JMS\Serializer\Annotation\SerializedName; | |
13 | use Symfony\Component\Validator\Constraints as Assert; | |
14 | use Wallabag\UserBundle\Entity\User; | |
15 | use Wallabag\AnnotationBundle\Entity\Annotation; | |
16 | ||
17 | /** | |
18 | * Entry. | |
19 | * | |
20 | * @XmlRoot("entry") | |
21 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") | |
22 | * @ORM\Table( | |
23 | * name="`entry`", | |
24 | * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, | |
25 | * indexes={@ORM\Index(name="created_at", columns={"created_at"})} | |
26 | * ) | |
27 | * @ORM\HasLifecycleCallbacks() | |
28 | * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") | |
29 | */ | |
30 | class Entry | |
31 | { | |
32 | /** @Serializer\XmlAttribute */ | |
33 | /** | |
34 | * @var int | |
35 | * | |
36 | * @ORM\Column(name="id", type="integer") | |
37 | * @ORM\Id | |
38 | * @ORM\GeneratedValue(strategy="AUTO") | |
39 | * | |
40 | * @Groups({"entries_for_user", "export_all"}) | |
41 | */ | |
42 | private $id; | |
43 | ||
44 | /** | |
45 | * @var string | |
46 | * | |
47 | * @ORM\Column(name="uuid", type="text", nullable=true) | |
48 | * | |
49 | * @Groups({"entries_for_user", "export_all"}) | |
50 | */ | |
51 | private $uuid; | |
52 | ||
53 | /** | |
54 | * @var string | |
55 | * | |
56 | * @ORM\Column(name="title", type="text", nullable=true) | |
57 | * | |
58 | * @Groups({"entries_for_user", "export_all"}) | |
59 | */ | |
60 | private $title; | |
61 | ||
62 | /** | |
63 | * @var string | |
64 | * | |
65 | * @Assert\NotBlank() | |
66 | * @ORM\Column(name="url", type="text", nullable=true) | |
67 | * | |
68 | * @Groups({"entries_for_user", "export_all"}) | |
69 | */ | |
70 | private $url; | |
71 | ||
72 | /** | |
73 | * @var bool | |
74 | * | |
75 | * @Exclude | |
76 | * | |
77 | * @ORM\Column(name="is_archived", type="boolean") | |
78 | * | |
79 | * @Groups({"entries_for_user", "export_all"}) | |
80 | */ | |
81 | private $isArchived = false; | |
82 | ||
83 | /** | |
84 | * @var bool | |
85 | * | |
86 | * @Exclude | |
87 | * | |
88 | * @ORM\Column(name="is_starred", type="boolean") | |
89 | * | |
90 | * @Groups({"entries_for_user", "export_all"}) | |
91 | */ | |
92 | private $isStarred = false; | |
93 | ||
94 | /** | |
95 | * @var string | |
96 | * | |
97 | * @ORM\Column(name="content", type="text", nullable=true) | |
98 | * | |
99 | * @Groups({"entries_for_user", "export_all"}) | |
100 | */ | |
101 | private $content; | |
102 | ||
103 | /** | |
104 | * @var \DateTime | |
105 | * | |
106 | * @ORM\Column(name="created_at", type="datetime") | |
107 | * | |
108 | * @Groups({"entries_for_user", "export_all"}) | |
109 | */ | |
110 | private $createdAt; | |
111 | ||
112 | /** | |
113 | * @var \DateTime | |
114 | * | |
115 | * @ORM\Column(name="updated_at", type="datetime") | |
116 | * | |
117 | * @Groups({"entries_for_user", "export_all"}) | |
118 | */ | |
119 | private $updatedAt; | |
120 | ||
121 | /** | |
122 | * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"}) | |
123 | * @ORM\JoinTable | |
124 | * | |
125 | * @Groups({"entries_for_user", "export_all"}) | |
126 | */ | |
127 | private $annotations; | |
128 | ||
129 | /** | |
130 | * @var string | |
131 | * | |
132 | * @ORM\Column(name="mimetype", type="text", nullable=true) | |
133 | * | |
134 | * @Groups({"entries_for_user", "export_all"}) | |
135 | */ | |
136 | private $mimetype; | |
137 | ||
138 | /** | |
139 | * @var string | |
140 | * | |
141 | * @ORM\Column(name="language", type="text", nullable=true) | |
142 | * | |
143 | * @Groups({"entries_for_user", "export_all"}) | |
144 | */ | |
145 | private $language; | |
146 | ||
147 | /** | |
148 | * @var int | |
149 | * | |
150 | * @ORM\Column(name="reading_time", type="integer", nullable=true) | |
151 | * | |
152 | * @Groups({"entries_for_user", "export_all"}) | |
153 | */ | |
154 | private $readingTime; | |
155 | ||
156 | /** | |
157 | * @var string | |
158 | * | |
159 | * @ORM\Column(name="domain_name", type="text", nullable=true) | |
160 | * | |
161 | * @Groups({"entries_for_user", "export_all"}) | |
162 | */ | |
163 | private $domainName; | |
164 | ||
165 | /** | |
166 | * @var string | |
167 | * | |
168 | * @ORM\Column(name="preview_picture", type="text", nullable=true) | |
169 | * | |
170 | * @Groups({"entries_for_user", "export_all"}) | |
171 | */ | |
172 | private $previewPicture; | |
173 | ||
174 | /** | |
175 | * @var bool | |
176 | * | |
177 | * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false}) | |
178 | * | |
179 | * @Groups({"export_all"}) | |
180 | */ | |
181 | private $isPublic; | |
182 | ||
183 | /** | |
184 | * @Exclude | |
185 | * | |
186 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries") | |
187 | * | |
188 | * @Groups({"export_all"}) | |
189 | */ | |
190 | private $user; | |
191 | ||
192 | /** | |
193 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) | |
194 | * @ORM\JoinTable( | |
195 | * name="entry_tag", | |
196 | * joinColumns={ | |
197 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade") | |
198 | * }, | |
199 | * inverseJoinColumns={ | |
200 | * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade") | |
201 | * } | |
202 | * ) | |
203 | */ | |
204 | private $tags; | |
205 | ||
206 | /* | |
207 | * @param User $user | |
208 | */ | |
209 | public function __construct(User $user) | |
210 | { | |
211 | $this->user = $user; | |
212 | $this->tags = new ArrayCollection(); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Get id. | |
217 | * | |
218 | * @return int | |
219 | */ | |
220 | public function getId() | |
221 | { | |
222 | return $this->id; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Set title. | |
227 | * | |
228 | * @param string $title | |
229 | * | |
230 | * @return Entry | |
231 | */ | |
232 | public function setTitle($title) | |
233 | { | |
234 | $this->title = $title; | |
235 | ||
236 | return $this; | |
237 | } | |
238 | ||
239 | /** | |
240 | * Get title. | |
241 | * | |
242 | * @return string | |
243 | */ | |
244 | public function getTitle() | |
245 | { | |
246 | return $this->title; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Set url. | |
251 | * | |
252 | * @param string $url | |
253 | * | |
254 | * @return Entry | |
255 | */ | |
256 | public function setUrl($url) | |
257 | { | |
258 | $this->url = $url; | |
259 | ||
260 | return $this; | |
261 | } | |
262 | ||
263 | /** | |
264 | * Get url. | |
265 | * | |
266 | * @return string | |
267 | */ | |
268 | public function getUrl() | |
269 | { | |
270 | return $this->url; | |
271 | } | |
272 | ||
273 | /** | |
274 | * Set isArchived. | |
275 | * | |
276 | * @param bool $isArchived | |
277 | * | |
278 | * @return Entry | |
279 | */ | |
280 | public function setArchived($isArchived) | |
281 | { | |
282 | $this->isArchived = $isArchived; | |
283 | ||
284 | return $this; | |
285 | } | |
286 | ||
287 | /** | |
288 | * Get isArchived. | |
289 | * | |
290 | * @return bool | |
291 | */ | |
292 | public function isArchived() | |
293 | { | |
294 | return $this->isArchived; | |
295 | } | |
296 | ||
297 | /** | |
298 | * @VirtualProperty | |
299 | * @SerializedName("is_archived") | |
300 | * @Groups({"entries_for_user", "export_all"}) | |
301 | */ | |
302 | public function is_Archived() | |
303 | { | |
304 | return (int) $this->isArchived(); | |
305 | } | |
306 | ||
307 | public function toggleArchive() | |
308 | { | |
309 | $this->isArchived = $this->isArchived() ^ 1; | |
310 | ||
311 | return $this; | |
312 | } | |
313 | ||
314 | /** | |
315 | * Set isStarred. | |
316 | * | |
317 | * @param bool $isStarred | |
318 | * | |
319 | * @return Entry | |
320 | */ | |
321 | public function setStarred($isStarred) | |
322 | { | |
323 | $this->isStarred = $isStarred; | |
324 | ||
325 | return $this; | |
326 | } | |
327 | ||
328 | /** | |
329 | * Get isStarred. | |
330 | * | |
331 | * @return bool | |
332 | */ | |
333 | public function isStarred() | |
334 | { | |
335 | return $this->isStarred; | |
336 | } | |
337 | ||
338 | /** | |
339 | * @VirtualProperty | |
340 | * @SerializedName("is_starred") | |
341 | * @Groups({"entries_for_user", "export_all"}) | |
342 | */ | |
343 | public function is_Starred() | |
344 | { | |
345 | return (int) $this->isStarred(); | |
346 | } | |
347 | ||
348 | public function toggleStar() | |
349 | { | |
350 | $this->isStarred = $this->isStarred() ^ 1; | |
351 | ||
352 | return $this; | |
353 | } | |
354 | ||
355 | /** | |
356 | * Set content. | |
357 | * | |
358 | * @param string $content | |
359 | * | |
360 | * @return Entry | |
361 | */ | |
362 | public function setContent($content) | |
363 | { | |
364 | $this->content = $content; | |
365 | ||
366 | return $this; | |
367 | } | |
368 | ||
369 | /** | |
370 | * Get content. | |
371 | * | |
372 | * @return string | |
373 | */ | |
374 | public function getContent() | |
375 | { | |
376 | return $this->content; | |
377 | } | |
378 | ||
379 | /** | |
380 | * @return User | |
381 | */ | |
382 | public function getUser() | |
383 | { | |
384 | return $this->user; | |
385 | } | |
386 | ||
387 | /** | |
388 | * @VirtualProperty | |
389 | * @SerializedName("user_name") | |
390 | */ | |
391 | public function getUserName() | |
392 | { | |
393 | return $this->user->getUserName(); | |
394 | } | |
395 | ||
396 | /** | |
397 | * @VirtualProperty | |
398 | * @SerializedName("user_email") | |
399 | */ | |
400 | public function getUserEmail() | |
401 | { | |
402 | return $this->user->getEmail(); | |
403 | } | |
404 | ||
405 | /** | |
406 | * @VirtualProperty | |
407 | * @SerializedName("user_id") | |
408 | */ | |
409 | public function getUserId() | |
410 | { | |
411 | return $this->user->getId(); | |
412 | } | |
413 | ||
414 | /** | |
415 | * Set created_at. | |
416 | * Only used when importing data from an other service. | |
417 | * | |
418 | * @param \DateTime $createdAt | |
419 | * | |
420 | * @return Entry | |
421 | */ | |
422 | public function setCreatedAt(\DateTime $createdAt) | |
423 | { | |
424 | $this->createdAt = $createdAt; | |
425 | ||
426 | return $this; | |
427 | } | |
428 | ||
429 | /** | |
430 | * @return \DateTime | |
431 | */ | |
432 | public function getCreatedAt() | |
433 | { | |
434 | return $this->createdAt; | |
435 | } | |
436 | ||
437 | /** | |
438 | * @return \DateTime | |
439 | */ | |
440 | public function getUpdatedAt() | |
441 | { | |
442 | return $this->updatedAt; | |
443 | } | |
444 | ||
445 | /** | |
446 | * @ORM\PrePersist | |
447 | * @ORM\PreUpdate | |
448 | */ | |
449 | public function timestamps() | |
450 | { | |
451 | if (is_null($this->createdAt)) { | |
452 | $this->createdAt = new \DateTime(); | |
453 | } | |
454 | ||
455 | $this->updatedAt = new \DateTime(); | |
456 | } | |
457 | ||
458 | /** | |
459 | * @return ArrayCollection<Annotation> | |
460 | */ | |
461 | public function getAnnotations() | |
462 | { | |
463 | return $this->annotations; | |
464 | } | |
465 | ||
466 | /** | |
467 | * @param Annotation $annotation | |
468 | */ | |
469 | public function setAnnotation(Annotation $annotation) | |
470 | { | |
471 | $this->annotations[] = $annotation; | |
472 | } | |
473 | ||
474 | /** | |
475 | * @return string | |
476 | */ | |
477 | public function getMimetype() | |
478 | { | |
479 | return $this->mimetype; | |
480 | } | |
481 | ||
482 | /** | |
483 | * @param string $mimetype | |
484 | */ | |
485 | public function setMimetype($mimetype) | |
486 | { | |
487 | $this->mimetype = $mimetype; | |
488 | } | |
489 | ||
490 | /** | |
491 | * @return int | |
492 | */ | |
493 | public function getReadingTime() | |
494 | { | |
495 | return $this->readingTime; | |
496 | } | |
497 | ||
498 | /** | |
499 | * @param int $readingTime | |
500 | */ | |
501 | public function setReadingTime($readingTime) | |
502 | { | |
503 | $this->readingTime = $readingTime; | |
504 | } | |
505 | ||
506 | /** | |
507 | * @return string | |
508 | */ | |
509 | public function getDomainName() | |
510 | { | |
511 | return $this->domainName; | |
512 | } | |
513 | ||
514 | /** | |
515 | * @param string $domainName | |
516 | */ | |
517 | public function setDomainName($domainName) | |
518 | { | |
519 | $this->domainName = $domainName; | |
520 | } | |
521 | ||
522 | /** | |
523 | * @return bool | |
524 | */ | |
525 | public function isPublic() | |
526 | { | |
527 | return $this->isPublic; | |
528 | } | |
529 | ||
530 | /** | |
531 | * @param bool $isPublic | |
532 | */ | |
533 | public function setIsPublic($isPublic) | |
534 | { | |
535 | $this->isPublic = $isPublic; | |
536 | } | |
537 | ||
538 | /** | |
539 | * @return ArrayCollection<Tag> | |
540 | */ | |
541 | public function getTags() | |
542 | { | |
543 | return $this->tags; | |
544 | } | |
545 | ||
546 | /** | |
547 | * @VirtualProperty | |
548 | * @SerializedName("tags") | |
549 | * @Groups({"entries_for_user", "export_all"}) | |
550 | */ | |
551 | public function getSerializedTags() | |
552 | { | |
553 | $data = []; | |
554 | foreach ($this->tags as $tag) { | |
555 | $data[] = $tag->getLabel(); | |
556 | } | |
557 | ||
558 | return $data; | |
559 | } | |
560 | ||
561 | /** | |
562 | * @param Tag $tag | |
563 | */ | |
564 | public function addTag(Tag $tag) | |
565 | { | |
566 | if ($this->tags->contains($tag)) { | |
567 | return; | |
568 | } | |
569 | ||
570 | // check if tag already exist but has not yet be persisted | |
571 | // it seems that the previous condition with `contains()` doesn't check that case | |
572 | foreach ($this->tags as $existingTag) { | |
573 | if ($existingTag->getLabel() === $tag->getLabel()) { | |
574 | return; | |
575 | } | |
576 | } | |
577 | ||
578 | $this->tags->add($tag); | |
579 | $tag->addEntry($this); | |
580 | } | |
581 | ||
582 | public function removeTag(Tag $tag) | |
583 | { | |
584 | if (!$this->tags->contains($tag)) { | |
585 | return; | |
586 | } | |
587 | ||
588 | $this->tags->removeElement($tag); | |
589 | $tag->removeEntry($this); | |
590 | } | |
591 | ||
592 | /** | |
593 | * Set previewPicture. | |
594 | * | |
595 | * @param string $previewPicture | |
596 | * | |
597 | * @return Entry | |
598 | */ | |
599 | public function setPreviewPicture($previewPicture) | |
600 | { | |
601 | $this->previewPicture = $previewPicture; | |
602 | ||
603 | return $this; | |
604 | } | |
605 | ||
606 | /** | |
607 | * Get previewPicture. | |
608 | * | |
609 | * @return string | |
610 | */ | |
611 | public function getPreviewPicture() | |
612 | { | |
613 | return $this->previewPicture; | |
614 | } | |
615 | ||
616 | /** | |
617 | * Set language. | |
618 | * | |
619 | * @param string $language | |
620 | * | |
621 | * @return Entry | |
622 | */ | |
623 | public function setLanguage($language) | |
624 | { | |
625 | $this->language = $language; | |
626 | ||
627 | return $this; | |
628 | } | |
629 | ||
630 | /** | |
631 | * Get language. | |
632 | * | |
633 | * @return string | |
634 | */ | |
635 | public function getLanguage() | |
636 | { | |
637 | return $this->language; | |
638 | } | |
639 | ||
640 | /** | |
641 | * @return string | |
642 | */ | |
643 | public function getUuid() | |
644 | { | |
645 | return $this->uuid; | |
646 | } | |
647 | ||
648 | /** | |
649 | * @param string $uuid | |
650 | * | |
651 | * @return Entry | |
652 | */ | |
653 | public function setUuid($uuid) | |
654 | { | |
655 | $this->uuid = $uuid; | |
656 | ||
657 | return $this; | |
658 | } | |
659 | ||
660 | public function generateUuid() | |
661 | { | |
662 | if (null === $this->uuid) { | |
663 | // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter | |
664 | $this->uuid = uniqid('', true); | |
665 | } | |
666 | } | |
667 | ||
668 | public function cleanUuid() | |
669 | { | |
670 | $this->uuid = null; | |
671 | } | |
672 | } |