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