2016-02-07 15:52:59 +00:00
|
|
|
<?php
|
|
|
|
|
2016-02-26 12:59:08 +00:00
|
|
|
namespace Wallabag\AnnotationBundle\Controller;
|
2016-02-07 15:52:59 +00:00
|
|
|
|
|
|
|
use FOS\RestBundle\Controller\FOSRestController;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2016-10-05 17:16:57 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2016-02-07 15:52:59 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2016-02-26 12:59:08 +00:00
|
|
|
use Wallabag\AnnotationBundle\Entity\Annotation;
|
2017-05-07 15:21:30 +00:00
|
|
|
use Wallabag\AnnotationBundle\Form\EditAnnotationType;
|
|
|
|
use Wallabag\AnnotationBundle\Form\NewAnnotationType;
|
2016-02-07 15:52:59 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
|
2016-02-26 12:59:08 +00:00
|
|
|
class WallabagAnnotationController extends FOSRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
|
|
|
/**
|
2016-02-26 12:59:08 +00:00
|
|
|
* Retrieve annotations for an entry.
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-03-18 15:41:23 +00:00
|
|
|
* @see Wallabag\ApiBundle\Controller\WallabagRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-10-05 17:16:57 +00:00
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
|
|
|
public function getAnnotationsAction(Entry $entry)
|
|
|
|
{
|
2016-02-26 12:59:08 +00:00
|
|
|
$annotationRows = $this
|
2016-10-05 17:16:57 +00:00
|
|
|
->getDoctrine()
|
|
|
|
->getRepository('WallabagAnnotationBundle:Annotation')
|
|
|
|
->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
|
2018-09-05 12:25:32 +00:00
|
|
|
$total = \count($annotationRows);
|
2016-10-09 12:01:28 +00:00
|
|
|
$annotations = ['total' => $total, 'rows' => $annotationRows];
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-10-09 14:45:09 +00:00
|
|
|
$json = $this->get('jms_serializer')->serialize($annotations, 'json');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-05 17:16:57 +00:00
|
|
|
return (new JsonResponse())->setJson($json);
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-26 12:59:08 +00:00
|
|
|
* Creates a new annotation.
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-10-05 17:16:57 +00:00
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-10-06 09:40:14 +00:00
|
|
|
* @see Wallabag\ApiBundle\Controller\WallabagRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
|
|
|
public function postAnnotationAction(Request $request, Entry $entry)
|
|
|
|
{
|
|
|
|
$data = json_decode($request->getContent(), true);
|
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
2016-02-26 12:59:08 +00:00
|
|
|
$annotation = new Annotation($this->getUser());
|
2017-05-07 15:21:30 +00:00
|
|
|
$annotation->setEntry($entry);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
$form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [
|
|
|
|
'csrf_protection' => false,
|
|
|
|
'allow_extra_fields' => true,
|
|
|
|
]);
|
|
|
|
$form->submit($data);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
if ($form->isValid()) {
|
|
|
|
$em->persist($annotation);
|
|
|
|
$em->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-10-09 14:45:09 +00:00
|
|
|
$json = $this->get('jms_serializer')->serialize($annotation, 'json');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
return JsonResponse::fromJsonString($json);
|
|
|
|
}
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
return $form;
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-26 12:59:08 +00:00
|
|
|
* Updates an annotation.
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-03-18 15:41:23 +00:00
|
|
|
* @see Wallabag\ApiBundle\Controller\WallabagRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-02-26 12:59:08 +00:00
|
|
|
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-10-05 17:16:57 +00:00
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
2016-02-26 12:59:08 +00:00
|
|
|
public function putAnnotationAction(Annotation $annotation, Request $request)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
|
|
|
$data = json_decode($request->getContent(), true);
|
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
$form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [
|
|
|
|
'csrf_protection' => false,
|
|
|
|
'allow_extra_fields' => true,
|
|
|
|
]);
|
|
|
|
$form->submit($data);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
if ($form->isValid()) {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist($annotation);
|
|
|
|
$em->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-10-09 14:45:09 +00:00
|
|
|
$json = $this->get('jms_serializer')->serialize($annotation, 'json');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2017-05-07 15:21:30 +00:00
|
|
|
return JsonResponse::fromJsonString($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-26 12:59:08 +00:00
|
|
|
* Removes an annotation.
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-03-18 15:41:23 +00:00
|
|
|
* @see Wallabag\ApiBundle\Controller\WallabagRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-02-26 12:59:08 +00:00
|
|
|
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
|
2016-02-07 15:52:59 +00:00
|
|
|
*
|
2016-10-05 17:16:57 +00:00
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
2016-02-26 12:59:08 +00:00
|
|
|
public function deleteAnnotationAction(Annotation $annotation)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
2016-02-26 12:59:08 +00:00
|
|
|
$em->remove($annotation);
|
2016-02-07 15:52:59 +00:00
|
|
|
$em->flush();
|
|
|
|
|
2017-10-09 14:45:09 +00:00
|
|
|
$json = $this->get('jms_serializer')->serialize($annotation, 'json');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-05 17:16:57 +00:00
|
|
|
return (new JsonResponse())->setJson($json);
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|