]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Entry.php
Fixed entry shortcuts available in list view
[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={@ORM\Index(name="created_at", columns={"created_at"})}
26 * )
27 * @ORM\HasLifecycleCallbacks()
28 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
29 */
30 class Entry
31 {
32 /** @Serializer\XmlAttribute */
33 /**
34 * @var int
35 *
36 * @ORM\Column(name="id", type="integer")
37 * @ORM\Id
38 * @ORM\GeneratedValue(strategy="AUTO")
39 *
40 * @Groups({"entries_for_user", "export_all"})
41 */
42 private $id;
43
44 /**
45 * @var string
46 *
47 * @ORM\Column(name="uuid", type="text", nullable=true)
48 *
49 * @Groups({"entries_for_user", "export_all"})
50 */
51 private $uuid;
52
53 /**
54 * @var string
55 *
56 * @ORM\Column(name="title", type="text", nullable=true)
57 *
58 * @Groups({"entries_for_user", "export_all"})
59 */
60 private $title;
61
62 /**
63 * @var string
64 *
65 * @Assert\NotBlank()
66 * @ORM\Column(name="url", type="text", nullable=true)
67 *
68 * @Groups({"entries_for_user", "export_all"})
69 */
70 private $url;
71
72 /**
73 * @var bool
74 *
75 * @Exclude
76 *
77 * @ORM\Column(name="is_archived", type="boolean")
78 *
79 * @Groups({"entries_for_user", "export_all"})
80 */
81 private $isArchived = false;
82
83 /**
84 * @var bool
85 *
86 * @Exclude
87 *
88 * @ORM\Column(name="is_starred", type="boolean")
89 *
90 * @Groups({"entries_for_user", "export_all"})
91 */
92 private $isStarred = false;
93
94 /**
95 * @var string
96 *
97 * @ORM\Column(name="content", type="text", nullable=true)
98 *
99 * @Groups({"entries_for_user", "export_all"})
100 */
101 private $content;
102
103 /**
104 * @var \DateTime
105 *
106 * @ORM\Column(name="created_at", type="datetime")
107 *
108 * @Groups({"entries_for_user", "export_all"})
109 */
110 private $createdAt;
111
112 /**
113 * @var \DateTime
114 *
115 * @ORM\Column(name="updated_at", type="datetime")
116 *
117 * @Groups({"entries_for_user", "export_all"})
118 */
119 private $updatedAt;
120
121 /**
122 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
123 * @ORM\JoinTable
124 *
125 * @Groups({"entries_for_user", "export_all"})
126 */
127 private $annotations;
128
129 /**
130 * @var string
131 *
132 * @ORM\Column(name="mimetype", type="text", nullable=true)
133 *
134 * @Groups({"entries_for_user", "export_all"})
135 */
136 private $mimetype;
137
138 /**
139 * @var string
140 *
141 * @ORM\Column(name="language", type="text", nullable=true)
142 *
143 * @Groups({"entries_for_user", "export_all"})
144 */
145 private $language;
146
147 /**
148 * @var int
149 *
150 * @ORM\Column(name="reading_time", type="integer", nullable=true)
151 *
152 * @Groups({"entries_for_user", "export_all"})
153 */
154 private $readingTime;
155
156 /**
157 * @var string
158 *
159 * @ORM\Column(name="domain_name", type="text", nullable=true)
160 *
161 * @Groups({"entries_for_user", "export_all"})
162 */
163 private $domainName;
164
165 /**
166 * @var string
167 *
168 * @ORM\Column(name="preview_picture", type="text", nullable=true)
169 *
170 * @Groups({"entries_for_user", "export_all"})
171 */
172 private $previewPicture;
173
174 /**
175 * @var bool
176 *
177 * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false})
178 *
179 * @Groups({"export_all"})
180 */
181 private $isPublic;
182
183 /**
184 * @var string
185 *
186 * @ORM\Column(name="http_status", type="text", nullable=true)
187 *
188 * @Groups({"entries_for_user", "export_all"})
189 */
190 private $httpStatus;
191
192 /**
193 * @Exclude
194 *
195 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
196 *
197 * @Groups({"export_all"})
198 */
199 private $user;
200
201 /**
202 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
203 * @ORM\JoinTable(
204 * name="entry_tag",
205 * joinColumns={
206 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
207 * },
208 * inverseJoinColumns={
209 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
210 * }
211 * )
212 */
213 private $tags;
214
215 /*
216 * @param User $user
217 */
218 public function __construct(User $user)
219 {
220 $this->user = $user;
221 $this->tags = new ArrayCollection();
222 }
223
224 /**
225 * Get id.
226 *
227 * @return int
228 */
229 public function getId()
230 {
231 return $this->id;
232 }
233
234 /**
235 * Set title.
236 *
237 * @param string $title
238 *
239 * @return Entry
240 */
241 public function setTitle($title)
242 {
243 $this->title = $title;
244
245 return $this;
246 }
247
248 /**
249 * Get title.
250 *
251 * @return string
252 */
253 public function getTitle()
254 {
255 return $this->title;
256 }
257
258 /**
259 * Set url.
260 *
261 * @param string $url
262 *
263 * @return Entry
264 */
265 public function setUrl($url)
266 {
267 $this->url = $url;
268
269 return $this;
270 }
271
272 /**
273 * Get url.
274 *
275 * @return string
276 */
277 public function getUrl()
278 {
279 return $this->url;
280 }
281
282 /**
283 * Set isArchived.
284 *
285 * @param bool $isArchived
286 *
287 * @return Entry
288 */
289 public function setArchived($isArchived)
290 {
291 $this->isArchived = $isArchived;
292
293 return $this;
294 }
295
296 /**
297 * Get isArchived.
298 *
299 * @return bool
300 */
301 public function isArchived()
302 {
303 return $this->isArchived;
304 }
305
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
316 public function toggleArchive()
317 {
318 $this->isArchived = $this->isArchived() ^ 1;
319
320 return $this;
321 }
322
323 /**
324 * Set isStarred.
325 *
326 * @param bool $isStarred
327 *
328 * @return Entry
329 */
330 public function setStarred($isStarred)
331 {
332 $this->isStarred = $isStarred;
333
334 return $this;
335 }
336
337 /**
338 * Get isStarred.
339 *
340 * @return bool
341 */
342 public function isStarred()
343 {
344 return $this->isStarred;
345 }
346
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
357 public function toggleStar()
358 {
359 $this->isStarred = $this->isStarred() ^ 1;
360
361 return $this;
362 }
363
364 /**
365 * Set content.
366 *
367 * @param string $content
368 *
369 * @return Entry
370 */
371 public function setContent($content)
372 {
373 $this->content = $content;
374
375 return $this;
376 }
377
378 /**
379 * Get content.
380 *
381 * @return string
382 */
383 public function getContent()
384 {
385 return $this->content;
386 }
387
388 /**
389 * @return User
390 */
391 public function getUser()
392 {
393 return $this->user;
394 }
395
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
423 /**
424 * Set created_at.
425 * Only used when importing data from an other service.
426 *
427 * @param \DateTime $createdAt
428 *
429 * @return Entry
430 */
431 public function setCreatedAt(\DateTime $createdAt)
432 {
433 $this->createdAt = $createdAt;
434
435 return $this;
436 }
437
438 /**
439 * @return \DateTime
440 */
441 public function getCreatedAt()
442 {
443 return $this->createdAt;
444 }
445
446 /**
447 * @return \DateTime
448 */
449 public function getUpdatedAt()
450 {
451 return $this->updatedAt;
452 }
453
454 /**
455 * @ORM\PrePersist
456 * @ORM\PreUpdate
457 */
458 public function timestamps()
459 {
460 if (is_null($this->createdAt)) {
461 $this->createdAt = new \DateTime();
462 }
463
464 $this->updatedAt = new \DateTime();
465 }
466
467 /**
468 * @return ArrayCollection<Annotation>
469 */
470 public function getAnnotations()
471 {
472 return $this->annotations;
473 }
474
475 /**
476 * @param Annotation $annotation
477 */
478 public function setAnnotation(Annotation $annotation)
479 {
480 $this->annotations[] = $annotation;
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 /**
532 * @return bool
533 */
534 public function isPublic()
535 {
536 return $this->isPublic;
537 }
538
539 /**
540 * @param bool $isPublic
541 */
542 public function setIsPublic($isPublic)
543 {
544 $this->isPublic = $isPublic;
545 }
546
547 /**
548 * @return ArrayCollection<Tag>
549 */
550 public function getTags()
551 {
552 return $this->tags;
553 }
554
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
570 /**
571 * @param Tag $tag
572 */
573 public function addTag(Tag $tag)
574 {
575 if ($this->tags->contains($tag)) {
576 return;
577 }
578
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) {
582 if ($existingTag->getLabel() === $tag->getLabel()) {
583 return;
584 }
585 }
586
587 $this->tags->add($tag);
588 $tag->addEntry($this);
589 }
590
591 public function removeTag(Tag $tag)
592 {
593 if (!$this->tags->contains($tag)) {
594 return;
595 }
596
597 $this->tags->removeElement($tag);
598 $tag->removeEntry($this);
599 }
600
601 /**
602 * Set previewPicture.
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 /**
616 * Get previewPicture.
617 *
618 * @return string
619 */
620 public function getPreviewPicture()
621 {
622 return $this->previewPicture;
623 }
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 }
648
649 /**
650 * @return string
651 */
652 public function getUuid()
653 {
654 return $this->uuid;
655 }
656
657 /**
658 * @param string $uuid
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 {
671 if (null === $this->uuid) {
672 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
673 $this->uuid = uniqid('', true);
674 }
675 }
676
677 public function cleanUuid()
678 {
679 $this->uuid = null;
680 }
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 }
701 }