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