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\CoreBundle\Helper\UrlHasher
;
17 use Wallabag\UserBundle\Entity\User
;
23 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
26 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
28 * @ORM\Index(name="created_at", columns={"created_at"}),
29 * @ORM\Index(name="uid", columns={"uid"}),
30 * @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}})
33 * @ORM\HasLifecycleCallbacks()
34 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
38 use EntityTimestampsTrait
;
40 /** @Serializer\XmlAttribute */
44 * @ORM\Column(name="id", type="integer")
46 * @ORM\GeneratedValue(strategy="AUTO")
48 * @Groups({"entries_for_user", "export_all"})
55 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
57 * @Groups({"entries_for_user", "export_all"})
64 * @ORM\Column(name="title", type="text", nullable=true)
66 * @Groups({"entries_for_user", "export_all"})
74 * @ORM\Column(name="url", type="text", nullable=true)
76 * @Groups({"entries_for_user", "export_all"})
83 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
92 * @ORM\Column(name="is_archived", type="boolean")
94 * @Groups({"entries_for_user", "export_all"})
96 private $isArchived = false;
101 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
103 * @Groups({"entries_for_user", "export_all"})
105 private $archivedAt = null;
112 * @ORM\Column(name="is_starred", type="boolean")
114 * @Groups({"entries_for_user", "export_all"})
116 private $isStarred = false;
121 * @ORM\Column(name="content", type="text", nullable=true)
123 * @Groups({"entries_for_user", "export_all"})
130 * @ORM\Column(name="created_at", type="datetime")
132 * @Groups({"entries_for_user", "export_all"})
139 * @ORM\Column(name="updated_at", type="datetime")
141 * @Groups({"entries_for_user", "export_all"})
148 * @ORM\Column(name="published_at", type="datetime", nullable=true)
150 * @Groups({"entries_for_user", "export_all"})
152 private $publishedAt;
157 * @ORM\Column(name="published_by", type="array", nullable=true)
159 * @Groups({"entries_for_user", "export_all"})
161 private $publishedBy;
166 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
168 * @Groups({"entries_for_user", "export_all"})
170 private $starredAt = null;
173 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
176 * @Groups({"entries_for_user", "export_all"})
178 private $annotations;
183 * @ORM\Column(name="mimetype", type="text", nullable=true)
185 * @Groups({"entries_for_user", "export_all"})
192 * @ORM\Column(name="language", type="text", nullable=true)
194 * @Groups({"entries_for_user", "export_all"})
201 * @ORM\Column(name="reading_time", type="integer", nullable=false)
203 * @Groups({"entries_for_user", "export_all"})
205 private $readingTime = 0;
210 * @ORM\Column(name="domain_name", type="text", nullable=true)
212 * @Groups({"entries_for_user", "export_all"})
219 * @ORM\Column(name="preview_picture", type="text", nullable=true)
221 * @Groups({"entries_for_user", "export_all"})
223 private $previewPicture;
228 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
230 * @Groups({"entries_for_user", "export_all"})
237 * @ORM\Column(name="headers", type="array", nullable=true)
239 * @Groups({"entries_for_user", "export_all"})
246 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
248 * @Groups({"export_all"})
253 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
257 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
259 * inverseJoinColumns={
260 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
269 * @ORM\Column(name="origin_url", type="text", nullable=true)
271 * @Groups({"entries_for_user", "export_all"})
278 public function __construct(User
$user)
281 $this->tags
= new ArrayCollection();
289 public function getId()
297 * @param string $title
301 public function setTitle($title)
303 $this->title
= $title;
313 public function getTitle()
325 public function setUrl($url)
328 $this->hashedUrl
= UrlHasher
::hashUrl($url);
338 public function getUrl()
346 * @param bool $isArchived
350 public function setArchived($isArchived)
352 $this->isArchived
= $isArchived;
358 * update isArchived and archive_at fields.
360 * @param bool $isArchived
364 public function updateArchived($isArchived = false)
366 $this->setArchived($isArchived);
367 $this->setArchivedAt(null);
368 if ($this->isArchived()) {
369 $this->setArchivedAt(new \
DateTime());
376 * @return \DateTime|null
378 public function getArchivedAt()
380 return $this->archivedAt
;
384 * @param \DateTime|null $archivedAt
388 public function setArchivedAt($archivedAt = null)
390 $this->archivedAt
= $archivedAt;
400 public function isArchived()
402 return $this->isArchived
;
407 * @SerializedName("is_archived")
408 * @Groups({"entries_for_user", "export_all"})
410 public function is_Archived()
412 return (int) $this->isArchived();
415 public function toggleArchive()
417 $this->updateArchived($this->isArchived() ^
1);
425 * @param bool $isStarred
429 public function setStarred($isStarred)
431 $this->isStarred
= $isStarred;
441 public function isStarred()
443 return $this->isStarred
;
448 * @SerializedName("is_starred")
449 * @Groups({"entries_for_user", "export_all"})
451 public function is_Starred()
453 return (int) $this->isStarred();
456 public function toggleStar()
458 $this->isStarred
= $this->isStarred() ^
1;
466 * @param string $content
470 public function setContent($content)
472 $this->content
= $content;
482 public function getContent()
484 return $this->content
;
490 public function getUser()
497 * @SerializedName("user_name")
499 public function getUserName()
501 return $this->user
->getUserName();
506 * @SerializedName("user_email")
508 public function getUserEmail()
510 return $this->user
->getEmail();
515 * @SerializedName("user_id")
517 public function getUserId()
519 return $this->user
->getId();
524 * Only used when importing data from an other service.
526 * @param \DateTime $createdAt
530 public function setCreatedAt(\DateTime
$createdAt)
532 $this->createdAt
= $createdAt;
540 public function getCreatedAt()
542 return $this->createdAt
;
548 public function getUpdatedAt()
550 return $this->updatedAt
;
554 * @return \DateTime|null
556 public function getStarredAt()
558 return $this->starredAt
;
562 * @param \DateTime|null $starredAt
566 public function setStarredAt($starredAt = null)
568 $this->starredAt
= $starredAt;
574 * update isStarred and starred_at fields.
576 * @param bool $isStarred
580 public function updateStar($isStarred = false)
582 $this->setStarred($isStarred);
583 $this->setStarredAt(null);
584 if ($this->isStarred()) {
585 $this->setStarredAt(new \
DateTime());
592 * @return ArrayCollection<Annotation>
594 public function getAnnotations()
596 return $this->annotations
;
600 * @param Annotation $annotation
602 public function setAnnotation(Annotation
$annotation)
604 $this->annotations
[] = $annotation;
610 public function getMimetype()
612 return $this->mimetype
;
616 * @param string $mimetype
618 public function setMimetype($mimetype)
620 $this->mimetype
= $mimetype;
626 public function getReadingTime()
628 return $this->readingTime
;
632 * @param int $readingTime
634 public function setReadingTime($readingTime)
636 $this->readingTime
= $readingTime;
642 public function getDomainName()
644 return $this->domainName
;
648 * @param string $domainName
650 public function setDomainName($domainName)
652 $this->domainName
= $domainName;
656 * @return ArrayCollection
658 public function getTags()
665 * @SerializedName("tags")
666 * @Groups({"entries_for_user", "export_all"})
668 public function getSerializedTags()
671 foreach ($this->tags
as $tag) {
672 $data[] = $tag->getLabel();
681 public function addTag(Tag
$tag)
683 if ($this->tags
->contains($tag)) {
687 // check if tag already exist but has not yet be persisted
688 // it seems that the previous condition with `contains()` doesn't check that case
689 foreach ($this->tags
as $existingTag) {
690 if ($existingTag->getLabel() === $tag->getLabel()) {
695 $this->tags
->add($tag);
696 $tag->addEntry($this);
700 * Remove the given tag from the entry (if the tag is associated).
704 public function removeTag(Tag
$tag)
706 if (!$this->tags
->contains($tag)) {
710 $this->tags
->removeElement($tag);
711 $tag->removeEntry($this);
715 * Remove all assigned tags from the entry.
717 public function removeAllTags()
719 foreach ($this->tags
as $tag) {
720 $this->tags
->removeElement($tag);
721 $tag->removeEntry($this);
726 * Set previewPicture.
728 * @param string $previewPicture
732 public function setPreviewPicture($previewPicture)
734 $this->previewPicture
= $previewPicture;
740 * Get previewPicture.
744 public function getPreviewPicture()
746 return $this->previewPicture
;
752 * @param string $language
756 public function setLanguage($language)
758 $this->language
= $language;
768 public function getLanguage()
770 return $this->language
;
774 * @return string|null
776 public function getUid()
786 public function setUid($uid)
793 public function generateUid()
795 if (null === $this->uid
) {
796 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
797 $this->uid
= uniqid('', true);
801 public function cleanUid()
807 * Used in the entries filter so it's more explicit for the end user than the uid.
808 * Also used in the API.
811 * @SerializedName("is_public")
812 * @Groups({"entries_for_user"})
816 public function isPublic()
818 return null !== $this->uid
;
824 public function getHttpStatus()
826 return $this->httpStatus
;
830 * @param string $httpStatus
834 public function setHttpStatus($httpStatus)
836 $this->httpStatus
= $httpStatus;
844 public function getPublishedAt()
846 return $this->publishedAt
;
850 * @param \Datetime $publishedAt
854 public function setPublishedAt(\Datetime
$publishedAt)
856 $this->publishedAt
= $publishedAt;
864 public function getPublishedBy()
866 return $this->publishedBy
;
870 * @param array $publishedBy
874 public function setPublishedBy($publishedBy)
876 $this->publishedBy
= $publishedBy;
884 public function getHeaders()
886 return $this->headers
;
890 * @param array $headers
894 public function setHeaders($headers)
896 $this->headers
= $headers;
904 * @param string $originUrl
908 public function setOriginUrl($originUrl)
910 $this->originUrl
= $originUrl;
920 public function getOriginUrl()
922 return $this->originUrl
;
928 public function getHashedUrl()
930 return $this->hashedUrl
;
934 * @param mixed $hashedUrl
938 public function setHashedUrl($hashedUrl)
940 $this->hashedUrl
= $hashedUrl;