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