]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
Convert array + phpDoc
[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;
6use Nelmio\ApiDocBundle\Annotation\ApiDoc;
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpFoundation\Response;
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
4dc87223 10use Wallabag\AnnotationBundle\Entity\Annotation;
f38e03dc
TC
11use Wallabag\CoreBundle\Entity\Entry;
12
4dc87223 13class WallabagAnnotationController extends FOSRestController
f38e03dc
TC
14{
15 /**
4dc87223 16 * Retrieve annotations for an entry.
f38e03dc
TC
17 *
18 * @ApiDoc(
19 * requirements={
20 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
21 * }
22 * )
23 *
24 * @return Response
25 */
26 public function getAnnotationsAction(Entry $entry)
27 {
4dc87223 28 $annotationRows = $this
f38e03dc 29 ->getDoctrine()
4dc87223
NL
30 ->getRepository('WallabagAnnotationBundle:Annotation')
31 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
32 $total = count($annotationRows);
4094ea47 33 $annotations = ['total' => $total, 'rows' => $annotationRows];
f38e03dc 34
4dc87223 35 $json = $this->get('serializer')->serialize($annotations, 'json');
f38e03dc
TC
36
37 return $this->renderJsonResponse($json);
38 }
39
40 /**
4dc87223 41 * Creates a new annotation.
f38e03dc
TC
42 *
43 * @param Entry $entry
44 *
45 * @ApiDoc(
46 * requirements={
47 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
4dc87223 48 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
f38e03dc
TC
49 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
50 * }
51 * )
52 *
53 * @return Response
54 */
55 public function postAnnotationAction(Request $request, Entry $entry)
56 {
57 $data = json_decode($request->getContent(), true);
58
59 $em = $this->getDoctrine()->getManager();
60
4dc87223 61 $annotation = new Annotation($this->getUser());
f38e03dc 62
4dc87223 63 $annotation->setText($data['text']);
f38e03dc 64 if (array_key_exists('quote', $data)) {
4dc87223 65 $annotation->setQuote($data['quote']);
f38e03dc
TC
66 }
67 if (array_key_exists('ranges', $data)) {
4dc87223 68 $annotation->setRanges($data['ranges']);
f38e03dc
TC
69 }
70
4dc87223 71 $annotation->setEntry($entry);
f38e03dc 72
4dc87223 73 $em->persist($annotation);
f38e03dc
TC
74 $em->flush();
75
4dc87223 76 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc
TC
77
78 return $this->renderJsonResponse($json);
79 }
80
81 /**
4dc87223 82 * Updates an annotation.
f38e03dc
TC
83 *
84 * @ApiDoc(
85 * requirements={
4dc87223 86 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
f38e03dc
TC
87 * }
88 * )
89 *
4dc87223 90 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
f38e03dc
TC
91 *
92 * @return Response
93 */
4dc87223 94 public function putAnnotationAction(Annotation $annotation, Request $request)
f38e03dc
TC
95 {
96 $data = json_decode($request->getContent(), true);
97
98 if (!is_null($data['text'])) {
4dc87223 99 $annotation->setText($data['text']);
f38e03dc
TC
100 }
101
102 $em = $this->getDoctrine()->getManager();
103 $em->flush();
104
4dc87223 105 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc
TC
106
107 return $this->renderJsonResponse($json);
108 }
109
110 /**
4dc87223 111 * Removes an annotation.
f38e03dc
TC
112 *
113 * @ApiDoc(
114 * requirements={
4dc87223 115 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
f38e03dc
TC
116 * }
117 * )
118 *
4dc87223 119 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
f38e03dc
TC
120 *
121 * @return Response
122 */
4dc87223 123 public function deleteAnnotationAction(Annotation $annotation)
f38e03dc
TC
124 {
125 $em = $this->getDoctrine()->getManager();
4dc87223 126 $em->remove($annotation);
f38e03dc
TC
127 $em->flush();
128
4dc87223 129 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc
TC
130
131 return $this->renderJsonResponse($json);
132 }
133
134 /**
135 * Send a JSON Response.
136 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
137 *
138 * @param string $json
139 *
140 * @return Response
141 */
142 private function renderJsonResponse($json, $code = 200)
143 {
4094ea47 144 return new Response($json, $code, ['application/json']);
f38e03dc
TC
145 }
146}