]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Entity; |
9d50517c | 4 | |
0a018fe0 | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
0f006880 | 7 | use Hateoas\Configuration\Annotation as Hateoas; |
268e9e72 | 8 | use JMS\Serializer\Annotation\Groups; |
0f006880 | 9 | use JMS\Serializer\Annotation\XmlRoot; |
7d1fdab2 TC |
10 | use JMS\Serializer\Annotation\Exclude; |
11 | use JMS\Serializer\Annotation\VirtualProperty; | |
12 | use JMS\Serializer\Annotation\SerializedName; | |
619cc453 | 13 | use Symfony\Component\Validator\Constraints as Assert; |
1210dae1 | 14 | use Wallabag\UserBundle\Entity\User; |
4dc87223 | 15 | use 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 | 26 | class 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 NL |
40 | /** |
41 | * @var int | |
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 NL |
99 | /** |
100 | * @var date | |
101 | * | |
be463487 | 102 | * @ORM\Column(name="created_at", type="datetime") |
5b7da076 TC |
103 | * |
104 | * @Groups({"export_all"}) | |
34d15eb4 NL |
105 | */ |
106 | private $createdAt; | |
107 | ||
108 | /** | |
109 | * @var date | |
110 | * | |
be463487 | 111 | * @ORM\Column(name="updated_at", type="datetime") |
5b7da076 TC |
112 | * |
113 | * @Groups({"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 | * ) | |
5b7da076 TC |
199 | * |
200 | * @Groups({"entries_for_user", "export_all"}) | |
0a018fe0 NL |
201 | */ |
202 | private $tags; | |
203 | ||
5f09650e NL |
204 | /* |
205 | * @param User $user | |
206 | */ | |
5d6f6f56 | 207 | public function __construct(User $user) |
5f09650e NL |
208 | { |
209 | $this->user = $user; | |
0a018fe0 | 210 | $this->tags = new ArrayCollection(); |
5f09650e NL |
211 | } |
212 | ||
9d50517c | 213 | /** |
4346a860 | 214 | * Get id. |
9d50517c | 215 | * |
4346a860 | 216 | * @return int |
9d50517c NL |
217 | */ |
218 | public function getId() | |
219 | { | |
220 | return $this->id; | |
221 | } | |
222 | ||
223 | /** | |
4346a860 JB |
224 | * Set title. |
225 | * | |
226 | * @param string $title | |
9d50517c | 227 | * |
be463487 | 228 | * @return Entry |
9d50517c NL |
229 | */ |
230 | public function setTitle($title) | |
231 | { | |
232 | $this->title = $title; | |
233 | ||
234 | return $this; | |
235 | } | |
236 | ||
237 | /** | |
4346a860 | 238 | * Get title. |
9d50517c | 239 | * |
7df80cb3 | 240 | * @return string |
9d50517c NL |
241 | */ |
242 | public function getTitle() | |
243 | { | |
244 | return $this->title; | |
245 | } | |
246 | ||
247 | /** | |
4346a860 JB |
248 | * Set url. |
249 | * | |
250 | * @param string $url | |
9d50517c | 251 | * |
be463487 | 252 | * @return Entry |
9d50517c NL |
253 | */ |
254 | public function setUrl($url) | |
255 | { | |
256 | $this->url = $url; | |
257 | ||
258 | return $this; | |
259 | } | |
260 | ||
261 | /** | |
4346a860 | 262 | * Get url. |
9d50517c | 263 | * |
7df80cb3 | 264 | * @return string |
9d50517c NL |
265 | */ |
266 | public function getUrl() | |
267 | { | |
268 | return $this->url; | |
269 | } | |
270 | ||
271 | /** | |
4346a860 JB |
272 | * Set isArchived. |
273 | * | |
8eedc8cf | 274 | * @param bool $isArchived |
9d50517c | 275 | * |
be463487 | 276 | * @return Entry |
9d50517c | 277 | */ |
905ae369 | 278 | public function setArchived($isArchived) |
9d50517c | 279 | { |
905ae369 | 280 | $this->isArchived = $isArchived; |
9d50517c NL |
281 | |
282 | return $this; | |
283 | } | |
284 | ||
285 | /** | |
4346a860 | 286 | * Get isArchived. |
9d50517c | 287 | * |
8eedc8cf | 288 | * @return bool |
9d50517c | 289 | */ |
905ae369 | 290 | public function isArchived() |
9d50517c | 291 | { |
905ae369 | 292 | return $this->isArchived; |
9d50517c NL |
293 | } |
294 | ||
189ef634 TC |
295 | /** |
296 | * @VirtualProperty | |
297 | * @SerializedName("is_archived") | |
298 | * @Groups({"entries_for_user", "export_all"}) | |
299 | */ | |
300 | public function is_Archived() | |
301 | { | |
302 | return (int) $this->isArchived(); | |
303 | } | |
304 | ||
163eae0b NL |
305 | public function toggleArchive() |
306 | { | |
905ae369 | 307 | $this->isArchived = $this->isArchived() ^ 1; |
7df80cb3 | 308 | |
163eae0b NL |
309 | return $this; |
310 | } | |
311 | ||
9d50517c | 312 | /** |
4346a860 JB |
313 | * Set isStarred. |
314 | * | |
8eedc8cf | 315 | * @param bool $isStarred |
9d50517c | 316 | * |
be463487 | 317 | * @return Entry |
9d50517c | 318 | */ |
905ae369 | 319 | public function setStarred($isStarred) |
9d50517c | 320 | { |
905ae369 | 321 | $this->isStarred = $isStarred; |
9d50517c NL |
322 | |
323 | return $this; | |
324 | } | |
325 | ||
326 | /** | |
4346a860 | 327 | * Get isStarred. |
9d50517c | 328 | * |
8eedc8cf | 329 | * @return bool |
9d50517c | 330 | */ |
905ae369 | 331 | public function isStarred() |
9d50517c | 332 | { |
905ae369 | 333 | return $this->isStarred; |
9d50517c NL |
334 | } |
335 | ||
189ef634 TC |
336 | /** |
337 | * @VirtualProperty | |
338 | * @SerializedName("is_starred") | |
339 | * @Groups({"entries_for_user", "export_all"}) | |
340 | */ | |
341 | public function is_Starred() | |
342 | { | |
343 | return (int) $this->isStarred(); | |
344 | } | |
345 | ||
163eae0b NL |
346 | public function toggleStar() |
347 | { | |
905ae369 | 348 | $this->isStarred = $this->isStarred() ^ 1; |
163eae0b NL |
349 | |
350 | return $this; | |
351 | } | |
352 | ||
9d50517c | 353 | /** |
4346a860 JB |
354 | * Set content. |
355 | * | |
356 | * @param string $content | |
9d50517c | 357 | * |
be463487 | 358 | * @return Entry |
9d50517c NL |
359 | */ |
360 | public function setContent($content) | |
361 | { | |
362 | $this->content = $content; | |
363 | ||
364 | return $this; | |
365 | } | |
366 | ||
367 | /** | |
4346a860 | 368 | * Get content. |
9d50517c | 369 | * |
7df80cb3 | 370 | * @return string |
9d50517c NL |
371 | */ |
372 | public function getContent() | |
373 | { | |
374 | return $this->content; | |
375 | } | |
376 | ||
377 | /** | |
5f09650e | 378 | * @return User |
9d50517c | 379 | */ |
5f09650e | 380 | public function getUser() |
9d50517c | 381 | { |
5f09650e | 382 | return $this->user; |
9d50517c | 383 | } |
42a90646 | 384 | |
7d1fdab2 TC |
385 | /** |
386 | * @VirtualProperty | |
387 | * @SerializedName("user_name") | |
388 | */ | |
389 | public function getUserName() | |
390 | { | |
391 | return $this->user->getUserName(); | |
392 | } | |
393 | ||
394 | /** | |
395 | * @VirtualProperty | |
396 | * @SerializedName("user_email") | |
397 | */ | |
398 | public function getUserEmail() | |
399 | { | |
400 | return $this->user->getEmail(); | |
401 | } | |
402 | ||
403 | /** | |
404 | * @VirtualProperty | |
405 | * @SerializedName("user_id") | |
406 | */ | |
407 | public function getUserId() | |
408 | { | |
409 | return $this->user->getId(); | |
410 | } | |
411 | ||
34d15eb4 | 412 | /** |
2f69eb4a | 413 | * @return string |
34d15eb4 NL |
414 | */ |
415 | public function getCreatedAt() | |
416 | { | |
417 | return $this->createdAt; | |
418 | } | |
419 | ||
34d15eb4 NL |
420 | /** |
421 | * @return string | |
422 | */ | |
423 | public function getUpdatedAt() | |
424 | { | |
425 | return $this->updatedAt; | |
426 | } | |
427 | ||
428 | /** | |
be463487 | 429 | * @ORM\PrePersist |
34d15eb4 NL |
430 | * @ORM\PreUpdate |
431 | */ | |
be463487 | 432 | public function timestamps() |
34d15eb4 | 433 | { |
be463487 NL |
434 | if (is_null($this->createdAt)) { |
435 | $this->createdAt = new \DateTime(); | |
436 | } | |
437 | ||
34d15eb4 | 438 | $this->updatedAt = new \DateTime(); |
f3d0cb91 NL |
439 | |
440 | $this->generateUuid(); | |
34d15eb4 NL |
441 | } |
442 | ||
443 | /** | |
4dc87223 | 444 | * @return ArrayCollection<Annotation> |
34d15eb4 | 445 | */ |
4dc87223 | 446 | public function getAnnotations() |
34d15eb4 | 447 | { |
4dc87223 | 448 | return $this->annotations; |
34d15eb4 NL |
449 | } |
450 | ||
451 | /** | |
4dc87223 | 452 | * @param Annotation $annotation |
34d15eb4 | 453 | */ |
4dc87223 | 454 | public function setAnnotation(Annotation $annotation) |
34d15eb4 | 455 | { |
4dc87223 | 456 | $this->annotations[] = $annotation; |
34d15eb4 NL |
457 | } |
458 | ||
459 | /** | |
460 | * @return string | |
461 | */ | |
462 | public function getMimetype() | |
463 | { | |
464 | return $this->mimetype; | |
465 | } | |
466 | ||
467 | /** | |
468 | * @param string $mimetype | |
469 | */ | |
470 | public function setMimetype($mimetype) | |
471 | { | |
472 | $this->mimetype = $mimetype; | |
473 | } | |
474 | ||
475 | /** | |
476 | * @return int | |
477 | */ | |
478 | public function getReadingTime() | |
479 | { | |
480 | return $this->readingTime; | |
481 | } | |
482 | ||
483 | /** | |
484 | * @param int $readingTime | |
485 | */ | |
486 | public function setReadingTime($readingTime) | |
487 | { | |
488 | $this->readingTime = $readingTime; | |
489 | } | |
490 | ||
491 | /** | |
492 | * @return string | |
493 | */ | |
494 | public function getDomainName() | |
495 | { | |
496 | return $this->domainName; | |
497 | } | |
498 | ||
499 | /** | |
500 | * @param string $domainName | |
501 | */ | |
502 | public function setDomainName($domainName) | |
503 | { | |
504 | $this->domainName = $domainName; | |
505 | } | |
506 | ||
507 | /** | |
4346a860 | 508 | * @return bool |
34d15eb4 | 509 | */ |
2c093b03 | 510 | public function isPublic() |
34d15eb4 NL |
511 | { |
512 | return $this->isPublic; | |
513 | } | |
514 | ||
515 | /** | |
4346a860 | 516 | * @param bool $isPublic |
34d15eb4 | 517 | */ |
82d6d9cb | 518 | public function setIsPublic($isPublic) |
34d15eb4 NL |
519 | { |
520 | $this->isPublic = $isPublic; | |
521 | } | |
0a018fe0 NL |
522 | |
523 | /** | |
524 | * @return ArrayCollection<Tag> | |
525 | */ | |
526 | public function getTags() | |
527 | { | |
528 | return $this->tags; | |
529 | } | |
530 | ||
531 | /** | |
532 | * @param Tag $tag | |
533 | */ | |
534 | public function addTag(Tag $tag) | |
535 | { | |
625acf33 KG |
536 | if ($this->tags->contains($tag)) { |
537 | return; | |
538 | } | |
539 | ||
fc031e57 JB |
540 | // check if tag already exist but has not yet be persisted |
541 | // it seems that the previous condition with `contains()` doesn't check that case | |
542 | foreach ($this->tags as $existingTag) { | |
fc732227 | 543 | if ($existingTag->getLabel() === $tag->getLabel()) { |
fc031e57 JB |
544 | return; |
545 | } | |
546 | } | |
547 | ||
e42b13bc | 548 | $this->tags->add($tag); |
46bbd8d3 | 549 | $tag->addEntry($this); |
0a018fe0 | 550 | } |
092ca707 NL |
551 | |
552 | public function removeTag(Tag $tag) | |
553 | { | |
e42b13bc JB |
554 | if (!$this->tags->contains($tag)) { |
555 | return; | |
556 | } | |
557 | ||
092ca707 | 558 | $this->tags->removeElement($tag); |
e42b13bc | 559 | $tag->removeEntry($this); |
092ca707 | 560 | } |
fad31615 JB |
561 | |
562 | /** | |
a1413a3d | 563 | * Set previewPicture. |
fad31615 JB |
564 | * |
565 | * @param string $previewPicture | |
566 | * | |
567 | * @return Entry | |
568 | */ | |
569 | public function setPreviewPicture($previewPicture) | |
570 | { | |
571 | $this->previewPicture = $previewPicture; | |
572 | ||
573 | return $this; | |
574 | } | |
575 | ||
576 | /** | |
a1413a3d | 577 | * Get previewPicture. |
fad31615 JB |
578 | * |
579 | * @return string | |
580 | */ | |
581 | public function getPreviewPicture() | |
582 | { | |
583 | return $this->previewPicture; | |
584 | } | |
98f0929f JB |
585 | |
586 | /** | |
587 | * Set language. | |
588 | * | |
589 | * @param string $language | |
590 | * | |
591 | * @return Entry | |
592 | */ | |
593 | public function setLanguage($language) | |
594 | { | |
595 | $this->language = $language; | |
596 | ||
597 | return $this; | |
598 | } | |
599 | ||
600 | /** | |
601 | * Get language. | |
602 | * | |
603 | * @return string | |
604 | */ | |
605 | public function getLanguage() | |
606 | { | |
607 | return $this->language; | |
608 | } | |
f3d0cb91 NL |
609 | |
610 | /** | |
611 | * @return int | |
612 | */ | |
613 | public function getUuid() | |
614 | { | |
615 | return $this->uuid; | |
616 | } | |
617 | ||
618 | /** | |
619 | * @param int $uuid | |
620 | * | |
621 | * @return Entry | |
622 | */ | |
623 | public function setUuid($uuid) | |
624 | { | |
625 | $this->uuid = $uuid; | |
626 | ||
627 | return $this; | |
628 | } | |
629 | ||
630 | public function generateUuid() | |
631 | { | |
632 | if (empty($this->uuid) || is_null($this->uuid)) { | |
a7e2218e NL |
633 | // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter |
634 | $this->uuid = uniqid('', true); | |
f3d0cb91 NL |
635 | } |
636 | } | |
9d50517c | 637 | } |