aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/AnnotationBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/AnnotationBundle')
-rw-r--r--src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php55
-rw-r--r--src/Wallabag/AnnotationBundle/Entity/Annotation.php30
-rw-r--r--src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php18
-rw-r--r--src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php35
-rw-r--r--src/Wallabag/AnnotationBundle/Form/RangeType.php19
-rw-r--r--src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php51
6 files changed, 152 insertions, 56 deletions
diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
index c13a034f..f3090e65 100644
--- a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
+++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
@@ -3,10 +3,12 @@
3namespace Wallabag\AnnotationBundle\Controller; 3namespace Wallabag\AnnotationBundle\Controller;
4 4
5use FOS\RestBundle\Controller\FOSRestController; 5use FOS\RestBundle\Controller\FOSRestController;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6use Symfony\Component\HttpFoundation\JsonResponse; 7use Symfony\Component\HttpFoundation\JsonResponse;
7use Symfony\Component\HttpFoundation\Request; 8use Symfony\Component\HttpFoundation\Request;
8use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9use Wallabag\AnnotationBundle\Entity\Annotation; 9use Wallabag\AnnotationBundle\Entity\Annotation;
10use Wallabag\AnnotationBundle\Form\EditAnnotationType;
11use Wallabag\AnnotationBundle\Form\NewAnnotationType;
10use Wallabag\CoreBundle\Entity\Entry; 12use Wallabag\CoreBundle\Entity\Entry;
11 13
12class WallabagAnnotationController extends FOSRestController 14class WallabagAnnotationController extends FOSRestController
@@ -29,7 +31,7 @@ class WallabagAnnotationController extends FOSRestController
29 $total = count($annotationRows); 31 $total = count($annotationRows);
30 $annotations = ['total' => $total, 'rows' => $annotationRows]; 32 $annotations = ['total' => $total, 'rows' => $annotationRows];
31 33
32 $json = $this->get('serializer')->serialize($annotations, 'json'); 34 $json = $this->get('jms_serializer')->serialize($annotations, 'json');
33 35
34 return (new JsonResponse())->setJson($json); 36 return (new JsonResponse())->setJson($json);
35 } 37 }
@@ -49,25 +51,25 @@ class WallabagAnnotationController extends FOSRestController
49 $data = json_decode($request->getContent(), true); 51 $data = json_decode($request->getContent(), true);
50 52
51 $em = $this->getDoctrine()->getManager(); 53 $em = $this->getDoctrine()->getManager();
52
53 $annotation = new Annotation($this->getUser()); 54 $annotation = new Annotation($this->getUser());
55 $annotation->setEntry($entry);
54 56
55 $annotation->setText($data['text']); 57 $form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [
56 if (array_key_exists('quote', $data)) { 58 'csrf_protection' => false,
57 $annotation->setQuote($data['quote']); 59 'allow_extra_fields' => true,
58 } 60 ]);
59 if (array_key_exists('ranges', $data)) { 61 $form->submit($data);
60 $annotation->setRanges($data['ranges']);
61 }
62 62
63 $annotation->setEntry($entry); 63 if ($form->isValid()) {
64 $em->persist($annotation);
65 $em->flush();
64 66
65 $em->persist($annotation); 67 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
66 $em->flush();
67 68
68 $json = $this->get('serializer')->serialize($annotation, 'json'); 69 return JsonResponse::fromJsonString($json);
70 }
69 71
70 return (new JsonResponse())->setJson($json); 72 return $form;
71 } 73 }
72 74
73 /** 75 /**
@@ -86,16 +88,23 @@ class WallabagAnnotationController extends FOSRestController
86 { 88 {
87 $data = json_decode($request->getContent(), true); 89 $data = json_decode($request->getContent(), true);
88 90
89 if (!is_null($data['text'])) { 91 $form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [
90 $annotation->setText($data['text']); 92 'csrf_protection' => false,
91 } 93 'allow_extra_fields' => true,
94 ]);
95 $form->submit($data);
92 96
93 $em = $this->getDoctrine()->getManager(); 97 if ($form->isValid()) {
94 $em->flush(); 98 $em = $this->getDoctrine()->getManager();
99 $em->persist($annotation);
100 $em->flush();
95 101
96 $json = $this->get('serializer')->serialize($annotation, 'json'); 102 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
97 103
98 return (new JsonResponse())->setJson($json); 104 return JsonResponse::fromJsonString($json);
105 }
106
107 return $form;
99 } 108 }
100 109
101 /** 110 /**
@@ -115,7 +124,7 @@ class WallabagAnnotationController extends FOSRestController
115 $em->remove($annotation); 124 $em->remove($annotation);
116 $em->flush(); 125 $em->flush();
117 126
118 $json = $this->get('serializer')->serialize($annotation, 'json'); 127 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
119 128
120 return (new JsonResponse())->setJson($json); 129 return (new JsonResponse())->setJson($json);
121 } 130 }
diff --git a/src/Wallabag/AnnotationBundle/Entity/Annotation.php b/src/Wallabag/AnnotationBundle/Entity/Annotation.php
index 0838f5aa..a180d504 100644
--- a/src/Wallabag/AnnotationBundle/Entity/Annotation.php
+++ b/src/Wallabag/AnnotationBundle/Entity/Annotation.php
@@ -3,13 +3,15 @@
3namespace Wallabag\AnnotationBundle\Entity; 3namespace Wallabag\AnnotationBundle\Entity;
4 4
5use Doctrine\ORM\Mapping as ORM; 5use Doctrine\ORM\Mapping as ORM;
6use JMS\Serializer\Annotation\ExclusionPolicy;
7use JMS\Serializer\Annotation\Exclude; 6use JMS\Serializer\Annotation\Exclude;
8use JMS\Serializer\Annotation\VirtualProperty; 7use JMS\Serializer\Annotation\ExclusionPolicy;
9use JMS\Serializer\Annotation\SerializedName;
10use JMS\Serializer\Annotation\Groups; 8use JMS\Serializer\Annotation\Groups;
11use Wallabag\UserBundle\Entity\User; 9use JMS\Serializer\Annotation\SerializedName;
10use JMS\Serializer\Annotation\VirtualProperty;
11use Symfony\Component\Validator\Constraints as Assert;
12use Wallabag\CoreBundle\Entity\Entry; 12use Wallabag\CoreBundle\Entity\Entry;
13use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
14use Wallabag\UserBundle\Entity\User;
13 15
14/** 16/**
15 * Annotation. 17 * Annotation.
@@ -21,6 +23,8 @@ use Wallabag\CoreBundle\Entity\Entry;
21 */ 23 */
22class Annotation 24class Annotation
23{ 25{
26 use EntityTimestampsTrait;
27
24 /** 28 /**
25 * @var int 29 * @var int
26 * 30 *
@@ -56,7 +60,11 @@ class Annotation
56 /** 60 /**
57 * @var string 61 * @var string
58 * 62 *
59 * @ORM\Column(name="quote", type="string") 63 * @Assert\Length(
64 * max = 10000,
65 * maxMessage = "validator.quote_length_too_high"
66 * )
67 * @ORM\Column(name="quote", type="text")
60 * 68 *
61 * @Groups({"entries_for_user", "export_all"}) 69 * @Groups({"entries_for_user", "export_all"})
62 */ 70 */
@@ -129,18 +137,6 @@ class Annotation
129 } 137 }
130 138
131 /** 139 /**
132 * @ORM\PrePersist
133 * @ORM\PreUpdate
134 */
135 public function timestamps()
136 {
137 if (is_null($this->createdAt)) {
138 $this->createdAt = new \DateTime();
139 }
140 $this->updatedAt = new \DateTime();
141 }
142
143 /**
144 * Get created. 140 * Get created.
145 * 141 *
146 * @return \DateTime 142 * @return \DateTime
diff --git a/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php
new file mode 100644
index 00000000..3b587478
--- /dev/null
+++ b/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php
@@ -0,0 +1,18 @@
1<?php
2
3namespace Wallabag\AnnotationBundle\Form;
4
5use Symfony\Component\Form\AbstractType;
6use Symfony\Component\Form\FormBuilderInterface;
7
8class EditAnnotationType extends AbstractType
9{
10 public function buildForm(FormBuilderInterface $builder, array $options)
11 {
12 $builder
13 ->add('text', null, [
14 'empty_data' => '',
15 ])
16 ;
17 }
18}
diff --git a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php
new file mode 100644
index 00000000..c73c3ded
--- /dev/null
+++ b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php
@@ -0,0 +1,35 @@
1<?php
2
3namespace Wallabag\AnnotationBundle\Form;
4
5use Symfony\Component\Form\AbstractType;
6use Symfony\Component\Form\Extension\Core\Type\CollectionType;
7use Symfony\Component\Form\FormBuilderInterface;
8use Symfony\Component\OptionsResolver\OptionsResolver;
9use Wallabag\AnnotationBundle\Entity\Annotation;
10
11class NewAnnotationType extends AbstractType
12{
13 public function buildForm(FormBuilderInterface $builder, array $options)
14 {
15 $builder
16 ->add('text', null, [
17 'empty_data' => '',
18 ])
19 ->add('quote', null, [
20 'empty_data' => null,
21 ])
22 ->add('ranges', CollectionType::class, [
23 'entry_type' => RangeType::class,
24 'allow_add' => true,
25 ])
26 ;
27 }
28
29 public function configureOptions(OptionsResolver $resolver)
30 {
31 $resolver->setDefaults([
32 'data_class' => Annotation::class,
33 ]);
34 }
35}
diff --git a/src/Wallabag/AnnotationBundle/Form/RangeType.php b/src/Wallabag/AnnotationBundle/Form/RangeType.php
new file mode 100644
index 00000000..0647375e
--- /dev/null
+++ b/src/Wallabag/AnnotationBundle/Form/RangeType.php
@@ -0,0 +1,19 @@
1<?php
2
3namespace Wallabag\AnnotationBundle\Form;
4
5use Symfony\Component\Form\AbstractType;
6use Symfony\Component\Form\FormBuilderInterface;
7
8class RangeType extends AbstractType
9{
10 public function buildForm(FormBuilderInterface $builder, array $options)
11 {
12 $builder
13 ->add('start')
14 ->add('startOffset')
15 ->add('end')
16 ->add('endOffset')
17 ;
18 }
19}
diff --git a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
index 8d3f07ee..b44f7e64 100644
--- a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
+++ b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
@@ -3,6 +3,8 @@
3namespace Wallabag\AnnotationBundle\Repository; 3namespace Wallabag\AnnotationBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\QueryBuilder;
7use Wallabag\AnnotationBundle\Entity\Annotation;
6 8
7/** 9/**
8 * AnnotationRepository. 10 * AnnotationRepository.
@@ -10,22 +12,6 @@ use Doctrine\ORM\EntityRepository;
10class AnnotationRepository extends EntityRepository 12class AnnotationRepository extends EntityRepository
11{ 13{
12 /** 14 /**
13 * Return a query builder to used by other getBuilderFor* method.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 private function getBuilderByUser($userId)
20 {
21 return $this->createQueryBuilder('a')
22 ->leftJoin('a.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('a.id', 'desc')
25 ;
26 }
27
28 /**
29 * Retrieves all annotations for a user. 15 * Retrieves all annotations for a user.
30 * 16 *
31 * @param int $userId 17 * @param int $userId
@@ -122,4 +108,37 @@ class AnnotationRepository extends EntityRepository
122 ->setParameter('userId', $userId) 108 ->setParameter('userId', $userId)
123 ->execute(); 109 ->execute();
124 } 110 }
111
112 /**
113 * Find all annotations related to archived entries.
114 *
115 * @param $userId
116 *
117 * @return mixed
118 */
119 public function findAllArchivedEntriesByUser($userId)
120 {
121 return $this->createQueryBuilder('a')
122 ->leftJoin('a.entry', 'e')
123 ->where('a.user = :userid')->setParameter(':userid', $userId)
124 ->andWhere('e.isArchived = true')
125 ->getQuery()
126 ->getResult();
127 }
128
129 /**
130 * Return a query builder to used by other getBuilderFor* method.
131 *
132 * @param int $userId
133 *
134 * @return QueryBuilder
135 */
136 private function getBuilderByUser($userId)
137 {
138 return $this->createQueryBuilder('a')
139 ->leftJoin('a.user', 'u')
140 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
141 ->orderBy('a.id', 'desc')
142 ;
143 }
125} 144}