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