From 3f4507958c12a3dc011dcd28e40a11dc64c9171b Mon Sep 17 00:00:00 2001 From: leroyv Date: Thu, 21 Nov 2024 14:58:12 +0100 Subject: [PATCH] Controller pour Plats avec le form et templates. --- src/Controller/CuisinierController.php | 46 --------------- src/Controller/PlatController.php | 78 ------------------------- src/Controller/PlatsController.php | 81 ++++++++++++++++++++++++++ src/Form/PlatType.php | 24 -------- src/Form/PlatsType.php | 42 +++++++++++++ templates/plats/_delete_form.html.twig | 4 ++ templates/plats/_form.html.twig | 4 ++ templates/plats/edit.html.twig | 13 +++++ templates/plats/index.html.twig | 45 ++++++++++++++ templates/plats/new.html.twig | 11 ++++ templates/plats/show.html.twig | 46 +++++++++++++++ 11 files changed, 246 insertions(+), 148 deletions(-) delete mode 100644 src/Controller/CuisinierController.php delete mode 100644 src/Controller/PlatController.php create mode 100644 src/Controller/PlatsController.php delete mode 100644 src/Form/PlatType.php create mode 100644 src/Form/PlatsType.php create mode 100644 templates/plats/_delete_form.html.twig create mode 100644 templates/plats/_form.html.twig create mode 100644 templates/plats/edit.html.twig create mode 100644 templates/plats/index.html.twig create mode 100644 templates/plats/new.html.twig create mode 100644 templates/plats/show.html.twig diff --git a/src/Controller/CuisinierController.php b/src/Controller/CuisinierController.php deleted file mode 100644 index c2097ea..0000000 --- a/src/Controller/CuisinierController.php +++ /dev/null @@ -1,46 +0,0 @@ -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'); - } -} \ No newline at end of file diff --git a/src/Controller/PlatController.php b/src/Controller/PlatController.php deleted file mode 100644 index 21bc50a..0000000 --- a/src/Controller/PlatController.php +++ /dev/null @@ -1,78 +0,0 @@ -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'); - } -} diff --git a/src/Controller/PlatsController.php b/src/Controller/PlatsController.php new file mode 100644 index 0000000..e9c034d --- /dev/null +++ b/src/Controller/PlatsController.php @@ -0,0 +1,81 @@ +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); + } +} diff --git a/src/Form/PlatType.php b/src/Form/PlatType.php deleted file mode 100644 index 3e3981c..0000000 --- a/src/Form/PlatType.php +++ /dev/null @@ -1,24 +0,0 @@ -add('field_name') - ; - } - - public function configureOptions(OptionsResolver $resolver): void - { - $resolver->setDefaults([ - // Configure your form options here - ]); - } -} diff --git a/src/Form/PlatsType.php b/src/Form/PlatsType.php new file mode 100644 index 0000000..5946a7d --- /dev/null +++ b/src/Form/PlatsType.php @@ -0,0 +1,42 @@ +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, + ]); + } +} diff --git a/templates/plats/_delete_form.html.twig b/templates/plats/_delete_form.html.twig new file mode 100644 index 0000000..bc89faf --- /dev/null +++ b/templates/plats/_delete_form.html.twig @@ -0,0 +1,4 @@ +
+ + +
diff --git a/templates/plats/_form.html.twig b/templates/plats/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/templates/plats/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/templates/plats/edit.html.twig b/templates/plats/edit.html.twig new file mode 100644 index 0000000..14afd7b --- /dev/null +++ b/templates/plats/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Plats{% endblock %} + +{% block body %} +

Edit Plats

+ + {{ include('plats/_form.html.twig', {'button_label': 'Update'}) }} + + back to list + + {{ include('plats/_delete_form.html.twig') }} +{% endblock %} diff --git a/templates/plats/index.html.twig b/templates/plats/index.html.twig new file mode 100644 index 0000000..cb94e30 --- /dev/null +++ b/templates/plats/index.html.twig @@ -0,0 +1,45 @@ +{% extends 'base.html.twig' %} + +{% block title %}Plats index{% endblock %} + +{% block body %} +

Plats index

+ + + + + + + + + + + + + + + + {% for plat in plats %} + + + + + + + + + + + {% else %} + + + + {% endfor %} + +
IdNomDescriptionPrixCategorieStatutNb_de_commandeactions
{{ plat.id }}{{ plat.Nom }}{{ plat.Description }}{{ plat.Prix }}{{ plat.Categorie }}{{ plat.Statut ? 'Yes' : 'No' }}{{ plat.NbDeCommande }} + show + edit +
no records found
+ + Create new +{% endblock %} diff --git a/templates/plats/new.html.twig b/templates/plats/new.html.twig new file mode 100644 index 0000000..a59e66b --- /dev/null +++ b/templates/plats/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Plats{% endblock %} + +{% block body %} +

Create new Plats

+ + {{ include('plats/_form.html.twig') }} + + back to list +{% endblock %} diff --git a/templates/plats/show.html.twig b/templates/plats/show.html.twig new file mode 100644 index 0000000..41fa96e --- /dev/null +++ b/templates/plats/show.html.twig @@ -0,0 +1,46 @@ +{% extends 'base.html.twig' %} + +{% block title %}Plats{% endblock %} + +{% block body %} +

Plats

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id{{ plat.id }}
Nom{{ plat.Nom }}
Description{{ plat.Description }}
Prix{{ plat.Prix }}
Categorie{{ plat.Categorie }}
Statut{{ plat.Statut ? 'Yes' : 'No' }}
Nb_de_commande{{ plat.NbDeCommande }}
+ + back to list + + edit + + {{ include('plats/_delete_form.html.twig') }} +{% endblock %}