Commit 86fe690f authored by Mat's avatar Mat
Browse files

affichage ajax de la table des légumes de l'offre

parent 505b08b2
......@@ -53,6 +53,7 @@ class LegumeAPIController extends MasterAPIController
return $this->api($legumes);
}
/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NoResultException
......@@ -108,7 +109,6 @@ class LegumeAPIController extends MasterAPIController
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
......@@ -128,7 +128,6 @@ class LegumeAPIController extends MasterAPIController
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
......@@ -222,7 +221,6 @@ class LegumeAPIController extends MasterAPIController
$formInsert = $request->request->get('offre_legumes_insert');
$id_offre = $formInsert['offre'];
$reference = $this->newReference($legume, $id_offre);
$offrelegumes = new OffreLegumes();
......@@ -240,18 +238,15 @@ class LegumeAPIController extends MasterAPIController
->setFruit($legume->getFruit())
->setRevente($legume->getRevente())
;
$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);
}
......
......@@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Response;
class OffreAPIController extends MasterAPIController
{
/**
* @return string
* @throws \Doctrine\ORM\NonUniqueResultException
......@@ -33,12 +34,10 @@ class OffreAPIController extends MasterAPIController
else {
$last = '01';
}
return 'OF' . $today . $last;
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
......@@ -51,7 +50,6 @@ class OffreAPIController extends MasterAPIController
$offre = new Offre($reference);
$form = $this->createForm(OffreAPIType::class, $offre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
......@@ -126,6 +124,5 @@ class OffreAPIController extends MasterAPIController
$em->flush();
}
return $this->api([]);
}
}
<?php
namespace APIBundle\Controller;
class OffreLegumesAPIController extends MasterAPIController
{
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readAction($id)
{
$em = $this->getDoctrine()->getManager();
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForRead($id);
return $this->api($legumes);
}
}
......@@ -104,3 +104,13 @@ api_offre_delete:
_controller: APIBundle:OffreAPI:delete
methods: [ DELETE ]
## CRUD OffreLegumesAPI
api_offre_legumes_read:
path: /offre/{id}/legumes
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreLegumesAPI:read
methods: [ GET ]
......@@ -2,6 +2,7 @@
namespace PotageBundle\Repository;
/**
* OffreLegumesRepository
*
......@@ -11,7 +12,6 @@ namespace PotageBundle\Repository;
class OffreLegumesRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @param $id_offre
* @return mixed
......@@ -27,4 +27,19 @@ class OffreLegumesRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id_offre
* @return array OffreLegumes
*/
public function findAllForRead($id_offre)
{
$qb = $this->createQueryBuilder('ol')
->join('ol.offre', 'o')
->addSelect('o')
->where('ol.offre = :id_offre')
->setParameter(':id_offre', $id_offre)
;
return $qb->getQuery()->getResult();
}
}
{% extends '@Potage/layout.html.twig' %}
{% block title %}
Offre # {{ id }}
{{ offre.reference }}
{% endblock %}
{% block searchbar %}
......@@ -27,31 +27,57 @@
{% endblock %}
{% block headerContent %}
<div class="row">
<div class="col left hd-1">
<h1>{{ block('title') }}</h1>
{#
{% include '@Potage/OffreLegumes/ajaxForm.html.twig' %}
{{ dump() }}
#}
{% endblock %}
</div>
<div class="col right hd-1">
<div style="margin-top: 0.65em;">
<a href="javascript: void(0);" class="btn btn-outline-dark mb-2 btn-reload">
<i class="fas fa-sync-alt"></i>
Rafraîchir la liste
</a>
<a href="javascript: void(0);" class="btn btn-dark mb-2 btn-create"
data-toggle="modal" data-target=".form-offre-modal">
<i class="fas fa-plus"></i>
Créer une nouvelle offre
</a>
{% block sidebarContent %}
<div class="row">
<section class="col-9">
<div class="row">
<div class="col left hd-1">
<h1>{{ block('title') }}</h1>
</div>
<div class="col right hd-1">
<div style="margin-top: 0.65em;">
<a href="javascript: void(0);" class="btn btn-outline-dark mb-2 btn-reload">
<i class="fas fa-sync-alt"></i>
Rafraîchir la liste
</a>
{#
<a href="javascript: void(0);" class="btn btn-dark mb-2 btn-create"
data-toggle="modal" data-target=".form-offre-modal">
<i class="fas fa-plus"></i>
Créer une nouvelle offre
</a>
#}
</div>
</div>
</div>
</div>
<table id="maintab" class="table offre-legumes table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Vignette</th>
<th scope="col">Nom</th>
<th scope="col">Prix</th>
<th scope="col">Ordre</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</section>
{% endblock %}
{% block sidebarContent %}
<div class="row">
{% block sectionContent %}
<aside class="col-3">
<div class="card">
......@@ -59,18 +85,19 @@
<div class="card-body">
<h3>
{{ offre.reference }}
Offre # {{ id }}
</h3>
</div>
<li class="list-group-item d-flex justify-content-between align-items-center">
Créée le {{ offre.createdAt.format('d/m/Y H:i') }}
</li>
{#
<li class="list-group-item d-flex justify-content-between align-items-center">
{#
Dernière modification: {{ offre.updatedAt.format('d/m/Y H:i') }}
</li>
#}
#}
</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>
......@@ -81,8 +108,8 @@
<button class="btn btn-dark btn-sm mr-2 mb-2">
Modifier
</button>
<button class="btn btn-danger btn-sm mb-2">
Supprimer
<button class="btn btn-info btn-sm mb-2">
Sauvegarder
</button>
</li>
......@@ -90,21 +117,6 @@
</div>
</aside>
{% endblock %}
{% block sectionContent %}
<section class="col-9">
{#
{% include '@Potage/OffreLegumes/ajaxForm.html.twig' %}
{{ dump() }}
#}
</section>
</div>
{% endblock %}
......@@ -114,6 +126,99 @@
<script type="text/javascript" src="{{ asset('js/ajax_functions.js') }}"></script>
<script type="text/javascript">
/*
* Recharge la table OffreLegumes
*/
function reloadTableLegumes()
{
let table = document.querySelector('table#maintab.offre-legumes');
let tbody = table.querySelector('tbody');
tbody.innerHTML = null;
let URL = Routing.generate('api_offre_legumes_read', {'id': id_offre });
AJAX('GET', URL, function(request)
{
let json = JSON.parse(request.responseText);
for(let i = 0; i < json.length; i++ )
{
let tr = buildRowOffreLegumes(json[i]);
tbody.appendChild(tr);
}
});
}
/*
* Construit une rangée du tableau des Offres
* @param json object jsonRow
*/
function buildRowOffreLegumes(jsonRow)
{
console.log(jsonRow);
let tr = document.createElement('tr');
let td1 = document.createElement('td');
td1.textContent = jsonRow.id;
tr.appendChild(td1);
let td2 = document.createElement('td');
td2.innerHTML = jsonRow.image ? '<div class="logo-sm"><div class="crop">' +
'<img src="/web/uploads/images/legumes/' + jsonRow.image + '" /></div></div>': null;
tr.appendChild(td2);
let td3 = document.createElement('td');
td3.textContent = jsonRow.nom;
tr.appendChild(td3);
let td4 = document.createElement('td');
td4.textContent = jsonRow.prix_unitaire + '€/' + jsonRow.unite;
tr.appendChild(td4);
let td5 = document.createElement('td');
td5.textContent = jsonRow.ordre;
tr.appendChild(td5);
let td6 = document.createElement('td');
td6.textContent = "boutons";
tr.appendChild(td6);
return tr;
}
/*
*
* Initialise l'affichage de la page
*/
let id_offre = {{ id }};
//let submitURL = null;
//let form = document.querySelector('form[name=offre]');
//let titleForm = document.querySelector('#form-offre');
reloadTableLegumes();
/*
* Bouton pour recharger la page
*/
let reloadBtns = document.querySelectorAll('.btn-reload');
reloadBtns.forEach(function(reloadBtn) {
reloadBtn.addEventListener('click', function()
{
reloadTableLegumes();
});
});
</script>
{% endblock %}
{#
TODO modifier l'ordre des items en drag'n drop (jquery)
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 ?)
#}
\ No newline at end of file
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