]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Entry.php
Remove isPublic from Entry entity fix #2598
[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={
26 * @ORM\Index(name="created_at", columns={"created_at"}),
27 * @ORM\Index(name="uid", columns={"uid"})
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="uid", type="string", length=23, nullable=true)
51 *
52 * @Groups({"entries_for_user", "export_all"})
53 */
54 private $uid;
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 string
179 *
180 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
181 *
182 * @Groups({"entries_for_user", "export_all"})
183 */
184 private $httpStatus;
185
186 /**
187 * @Exclude
188 *
189 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
190 *
191 * @Groups({"export_all"})
192 */
193 private $user;
194
195 /**
196 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
197 * @ORM\JoinTable(
198 * name="entry_tag",
199 * joinColumns={
200 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
201 * },
202 * inverseJoinColumns={
203 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
204 * }
205 * )
206 */
207 private $tags;
208
209 /*
210 * @param User $user
211 */
212 public function __construct(User $user)
213 {
214 $this->user = $user;
215 $this->tags = new ArrayCollection();
216 }
217
218 /**
219 * Get id.
220 *
221 * @return int
222 */
223 public function getId()
224 {
225 return $this->id;
226 }
227
228 /**
229 * Set title.
230 *
231 * @param string $title
232 *
233 * @return Entry
234 */
235 public function setTitle($title)
236 {
237 $this->title = $title;
238
239 return $this;
240 }
241
242 /**
243 * Get title.
244 *
245 * @return string
246 */
247 public function getTitle()
248 {
249 return $this->title;
250 }
251
252 /**
253 * Set url.
254 *
255 * @param string $url
256 *
257 * @return Entry
258 */
259 public function setUrl($url)
260 {
261 $this->url = $url;
262
263 return $this;
264 }
265
266 /**
267 * Get url.
268 *
269 * @return string
270 */
271 public function getUrl()
272 {
273 return $this->url;
274 }
275
276 /**
277 * Set isArchived.
278 *
279 * @param bool $isArchived
280 *
281 * @return Entry
282 */
283 public function setArchived($isArchived)
284 {
285 $this->isArchived = $isArchived;
286
287 return $this;
288 }
289
290 /**
291 * Get isArchived.
292 *
293 * @return bool
294 */
295 public function isArchived()
296 {
297 return $this->isArchived;
298 }
299
300 /**
301 * @VirtualProperty
302 * @SerializedName("is_archived")
303 * @Groups({"entries_for_user", "export_all"})
304 */
305 public function is_Archived()
306 {
307 return (int) $this->isArchived();
308 }
309
310 public function toggleArchive()
311 {
312 $this->isArchived = $this->isArchived() ^ 1;
313
314 return $this;
315 }
316
317 /**
318 * Set isStarred.
319 *
320 * @param bool $isStarred
321 *
322 * @return Entry
323 */
324 public function setStarred($isStarred)
325 {
326 $this->isStarred = $isStarred;
327
328 return $this;
329 }
330
331 /**
332 * Get isStarred.
333 *
334 * @return bool
335 */
336 public function isStarred()
337 {
338 return $this->isStarred;
339 }
340
341 /**
342 * @VirtualProperty
343 * @SerializedName("is_starred")
344 * @Groups({"entries_for_user", "export_all"})
345 */
346 public function is_Starred()
347 {
348 return (int) $this->isStarred();
349 }
350
351 public function toggleStar()
352 {
353 $this->isStarred = $this->isStarred() ^ 1;
354
355 return $this;
356 }
357
358 /**
359 * Set content.
360 *
361 * @param string $content
362 *
363 * @return Entry
364 */
365 public function setContent($content)
366 {
367 $this->content = $content;
368
369 return $this;
370 }
371
372 /**
373 * Get content.
374 *
375 * @return string
376 */
377 public function getContent()
378 {
379 return $this->content;
380 }
381
382 /**
383 * @return User
384 */
385 public function getUser()
386 {
387 return $this->user;
388 }
389
390 /**
391 * @VirtualProperty
392 * @SerializedName("user_name")
393 */
394 public function getUserName()
395 {
396 return $this->user->getUserName();
397 }
398
399 /**
400 * @VirtualProperty
401 * @SerializedName("user_email")
402 */
403 public function getUserEmail()
404 {
405 return $this->user->getEmail();
406 }
407
408 /**
409 * @VirtualProperty
410 * @SerializedName("user_id")
411 */
412 public function getUserId()
413 {
414 return $this->user->getId();
415 }
416
417 /**
418 * Set created_at.
419 * Only used when importing data from an other service.
420 *
421 * @param \DateTime $createdAt
422 *
423 * @return Entry
424 */
425 public function setCreatedAt(\DateTime $createdAt)
426 {
427 $this->createdAt = $createdAt;
428
429 return $this;
430 }
431
432 /**
433 * @return \DateTime
434 */
435 public function getCreatedAt()
436 {
437 return $this->createdAt;
438 }
439
440 /**
441 * @return \DateTime
442 */
443 public function getUpdatedAt()
444 {
445 return $this->updatedAt;
446 }
447
448 /**
449 * @ORM\PrePersist
450 * @ORM\PreUpdate
451 */
452 public function timestamps()
453 {
454 if (is_null($this->createdAt)) {
455 $this->createdAt = new \DateTime();
456 }
457
458 $this->updatedAt = new \DateTime();
459 }
460
461 /**
462 * @return ArrayCollection<Annotation>
463 */
464 public function getAnnotations()
465 {
466 return $this->annotations;
467 }
468
469 /**
470 * @param Annotation $annotation
471 */
472 public function setAnnotation(Annotation $annotation)
473 {
474 $this->annotations[] = $annotation;
475 }
476
477 /**
478 * @return string
479 */
480 public function getMimetype()
481 {
482 return $this->mimetype;
483 }
484
485 /**
486 * @param string $mimetype
487 */
488 public function setMimetype($mimetype)
489 {
490 $this->mimetype = $mimetype;
491 }
492
493 /**
494 * @return int
495 */
496 public function getReadingTime()
497 {
498 return $this->readingTime;
499 }
500
501 /**
502 * @param int $readingTime
503 */
504 public function setReadingTime($readingTime)
505 {
506 $this->readingTime = $readingTime;
507 }
508
509 /**
510 * @return string
511 */
512 public function getDomainName()
513 {
514 return $this->domainName;
515 }
516
517 /**
518 * @param string $domainName
519 */
520 public function setDomainName($domainName)
521 {
522 $this->domainName = $domainName;
523 }
524
525 /**
526 * @return ArrayCollection<Tag>
527 */
528 public function getTags()
529 {
530 return $this->tags;
531 }
532
533 /**
534 * @VirtualProperty
535 * @SerializedName("tags")
536 * @Groups({"entries_for_user", "export_all"})
537 */
538 public function getSerializedTags()
539 {
540 $data = [];
541 foreach ($this->tags as $tag) {
542 $data[] = $tag->getLabel();
543 }
544
545 return $data;
546 }
547
548 /**
549 * @param Tag $tag
550 */
551 public function addTag(Tag $tag)
552 {
553 if ($this->tags->contains($tag)) {
554 return;
555 }
556
557 // check if tag already exist but has not yet be persisted
558 // it seems that the previous condition with `contains()` doesn't check that case
559 foreach ($this->tags as $existingTag) {
560 if ($existingTag->getLabel() === $tag->getLabel()) {
561 return;
562 }
563 }
564
565 $this->tags->add($tag);
566 $tag->addEntry($this);
567 }
568
569 public function removeTag(Tag $tag)
570 {
571 if (!$this->tags->contains($tag)) {
572 return;
573 }
574
575 $this->tags->removeElement($tag);
576 $tag->removeEntry($this);
577 }
578
579 /**
580 * Set previewPicture.
581 *
582 * @param string $previewPicture
583 *
584 * @return Entry
585 */
586 public function setPreviewPicture($previewPicture)
587 {
588 $this->previewPicture = $previewPicture;
589
590 return $this;
591 }
592
593 /**
594 * Get previewPicture.
595 *
596 * @return string
597 */
598 public function getPreviewPicture()
599 {
600 return $this->previewPicture;
601 }
602
603 /**
604 * Set language.
605 *
606 * @param string $language
607 *
608 * @return Entry
609 */
610 public function setLanguage($language)
611 {
612 $this->language = $language;
613
614 return $this;
615 }
616
617 /**
618 * Get language.
619 *
620 * @return string
621 */
622 public function getLanguage()
623 {
624 return $this->language;
625 }
626
627 /**
628 * @return string
629 */
630 public function getUid()
631 {
632 return $this->uid;
633 }
634
635 /**
636 * @param string $uid
637 *
638 * @return Entry
639 */
640 public function setUid($uid)
641 {
642 $this->uid = $uid;
643
644 return $this;
645 }
646
647 public function generateUid()
648 {
649 if (null === $this->uid) {
650 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
651 $this->uid = uniqid('', true);
652 }
653 }
654
655 public function cleanUid()
656 {
657 $this->uid = null;
658 }
659
660 /**
661 * @return int
662 */
663 public function getHttpStatus()
664 {
665 return $this->httpStatus;
666 }
667
668 /**
669 * @param int $httpStatus
670 *
671 * @return Entry
672 */
673 public function setHttpStatus($httpStatus)
674 {
675 $this->httpStatus = $httpStatus;
676
677 return $this;
678 }
679 }