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