Commit 352675f7 authored by Mat's avatar Mat

Légume, un bouton insérer ouvre une modale avec un select des offres ->

WIP
* cette construction joue avec les 3 entités,
* TODO filtrer le select (EntityType) pour qu'il n'affiche que les offres en préparation
* TODO submit du formulaire, persist et flush
parent 15daa735
......@@ -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;
......@@ -161,6 +163,63 @@ class LegumeAPIController extends MasterAPIController
}
}
/**
* @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
<?php
namespace APIBundle\Form;
use PotageBundle\Form\Offre\OffreLegumesInsertType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreLegumesInsertAPIType extends OffreLegumesInsertType
{
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->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));
}
}
......@@ -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:
......
......@@ -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;
......@@ -38,9 +39,8 @@ class LegumeController extends MasterController
}
/**
*
* @param $page
* @return \Symfony\Component\HttpFoundation\Response
*
*/
public function readAction($page)
{
......@@ -107,7 +107,11 @@ 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()
));
}
}
......@@ -157,6 +157,14 @@ class OffreLegumes
private $promo;
/**
* OffreLegumes constructor.
*/
public function __construct()
{
$this->isPromo = false;
}
/**
*
* @ORM\PrePersist()
......@@ -593,6 +601,31 @@ 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
*
......@@ -608,12 +641,19 @@ class OffreLegumes
}
/**
* Get offre
*
* @return \PotageBundle\Entity\Offre
* @return int
*/
public function getOffre()
{
return $this->offre;
}
/*
private OffrePrepa;
public function setOffrePrepa($offresPrepa)
{
}
*/
}
<?php
namespace PotageBundle\Form\Offre;
use PotageBundle\Entity\Offre;
use PotageBundle\Entity\OffreLegumes;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreLegumesInsertType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->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';
}
}
......@@ -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();
}
}
......@@ -67,6 +67,7 @@
</nav>
{% include '@Potage/Legume/ajaxForm.html.twig' %}
{% include '@Potage/OffreLegumes/ajaxInsertForm.html.twig' %}
<table id="maintab" class="table legumes table-striped">
<thead>
......@@ -281,6 +282,7 @@
td7.appendChild(displayEditLegume(jsonRow));
td7.appendChild(displayDuplicateLegume(jsonRow));
td7.appendChild(displayDeleteLegume(jsonRow));
td7.appendChild(displayInsertLegume(jsonRow));
tr.appendChild(td7);
return tr;
......@@ -311,17 +313,10 @@
form.querySelector('#legume_nom').value = json.nom;
let legumeFruit = form.querySelector('#legume_fruit');
//console.log(json.fruit);
//console.log(legumeFruit);
//console.log(legumeFruit.options);
for (let i = 0; i < legumeFruit.options.length; i++)
{
//console.log(legumeFruit.options[i]);
if (legumeFruit.options[i].value == json.fruit)
{
//console.log(legumeFruit[i].label);
//console.log(legumeFruit[i].index);
//console.log(legumeFruit[i].value);
legumeFruit.selectedIndex = legumeFruit[i].index;
}
}
......@@ -380,7 +375,8 @@
deleteBtn.innerHTML = '<i class="fas fa-trash fa-fw"></i>';
deleteBtn.classList.add('btn', 'btn-danger', 'btn-sm', 'mb-1', 'mr-1');
deleteBtn.dataset.legumeId = jsonRow.id;
deleteBtn.addEventListener('click', function () {
deleteBtn.addEventListener('click', function()
{
let legumeId = this.dataset.legumeId;
let r = confirm('Effacer le légume n°' + legumeId
+ '\nCette action n\'est pas réversible. Souhaitez-vous continuer ?\n');
......@@ -395,6 +391,41 @@
}
/*
* Affiche un bouton pour insérer le légume dans une offre
* @param json object jsonRow
*/
function displayInsertLegume(jsonRow)
{
let insertBtn = document.createElement('button');
insertBtn.setAttribute('title', 'Insérer dans une offre');
insertBtn.innerHTML = '<i class="fas fa-clipboard-check fa-fw"></i> Insérer';
insertBtn.classList.add('btn', 'btn-info', 'btn-sm', 'mb-1', 'mr-1');
insertBtn.dataset.legumeId = jsonRow.id;
insertBtn.dataset.toggle = "modal";
insertBtn.dataset.target = ".form-offre-legume-insert-modal";
insertBtn.addEventListener('click', function()
{
let legumeId = this.dataset.legumeId;
submitURL = Routing.generate('api_legume_insert', {'id': legumeId});
let titleInsertForm = document.querySelector('#form-offre-legume-insert');
titleInsertForm.textContent = 'Insérer le légume # ' + jsonRow.id;
AJAX('GET', submitURL, function(request)
{
console.log('getget');
let json = JSON.parse(request.responseText);
reloadTableLegume();
});
});
return insertBtn;
}
/*
*
* Initialise l'affichage de la page
......
<div class="modal fade form-offre-legume-insert-modal" tabindex="-1" role="dialog" aria-labelledby="FormOffreLegumesInsertModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="form-offre-legume-insert"></h2>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{{ form_start(formOffreLegumesInsert) }}
<div class="modal-body">
{{ form_errors(formOffreLegumesInsert) }}
<div class="form-group row">
<div class="col-sm-4">
{{ form_label(formOffreLegumesInsert.offre) }}
</div>
<div class="col-sm-8">
{{ form_widget(formOffreLegumesInsert.offre) }}
{{ form_errors(formOffreLegumesInsert.offre) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary mb-2" data-dismiss="modal">Fermer</button>
{{ form_label(formOffreLegumesInsert.sauver) }}
{{ form_widget(formOffreLegumesInsert.sauver) }}
</div>
{{ form_rest(formOffreLegumesInsert) }}
{{ form_end(formOffreLegumesInsert) }}
</div>
</div>
</div>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment