]>
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", "remove"}) | |
181 | * @ORM\JoinTable | |
182 | * | |
183 | * @Groups({"entries_for_user", "export_all"}) | |
184 | */ | |
185 | private $tags; | |
186 | ||
187 | /* | |
188 | * @param User $user | |
189 | */ | |
190 | public function __construct(User $user) | |
191 | { | |
192 | $this->user = $user; | |
193 | $this->tags = new ArrayCollection(); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get id. | |
198 | * | |
199 | * @return int | |
200 | */ | |
201 | public function getId() | |
202 | { | |
203 | return $this->id; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Set title. | |
208 | * | |
209 | * @param string $title | |
210 | * | |
211 | * @return Entry | |
212 | */ | |
213 | public function setTitle($title) | |
214 | { | |
215 | $this->title = $title; | |
216 | ||
217 | return $this; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Get title. | |
222 | * | |
223 | * @return string | |
224 | */ | |
225 | public function getTitle() | |
226 | { | |
227 | return $this->title; | |
228 | } | |
229 | ||
230 | /** | |
231 | * Set url. | |
232 | * | |
233 | * @param string $url | |
234 | * | |
235 | * @return Entry | |
236 | */ | |
237 | public function setUrl($url) | |
238 | { | |
239 | $this->url = $url; | |
240 | ||
241 | return $this; | |
242 | } | |
243 | ||
244 | /** | |
245 | * Get url. | |
246 | * | |
247 | * @return string | |
248 | */ | |
249 | public function getUrl() | |
250 | { | |
251 | return $this->url; | |
252 | } | |
253 | ||
254 | /** | |
255 | * Set isArchived. | |
256 | * | |
257 | * @param bool $isArchived | |
258 | * | |
259 | * @return Entry | |
260 | */ | |
261 | public function setArchived($isArchived) | |
262 | { | |
263 | $this->isArchived = $isArchived; | |
264 | ||
265 | return $this; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Get isArchived. | |
270 | * | |
271 | * @return bool | |
272 | */ | |
273 | public function isArchived() | |
274 | { | |
275 | return $this->isArchived; | |
276 | } | |
277 | ||
278 | /** | |
279 | * @VirtualProperty | |
280 | * @SerializedName("is_archived") | |
281 | * @Groups({"entries_for_user", "export_all"}) | |
282 | */ | |
283 | public function is_Archived() | |
284 | { | |
285 | return (int) $this->isArchived(); | |
286 | } | |
287 | ||
288 | public function toggleArchive() | |
289 | { | |
290 | $this->isArchived = $this->isArchived() ^ 1; | |
291 | ||
292 | return $this; | |
293 | } | |
294 | ||
295 | /** | |
296 | * Set isStarred. | |
297 | * | |
298 | * @param bool $isStarred | |
299 | * | |
300 | * @return Entry | |
301 | */ | |
302 | public function setStarred($isStarred) | |
303 | { | |
304 | $this->isStarred = $isStarred; | |
305 | ||
306 | return $this; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Get isStarred. | |
311 | * | |
312 | * @return bool | |
313 | */ | |
314 | public function isStarred() | |
315 | { | |
316 | return $this->isStarred; | |
317 | } | |
318 | ||
319 | /** | |
320 | * @VirtualProperty | |
321 | * @SerializedName("is_starred") | |
322 | * @Groups({"entries_for_user", "export_all"}) | |
323 | */ | |
324 | public function is_Starred() | |
325 | { | |
326 | return (int) $this->isStarred(); | |
327 | } | |
328 | ||
329 | public function toggleStar() | |
330 | { | |
331 | $this->isStarred = $this->isStarred() ^ 1; | |
332 | ||
333 | return $this; | |
334 | } | |
335 | ||
336 | /** | |
337 | * Set content. | |
338 | * | |
339 | * @param string $content | |
340 | * | |
341 | * @return Entry | |
342 | */ | |
343 | public function setContent($content) | |
344 | { | |
345 | $this->content = $content; | |
346 | ||
347 | return $this; | |
348 | } | |
349 | ||
350 | /** | |
351 | * Get content. | |
352 | * | |
353 | * @return string | |
354 | */ | |
355 | public function getContent() | |
356 | { | |
357 | return $this->content; | |
358 | } | |
359 | ||
360 | /** | |
361 | * @return User | |
362 | */ | |
363 | public function getUser() | |
364 | { | |
365 | return $this->user; | |
366 | } | |
367 | ||
368 | /** | |
369 | * @VirtualProperty | |
370 | * @SerializedName("user_name") | |
371 | */ | |
372 | public function getUserName() | |
373 | { | |
374 | return $this->user->getUserName(); | |
375 | } | |
376 | ||
377 | /** | |
378 | * @VirtualProperty | |
379 | * @SerializedName("user_email") | |
380 | */ | |
381 | public function getUserEmail() | |
382 | { | |
383 | return $this->user->getEmail(); | |
384 | } | |
385 | ||
386 | /** | |
387 | * @VirtualProperty | |
388 | * @SerializedName("user_id") | |
389 | */ | |
390 | public function getUserId() | |
391 | { | |
392 | return $this->user->getId(); | |
393 | } | |
394 | ||
395 | /** | |
396 | * @return string | |
397 | */ | |
398 | public function getCreatedAt() | |
399 | { | |
400 | return $this->createdAt; | |
401 | } | |
402 | ||
403 | /** | |
404 | * @return string | |
405 | */ | |
406 | public function getUpdatedAt() | |
407 | { | |
408 | return $this->updatedAt; | |
409 | } | |
410 | ||
411 | /** | |
412 | * @ORM\PrePersist | |
413 | * @ORM\PreUpdate | |
414 | */ | |
415 | public function timestamps() | |
416 | { | |
417 | if (is_null($this->createdAt)) { | |
418 | $this->createdAt = new \DateTime(); | |
419 | } | |
420 | ||
421 | $this->updatedAt = new \DateTime(); | |
422 | } | |
423 | ||
424 | /** | |
425 | * @return ArrayCollection<Annotation> | |
426 | */ | |
427 | public function getAnnotations() | |
428 | { | |
429 | return $this->annotations; | |
430 | } | |
431 | ||
432 | /** | |
433 | * @param Annotation $annotation | |
434 | */ | |
435 | public function setAnnotation(Annotation $annotation) | |
436 | { | |
437 | $this->annotations[] = $annotation; | |
438 | } | |
439 | ||
440 | /** | |
441 | * @return string | |
442 | */ | |
443 | public function getMimetype() | |
444 | { | |
445 | return $this->mimetype; | |
446 | } | |
447 | ||
448 | /** | |
449 | * @param string $mimetype | |
450 | */ | |
451 | public function setMimetype($mimetype) | |
452 | { | |
453 | $this->mimetype = $mimetype; | |
454 | } | |
455 | ||
456 | /** | |
457 | * @return int | |
458 | */ | |
459 | public function getReadingTime() | |
460 | { | |
461 | return $this->readingTime; | |
462 | } | |
463 | ||
464 | /** | |
465 | * @param int $readingTime | |
466 | */ | |
467 | public function setReadingTime($readingTime) | |
468 | { | |
469 | $this->readingTime = $readingTime; | |
470 | } | |
471 | ||
472 | /** | |
473 | * @return string | |
474 | */ | |
475 | public function getDomainName() | |
476 | { | |
477 | return $this->domainName; | |
478 | } | |
479 | ||
480 | /** | |
481 | * @param string $domainName | |
482 | */ | |
483 | public function setDomainName($domainName) | |
484 | { | |
485 | $this->domainName = $domainName; | |
486 | } | |
487 | ||
488 | /** | |
489 | * @return bool | |
490 | */ | |
491 | public function isPublic() | |
492 | { | |
493 | return $this->isPublic; | |
494 | } | |
495 | ||
496 | /** | |
497 | * @param bool $isPublic | |
498 | */ | |
499 | public function setIsPublic($isPublic) | |
500 | { | |
501 | $this->isPublic = $isPublic; | |
502 | } | |
503 | ||
504 | /** | |
505 | * @return ArrayCollection<Tag> | |
506 | */ | |
507 | public function getTags() | |
508 | { | |
509 | return $this->tags; | |
510 | } | |
511 | ||
512 | /** | |
513 | * @param Tag $tag | |
514 | */ | |
515 | public function addTag(Tag $tag) | |
516 | { | |
517 | if ($this->tags->contains($tag)) { | |
518 | return; | |
519 | } | |
520 | ||
521 | // check if tag already exist but has not yet be persisted | |
522 | // it seems that the previous condition with `contains()` doesn't check that case | |
523 | foreach ($this->tags as $existingTag) { | |
524 | if ($existingTag->getLabel() === $tag->getLabel()) { | |
525 | return; | |
526 | } | |
527 | } | |
528 | ||
529 | $this->tags[] = $tag; | |
530 | $tag->addEntry($this); | |
531 | } | |
532 | ||
533 | public function removeTag(Tag $tag) | |
534 | { | |
535 | $this->tags->removeElement($tag); | |
536 | } | |
537 | ||
538 | /** | |
539 | * Set previewPicture. | |
540 | * | |
541 | * @param string $previewPicture | |
542 | * | |
543 | * @return Entry | |
544 | */ | |
545 | public function setPreviewPicture($previewPicture) | |
546 | { | |
547 | $this->previewPicture = $previewPicture; | |
548 | ||
549 | return $this; | |
550 | } | |
551 | ||
552 | /** | |
553 | * Get previewPicture. | |
554 | * | |
555 | * @return string | |
556 | */ | |
557 | public function getPreviewPicture() | |
558 | { | |
559 | return $this->previewPicture; | |
560 | } | |
561 | ||
562 | /** | |
563 | * Set language. | |
564 | * | |
565 | * @param string $language | |
566 | * | |
567 | * @return Entry | |
568 | */ | |
569 | public function setLanguage($language) | |
570 | { | |
571 | $this->language = $language; | |
572 | ||
573 | return $this; | |
574 | } | |
575 | ||
576 | /** | |
577 | * Get language. | |
578 | * | |
579 | * @return string | |
580 | */ | |
581 | public function getLanguage() | |
582 | { | |
583 | return $this->language; | |
584 | } | |
585 | } |