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