]>
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={ | |
26 | * @ORM\Index(name="created_at", columns={"created_at"}), | |
27 | * @ORM\Index(name="uid", columns={"uid"}) | |
28 | * } | |
29 | * ) | |
30 | * @ORM\HasLifecycleCallbacks() | |
31 | * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") | |
32 | */ | |
33 | class Entry | |
34 | { | |
35 | /** @Serializer\XmlAttribute */ | |
36 | /** | |
37 | * @var int | |
38 | * | |
39 | * @ORM\Column(name="id", type="integer") | |
40 | * @ORM\Id | |
41 | * @ORM\GeneratedValue(strategy="AUTO") | |
42 | * | |
43 | * @Groups({"entries_for_user", "export_all"}) | |
44 | */ | |
45 | private $id; | |
46 | ||
47 | /** | |
48 | * @var string | |
49 | * | |
50 | * @ORM\Column(name="uid", type="string", length=23, nullable=true) | |
51 | * | |
52 | * @Groups({"entries_for_user", "export_all"}) | |
53 | */ | |
54 | private $uid; | |
55 | ||
56 | /** | |
57 | * @var string | |
58 | * | |
59 | * @ORM\Column(name="title", type="text", nullable=true) | |
60 | * | |
61 | * @Groups({"entries_for_user", "export_all"}) | |
62 | */ | |
63 | private $title; | |
64 | ||
65 | /** | |
66 | * @var string | |
67 | * | |
68 | * @Assert\NotBlank() | |
69 | * @ORM\Column(name="url", type="text", nullable=true) | |
70 | * | |
71 | * @Groups({"entries_for_user", "export_all"}) | |
72 | */ | |
73 | private $url; | |
74 | ||
75 | /** | |
76 | * @var bool | |
77 | * | |
78 | * @Exclude | |
79 | * | |
80 | * @ORM\Column(name="is_archived", type="boolean") | |
81 | * | |
82 | * @Groups({"entries_for_user", "export_all"}) | |
83 | */ | |
84 | private $isArchived = false; | |
85 | ||
86 | /** | |
87 | * @var bool | |
88 | * | |
89 | * @Exclude | |
90 | * | |
91 | * @ORM\Column(name="is_starred", type="boolean") | |
92 | * | |
93 | * @Groups({"entries_for_user", "export_all"}) | |
94 | */ | |
95 | private $isStarred = false; | |
96 | ||
97 | /** | |
98 | * @var string | |
99 | * | |
100 | * @ORM\Column(name="content", type="text", nullable=true) | |
101 | * | |
102 | * @Groups({"entries_for_user", "export_all"}) | |
103 | */ | |
104 | private $content; | |
105 | ||
106 | /** | |
107 | * @var \DateTime | |
108 | * | |
109 | * @ORM\Column(name="created_at", type="datetime") | |
110 | * | |
111 | * @Groups({"entries_for_user", "export_all"}) | |
112 | */ | |
113 | private $createdAt; | |
114 | ||
115 | /** | |
116 | * @var \DateTime | |
117 | * | |
118 | * @ORM\Column(name="updated_at", type="datetime") | |
119 | * | |
120 | * @Groups({"entries_for_user", "export_all"}) | |
121 | */ | |
122 | private $updatedAt; | |
123 | ||
124 | /** | |
125 | * @var \DateTime | |
126 | * | |
127 | * @ORM\Column(name="published_at", type="datetime", nullable=true) | |
128 | * | |
129 | * @Groups({"entries_for_user", "export_all"}) | |
130 | */ | |
131 | private $publishedAt; | |
132 | ||
133 | /** | |
134 | * @var array | |
135 | * | |
136 | * @ORM\Column(name="published_by", type="array", nullable=true) | |
137 | * | |
138 | * @Groups({"entries_for_user", "export_all"}) | |
139 | */ | |
140 | private $publishedBy; | |
141 | ||
142 | /** | |
143 | * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"}) | |
144 | * @ORM\JoinTable | |
145 | * | |
146 | * @Groups({"entries_for_user", "export_all"}) | |
147 | */ | |
148 | private $annotations; | |
149 | ||
150 | /** | |
151 | * @var string | |
152 | * | |
153 | * @ORM\Column(name="mimetype", type="text", nullable=true) | |
154 | * | |
155 | * @Groups({"entries_for_user", "export_all"}) | |
156 | */ | |
157 | private $mimetype; | |
158 | ||
159 | /** | |
160 | * @var string | |
161 | * | |
162 | * @ORM\Column(name="language", type="text", nullable=true) | |
163 | * | |
164 | * @Groups({"entries_for_user", "export_all"}) | |
165 | */ | |
166 | private $language; | |
167 | ||
168 | /** | |
169 | * @var int | |
170 | * | |
171 | * @ORM\Column(name="reading_time", type="integer", nullable=true) | |
172 | * | |
173 | * @Groups({"entries_for_user", "export_all"}) | |
174 | */ | |
175 | private $readingTime; | |
176 | ||
177 | /** | |
178 | * @var string | |
179 | * | |
180 | * @ORM\Column(name="domain_name", type="text", nullable=true) | |
181 | * | |
182 | * @Groups({"entries_for_user", "export_all"}) | |
183 | */ | |
184 | private $domainName; | |
185 | ||
186 | /** | |
187 | * @var string | |
188 | * | |
189 | * @ORM\Column(name="preview_picture", type="text", nullable=true) | |
190 | * | |
191 | * @Groups({"entries_for_user", "export_all"}) | |
192 | */ | |
193 | private $previewPicture; | |
194 | ||
195 | /** | |
196 | * @var string | |
197 | * | |
198 | * @ORM\Column(name="http_status", type="string", length=3, nullable=true) | |
199 | * | |
200 | * @Groups({"entries_for_user", "export_all"}) | |
201 | */ | |
202 | private $httpStatus; | |
203 | ||
204 | /** | |
205 | * @var array | |
206 | * | |
207 | * @ORM\Column(name="headers", type="array", nullable=true) | |
208 | * | |
209 | * @Groups({"entries_for_user", "export_all"}) | |
210 | */ | |
211 | private $headers; | |
212 | ||
213 | /** | |
214 | * @Exclude | |
215 | * | |
216 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries") | |
217 | * | |
218 | * @Groups({"export_all"}) | |
219 | */ | |
220 | private $user; | |
221 | ||
222 | /** | |
223 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) | |
224 | * @ORM\JoinTable( | |
225 | * name="entry_tag", | |
226 | * joinColumns={ | |
227 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade") | |
228 | * }, | |
229 | * inverseJoinColumns={ | |
230 | * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade") | |
231 | * } | |
232 | * ) | |
233 | */ | |
234 | private $tags; | |
235 | ||
236 | /** | |
237 | * @var ArrayCollection | |
238 | * @ORM\ManyToMany(targetEntity="Wallabag\GroupBundle\Entity\Group", inversedBy="entries", cascade={"persist"}) | |
239 | */ | |
240 | private $groupShares; | |
241 | ||
242 | /* | |
243 | * @param User $user | |
244 | */ | |
245 | public function __construct(User $user) | |
246 | { | |
247 | $this->user = $user; | |
248 | $this->tags = new ArrayCollection(); | |
249 | } | |
250 | ||
251 | /** | |
252 | * Get id. | |
253 | * | |
254 | * @return int | |
255 | */ | |
256 | public function getId() | |
257 | { | |
258 | return $this->id; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Set title. | |
263 | * | |
264 | * @param string $title | |
265 | * | |
266 | * @return Entry | |
267 | */ | |
268 | public function setTitle($title) | |
269 | { | |
270 | $this->title = $title; | |
271 | ||
272 | return $this; | |
273 | } | |
274 | ||
275 | /** | |
276 | * Get title. | |
277 | * | |
278 | * @return string | |
279 | */ | |
280 | public function getTitle() | |
281 | { | |
282 | return $this->title; | |
283 | } | |
284 | ||
285 | /** | |
286 | * Set url. | |
287 | * | |
288 | * @param string $url | |
289 | * | |
290 | * @return Entry | |
291 | */ | |
292 | public function setUrl($url) | |
293 | { | |
294 | $this->url = $url; | |
295 | ||
296 | return $this; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Get url. | |
301 | * | |
302 | * @return string | |
303 | */ | |
304 | public function getUrl() | |
305 | { | |
306 | return $this->url; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Set isArchived. | |
311 | * | |
312 | * @param bool $isArchived | |
313 | * | |
314 | * @return Entry | |
315 | */ | |
316 | public function setArchived($isArchived) | |
317 | { | |
318 | $this->isArchived = $isArchived; | |
319 | ||
320 | return $this; | |
321 | } | |
322 | ||
323 | /** | |
324 | * Get isArchived. | |
325 | * | |
326 | * @return bool | |
327 | */ | |
328 | public function isArchived() | |
329 | { | |
330 | return $this->isArchived; | |
331 | } | |
332 | ||
333 | /** | |
334 | * @VirtualProperty | |
335 | * @SerializedName("is_archived") | |
336 | * @Groups({"entries_for_user", "export_all"}) | |
337 | */ | |
338 | public function is_Archived() | |
339 | { | |
340 | return (int) $this->isArchived(); | |
341 | } | |
342 | ||
343 | public function toggleArchive() | |
344 | { | |
345 | $this->isArchived = $this->isArchived() ^ 1; | |
346 | ||
347 | return $this; | |
348 | } | |
349 | ||
350 | /** | |
351 | * Set isStarred. | |
352 | * | |
353 | * @param bool $isStarred | |
354 | * | |
355 | * @return Entry | |
356 | */ | |
357 | public function setStarred($isStarred) | |
358 | { | |
359 | $this->isStarred = $isStarred; | |
360 | ||
361 | return $this; | |
362 | } | |
363 | ||
364 | /** | |
365 | * Get isStarred. | |
366 | * | |
367 | * @return bool | |
368 | */ | |
369 | public function isStarred() | |
370 | { | |
371 | return $this->isStarred; | |
372 | } | |
373 | ||
374 | /** | |
375 | * @VirtualProperty | |
376 | * @SerializedName("is_starred") | |
377 | * @Groups({"entries_for_user", "export_all"}) | |
378 | */ | |
379 | public function is_Starred() | |
380 | { | |
381 | return (int) $this->isStarred(); | |
382 | } | |
383 | ||
384 | public function toggleStar() | |
385 | { | |
386 | $this->isStarred = $this->isStarred() ^ 1; | |
387 | ||
388 | return $this; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Set content. | |
393 | * | |
394 | * @param string $content | |
395 | * | |
396 | * @return Entry | |
397 | */ | |
398 | public function setContent($content) | |
399 | { | |
400 | $this->content = $content; | |
401 | ||
402 | return $this; | |
403 | } | |
404 | ||
405 | /** | |
406 | * Get content. | |
407 | * | |
408 | * @return string | |
409 | */ | |
410 | public function getContent() | |
411 | { | |
412 | return $this->content; | |
413 | } | |
414 | ||
415 | /** | |
416 | * @return User | |
417 | */ | |
418 | public function getUser() | |
419 | { | |
420 | return $this->user; | |
421 | } | |
422 | ||
423 | /** | |
424 | * @VirtualProperty | |
425 | * @SerializedName("user_name") | |
426 | */ | |
427 | public function getUserName() | |
428 | { | |
429 | return $this->user->getUserName(); | |
430 | } | |
431 | ||
432 | /** | |
433 | * @VirtualProperty | |
434 | * @SerializedName("user_email") | |
435 | */ | |
436 | public function getUserEmail() | |
437 | { | |
438 | return $this->user->getEmail(); | |
439 | } | |
440 | ||
441 | /** | |
442 | * @VirtualProperty | |
443 | * @SerializedName("user_id") | |
444 | */ | |
445 | public function getUserId() | |
446 | { | |
447 | return $this->user->getId(); | |
448 | } | |
449 | ||
450 | /** | |
451 | * Set created_at. | |
452 | * Only used when importing data from an other service. | |
453 | * | |
454 | * @param \DateTime $createdAt | |
455 | * | |
456 | * @return Entry | |
457 | */ | |
458 | public function setCreatedAt(\DateTime $createdAt) | |
459 | { | |
460 | $this->createdAt = $createdAt; | |
461 | ||
462 | return $this; | |
463 | } | |
464 | ||
465 | /** | |
466 | * @return \DateTime | |
467 | */ | |
468 | public function getCreatedAt() | |
469 | { | |
470 | return $this->createdAt; | |
471 | } | |
472 | ||
473 | /** | |
474 | * @return \DateTime | |
475 | */ | |
476 | public function getUpdatedAt() | |
477 | { | |
478 | return $this->updatedAt; | |
479 | } | |
480 | ||
481 | /** | |
482 | * @ORM\PrePersist | |
483 | * @ORM\PreUpdate | |
484 | */ | |
485 | public function timestamps() | |
486 | { | |
487 | if (is_null($this->createdAt)) { | |
488 | $this->createdAt = new \DateTime(); | |
489 | } | |
490 | ||
491 | $this->updatedAt = new \DateTime(); | |
492 | } | |
493 | ||
494 | /** | |
495 | * @return ArrayCollection<Annotation> | |
496 | */ | |
497 | public function getAnnotations() | |
498 | { | |
499 | return $this->annotations; | |
500 | } | |
501 | ||
502 | /** | |
503 | * @param Annotation $annotation | |
504 | */ | |
505 | public function setAnnotation(Annotation $annotation) | |
506 | { | |
507 | $this->annotations[] = $annotation; | |
508 | } | |
509 | ||
510 | /** | |
511 | * @return string | |
512 | */ | |
513 | public function getMimetype() | |
514 | { | |
515 | return $this->mimetype; | |
516 | } | |
517 | ||
518 | /** | |
519 | * @param string $mimetype | |
520 | */ | |
521 | public function setMimetype($mimetype) | |
522 | { | |
523 | $this->mimetype = $mimetype; | |
524 | } | |
525 | ||
526 | /** | |
527 | * @return int | |
528 | */ | |
529 | public function getReadingTime() | |
530 | { | |
531 | return $this->readingTime; | |
532 | } | |
533 | ||
534 | /** | |
535 | * @param int $readingTime | |
536 | */ | |
537 | public function setReadingTime($readingTime) | |
538 | { | |
539 | $this->readingTime = $readingTime; | |
540 | } | |
541 | ||
542 | /** | |
543 | * @return string | |
544 | */ | |
545 | public function getDomainName() | |
546 | { | |
547 | return $this->domainName; | |
548 | } | |
549 | ||
550 | /** | |
551 | * @param string $domainName | |
552 | */ | |
553 | public function setDomainName($domainName) | |
554 | { | |
555 | $this->domainName = $domainName; | |
556 | } | |
557 | ||
558 | /** | |
559 | * @return ArrayCollection | |
560 | */ | |
561 | public function getTags() | |
562 | { | |
563 | return $this->tags; | |
564 | } | |
565 | ||
566 | /** | |
567 | * @VirtualProperty | |
568 | * @SerializedName("tags") | |
569 | * @Groups({"entries_for_user", "export_all"}) | |
570 | */ | |
571 | public function getSerializedTags() | |
572 | { | |
573 | $data = []; | |
574 | foreach ($this->tags as $tag) { | |
575 | $data[] = $tag->getLabel(); | |
576 | } | |
577 | ||
578 | return $data; | |
579 | } | |
580 | ||
581 | /** | |
582 | * @param Tag $tag | |
583 | */ | |
584 | public function addTag(Tag $tag) | |
585 | { | |
586 | if ($this->tags->contains($tag)) { | |
587 | return; | |
588 | } | |
589 | ||
590 | // check if tag already exist but has not yet be persisted | |
591 | // it seems that the previous condition with `contains()` doesn't check that case | |
592 | foreach ($this->tags as $existingTag) { | |
593 | if ($existingTag->getLabel() === $tag->getLabel()) { | |
594 | return; | |
595 | } | |
596 | } | |
597 | ||
598 | $this->tags->add($tag); | |
599 | $tag->addEntry($this); | |
600 | } | |
601 | ||
602 | public function removeTag(Tag $tag) | |
603 | { | |
604 | if (!$this->tags->contains($tag)) { | |
605 | return; | |
606 | } | |
607 | ||
608 | $this->tags->removeElement($tag); | |
609 | $tag->removeEntry($this); | |
610 | } | |
611 | ||
612 | /** | |
613 | * Set previewPicture. | |
614 | * | |
615 | * @param string $previewPicture | |
616 | * | |
617 | * @return Entry | |
618 | */ | |
619 | public function setPreviewPicture($previewPicture) | |
620 | { | |
621 | $this->previewPicture = $previewPicture; | |
622 | ||
623 | return $this; | |
624 | } | |
625 | ||
626 | /** | |
627 | * Get previewPicture. | |
628 | * | |
629 | * @return string | |
630 | */ | |
631 | public function getPreviewPicture() | |
632 | { | |
633 | return $this->previewPicture; | |
634 | } | |
635 | ||
636 | /** | |
637 | * Set language. | |
638 | * | |
639 | * @param string $language | |
640 | * | |
641 | * @return Entry | |
642 | */ | |
643 | public function setLanguage($language) | |
644 | { | |
645 | $this->language = $language; | |
646 | ||
647 | return $this; | |
648 | } | |
649 | ||
650 | /** | |
651 | * Get language. | |
652 | * | |
653 | * @return string | |
654 | */ | |
655 | public function getLanguage() | |
656 | { | |
657 | return $this->language; | |
658 | } | |
659 | ||
660 | /** | |
661 | * @return string | |
662 | */ | |
663 | public function getUid() | |
664 | { | |
665 | return $this->uid; | |
666 | } | |
667 | ||
668 | /** | |
669 | * @param string $uid | |
670 | * | |
671 | * @return Entry | |
672 | */ | |
673 | public function setUid($uid) | |
674 | { | |
675 | $this->uid = $uid; | |
676 | ||
677 | return $this; | |
678 | } | |
679 | ||
680 | public function generateUid() | |
681 | { | |
682 | if (null === $this->uid) { | |
683 | // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter | |
684 | $this->uid = uniqid('', true); | |
685 | } | |
686 | } | |
687 | ||
688 | public function cleanUid() | |
689 | { | |
690 | $this->uid = null; | |
691 | } | |
692 | ||
693 | /** | |
694 | * Used in the entries filter so it's more explicit for the end user than the uid. | |
695 | * Also used in the API. | |
696 | * | |
697 | * @VirtualProperty | |
698 | * @SerializedName("is_public") | |
699 | * @Groups({"entries_for_user"}) | |
700 | * | |
701 | * @return bool | |
702 | */ | |
703 | public function isPublic() | |
704 | { | |
705 | return null !== $this->uid; | |
706 | } | |
707 | ||
708 | /** | |
709 | * @return string | |
710 | */ | |
711 | public function getHttpStatus() | |
712 | { | |
713 | return $this->httpStatus; | |
714 | } | |
715 | ||
716 | /** | |
717 | * @param string $httpStatus | |
718 | * | |
719 | * @return Entry | |
720 | */ | |
721 | public function setHttpStatus($httpStatus) | |
722 | { | |
723 | $this->httpStatus = $httpStatus; | |
724 | ||
725 | return $this; | |
726 | } | |
727 | ||
728 | /** | |
729 | * @return \Datetime | |
730 | */ | |
731 | public function getPublishedAt() | |
732 | { | |
733 | return $this->publishedAt; | |
734 | } | |
735 | ||
736 | /** | |
737 | * @param \Datetime $publishedAt | |
738 | * | |
739 | * @return Entry | |
740 | */ | |
741 | public function setPublishedAt(\Datetime $publishedAt) | |
742 | { | |
743 | $this->publishedAt = $publishedAt; | |
744 | ||
745 | return $this; | |
746 | } | |
747 | ||
748 | /** | |
749 | * @return array | |
750 | */ | |
751 | public function getPublishedBy() | |
752 | { | |
753 | return $this->publishedBy; | |
754 | } | |
755 | ||
756 | /** | |
757 | * @param array $publishedBy | |
758 | * | |
759 | * @return Entry | |
760 | */ | |
761 | public function setPublishedBy($publishedBy) | |
762 | { | |
763 | $this->publishedBy = $publishedBy; | |
764 | ||
765 | return $this; | |
766 | } | |
767 | ||
768 | /** | |
769 | * @return array | |
770 | */ | |
771 | public function getHeaders() | |
772 | { | |
773 | return $this->headers; | |
774 | } | |
775 | ||
776 | /** | |
777 | * @param array $headers | |
778 | * | |
779 | * @return Entry | |
780 | */ | |
781 | public function setHeaders($headers) | |
782 | { | |
783 | $this->headers = $headers; | |
784 | ||
785 | return $this; | |
786 | } | |
787 | ||
788 | /** | |
789 | * @return ArrayCollection | |
790 | */ | |
791 | public function getGroupShares() | |
792 | { | |
793 | return $this->groupShares; | |
794 | } | |
795 | ||
796 | /** | |
797 | * @param ArrayCollection $groupShares | |
798 | */ | |
799 | public function setGroupShares($groupShares) | |
800 | { | |
801 | $this->groupShares = $groupShares; | |
802 | } | |
803 | } |