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