diff --git a/src/APIBundle/Controller/LegumeAPIController.php b/src/APIBundle/Controller/LegumeAPIController.php index 92b0a666cc2a2c5756774dc10b1c32481471c800..f7b11e7d59ee1dd97bb8ed28f1039d332202b0d6 100755 --- a/src/APIBundle/Controller/LegumeAPIController.php +++ b/src/APIBundle/Controller/LegumeAPIController.php @@ -3,7 +3,9 @@ namespace APIBundle\Controller; use APIBundle\Form\LegumeAPIType; +use APIBundle\Form\OffreLegumesInsertAPIType; use PotageBundle\Entity\Legume; +use PotageBundle\Entity\OffreLegumes; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -160,7 +162,64 @@ class LegumeAPIController extends MasterAPIController return $this->api($legumes); } } + + /** + * @param Request $request + * @param $id + * @return \Symfony\Component\HttpFoundation\JsonResponse + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function insertAction(Request $request, $id) + { + $em = $this->getDoctrine()->getManager(); + + $legume = $em->getRepository('PotageBundle:Legume') + ->findOneForAPIRead($id); // TODO temporaire, réutilise un repository déjà existant + + if ($legume === null ) { + return $this->api($legume, Response::HTTP_NOT_FOUND); + } - - + $offresPrepa = $em->getRepository('PotageBundle:Offre') + ->findAllByStatusForInsert(); + + dump($legume); + dump($offresPrepa); + + $offrelegumes = new OffreLegumes(); + $offrelegumes + ->setNom($legume) + ->setDescription($legume) + ->setOrigine($legume) + ->setUnite($legume) + ->setQuantiteMin($legume) + ->setQuantiteMax($legume) + ->setQuantiteStep($legume) + ->setPrixUnitaire($legume) + ->setFruit($legume) + ->setRevente($legume) + ->setImage($legume); + + //$offrelegumes->setOffrePrepa($offresPrepa); + + + + $form = $this->createForm(OffreLegumesInsertAPIType::class, $offrelegumes); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) + { + + $em->persist($offrelegumes); + $em->flush(); + return $this->api($offrelegumes); + } + + return $this->api($form, Response::HTTP_BAD_REQUEST); + } + } +/* +* TODO filtrer le select (EntityType) pour qu'il n'affiche que les offres en préparation +* TODO submit du formulaire, persist et flush +*/ \ No newline at end of file diff --git a/src/APIBundle/Form/OffreLegumesInsertAPIType.php b/src/APIBundle/Form/OffreLegumesInsertAPIType.php new file mode 100755 index 0000000000000000000000000000000000000000..8e26c2f23c7f5b5daf45b66469de59383402163e --- /dev/null +++ b/src/APIBundle/Form/OffreLegumesInsertAPIType.php @@ -0,0 +1,23 @@ +setDefault('csrf_protection', false); + + // TODO temporaire, le temps de régler les validations au niveau des Asserts et des FormType + $resolver->setDefault('attr', array('novalidate' => true)); + } + + +} diff --git a/src/APIBundle/Resources/config/routing.yml b/src/APIBundle/Resources/config/routing.yml index a6b93a2329d1775f397b86bb294d36faacba7eb4..db71ae8fc09971ace96b4636ef392b2dc0a0a3cf 100755 --- a/src/APIBundle/Resources/config/routing.yml +++ b/src/APIBundle/Resources/config/routing.yml @@ -50,6 +50,15 @@ api_legume_search: _controller: APIBundle:LegumeAPI:search methods: [ GET ] +api_legume_insert: + path: /legume/inserer/{id} + requirements: + id: \d+ + defaults: + _controller: APIBundle:LegumeAPI:insert + methods: [ POST ] + + ## CRUD OffreAPI api_offre_create: diff --git a/src/PotageBundle/Controller/LegumeController.php b/src/PotageBundle/Controller/LegumeController.php index 569a838f7aca7f2765da57f3e826cd0bd4657f6b..c1b0fe0d3c46d58831d372be42ae9e91f3390705 100755 --- a/src/PotageBundle/Controller/LegumeController.php +++ b/src/PotageBundle/Controller/LegumeController.php @@ -3,6 +3,7 @@ namespace PotageBundle\Controller; use APIBundle\Form\LegumeAPIType; +use APIBundle\Form\OffreLegumesInsertAPIType; use Symfony\Component\HttpFoundation\Request; use PotageBundle\Entity\Legume; use PotageBundle\Form\Legume\LegumeType; @@ -36,12 +37,11 @@ class LegumeController extends MasterController 'formLegume' => $form->createView() )); } - + /** - * - * @return \Symfony\Component\HttpFoundation\Response - * - */ + * @param $page + * @return \Symfony\Component\HttpFoundation\Response + */ public function readAction($page) { if ($page < 1) { @@ -106,8 +106,12 @@ class LegumeController extends MasterController { $em = $this->getDoctrine()->getManager(); $legume = $em->getRepository('PotageBundle:Legume')->findOneForDelete($id); - - if ($legume !== null) + + if ($legume === null) + { + throw $this->createNotFoundException('Non trouvé'); + + } else { $em->remove($legume); $em->flush(); @@ -128,7 +132,11 @@ class LegumeController extends MasterController $em = $this->getDoctrine()->getManager(); $legume = $em->getRepository('PotageBundle:Legume')->findOneForDuplicate($id); - if ($legume !== null) + if ($legume === null) + { + throw $this->createNotFoundException('Non trouvé'); + + } else { $newRow = clone $legume; $em->detach($newRow); @@ -150,11 +158,15 @@ class LegumeController extends MasterController { $nbRows = $this->getDoctrine()->getManager() ->getRepository('PotageBundle:Legume')->countRowsForAjaxDisplay(); + // TODO trouver une meilleure façon d'insérer le nbre total de rangées $form = $this->createForm(LegumeAPIType::class); + $formInsert = $this->createForm(OffreLegumesInsertAPIType::class); + return $this->render('@Potage/Legume/ajaxDisplay.html.twig', array( 'nbRows' => $nbRows, - 'formLegume' => $form->createView() + 'formLegume' => $form->createView(), + 'formOffreLegumesInsert' => $formInsert->createView() )); } } diff --git a/src/PotageBundle/Entity/OffreLegumes.php b/src/PotageBundle/Entity/OffreLegumes.php index 6f1f6bf75ee1b7d1aba0698b0971582bccc92909..bf48f9031b75bbe16cc8a7eaf032b7b18d49dae1 100755 --- a/src/PotageBundle/Entity/OffreLegumes.php +++ b/src/PotageBundle/Entity/OffreLegumes.php @@ -157,6 +157,14 @@ class OffreLegumes private $promo; + /** + * OffreLegumes constructor. + */ + public function __construct() + { + $this->isPromo = false; + } + /** * * @ORM\PrePersist() @@ -592,7 +600,32 @@ class OffreLegumes { return $this->image; } - + + /** + * @param File|null $image + */ + public function setImageFile(File $image = null) + { + $this->imageFile = $image; + + // VERY IMPORTANT: + // It is required that at least one field changes if you are using Doctrine, + // otherwise the event listeners won't be called and the file is lost + if ($image) { + // if 'updatedAt' is not defined in your entity, use another property + $this->updatedAt = new \DateTime('now'); + } + } + + /** + * @return File + */ + public function getImageFile() + { + return $this->imageFile; + } + + /** * Set offre * @@ -606,14 +639,21 @@ class OffreLegumes return $this; } - + /** - * Get offre - * - * @return \PotageBundle\Entity\Offre + * @return int */ public function getOffre() { return $this->offre; } + + /* + private OffrePrepa; + + public function setOffrePrepa($offresPrepa) + { + + } + */ } diff --git a/src/PotageBundle/Form/Offre/OffreLegumesInsertType.php b/src/PotageBundle/Form/Offre/OffreLegumesInsertType.php new file mode 100755 index 0000000000000000000000000000000000000000..370541dd7f1fc0fc26c17eac9cdf634b2a16d567 --- /dev/null +++ b/src/PotageBundle/Form/Offre/OffreLegumesInsertType.php @@ -0,0 +1,45 @@ +add('offre', EntityType::class, array( + 'class' => Offre::class, + 'label' => 'Offres en préparation', + 'attr' => array( + 'class' => 'form-control form-control-sm' + ), + 'choice_label' => 'getReference' + )); + + $builder->add('sauver', SubmitType::class, array( + 'label' => 'Enregistrer', + 'attr' => array('class' => 'btn btn-dark mb-2') + )); + } + + public function configureOptions(OptionsResolver $resolver) + { + parent::configureOptions($resolver); + $resolver->setDefault('data_class', OffreLegumes::class); + $resolver->setDefault('attr', array( + 'class' => 'formulaire formulaire_offre_legumes_insert', + )); + } + + public function getBlockPrefix() + { + return 'offre_legumes_insert_type'; + } +} diff --git a/src/PotageBundle/Repository/OffreRepository.php b/src/PotageBundle/Repository/OffreRepository.php index 1a9d2d951f2394ca5076dfce9c734161f6db1196..aa608202eb5a81c46526d53c21f19be78adbda5b 100755 --- a/src/PotageBundle/Repository/OffreRepository.php +++ b/src/PotageBundle/Repository/OffreRepository.php @@ -89,4 +89,18 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository ->setParameter(':id', $id); return $qb->getQuery()->getOneOrNullResult(); } + + + /** + * @param $status + * @return Offre[] + */ + public function findAllByStatusForInsert() + { + $qb = $this->createQueryBuilder('o') + ->where('o.status = :status') + ->setParameter(':status', 'draft') + ->orderBy('o.reference', 'ASC'); + return $qb->getQuery()->getResult(); + } } diff --git a/src/PotageBundle/Resources/views/Legume/ajaxDisplay.html.twig b/src/PotageBundle/Resources/views/Legume/ajaxDisplay.html.twig index 20031b3fac8393dd3a42cab19d97c71048daa8f1..70d8eb438ea0941089a4c8f29adaa810c9658e62 100755 --- a/src/PotageBundle/Resources/views/Legume/ajaxDisplay.html.twig +++ b/src/PotageBundle/Resources/views/Legume/ajaxDisplay.html.twig @@ -67,6 +67,7 @@ {% include '@Potage/Legume/ajaxForm.html.twig' %} + {% include '@Potage/OffreLegumes/ajaxInsertForm.html.twig' %}