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"}),
29 * @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}})
32 * @ORM\HasLifecycleCallbacks()
33 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
37 use EntityTimestampsTrait
;
39 /** @Serializer\XmlAttribute */
43 * @ORM\Column(name="id", type="integer")
45 * @ORM\GeneratedValue(strategy="AUTO")
47 * @Groups({"entries_for_user", "export_all"})
54 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
56 * @Groups({"entries_for_user", "export_all"})
63 * @ORM\Column(name="title", type="text", nullable=true)
65 * @Groups({"entries_for_user", "export_all"})
73 * @ORM\Column(name="url", type="text", nullable=true)
75 * @Groups({"entries_for_user", "export_all"})
82 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
91 * @ORM\Column(name="is_archived", type="boolean")
93 * @Groups({"entries_for_user", "export_all"})
95 private $isArchived = false;
100 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
102 * @Groups({"entries_for_user", "export_all"})
104 private $archivedAt = null;
111 * @ORM\Column(name="is_starred", type="boolean")
113 * @Groups({"entries_for_user", "export_all"})
115 private $isStarred = false;
120 * @ORM\Column(name="content", type="text", nullable=true)
122 * @Groups({"entries_for_user", "export_all"})
129 * @ORM\Column(name="created_at", type="datetime")
131 * @Groups({"entries_for_user", "export_all"})
138 * @ORM\Column(name="updated_at", type="datetime")
140 * @Groups({"entries_for_user", "export_all"})
147 * @ORM\Column(name="published_at", type="datetime", nullable=true)
149 * @Groups({"entries_for_user", "export_all"})
151 private $publishedAt;
156 * @ORM\Column(name="published_by", type="array", nullable=true)
158 * @Groups({"entries_for_user", "export_all"})
160 private $publishedBy;
165 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
167 * @Groups({"entries_for_user", "export_all"})
169 private $starredAt = null;
172 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
175 * @Groups({"entries_for_user", "export_all"})
177 private $annotations;
182 * @ORM\Column(name="mimetype", type="text", nullable=true)
184 * @Groups({"entries_for_user", "export_all"})
191 * @ORM\Column(name="language", type="text", nullable=true)
193 * @Groups({"entries_for_user", "export_all"})
200 * @ORM\Column(name="reading_time", type="integer", nullable=false)
202 * @Groups({"entries_for_user", "export_all"})
204 private $readingTime = 0;
209 * @ORM\Column(name="domain_name", type="text", nullable=true)
211 * @Groups({"entries_for_user", "export_all"})
218 * @ORM\Column(name="preview_picture", type="text", nullable=true)
220 * @Groups({"entries_for_user", "export_all"})
222 private $previewPicture;
227 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
229 * @Groups({"entries_for_user", "export_all"})
236 * @ORM\Column(name="headers", type="array", nullable=true)
238 * @Groups({"entries_for_user", "export_all"})
245 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
247 * @Groups({"export_all"})
252 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
256 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
258 * inverseJoinColumns={
259 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
268 * @ORM\Column(name="origin_url", type="text", nullable=true)
270 * @Groups({"entries_for_user", "export_all"})
277 public function __construct(User
$user)
280 $this->tags
= new ArrayCollection();
288 public function getId()
296 * @param string $title
300 public function setTitle($title)
302 $this->title
= $title;
312 public function getTitle()
324 public function setUrl($url)
327 $this->hashedUrl
= hash('sha1', $url);
337 public function getUrl()
345 * @param bool $isArchived
349 public function setArchived($isArchived)
351 $this->isArchived
= $isArchived;
357 * update isArchived and archive_at fields.
359 * @param bool $isArchived
363 public function updateArchived($isArchived = false)
365 $this->setArchived($isArchived);
366 $this->setArchivedAt(null);
367 if ($this->isArchived()) {
368 $this->setArchivedAt(new \
DateTime());
375 * @return \DateTime|null
377 public function getArchivedAt()
379 return $this->archivedAt
;
383 * @param \DateTime|null $archivedAt
387 public function setArchivedAt($archivedAt = null)
389 $this->archivedAt
= $archivedAt;
399 public function isArchived()
401 return $this->isArchived
;
406 * @SerializedName("is_archived")
407 * @Groups({"entries_for_user", "export_all"})
409 public function is_Archived()
411 return (int) $this->isArchived();
414 public function toggleArchive()
416 $this->updateArchived($this->isArchived() ^
1);
424 * @param bool $isStarred
428 public function setStarred($isStarred)
430 $this->isStarred
= $isStarred;
440 public function isStarred()
442 return $this->isStarred
;
447 * @SerializedName("is_starred")
448 * @Groups({"entries_for_user", "export_all"})
450 public function is_Starred()
452 return (int) $this->isStarred();
455 public function toggleStar()
457 $this->isStarred
= $this->isStarred() ^
1;
465 * @param string $content
469 public function setContent($content)
471 $this->content
= $content;
481 public function getContent()
483 return $this->content
;
489 public function getUser()
496 * @SerializedName("user_name")
498 public function getUserName()
500 return $this->user
->getUserName();
505 * @SerializedName("user_email")
507 public function getUserEmail()
509 return $this->user
->getEmail();
514 * @SerializedName("user_id")
516 public function getUserId()
518 return $this->user
->getId();
523 * Only used when importing data from an other service.
525 * @param \DateTime $createdAt
529 public function setCreatedAt(\DateTime
$createdAt)
531 $this->createdAt
= $createdAt;
539 public function getCreatedAt()
541 return $this->createdAt
;
547 public function getUpdatedAt()
549 return $this->updatedAt
;
553 * @return \DateTime|null
555 public function getStarredAt()
557 return $this->starredAt
;
561 * @param \DateTime|null $starredAt
565 public function setStarredAt($starredAt = null)
567 $this->starredAt
= $starredAt;
573 * update isStarred and starred_at fields.
575 * @param bool $isStarred
579 public function updateStar($isStarred = false)
581 $this->setStarred($isStarred);
582 $this->setStarredAt(null);
583 if ($this->isStarred()) {
584 $this->setStarredAt(new \
DateTime());
591 * @return ArrayCollection<Annotation>
593 public function getAnnotations()
595 return $this->annotations
;
599 * @param Annotation $annotation
601 public function setAnnotation(Annotation
$annotation)
603 $this->annotations
[] = $annotation;
609 public function getMimetype()
611 return $this->mimetype
;
615 * @param string $mimetype
617 public function setMimetype($mimetype)
619 $this->mimetype
= $mimetype;
625 public function getReadingTime()
627 return $this->readingTime
;
631 * @param int $readingTime
633 public function setReadingTime($readingTime)
635 $this->readingTime
= $readingTime;
641 public function getDomainName()
643 return $this->domainName
;
647 * @param string $domainName
649 public function setDomainName($domainName)
651 $this->domainName
= $domainName;
655 * @return ArrayCollection
657 public function getTags()
664 * @SerializedName("tags")
665 * @Groups({"entries_for_user", "export_all"})
667 public function getSerializedTags()
670 foreach ($this->tags
as $tag) {
671 $data[] = $tag->getLabel();
680 public function addTag(Tag
$tag)
682 if ($this->tags
->contains($tag)) {
686 // check if tag already exist but has not yet be persisted
687 // it seems that the previous condition with `contains()` doesn't check that case
688 foreach ($this->tags
as $existingTag) {
689 if ($existingTag->getLabel() === $tag->getLabel()) {
694 $this->tags
->add($tag);
695 $tag->addEntry($this);
699 * Remove the given tag from the entry (if the tag is associated).
703 public function removeTag(Tag
$tag)
705 if (!$this->tags
->contains($tag)) {
709 $this->tags
->removeElement($tag);
710 $tag->removeEntry($this);
714 * Remove all assigned tags from the entry.
716 public function removeAllTags()
718 foreach ($this->tags
as $tag) {
719 $this->tags
->removeElement($tag);
720 $tag->removeEntry($this);
725 * Set previewPicture.
727 * @param string $previewPicture
731 public function setPreviewPicture($previewPicture)
733 $this->previewPicture
= $previewPicture;
739 * Get previewPicture.
743 public function getPreviewPicture()
745 return $this->previewPicture
;
751 * @param string $language
755 public function setLanguage($language)
757 $this->language
= $language;
767 public function getLanguage()
769 return $this->language
;
775 public function getUid()
785 public function setUid($uid)
792 public function generateUid()
794 if (null === $this->uid
) {
795 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
796 $this->uid
= uniqid('', true);
800 public function cleanUid()
806 * Used in the entries filter so it's more explicit for the end user than the uid.
807 * Also used in the API.
810 * @SerializedName("is_public")
811 * @Groups({"entries_for_user"})
815 public function isPublic()
817 return null !== $this->uid
;
823 public function getHttpStatus()
825 return $this->httpStatus
;
829 * @param string $httpStatus
833 public function setHttpStatus($httpStatus)
835 $this->httpStatus
= $httpStatus;
843 public function getPublishedAt()
845 return $this->publishedAt
;
849 * @param \Datetime $publishedAt
853 public function setPublishedAt(\Datetime
$publishedAt)
855 $this->publishedAt
= $publishedAt;
863 public function getPublishedBy()
865 return $this->publishedBy
;
869 * @param array $publishedBy
873 public function setPublishedBy($publishedBy)
875 $this->publishedBy
= $publishedBy;
883 public function getHeaders()
885 return $this->headers
;
889 * @param array $headers
893 public function setHeaders($headers)
895 $this->headers
= $headers;
903 * @param string $originUrl
907 public function setOriginUrl($originUrl)
909 $this->originUrl
= $originUrl;
919 public function getOriginUrl()
921 return $this->originUrl
;
927 public function getHashedUrl()
929 return $this->hashedUrl
;
933 * @param mixed $hashedUrl
937 public function setHashedUrl($hashedUrl)
939 $this->hashedUrl
= $hashedUrl;