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