forked from wallabag/wallabag
@ -7,6 +7,8 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Wallabag\AnnotationBundle\Entity\Annotation;
|
||||
use Wallabag\AnnotationBundle\Form\EditAnnotationType;
|
||||
use Wallabag\AnnotationBundle\Form\NewAnnotationType;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
|
||||
class WallabagAnnotationController extends FOSRestController
|
||||
@ -49,25 +51,25 @@ class WallabagAnnotationController extends FOSRestController
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$annotation = new Annotation($this->getUser());
|
||||
|
||||
$annotation->setText($data['text']);
|
||||
if (array_key_exists('quote', $data)) {
|
||||
$annotation->setQuote($data['quote']);
|
||||
}
|
||||
if (array_key_exists('ranges', $data)) {
|
||||
$annotation->setRanges($data['ranges']);
|
||||
}
|
||||
|
||||
$annotation->setEntry($entry);
|
||||
|
||||
$em->persist($annotation);
|
||||
$em->flush();
|
||||
$form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [
|
||||
'csrf_protection' => false,
|
||||
'allow_extra_fields' => true,
|
||||
]);
|
||||
$form->submit($data);
|
||||
|
||||
$json = $this->get('serializer')->serialize($annotation, 'json');
|
||||
if ($form->isValid()) {
|
||||
$em->persist($annotation);
|
||||
$em->flush();
|
||||
|
||||
return (new JsonResponse())->setJson($json);
|
||||
$json = $this->get('serializer')->serialize($annotation, 'json');
|
||||
|
||||
return JsonResponse::fromJsonString($json);
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,16 +88,23 @@ class WallabagAnnotationController extends FOSRestController
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
if (!is_null($data['text'])) {
|
||||
$annotation->setText($data['text']);
|
||||
$form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [
|
||||
'csrf_protection' => false,
|
||||
'allow_extra_fields' => true,
|
||||
]);
|
||||
$form->submit($data);
|
||||
|
||||
if ($form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($annotation);
|
||||
$em->flush();
|
||||
|
||||
$json = $this->get('serializer')->serialize($annotation, 'json');
|
||||
|
||||
return JsonResponse::fromJsonString($json);
|
||||
}
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->flush();
|
||||
|
||||
$json = $this->get('serializer')->serialize($annotation, 'json');
|
||||
|
||||
return (new JsonResponse())->setJson($json);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -8,6 +8,7 @@ use JMS\Serializer\Annotation\Exclude;
|
||||
use JMS\Serializer\Annotation\VirtualProperty;
|
||||
use JMS\Serializer\Annotation\SerializedName;
|
||||
use JMS\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Wallabag\UserBundle\Entity\User;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
|
||||
@ -56,7 +57,11 @@ class Annotation
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(name="quote", type="string")
|
||||
* @Assert\Length(
|
||||
* max = 10000,
|
||||
* maxMessage = "validator.quote_length_too_high"
|
||||
* )
|
||||
* @ORM\Column(name="quote", type="text")
|
||||
*
|
||||
* @Groups({"entries_for_user", "export_all"})
|
||||
*/
|
||||
|
||||
18
src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php
Normal file
18
src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\AnnotationBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class EditAnnotationType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('text', null, [
|
||||
'empty_data' => '',
|
||||
])
|
||||
;
|
||||
}
|
||||
}
|
||||
35
src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php
Normal file
35
src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\AnnotationBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Wallabag\AnnotationBundle\Entity\Annotation;
|
||||
|
||||
class NewAnnotationType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('text', null, [
|
||||
'empty_data' => '',
|
||||
])
|
||||
->add('quote', null, [
|
||||
'empty_data' => null,
|
||||
])
|
||||
->add('ranges', CollectionType::class, [
|
||||
'entry_type' => RangeType::class,
|
||||
'allow_add' => true,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Annotation::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
19
src/Wallabag/AnnotationBundle/Form/RangeType.php
Normal file
19
src/Wallabag/AnnotationBundle/Form/RangeType.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\AnnotationBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class RangeType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('start')
|
||||
->add('startOffset')
|
||||
->add('end')
|
||||
->add('endOffset')
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user