Commit b9149828 authored by Mat's avatar Mat
Browse files

Supprime le CRUD légume classique, celui qui n'interroge pas l'API en ajax

parent 86fe690f
......@@ -39,7 +39,6 @@ class LegumeAPIController extends MasterAPIController
*/
public function readAction(Request $request)
{
// defaults
$paginate = ($request->query->get('step') !== null) ? $request->query->get('step') : 10;
$start = ($request->query->get('page') !== null) ? $request->query->get('page') : 1;
......@@ -62,7 +61,7 @@ class LegumeAPIController extends MasterAPIController
public function countAction()
{
$nbLegumes = $this->getDoctrine()->getManager()
->getRepository('PotageBundle:Legume')->countRows();
->getRepository('PotageBundle:Legume')->countRowsForAPI();
return $this->api($nbLegumes);
}
......@@ -92,13 +91,15 @@ class LegumeAPIController extends MasterAPIController
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')
->findOneForAPIUpdate($id);
$legume = $em->getRepository('PotageBundle:Legume')->findOneForAPIUpdate($id);
if ($legume === null) {
return $this->api($legume, Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(LegumeAPIType::class, $legume);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->persist($legume);
......@@ -117,8 +118,8 @@ class LegumeAPIController extends MasterAPIController
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')
->findOneForAPIdelete($id);
$legume = $em->getRepository('PotageBundle:Legume')->findOneForAPIDelete($id);
if ($legume !== null)
{
$em->remove($legume);
......@@ -136,8 +137,7 @@ class LegumeAPIController extends MasterAPIController
public function duplicateAction($id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')
->findOneForAPIDuplicate($id);
$legume = $em->getRepository('PotageBundle:Legume')->findOneForAPIDuplicate($id);
if ($legume !== null)
{
......@@ -160,8 +160,7 @@ class LegumeAPIController extends MasterAPIController
$em = $this->getDoctrine()->getManager();
$requestString = $request->get('q');
$legumes = $em->getRepository('PotageBundle:Legume')
->findRowsByString($requestString);
$legumes = $em->getRepository('PotageBundle:Legume')->findRowsByString($requestString);
if ($legumes === null)
{
......@@ -175,6 +174,9 @@ class LegumeAPIController extends MasterAPIController
/**
* Fonction interne pour construire le code de référence
* lors d'une insertion dans la table OffreLegumes
*
* @param Legume $legume
* @param $id_offre
* @return string
......@@ -251,6 +253,3 @@ class LegumeAPIController extends MasterAPIController
}
}
/*
* TODO submit du formulaire, persist et flush
*/
\ No newline at end of file
......@@ -4,155 +4,13 @@ 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;
class LegumeController extends MasterController
{
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function createAction(Request $request)
{
$legume = new Legume();
$form = $this->createForm(LegumeType::class, $legume);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($legume);
$em->flush();
$this->addFlash('success', "Le nouveau légume a bien été ajouté");
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
return $this->render('@Potage/Legume/create.html.twig', array(
'formLegume' => $form->createView()
));
}
/**
* @param $page
* @return \Symfony\Component\HttpFoundation\Response
*/
public function readAction($page)
{
if ($page < 1) {
throw $this->createNotFoundException("Page ".$page." inexistante");
}
$nbPerPage = 10;
$em = $this->getDoctrine()->getManager();
$legumes = $em->getRepository('PotageBundle:Legume')->findAllForRead($page, $nbPerPage);
$nbPages = ceil(count($legumes)/$nbPerPage);
if ($page > $nbPerPage) {
throw $this->createNotFoundException("Page ".$page." inexistante");
}
return $this->render('@Potage/Legume/read.html.twig', array(
'legumes' => $legumes,
'nbPages' => $nbPages,
'page' => $page
));
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')->findOneForUpdate($id);
if ($legume === null) {
throw $this->createNotFoundException();
}
$form = $this->createForm(LegumeType::class, $legume);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->persist($legume);
$em->flush();
$this->addFlash('success', "Le légume a bien été modifié");
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
return $this->render('@Potage/Legume/update.html.twig', array(
'id' => $id,
'formLegume' => $form->createView()
));
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')->findOneForDelete($id);
if ($legume === null)
{
throw $this->createNotFoundException('Non trouvé');
} else
{
$em->remove($legume);
$em->flush();
$this->addFlash('success', "Le légume a bien été supprimé");
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function duplicateAction($id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')->findOneForDuplicate($id);
if ($legume === null)
{
throw $this->createNotFoundException('Non trouvé');
} else
{
$newRow = clone $legume;
$em->detach($newRow);
$em->persist($newRow);
$em->flush();
$this->addFlash('success', "Le légume a bien été dupliqué");
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function ajaxDisplayAction()
{
......
......@@ -15,80 +15,6 @@ use PotageBundle\Entity\Legume;
class LegumeRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @param $page
* @param $nbPerPage
* @return Paginator
*/
public function findAllForRead($page, $nbPerPage)
{
$qb = $this->createQueryBuilder('l')
->orderBy('l.id' , 'ASC')
->getQuery();
$qb
->setFirstResult(($page-1)* $nbPerPage)
->setMaxResults($nbPerPage);
return new Paginator($qb, true);
}
/**
* @return mixed
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countRows()
{
$qb = $this->createQueryBuilder('l')
->select('count(l.id)');
return $qb->getQuery()->getSingleScalarResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForUpdate($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForDelete($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForDuplicate($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
//
// pour LegumeAPI
//
/**
* @param $paginate
......@@ -139,7 +65,7 @@ class LegumeRepository extends \Doctrine\ORM\EntityRepository
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIdelete($id)
public function findOneForAPIDelete($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
......@@ -153,15 +79,15 @@ class LegumeRepository extends \Doctrine\ORM\EntityRepository
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countRowsForAjaxDisplay()
public function countRowsForAPI()
{
$qb = $this->createQueryBuilder('l')
->select('count(l.id)');
return $qb->getQuery()->getSingleScalarResult();
}
/**
* @param $id
* @return mixed
......@@ -177,8 +103,8 @@ class LegumeRepository extends \Doctrine\ORM\EntityRepository
}
/**
* Champs de recherche
* @param $string
* @return Legume[]
*/
......
......@@ -3,34 +3,6 @@ potage_homepage:
defaults:
_controller: PotageBundle:Default:index
## CRUD Legume
potage_legume_create:
path: /admin/legume/ajouter
defaults:
_controller: PotageBundle:Legume:create
potage_legume_read:
path: /admin/legumes/{page}
defaults:
_controller: PotageBundle:Legume:read
potage_legume_update:
path: /admin/legume/editer/{id}
defaults:
_controller: PotageBundle:Legume:update
potage_legume_delete:
path: /admin/legume/supprimer/{id}
defaults:
_controller: PotageBundle:Legume:delete
potage_legume_duplicate:
path: /admin/legume/dupliquer/{id}
defaults:
_controller: PotageBundle:Legume:duplicate
### les routes des fonctions qui interrogent l'APIBundle
potage_legume_ajax_display:
......
{% extends '@Potage/layout.html.twig' %}
{% block title %}
Ajouter un légume
{% endblock %}
{% block content %}
<h1>{{ block('title') }}</h1>
<p>
<a href="{{ path('potage_legume_read', {'page': 1}) }}"><i class="fas fa-arrow-left fa-fw"></i>Retour vers la liste</a>
</p>
<div class="row">
<div class="col-9">
{{ form_start(formLegume) }}
{{ form_errors(formLegume) }}
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.fruit) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.fruit) }}</div>
{{ form_errors(formLegume.fruit) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.nom) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.nom) }}</div>
{{ form_errors(formLegume.nom) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.description) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.description) }}</div>
{{ form_errors(formLegume.description) }}</div>
<div class="form-group row">
<div class="col-sm-4"></div><div class="col-sm-8">
{{ form_widget(formLegume.revente) }}{{ form_label(formLegume.revente) }}
{{ form_errors(formLegume.revente) }}</div>
<div class="col-sm-4">{{ form_label(formLegume.origine) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.origine) }}</div>
{{ form_errors(formLegume.origine) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.imageFile) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.imageFile) }}</div>
{{ form_errors(formLegume.imageFile) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.unite) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.unite) }}</div>
{{ form_errors(formLegume.unite) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.quantiteMin) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.quantiteMin) }}</div>
{{ form_errors(formLegume.quantiteMin) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.quantiteMax) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.quantiteMax) }}</div>
{{ form_errors(formLegume.quantiteMax) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.quantiteStep) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.quantiteStep) }}</div>
{{ form_errors(formLegume.quantiteStep) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.prixUnitaire) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.prixUnitaire) }}</div>
{{ form_errors(formLegume.prixUnitaire) }}</div>
<div class="form-group row">
<div class="col-sm-4">{{ form_label(formLegume.sauver) }}</div>
<div class="col-sm-8">{{ form_widget(formLegume.sauver) }}</div>
</div>
{{ form_end(formLegume) }}
</div>
</div>
{% endblock %}
<div class="modal fade" id="delmodal-{{ legume.id }}"
tabindex="-1" role="dialog" aria-labelledby="DeleteModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Effacer le légume n°{{ legume.id }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>Cette action n'est pas réversible. Souhaitez-vous continuer ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
<a class="btn btn-danger" role="button" href="{{ path('potage_legume_delete', {'id': legume.id }) }}">
Oui, effacer !
</a>
</div>
</div>
</div>
</div>
<div class="modal fade" id="dupmodal-{{ legume.id }}"
tabindex="-1" role="dialog" aria-labelledby="DuplicateModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Dupliquer la rangée n°{{ legume.id }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>Un nouvel élément va être créé. Souhaitez-vous continuer ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
<a class="btn btn-primary" role="button" href="{{ path('potage_legume_duplicate', {'id': legume.id }) }}">
Dupliquer
</a>
</div>
</div>
</div>
</div>
{% extends '@Potage/layout.html.twig' %}
{% block title %}
Liste des légumes
{% endblock %}
{% block content %}
<h1>{{ block('title') }}</h1>
{% for label, messages in app.flashes() %}
{% for message in messages %}
<div class="flash-{{ label }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}
<p>
{{ legumes|length }} éléments — page {{ page }}/{{ nbPages }}
</p>
<p class="right">
<a class="btn btn-primary" role="button" aria-pressed="true"
href="{{ path('potage_legume_create') }}"><i class="fas fa-plus fa-fw"></i> Ajouter un légume</a>
</p>
<table id="maintab" class="table legumes">
<thead>
<tr>
{% set cols = ["#", "Nom", "Description", "Unité", "Qté min", "Max", "Step", "Prix unitaire"] %}
{% for col in cols %}
<th scope="col">{{col}}
<a href="#" class="btn-tri" id="btncol{{ loop.index0 }}_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol{{ loop.index0 }}_desc"><i class="fa fa-caret-up"></i></a>
</th>
{% endfor %}
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for legume in legumes %}
<tr id="row{{ legume.id }}">
<td scope="row">{{ legume.id }}</td>
<td>{{ legume.nom }}</td>
<td>{{ legume.description }}</td>
<td>{{ legume.unite }}</td>
<td>{{ legume.quantiteMin }}</td>
<td>{{ legume.quantiteMax }}</td>
<td>{{ legume.quantiteStep }}</td>
<td>{{ legume.prixUnitaire }} €/{{ legume.unite }}</td>
<td>
<a class="btn btn-primary btn-sm" role="button" href="{{ path('potage_legume_update', {'id': legume.id }) }}" title="Editer">
<i class="fas fa-edit fa-fw"></i></a>
<a class="btn btn-primary btn-sm btn-modal" role="button" href="#" id="dup-{{ legume.id }}" title="Dupliquer">
<i class="fas fa-clone fa-fw"></i></a>
{% include '@Potage/Legume/duplicate-modal.html.twig' %}
<a class="btn btn-danger btn-sm btn-modal" role="button" href="#" id="del-{{ legume.id }}" title="Effacer">
<i class="fas fa-trash-alt fa-fw"></i></a>
{% include '@Potage/Legume/delete-modal.html.twig' %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{#<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only"&g