Commit 3beeba35 authored by Mat's avatar Mat
Browse files

Légume: Create and Read

parent 22e0fffd
......@@ -4,6 +4,9 @@ namespace PotageBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use PotageBundle\Entity\Legume;
use PotageBundle\Form\Legume\LegumeType;
class LegumeController extends Controller
{
......@@ -15,7 +18,23 @@ class LegumeController extends Controller
*/
public function createAction(Request $request)
{
return $this->render('@Potage/Legume/create.html.twig', array());
$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();
return $this->redirectToRoute('potage_legume_read');
}
return $this->render('@Potage/Legume/create.html.twig', array(
'formLegume' => $form->createView()
));
}
/**
......@@ -25,7 +44,11 @@ class LegumeController extends Controller
*/
public function readAction()
{
return $this->render('@Potage/Legume/read.html.twig', array());
$legumes = $this->getDoctrine()->getManager()->getRepository('PotageBundle:Legume')->findAllByRead();
dump($legumes);
return $this->render('@Potage/Legume/read.html.twig', array(
'legumes' => $legumes
));
}
/**
......
<?php
namespace PotageBundle\Form\Legume;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use PotageBundle\Entity\Legume;
class LegumeType extends AbstractType
{
/**
*
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('nom', TextType::class, array(
'label' => "Nom",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('description', TextareaType::class, array(
'label' => "Description",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('logoPath', TextType::class, array(
'required' => false,
'label' => "Chemin du logo",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('unite', TextType::class, array(
'label' => "Unité",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('quantiteMin', IntegerType::class, array(
'label' => "Quantité minimale",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('quantiteMax', IntegerType::class, array(
'label' => "Quantité maximale",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('quantiteStep', IntegerType::class, array(
'label' => "Écart (step)",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('prixUnitaire', MoneyType::class, array(
'label' => "Prix unitaire",
'label_attr' => array(),
'attr' => array(),
));
$builder->add('sauver', SubmitType::class, array(
'label' => 'Enregistrer',
));
}
/**
*
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', Legume::class);
$resolver->setDefault('attr', array(
'class' => 'custom_input',
'novalidate' => true
));
}
}
......@@ -10,4 +10,21 @@ namespace PotageBundle\Repository;
*/
class LegumeRepository extends \Doctrine\ORM\EntityRepository
{
/**
*
* @return Legume[]
*/
public function findAllByRead()
{
$qb = $this->createQueryBuilder('l')
->orderBy('l.nom' , 'ASC');
return $qb->getQuery()->getResult();
}
/**
* @var id
* @return Legume
* @throws \Doctrine\ORM\NonUniqueResultException
*/
}
......@@ -10,5 +10,6 @@
<h1>{{ block('title') }}</h1>
{{ form(formLegume) }}
{% endblock %}
{% extends '@Potage/layout.html.twig' %}
{% block title %}
Les légumes
Liste des légumes
{% endblock %}
{% block content %}
......@@ -10,5 +10,81 @@
<h1>{{ block('title') }}</h1>
<table id="maintab" class="table legumes">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nom
<a href="#" class="btn-tri" id="btncol0_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol0_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Description
<a href="#" class="btn-tri" id="btncol1_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol1_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Unité
<a href="#" class="btn-tri" id="btncol2_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol2_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Qté min
<a href="#" class="btn-tri" id="btncol3_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol3_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Max
<a href="#" class="btn-tri" id="btncol3_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol3_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Step
<a href="#" class="btn-tri" id="btncol3_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol3_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Prix unitaire
<a href="#" class="btn-tri" id="btncol4_asc"><i class="fa fa-caret-down"></i></a>
<a href="#" class="btn-tri" id="btncol4_desc"><i class="fa fa-caret-up"></i></a>
</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for legume in legumes %}
<tr id="row{{ legume.id }}">
<th scope="row">{{ legume.id }}</th>
<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 }}</td>
<td>
<a class="btn btn-primary btn-sm" role="button" href="{{ path('potage_legume_update', {'id': legume.id }) }}">Editer</a>
<a class="btn btn-danger btn-sm" role="button" href="#" id="btn-{{ legume.id }}">Effacer</a>
<div class="modal modal-{{ legume.id }} fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Effacer l'association 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>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
// 1
// Modale de confirmation avant effacement
let dropbtns = document.querySelectorAll("table.table a.btn-danger:not(.disabled)");
for (let bouton of dropbtns)
{
if (bouton.getAttribute('id') !== null) {
let id = bouton.getAttribute('id').replace("btn-", "");
bouton.addEventListener("click", function() {
$('.modal-' + id ).modal({
focus: true
});
});
}
}
// 2
// Tri par colonnes
function ASC (a, b) {
a = a[1]; b = b[1];
if (a > b) { return 1; }
else if (a < b) { return -1; }
else { return 0; }
}
function DESC (a, b) {
a = a[1]; b = b[1];
if (a > b) { return -1; }
else if (a < b) { return 1; }
else { return 0; }
}
function sortTable(table, col, tri)
{
let tbody = document.getElementById(table).getElementsByTagName('tbody')[0];
let rows = tbody.getElementsByTagName('tr');
let resort = new Array();
for (let row of rows)
{
let sortcol = row.getElementsByTagName('td')[col].innerHTML;
resort.push([row, sortcol]);
}
if (tri === 'ASC') { resort.sort(ASC); }
if (tri === 'DESC') { resort.sort(DESC); }
for (var i = 0; i < resort.length; i++) {
tbody.appendChild(resort[i][0]);
}
}
let buttons = document.querySelectorAll('a.btn-tri');
for (let btn of buttons)
{
btn.addEventListener("click", function()
{
let col = btn.getAttribute('id').replace("btncol","").split("_")[0];
let tri = btn.getAttribute('id').replace("btncol","").split("_")[1].toUpperCase();
sortTable('maintab', col, tri);
});
}
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