3 namespace Wallabag\CoreBundle\Entity
;
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
;
22 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
25 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
27 * @ORM\Index(name="created_at", columns={"created_at"}),
28 * @ORM\Index(name="uid", columns={"uid"})
31 * @ORM\HasLifecycleCallbacks()
32 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
36 use EntityTimestampsTrait
;
38 /** @Serializer\XmlAttribute */
42 * @ORM\Column(name="id", type="integer")
44 * @ORM\GeneratedValue(strategy="AUTO")
46 * @Groups({"entries_for_user", "export_all"})
53 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
55 * @Groups({"entries_for_user", "export_all"})
62 * @ORM\Column(name="title", type="text", nullable=true)
64 * @Groups({"entries_for_user", "export_all"})
72 * @ORM\Column(name="url", type="text", nullable=true)
74 * @Groups({"entries_for_user", "export_all"})
83 * @ORM\Column(name="is_archived", type="boolean")
85 * @Groups({"entries_for_user", "export_all"})
87 private $isArchived = false;
94 * @ORM\Column(name="is_starred", type="boolean")
96 * @Groups({"entries_for_user", "export_all"})
98 private $isStarred = false;
103 * @ORM\Column(name="content", type="text", nullable=true)
105 * @Groups({"entries_for_user", "export_all"})
112 * @ORM\Column(name="created_at", type="datetime")
114 * @Groups({"entries_for_user", "export_all"})
121 * @ORM\Column(name="updated_at", type="datetime")
123 * @Groups({"entries_for_user", "export_all"})
130 * @ORM\Column(name="published_at", type="datetime", nullable=true)
132 * @Groups({"entries_for_user", "export_all"})
134 private $publishedAt;
139 * @ORM\Column(name="published_by", type="array", nullable=true)
141 * @Groups({"entries_for_user", "export_all"})
143 private $publishedBy;
148 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
150 * @Groups({"entries_for_user", "export_all"})
152 private $starredAt = null;
155 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
158 * @Groups({"entries_for_user", "export_all"})
160 private $annotations;
165 * @ORM\Column(name="mimetype", type="text", nullable=true)
167 * @Groups({"entries_for_user", "export_all"})
174 * @ORM\Column(name="language", type="text", nullable=true)
176 * @Groups({"entries_for_user", "export_all"})
183 * @ORM\Column(name="reading_time", type="integer", nullable=false)
185 * @Groups({"entries_for_user", "export_all"})
187 private $readingTime = 0;
192 * @ORM\Column(name="domain_name", type="text", nullable=true)
194 * @Groups({"entries_for_user", "export_all"})
201 * @ORM\Column(name="preview_picture", type="text", nullable=true)
203 * @Groups({"entries_for_user", "export_all"})
205 private $previewPicture;
210 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
212 * @Groups({"entries_for_user", "export_all"})
219 * @ORM\Column(name="headers", type="array", nullable=true)
221 * @Groups({"entries_for_user", "export_all"})
228 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
230 * @Groups({"export_all"})
235 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
239 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
241 * inverseJoinColumns={
242 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
251 public function __construct(User
$user)
254 $this->tags
= new ArrayCollection();
262 public function getId()
270 * @param string $title
274 public function setTitle($title)
276 $this->title
= $title;
286 public function getTitle()
298 public function setUrl($url)
310 public function getUrl()
318 * @param bool $isArchived
322 public function setArchived($isArchived)
324 $this->isArchived
= $isArchived;
334 public function isArchived()
336 return $this->isArchived
;
341 * @SerializedName("is_archived")
342 * @Groups({"entries_for_user", "export_all"})
344 public function is_Archived()
346 return (int) $this->isArchived();
349 public function toggleArchive()
351 $this->isArchived
= $this->isArchived() ^
1;
359 * @param bool $isStarred
363 public function setStarred($isStarred)
365 $this->isStarred
= $isStarred;
375 public function isStarred()
377 return $this->isStarred
;
382 * @SerializedName("is_starred")
383 * @Groups({"entries_for_user", "export_all"})
385 public function is_Starred()
387 return (int) $this->isStarred();
390 public function toggleStar()
392 $this->isStarred
= $this->isStarred() ^
1;
400 * @param string $content
404 public function setContent($content)
406 $this->content
= $content;
416 public function getContent()
418 return $this->content
;
424 public function getUser()
431 * @SerializedName("user_name")
433 public function getUserName()
435 return $this->user
->getUserName();
440 * @SerializedName("user_email")
442 public function getUserEmail()
444 return $this->user
->getEmail();
449 * @SerializedName("user_id")
451 public function getUserId()
453 return $this->user
->getId();
458 * Only used when importing data from an other service.
460 * @param \DateTime $createdAt
464 public function setCreatedAt(\DateTime
$createdAt)
466 $this->createdAt
= $createdAt;
474 public function getCreatedAt()
476 return $this->createdAt
;
482 public function getUpdatedAt()
484 return $this->updatedAt
;
488 * @return \DateTime|null
490 public function getStarredAt()
492 return $this->starredAt
;
496 * @param \DateTime|null $starredAt
500 public function setStarredAt($starredAt = null)
502 $this->starredAt
= $starredAt;
508 * update isStarred and starred_at fields.
510 * @param bool $isStarred
514 public function updateStar($isStarred = false)
516 $this->setStarred($isStarred);
517 $this->setStarredAt(null);
518 if ($this->isStarred()) {
519 $this->setStarredAt(new \
DateTime());
526 * @return ArrayCollection<Annotation>
528 public function getAnnotations()
530 return $this->annotations
;
534 * @param Annotation $annotation
536 public function setAnnotation(Annotation
$annotation)
538 $this->annotations
[] = $annotation;
544 public function getMimetype()
546 return $this->mimetype
;
550 * @param string $mimetype
552 public function setMimetype($mimetype)
554 $this->mimetype
= $mimetype;
560 public function getReadingTime()
562 return $this->readingTime
;
566 * @param int $readingTime
568 public function setReadingTime($readingTime)
570 $this->readingTime
= $readingTime;
576 public function getDomainName()
578 return $this->domainName
;
582 * @param string $domainName
584 public function setDomainName($domainName)
586 $this->domainName
= $domainName;
590 * @return ArrayCollection
592 public function getTags()
599 * @SerializedName("tags")
600 * @Groups({"entries_for_user", "export_all"})
602 public function getSerializedTags()
605 foreach ($this->tags
as $tag) {
606 $data[] = $tag->getLabel();
615 public function addTag(Tag
$tag)
617 if ($this->tags
->contains($tag)) {
621 // check if tag already exist but has not yet be persisted
622 // it seems that the previous condition with `contains()` doesn't check that case
623 foreach ($this->tags
as $existingTag) {
624 if ($existingTag->getLabel() === $tag->getLabel()) {
629 $this->tags
->add($tag);
630 $tag->addEntry($this);
634 * Remove the given tag from the entry (if the tag is associated).
638 public function removeTag(Tag
$tag)
640 if (!$this->tags
->contains($tag)) {
644 $this->tags
->removeElement($tag);
645 $tag->removeEntry($this);
649 * Remove all assigned tags from the entry.
651 public function removeAllTags()
653 foreach ($this->tags
as $tag) {
654 $this->tags
->removeElement($tag);
655 $tag->removeEntry($this);
660 * Set previewPicture.
662 * @param string $previewPicture
666 public function setPreviewPicture($previewPicture)
668 $this->previewPicture
= $previewPicture;
674 * Get previewPicture.
678 public function getPreviewPicture()
680 return $this->previewPicture
;
686 * @param string $language
690 public function setLanguage($language)
692 $this->language
= $language;
702 public function getLanguage()
704 return $this->language
;
710 public function getUid()
720 public function setUid($uid)
727 public function generateUid()
729 if (null === $this->uid
) {
730 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
731 $this->uid
= uniqid('', true);
735 public function cleanUid()
741 * Used in the entries filter so it's more explicit for the end user than the uid.
742 * Also used in the API.
745 * @SerializedName("is_public")
746 * @Groups({"entries_for_user"})
750 public function isPublic()
752 return null !== $this->uid
;
758 public function getHttpStatus()
760 return $this->httpStatus
;
764 * @param string $httpStatus
768 public function setHttpStatus($httpStatus)
770 $this->httpStatus
= $httpStatus;
778 public function getPublishedAt()
780 return $this->publishedAt
;
784 * @param \Datetime $publishedAt
788 public function setPublishedAt(\Datetime
$publishedAt)
790 $this->publishedAt
= $publishedAt;
798 public function getPublishedBy()
800 return $this->publishedBy
;
804 * @param array $publishedBy
808 public function setPublishedBy($publishedBy)
810 $this->publishedBy
= $publishedBy;
818 public function getHeaders()
820 return $this->headers
;
824 * @param array $headers
828 public function setHeaders($headers)
830 $this->headers
= $headers;