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