Commit 1f144de3 authored by Mat's avatar Mat
Browse files

Complète le template OffreLegumes avec le formOffre pour éditer l'offre ..wip

* la base est posée, mais y a qqch qui bloque le GET du update et le POST du update
parent 0673aebe
......@@ -76,16 +76,59 @@ class OffreAPIController extends MasterAPIController
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function readOneAction($id)
{
$em = $this->getDoctrine()->getManager();
$offre = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id);
if ($offre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
return $this->api($offre);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateStatusAction(Request $request, $id)
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offre = $em->getRepository('PotageBundle:Offre')->findOneForUpdate($id);
$form = $this->createForm(OffreAPIType::class, $offre);
$form->handleRequest($request);
if ($offre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($offre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateStatusAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offre = $em->getRepository('PotageBundle:Offre')->findOneForUpdateStatus($id);
$form = $this->createForm(OffreStatusAPIType::class, $offre);
$form->handleRequest($request);
......
......@@ -44,7 +44,7 @@ class OffreLegumesAPIController extends MasterAPIController
$em = $this->getDoctrine()->getManager();
// Récupère l'ordre de tri
$sorted = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id_offre)->getSorted();
$sorted = $em->getRepository('PotageBundle:Offre')->findOneForAPISorted($id_offre)->getSorted();
if (!empty($sorted))
{
......
......@@ -88,8 +88,24 @@ api_offre_create:
_controller: APIBundle:OffreAPI:create
methods: [ POST ]
api_offre_update_status:
api_offre_read_one:
path: /offre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreAPI:readOne
methods: [ GET ]
api_offre_update:
path: /offre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreAPI:update
methods: [ POST ]
api_offre_update_status:
path: /offre/{id}/status
requirements:
id: \d+
defaults:
......
......@@ -3,6 +3,7 @@
namespace PotageBundle\Controller;
//use APIBundle\Form\OffreLegumesAPIType;
use APIBundle\Form\OffreAPIType;
use APIBundle\Form\OffreSortedAPIType;
class OffreLegumesController extends MasterController
......@@ -24,12 +25,14 @@ class OffreLegumesController extends MasterController
}
//$form = $this->createForm(OffreLegumesAPIType::class);
$formOffre = $this->createForm(OffreAPIType::class);
$formSorted = $this->createForm(OffreSortedAPIType::class);
return $this->render('@Potage/OffreLegumes/ajaxDisplay.html.twig', array(
'id' => $id,
'offre' => $offre,
//'formOffreLegumes' => $form->createView(),
'formOffre' => $formOffre->createView(),
'formOffreSorted' => $formSorted->createView()
));
}
......
......@@ -18,7 +18,7 @@ class OffreType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('endedAt', DateType::class, array(
'label' => "Date de clôture de la nouvelle offre",
'label' => "Date de clôture de l'offre",
'widget' => 'single_text',
'data' => new \DateTime('now'),
'label_attr' => array('class' => 'col-form-label'),
......
......@@ -60,6 +60,23 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForUpdateStatus($id)
{
$qb = $this->createQueryBuilder('o')
->where('o.id = :id')
->andWhere('o.status = :status ')
->setParameters(array(
':id'=> $id,
':status' => 'draft'
));
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
......@@ -100,6 +117,20 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIRead($id)
{
$qb = $this->createQueryBuilder('o')
->select('o.id', 'o.createdAt', 'o.updatedAt', 'o.endedAt', 'o.status', 'o.reference')
->where('o.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPISorted($id)
{
$qb = $this->createQueryBuilder('o')
->where('o.id = :id')
......
......@@ -20,9 +20,8 @@
{% endblock %}
{% block headerContent %}
{#
{% include '@Potage/OffreLegumes/modalForm.html.twig' %}
#}
{# la modale est dans le dossier à côté, dans Offre #}
{% include '@Potage/Offre/modalForm.html.twig' %}
{% endblock %}
{% block sidebarContent %}
......@@ -67,49 +66,53 @@
<aside class="col-3">
<div class="card">
<div class="card-header">
<h2>
Offre # {{ id }}
</h2>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<h2>
Offre # {{ id }}
</h2>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Créée le {{ offre.createdAt.format('d/m/Y H:i') }}
{#
Dernière modification: {{ offre.updatedAt.format('d/m/Y H:i') }}
#}
<dl>
<dt>Créée le:</dt>
<dd class="field-offre field1">{{ offre.createdAt.format('d/m/Y H:i') }}</dd>
<dt>Dernière modification:</dt>
<dd class="field-offre field2">{{ offre.updatedAt.format('d/m/Y H:i') }}</dd>
</dl>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ offre.getOffreLegumes|length }}{{ offre.getOffreLegumes|length > 1 ? ' légumes' : ' légume' }}
</li>
<li class="list-group-item list-group-item-action {#list-group-item-warning#}">
Statut: {{ offre.status }}
<li class="list-group-item">
<dl>
<dt>Statut:</dt>
<dd class="field-offre field3">{{ offre.status }}</dd>
</dl>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Date d'expiration: {{ offre.endedAt.format('d/m/Y H:i') }}
<li class="list-group-item border-bottom-0">
<dl>
<dt>Expire le:</dt>
<dd class="field-offre field4">{{ offre.endedAt.format('d/m/Y H:i') }}</dd>
</dl>
</li>
{#
<li class="list-group-item d-flex">
<button class="btn btn-dark btn-sm mr-2 mb-2">
Modifier
</button>
<button class="btn btn-info btn-sm mb-2 btn-order">
Sauvegarder
</ul>
{% if offre.status == 'draft' %}
<div class="card-footer">
<button class="btn btn-dark btn-sm mb-1 mr-1" title="Éditer" data-vivaldi-spatnav-clickable="1"
data-toggle="modal" data-target=".form-offre-modal" data-offre-id="{{ id }}">
<i class="fas fa-pencil-alt fa-fw"></i> Modifier
</button>
</li>
#}
{% if offre.status == 'draft' %}
<li class="list-group-item">
</div>
{% endif %}
</div>
<div class="card mt-3">
<div class="card-header">
{{ offre.getOffreLegumes|length }}{{ offre.getOffreLegumes|length > 1 ? ' légumes' : ' légume' }}
</div>
{% if offre.status == 'draft' %}
<div class="card-body">
<p>Sauvegarder la position</p>
{{ form(formOffreSorted) }}
</li>
{% endif %}
</ul>
</div>
{% endif %}
</div>
</aside>
......@@ -132,9 +135,8 @@
{% endblock %}
{#
TODO sauvegarder l'ordre dans la db quand on enregistre
TODO promo sur l'offre ou sur un légume
TODO volume_max par item
TODO gérer ce qu'on peut/ne peut pas faire en fonction du status de l'offre
TODO rendu des légumes proche de l'offre diffusée (masonry ?)
TODO qu'est-ce qui est différent quand status = draft ou = current, closed ??
#}
\ No newline at end of file
......@@ -131,4 +131,75 @@ sortForm.addEventListener('submit', function(e)
$( function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
} );
\ No newline at end of file
} );
/*
*
*
* Modifier l'offre
*
*
*/
/*
* Recharge le bloc dans aside
*/
function reloadBlocOffre()
{
let fieldsOffre = document.querySelectorAll('.field-offre');
AJAX('GET',
Routing.generate('api_offre_read_one', {'id': id_offre}),
function(request) {
let json = JSON.parse(request.responseText);
let listStatus = { 'draft': 'Brouillon', 'posted': 'Publié', 'trash': 'Poubelle' };
let couleurs = {'draft': 'warning', 'posted': 'success', 'trash': 'danger'};
fieldsOffre[0].textContent = formatStringDateTime(json.createdAt, true, true, false, false);
fieldsOffre[1].textContent = formatStringDateTime(json.updatedAt, true, true, false, false);
fieldsOffre[2].innerHTML = buildColorSquare(json.status, couleurs) + listStatus[json.status];
fieldsOffre[3].textContent = formatStringDateTime(json.endedAt, true, true, false, false);
}
);
}
/*
* Ouvre le formulaire d'édition de Offre (modale)
*/
function editBlocOffre()
{
AJAX('GET',
Routing.generate('api_offre_read_one', {'id': id_offre}),
function(request) {
let json = JSON.parse(request.responseText);
formTitle.textContent = "Modifier l'offre " + json.reference;
form.querySelector('#offre_status').value = json.status;
form.querySelector('#offre_endedAt').value = json.endedAt;
submitURL = Routing.generate('api_offre_update', {'id': id_offre});
});
}
let formTitle = document.querySelector('h2#form-offre');
let form = document.querySelector('form[name=offre]');
reloadBlocOffre();
editBlocOffre();
/*
* Soumission du formulaire
*/
form.addEventListener('submit', function(e)
{
let form = this;
e.preventDefault();
AJAX('POST', submitURL, function() {
reloadBlocOffre();
$('.form-offre-modal').modal('hide'); // en jquery
}, form);
});
\ No newline at end of file
......@@ -123,6 +123,11 @@ form.formulaire {
}
dd {
padding-left: 5em;
}
aside.col-3 dd {
padding-left: 0;
}
table.lettres dd {
margin-top: -1.5em; /* nécessaire template Lettre */
}
......
Supports Markdown
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