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