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