]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
Jump to Symfony 3.3 & update others deps
[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 * @param Entry $entry
20 *
21 * @see Wallabag\ApiBundle\Controller\WallabagRestController
22 *
23 * @return JsonResponse
24 */
25 public function getAnnotationsAction(Entry $entry)
26 {
27 $annotationRows = $this
28 ->getDoctrine()
29 ->getRepository('WallabagAnnotationBundle:Annotation')
30 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
31 $total = count($annotationRows);
32 $annotations = ['total' => $total, 'rows' => $annotationRows];
33
34 $json = $this->get('jms_serializer')->serialize($annotations, 'json');
35
36 return (new JsonResponse())->setJson($json);
37 }
38
39 /**
40 * Creates a new annotation.
41 *
42 * @param Request $request
43 * @param Entry $entry
44 *
45 * @return JsonResponse
46 *
47 * @see Wallabag\ApiBundle\Controller\WallabagRestController
48 */
49 public function postAnnotationAction(Request $request, Entry $entry)
50 {
51 $data = json_decode($request->getContent(), true);
52
53 $em = $this->getDoctrine()->getManager();
54 $annotation = new Annotation($this->getUser());
55 $annotation->setEntry($entry);
56
57 $form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [
58 'csrf_protection' => false,
59 'allow_extra_fields' => true,
60 ]);
61 $form->submit($data);
62
63 if ($form->isValid()) {
64 $em->persist($annotation);
65 $em->flush();
66
67 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
68
69 return JsonResponse::fromJsonString($json);
70 }
71
72 return $form;
73 }
74
75 /**
76 * Updates an annotation.
77 *
78 * @see Wallabag\ApiBundle\Controller\WallabagRestController
79 *
80 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
81 *
82 * @param Annotation $annotation
83 * @param Request $request
84 *
85 * @return JsonResponse
86 */
87 public function putAnnotationAction(Annotation $annotation, Request $request)
88 {
89 $data = json_decode($request->getContent(), true);
90
91 $form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [
92 'csrf_protection' => false,
93 'allow_extra_fields' => true,
94 ]);
95 $form->submit($data);
96
97 if ($form->isValid()) {
98 $em = $this->getDoctrine()->getManager();
99 $em->persist($annotation);
100 $em->flush();
101
102 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
103
104 return JsonResponse::fromJsonString($json);
105 }
106
107 return $form;
108 }
109
110 /**
111 * Removes an annotation.
112 *
113 * @see Wallabag\ApiBundle\Controller\WallabagRestController
114 *
115 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
116 *
117 * @param Annotation $annotation
118 *
119 * @return JsonResponse
120 */
121 public function deleteAnnotationAction(Annotation $annotation)
122 {
123 $em = $this->getDoctrine()->getManager();
124 $em->remove($annotation);
125 $em->flush();
126
127 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
128
129 return (new JsonResponse())->setJson($json);
130 }
131 }