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