2016-02-07 15:52:59 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-25 17:37:15 +00:00
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-08-14 22:24:19 +00:00
|
|
|
use FOS\RestBundle\Controller\AbstractFOSRestController;
|
2022-08-28 00:01:46 +00:00
|
|
|
use JMS\Serializer\SerializerInterface;
|
|
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
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;
|
2023-01-23 11:16:09 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2022-11-23 11:44:55 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2023-12-25 17:50:29 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Annotation;
|
2016-02-07 15:52:59 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2023-12-25 18:23:32 +00:00
|
|
|
use Wallabag\CoreBundle\Form\Type\EditAnnotationType;
|
|
|
|
use Wallabag\CoreBundle\Form\Type\NewAnnotationType;
|
2023-12-25 18:17:46 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\AnnotationRepository;
|
2024-01-01 18:51:22 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-12-25 17:37:15 +00:00
|
|
|
class AnnotationController extends AbstractFOSRestController
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
protected EntityManagerInterface $entityManager;
|
|
|
|
protected SerializerInterface $serializer;
|
|
|
|
protected FormFactoryInterface $formFactory;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, SerializerInterface $serializer, FormFactoryInterface $formFactory)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->serializer = $serializer;
|
|
|
|
$this->formFactory = $formFactory;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/annotations/{entry}.{_format}", methods={"GET"}, name="annotations_get_annotations", defaults={"_format": "json"})
|
|
|
|
*
|
2016-10-05 17:16:57 +00:00
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function getAnnotationsAction(Entry $entry, AnnotationRepository $annotationRepository)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2023-04-24 12:36:32 +00:00
|
|
|
$annotationRows = $annotationRepository->findByEntryIdAndUserId($entry->getId(), $this->getUser()->getId());
|
2022-12-19 09:37:22 +00:00
|
|
|
|
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
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->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-06 09:40:14 +00:00
|
|
|
* @see Wallabag\ApiBundle\Controller\WallabagRestController
|
2022-11-23 11:44:55 +00:00
|
|
|
*
|
|
|
|
* @Route("/annotations/{entry}.{_format}", methods={"POST"}, name="annotations_post_annotation", defaults={"_format": "json"})
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2016-02-07 15:52:59 +00:00
|
|
|
*/
|
|
|
|
public function postAnnotationAction(Request $request, Entry $entry)
|
|
|
|
{
|
|
|
|
$data = json_decode($request->getContent(), true);
|
|
|
|
|
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
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$form = $this->formFactory->createNamed('', NewAnnotationType::class, $annotation, [
|
2017-05-07 15:21:30 +00:00
|
|
|
'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()) {
|
2022-12-19 09:37:22 +00:00
|
|
|
$this->entityManager->persist($annotation);
|
|
|
|
$this->entityManager->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->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
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/annotations/{annotation}.{_format}", methods={"PUT"}, name="annotations_put_annotation", defaults={"_format": "json"})
|
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
|
|
|
*/
|
2023-04-24 12:36:32 +00:00
|
|
|
public function putAnnotationAction(Request $request, AnnotationRepository $annotationRepository, int $annotation)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2023-01-23 11:16:09 +00:00
|
|
|
try {
|
2023-04-24 12:36:32 +00:00
|
|
|
$annotation = $this->validateAnnotation($annotationRepository, $annotation, $this->getUser()->getId());
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-01-23 11:16:09 +00:00
|
|
|
$data = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-04-24 12:36:32 +00:00
|
|
|
$form = $this->formFactory->createNamed('', EditAnnotationType::class, $annotation, [
|
2023-01-23 11:16:09 +00:00
|
|
|
'csrf_protection' => false,
|
|
|
|
'allow_extra_fields' => true,
|
|
|
|
]);
|
|
|
|
$form->submit($data);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-01-23 11:16:09 +00:00
|
|
|
if ($form->isValid()) {
|
2023-04-24 12:36:32 +00:00
|
|
|
$this->entityManager->persist($annotation);
|
|
|
|
$this->entityManager->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-04-24 12:36:32 +00:00
|
|
|
$json = $this->serializer->serialize($annotation, 'json');
|
2017-05-07 15:21:30 +00:00
|
|
|
|
2023-01-23 11:16:09 +00:00
|
|
|
return JsonResponse::fromJsonString($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e);
|
|
|
|
}
|
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
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/annotations/{annotation}.{_format}", methods={"DELETE"}, name="annotations_delete_annotation", defaults={"_format": "json"})
|
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
|
|
|
*/
|
2023-04-24 12:36:32 +00:00
|
|
|
public function deleteAnnotationAction(AnnotationRepository $annotationRepository, int $annotation)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2023-01-23 11:16:09 +00:00
|
|
|
try {
|
2023-04-24 12:36:32 +00:00
|
|
|
$annotation = $this->validateAnnotation($annotationRepository, $annotation, $this->getUser()->getId());
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-04-24 12:36:32 +00:00
|
|
|
$this->entityManager->remove($annotation);
|
|
|
|
$this->entityManager->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-04-24 12:36:32 +00:00
|
|
|
$json = $this->serializer->serialize($annotation, 'json');
|
2023-01-23 11:16:09 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 18:51:22 +00:00
|
|
|
/**
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
protected function getUser()
|
|
|
|
{
|
|
|
|
$user = parent::getUser();
|
|
|
|
\assert(null === $user || $user instanceof User);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:36:32 +00:00
|
|
|
private function validateAnnotation(AnnotationRepository $annotationRepository, int $annotationId, int $userId)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2023-04-24 12:36:32 +00:00
|
|
|
$annotation = $annotationRepository->findOneByIdAndUserId($annotationId, $userId);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2023-01-23 11:16:09 +00:00
|
|
|
if (null === $annotation) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $annotation;
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|