]>
Commit | Line | Data |
---|---|---|
f38e03dc TC |
1 | <?php |
2 | ||
4dc87223 | 3 | namespace Wallabag\AnnotationBundle\Controller; |
f38e03dc TC |
4 | |
5 | use FOS\RestBundle\Controller\FOSRestController; | |
f808b016 | 6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
1eea248b | 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
f38e03dc | 8 | use Symfony\Component\HttpFoundation\Request; |
4dc87223 | 9 | use Wallabag\AnnotationBundle\Entity\Annotation; |
2c3e148b | 10 | use Wallabag\AnnotationBundle\Form\EditAnnotationType; |
11 | use Wallabag\AnnotationBundle\Form\NewAnnotationType; | |
f38e03dc TC |
12 | use Wallabag\CoreBundle\Entity\Entry; |
13 | ||
4dc87223 | 14 | class WallabagAnnotationController extends FOSRestController |
f38e03dc TC |
15 | { |
16 | /** | |
4dc87223 | 17 | * Retrieve annotations for an entry. |
f38e03dc | 18 | * |
351eb8d9 | 19 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc | 20 | * |
1eea248b | 21 | * @return JsonResponse |
f38e03dc TC |
22 | */ |
23 | public function getAnnotationsAction(Entry $entry) | |
24 | { | |
4dc87223 | 25 | $annotationRows = $this |
1eea248b TC |
26 | ->getDoctrine() |
27 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
28 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | |
2a1ceb67 | 29 | $total = \count($annotationRows); |
0c271b9e | 30 | $annotations = ['total' => $total, 'rows' => $annotationRows]; |
f38e03dc | 31 | |
f40c88eb | 32 | $json = $this->get('jms_serializer')->serialize($annotations, 'json'); |
f38e03dc | 33 | |
1eea248b | 34 | return (new JsonResponse())->setJson($json); |
f38e03dc TC |
35 | } |
36 | ||
37 | /** | |
4dc87223 | 38 | * Creates a new annotation. |
f38e03dc | 39 | * |
1eea248b | 40 | * @return JsonResponse |
f38e03dc | 41 | * |
b1e92f8c | 42 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc TC |
43 | */ |
44 | public function postAnnotationAction(Request $request, Entry $entry) | |
45 | { | |
46 | $data = json_decode($request->getContent(), true); | |
47 | ||
48 | $em = $this->getDoctrine()->getManager(); | |
4dc87223 | 49 | $annotation = new Annotation($this->getUser()); |
2c3e148b | 50 | $annotation->setEntry($entry); |
f38e03dc | 51 | |
2c3e148b | 52 | $form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [ |
53 | 'csrf_protection' => false, | |
54 | 'allow_extra_fields' => true, | |
55 | ]); | |
56 | $form->submit($data); | |
f38e03dc | 57 | |
2c3e148b | 58 | if ($form->isValid()) { |
59 | $em->persist($annotation); | |
60 | $em->flush(); | |
f38e03dc | 61 | |
f40c88eb | 62 | $json = $this->get('jms_serializer')->serialize($annotation, 'json'); |
f38e03dc | 63 | |
2c3e148b | 64 | return JsonResponse::fromJsonString($json); |
65 | } | |
f38e03dc | 66 | |
2c3e148b | 67 | return $form; |
f38e03dc TC |
68 | } |
69 | ||
70 | /** | |
4dc87223 | 71 | * Updates an annotation. |
f38e03dc | 72 | * |
351eb8d9 | 73 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc | 74 | * |
4dc87223 | 75 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
f38e03dc | 76 | * |
1eea248b | 77 | * @return JsonResponse |
f38e03dc | 78 | */ |
4dc87223 | 79 | public function putAnnotationAction(Annotation $annotation, Request $request) |
f38e03dc TC |
80 | { |
81 | $data = json_decode($request->getContent(), true); | |
82 | ||
2c3e148b | 83 | $form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [ |
84 | 'csrf_protection' => false, | |
85 | 'allow_extra_fields' => true, | |
86 | ]); | |
87 | $form->submit($data); | |
f38e03dc | 88 | |
2c3e148b | 89 | if ($form->isValid()) { |
90 | $em = $this->getDoctrine()->getManager(); | |
91 | $em->persist($annotation); | |
92 | $em->flush(); | |
f38e03dc | 93 | |
f40c88eb | 94 | $json = $this->get('jms_serializer')->serialize($annotation, 'json'); |
f38e03dc | 95 | |
2c3e148b | 96 | return JsonResponse::fromJsonString($json); |
97 | } | |
98 | ||
99 | return $form; | |
f38e03dc TC |
100 | } |
101 | ||
102 | /** | |
4dc87223 | 103 | * Removes an annotation. |
f38e03dc | 104 | * |
351eb8d9 | 105 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc | 106 | * |
4dc87223 | 107 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
f38e03dc | 108 | * |
1eea248b | 109 | * @return JsonResponse |
f38e03dc | 110 | */ |
4dc87223 | 111 | public function deleteAnnotationAction(Annotation $annotation) |
f38e03dc TC |
112 | { |
113 | $em = $this->getDoctrine()->getManager(); | |
4dc87223 | 114 | $em->remove($annotation); |
f38e03dc TC |
115 | $em->flush(); |
116 | ||
f40c88eb | 117 | $json = $this->get('jms_serializer')->serialize($annotation, 'json'); |
f38e03dc | 118 | |
1eea248b | 119 | return (new JsonResponse())->setJson($json); |
f38e03dc TC |
120 | } |
121 | } |