Controller pour Plats avec le form et templates.

This commit is contained in:
leroyv 2024-11-21 14:58:12 +01:00
parent 41dbddb1f7
commit 3f4507958c
11 changed files with 246 additions and 148 deletions

View File

@ -1,46 +0,0 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class CuisinierController extends AbstractController
{
#[Route('/cuisinier/ajouter', name: 'ajouter_cuisinier')]
public function ajouter(Request $request, EntityManagerInterface $entityManager): Response
{
$cuisinier = new Cuisinier();
$form = $this->createForm(CuisinierType::class, $cuisinier);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($cuisinier);
$entityManager->flush();
$this->addFlash('success', 'Cuisinier ajouté avec succès !');
return $this->redirectToRoute('ajouter_cuisinier');
}
return $this->render('cuisinier/ajouter.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/cuisinier/supprimer/{id}', name: 'supprimer_cuisinier', methods: ['POST'])]
public function supprimer(int $id, EntityManagerInterface $entityManager): Response
{
$cuisinier = $entityManager->getRepository(Cuisinier::class)->find($id);
if (!$cuisinier) {
throw $this->createNotFoundException('Ce cuisinier n\'existe pas');
}
$entityManager->remove($cuisinier);
$entityManager->flush();
$this->addFlash('success', 'Cuisinier supprimé avec succès !');
return $this->redirectToRoute('ajouter_cuisinier');
}
}

View File

@ -1,78 +0,0 @@
<?php
namespace App\Controller;
use App\Entity\Plats;
use App\Form\PlatType;
use App\Form\StatutCommandesType;
use App\Repository\PlatsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
#[Route('/plats', name: 'app_plat')]
class PlatController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly PlatsRepository $platsRepository
)
{
}
#[Route('/add', name: '_add')]
public function add(Request $request): Response
{
$plat = new Plats();
$form = $this->createForm(PlatType::class, $plat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($plat);
$this->entityManager->flush();
}
return $this->render('plat/add.html.twig', [
'formAdd' => $form,
]);
}
#[Route('/list', name: '_list')]
public function list(): Response
{
$plat = $this->platsRepository->findAll();
return $this->render('plat/list.html.twig', [
'plat' => $plat,
]);
}
#[Route('/update/{id}', name: '_update')]
public function update(int $id, Request $request): Response
{
$plat = $this->platsRepository->find($id);
$form = $this->createForm(PlatType::class, $plat);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$this->entityManager->persist($plat);
$this->entityManager->flush();
}
return $this->render('plat/add.html.twig', [
'formAdd' => $form,
]);
}
#[Route('/delete/{id}', name: '_delete')]
public function delete(int $id): Response
{
$statutCommandes = $this->platsRepository->find($id);
$this->entityManager->remove($statutCommandes);
$this->entityManager->flush();
return $this->redirectToRoute('app_plat_list');
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Controller;
use App\Entity\Plats;
use App\Form\PlatsType;
use App\Repository\PlatsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/plats')]
final class PlatsController extends AbstractController
{
#[Route(name: 'app_plats_index', methods: ['GET'])]
public function index(PlatsRepository $platsRepository): Response
{
return $this->render('plats/index.html.twig', [
'plats' => $platsRepository->findAll(),
]);
}
#[Route('/new', name: 'app_plats_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$plat = new Plats();
$form = $this->createForm(PlatsType::class, $plat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($plat);
$entityManager->flush();
return $this->redirectToRoute('app_plats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('plats/new.html.twig', [
'plat' => $plat,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_plats_show', methods: ['GET'])]
public function show(Plats $plat): Response
{
return $this->render('plats/show.html.twig', [
'plat' => $plat,
]);
}
#[Route('/{id}/edit', name: 'app_plats_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Plats $plat, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(PlatsType::class, $plat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_plats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('plats/edit.html.twig', [
'plat' => $plat,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_plats_delete', methods: ['POST'])]
public function delete(Request $request, Plats $plat, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$plat->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($plat);
$entityManager->flush();
}
return $this->redirectToRoute('app_plats_index', [], Response::HTTP_SEE_OTHER);
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PlatType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('field_name')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
// Configure your form options here
]);
}
}

42
src/Form/PlatsType.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace App\Form;
use App\Entity\Commandes;
use App\Entity\Plats;
use App\Entity\Reductions;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PlatsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('Nom')
->add('Description')
->add('Prix')
->add('Categorie')
->add('Statut')
->add('Nb_de_commande')
->add('Commande', EntityType::class, [
'class' => Commandes::class,
'choice_label' => 'id',
'multiple' => true,
])
->add('Reduction', EntityType::class, [
'class' => Reductions::class,
'choice_label' => 'id',
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Plats::class,
]);
}
}

View File

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_plats_delete', {'id': plat.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ plat.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Plats{% endblock %}
{% block body %}
<h1>Edit Plats</h1>
{{ include('plats/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_plats_index') }}">back to list</a>
{{ include('plats/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,45 @@
{% extends 'base.html.twig' %}
{% block title %}Plats index{% endblock %}
{% block body %}
<h1>Plats index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Nom</th>
<th>Description</th>
<th>Prix</th>
<th>Categorie</th>
<th>Statut</th>
<th>Nb_de_commande</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for plat in plats %}
<tr>
<td>{{ plat.id }}</td>
<td>{{ plat.Nom }}</td>
<td>{{ plat.Description }}</td>
<td>{{ plat.Prix }}</td>
<td>{{ plat.Categorie }}</td>
<td>{{ plat.Statut ? 'Yes' : 'No' }}</td>
<td>{{ plat.NbDeCommande }}</td>
<td>
<a href="{{ path('app_plats_show', {'id': plat.id}) }}">show</a>
<a href="{{ path('app_plats_edit', {'id': plat.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="8">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_plats_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Plats{% endblock %}
{% block body %}
<h1>Create new Plats</h1>
{{ include('plats/_form.html.twig') }}
<a href="{{ path('app_plats_index') }}">back to list</a>
{% endblock %}

View File

@ -0,0 +1,46 @@
{% extends 'base.html.twig' %}
{% block title %}Plats{% endblock %}
{% block body %}
<h1>Plats</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ plat.id }}</td>
</tr>
<tr>
<th>Nom</th>
<td>{{ plat.Nom }}</td>
</tr>
<tr>
<th>Description</th>
<td>{{ plat.Description }}</td>
</tr>
<tr>
<th>Prix</th>
<td>{{ plat.Prix }}</td>
</tr>
<tr>
<th>Categorie</th>
<td>{{ plat.Categorie }}</td>
</tr>
<tr>
<th>Statut</th>
<td>{{ plat.Statut ? 'Yes' : 'No' }}</td>
</tr>
<tr>
<th>Nb_de_commande</th>
<td>{{ plat.NbDeCommande }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_plats_index') }}">back to list</a>
<a href="{{ path('app_plats_edit', {'id': plat.id}) }}">edit</a>
{{ include('plats/_delete_form.html.twig') }}
{% endblock %}