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}})
32 * uniqueConstraints={@ORM\UniqueConstraint(name="IDX_entry_given_url",columns={"url", "given_url", "user_id"})}
34 * @ORM\HasLifecycleCallbacks()
35 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
39 use EntityTimestampsTrait
;
41 /** @Serializer\XmlAttribute */
45 * @ORM\Column(name="id", type="integer")
47 * @ORM\GeneratedValue(strategy="AUTO")
49 * @Groups({"entries_for_user", "export_all"})
56 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
58 * @Groups({"entries_for_user", "export_all"})
65 * @ORM\Column(name="title", type="text", nullable=true)
67 * @Groups({"entries_for_user", "export_all"})
74 * @ORM\Column(name="given_url", type="text", nullable=true)
76 * @Groups({"entries_for_user", "export_all"})
84 * @ORM\Column(name="url", type="text", nullable=true)
86 * @Groups({"entries_for_user", "export_all"})
93 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
102 * @ORM\Column(name="is_archived", type="boolean")
104 * @Groups({"entries_for_user", "export_all"})
106 private $isArchived = false;
111 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
113 * @Groups({"entries_for_user", "export_all"})
115 private $archivedAt = null;
122 * @ORM\Column(name="is_starred", type="boolean")
124 * @Groups({"entries_for_user", "export_all"})
126 private $isStarred = false;
131 * @ORM\Column(name="content", type="text", nullable=true)
133 * @Groups({"entries_for_user", "export_all"})
140 * @ORM\Column(name="created_at", type="datetime")
142 * @Groups({"entries_for_user", "export_all"})
149 * @ORM\Column(name="updated_at", type="datetime")
151 * @Groups({"entries_for_user", "export_all"})
158 * @ORM\Column(name="published_at", type="datetime", nullable=true)
160 * @Groups({"entries_for_user", "export_all"})
162 private $publishedAt;
167 * @ORM\Column(name="published_by", type="array", nullable=true)
169 * @Groups({"entries_for_user", "export_all"})
171 private $publishedBy;
176 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
178 * @Groups({"entries_for_user", "export_all"})
180 private $starredAt = null;
183 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
186 * @Groups({"entries_for_user", "export_all"})
188 private $annotations;
193 * @ORM\Column(name="mimetype", type="text", nullable=true)
195 * @Groups({"entries_for_user", "export_all"})
202 * @ORM\Column(name="language", type="text", nullable=true)
204 * @Groups({"entries_for_user", "export_all"})
211 * @ORM\Column(name="reading_time", type="integer", nullable=false)
213 * @Groups({"entries_for_user", "export_all"})
215 private $readingTime = 0;
220 * @ORM\Column(name="domain_name", type="text", nullable=true)
222 * @Groups({"entries_for_user", "export_all"})
229 * @ORM\Column(name="preview_picture", type="text", nullable=true)
231 * @Groups({"entries_for_user", "export_all"})
233 private $previewPicture;
238 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
240 * @Groups({"entries_for_user", "export_all"})
247 * @ORM\Column(name="headers", type="array", nullable=true)
249 * @Groups({"entries_for_user", "export_all"})
256 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
258 * @Groups({"export_all"})
263 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
267 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
269 * inverseJoinColumns={
270 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
279 * @ORM\Column(name="origin_url", type="text", nullable=true)
281 * @Groups({"entries_for_user", "export_all"})
288 public function __construct(User
$user)
291 $this->tags
= new ArrayCollection();
299 public function getId()
307 * @param string $title
311 public function setTitle($title)
313 $this->title
= $title;
323 public function getTitle()
331 * @param string $givenUrl
335 public function setGivenUrl($givenUrl)
337 $this->givenUrl
= $givenUrl;
347 public function getGivenUrl()
349 return $this->givenUrl
;
359 public function setUrl($url)
362 $this->hashedUrl
= UrlHasher
::hashUrl($url);
372 public function getUrl()
380 * @param bool $isArchived
384 public function setArchived($isArchived)
386 $this->isArchived
= $isArchived;
392 * update isArchived and archive_at fields.
394 * @param bool $isArchived
398 public function updateArchived($isArchived = false)
400 $this->setArchived($isArchived);
401 $this->setArchivedAt(null);
402 if ($this->isArchived()) {
403 $this->setArchivedAt(new \
DateTime());
410 * @return \DateTime|null
412 public function getArchivedAt()
414 return $this->archivedAt
;
418 * @param \DateTime|null $archivedAt
422 public function setArchivedAt($archivedAt = null)
424 $this->archivedAt
= $archivedAt;
434 public function isArchived()
436 return $this->isArchived
;
441 * @SerializedName("is_archived")
442 * @Groups({"entries_for_user", "export_all"})
444 public function is_Archived()
446 return (int) $this->isArchived();
449 public function toggleArchive()
451 $this->updateArchived($this->isArchived() ^
1);
459 * @param bool $isStarred
463 public function setStarred($isStarred)
465 $this->isStarred
= $isStarred;
475 public function isStarred()
477 return $this->isStarred
;
482 * @SerializedName("is_starred")
483 * @Groups({"entries_for_user", "export_all"})
485 public function is_Starred()
487 return (int) $this->isStarred();
490 public function toggleStar()
492 $this->isStarred
= $this->isStarred() ^
1;
500 * @param string $content
504 public function setContent($content)
506 $this->content
= $content;
516 public function getContent()
518 return $this->content
;
524 public function getUser()
531 * @SerializedName("user_name")
533 public function getUserName()
535 return $this->user
->getUserName();
540 * @SerializedName("user_email")
542 public function getUserEmail()
544 return $this->user
->getEmail();
549 * @SerializedName("user_id")
551 public function getUserId()
553 return $this->user
->getId();
558 * Only used when importing data from an other service.
560 * @param \DateTime $createdAt
564 public function setCreatedAt(\DateTime
$createdAt)
566 $this->createdAt
= $createdAt;
574 public function getCreatedAt()
576 return $this->createdAt
;
582 public function getUpdatedAt()
584 return $this->updatedAt
;
588 * @return \DateTime|null
590 public function getStarredAt()
592 return $this->starredAt
;
596 * @param \DateTime|null $starredAt
600 public function setStarredAt($starredAt = null)
602 $this->starredAt
= $starredAt;
608 * update isStarred and starred_at fields.
610 * @param bool $isStarred
614 public function updateStar($isStarred = false)
616 $this->setStarred($isStarred);
617 $this->setStarredAt(null);
618 if ($this->isStarred()) {
619 $this->setStarredAt(new \
DateTime());
626 * @return ArrayCollection<Annotation>
628 public function getAnnotations()
630 return $this->annotations
;
634 * @param Annotation $annotation
636 public function setAnnotation(Annotation
$annotation)
638 $this->annotations
[] = $annotation;
644 public function getMimetype()
646 return $this->mimetype
;
650 * @param string $mimetype
652 public function setMimetype($mimetype)
654 $this->mimetype
= $mimetype;
660 public function getReadingTime()
662 return $this->readingTime
;
666 * @param int $readingTime
668 public function setReadingTime($readingTime)
670 $this->readingTime
= $readingTime;
676 public function getDomainName()
678 return $this->domainName
;
682 * @param string $domainName
684 public function setDomainName($domainName)
686 $this->domainName
= $domainName;
690 * @return ArrayCollection
692 public function getTags()
699 * @SerializedName("tags")
700 * @Groups({"entries_for_user", "export_all"})
702 public function getSerializedTags()
705 foreach ($this->tags
as $tag) {
706 $data[] = $tag->getLabel();
715 public function addTag(Tag
$tag)
717 if ($this->tags
->contains($tag)) {
721 // check if tag already exist but has not yet be persisted
722 // it seems that the previous condition with `contains()` doesn't check that case
723 foreach ($this->tags
as $existingTag) {
724 if ($existingTag->getLabel() === $tag->getLabel()) {
729 $this->tags
->add($tag);
730 $tag->addEntry($this);
734 * Remove the given tag from the entry (if the tag is associated).
738 public function removeTag(Tag
$tag)
740 if (!$this->tags
->contains($tag)) {
744 $this->tags
->removeElement($tag);
745 $tag->removeEntry($this);
749 * Remove all assigned tags from the entry.
751 public function removeAllTags()
753 foreach ($this->tags
as $tag) {
754 $this->tags
->removeElement($tag);
755 $tag->removeEntry($this);
760 * Set previewPicture.
762 * @param string $previewPicture
766 public function setPreviewPicture($previewPicture)
768 $this->previewPicture
= $previewPicture;
774 * Get previewPicture.
778 public function getPreviewPicture()
780 return $this->previewPicture
;
786 * @param string $language
790 public function setLanguage($language)
792 $this->language
= $language;
802 public function getLanguage()
804 return $this->language
;
810 public function getUid()
820 public function setUid($uid)
827 public function generateUid()
829 if (null === $this->uid
) {
830 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
831 $this->uid
= uniqid('', true);
835 public function cleanUid()
841 * Used in the entries filter so it's more explicit for the end user than the uid.
842 * Also used in the API.
845 * @SerializedName("is_public")
846 * @Groups({"entries_for_user"})
850 public function isPublic()
852 return null !== $this->uid
;
858 public function getHttpStatus()
860 return $this->httpStatus
;
864 * @param string $httpStatus
868 public function setHttpStatus($httpStatus)
870 $this->httpStatus
= $httpStatus;
878 public function getPublishedAt()
880 return $this->publishedAt
;
884 * @param \Datetime $publishedAt
888 public function setPublishedAt(\Datetime
$publishedAt)
890 $this->publishedAt
= $publishedAt;
898 public function getPublishedBy()
900 return $this->publishedBy
;
904 * @param array $publishedBy
908 public function setPublishedBy($publishedBy)
910 $this->publishedBy
= $publishedBy;
918 public function getHeaders()
920 return $this->headers
;
924 * @param array $headers
928 public function setHeaders($headers)
930 $this->headers
= $headers;
938 * @param string $originUrl
942 public function setOriginUrl($originUrl)
944 $this->originUrl
= $originUrl;
954 public function getOriginUrl()
956 return $this->originUrl
;
962 public function getHashedUrl()
964 return $this->hashedUrl
;
968 * @param mixed $hashedUrl
972 public function setHashedUrl($hashedUrl)
974 $this->hashedUrl
= $hashedUrl;