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