From 5cde70a7b39d3a05a4165a3aa5fc616fd80f4e00 Mon Sep 17 00:00:00 2001 From: besbota Date: Thu, 5 Dec 2024 16:44:09 +0100 Subject: [PATCH 1/5] Form login, WIP --- src/Controller/AffectationController.php | 38 ++++++++++++ src/Controller/CategoryController.php | 74 +++++++++++++++++++++++ src/Form/CategoryType.php | 28 +++++++++ templates/affectation/index.html.twig | 21 +++++++ templates/category/_delete_form.html.twig | 4 ++ templates/category/_form.html.twig | 4 ++ templates/category/edit.html.twig | 13 ++++ templates/category/index.html.twig | 34 +++++++++++ templates/category/new.html.twig | 11 ++++ 9 files changed, 227 insertions(+) create mode 100644 src/Controller/CategoryController.php create mode 100644 src/Form/CategoryType.php create mode 100644 templates/affectation/index.html.twig create mode 100644 templates/category/_delete_form.html.twig create mode 100644 templates/category/_form.html.twig create mode 100644 templates/category/edit.html.twig create mode 100644 templates/category/index.html.twig create mode 100644 templates/category/new.html.twig diff --git a/src/Controller/AffectationController.php b/src/Controller/AffectationController.php index db70093..c6c0047 100644 --- a/src/Controller/AffectationController.php +++ b/src/Controller/AffectationController.php @@ -17,4 +17,42 @@ class AffectationController extends AbstractController 'controller_name' => 'AffectationController', ]); } + + public function add(Request $request) + { + $validated = $request->validate([ + 'name' => 'required|string|max:255', + 'description' => 'nullable|string', + ]); + + $affectation = Affectation::create($validated); + + return response()->json([ + 'message' => 'Affectation réussie', + 'data' => $affectation + ], 201); + } + + public function list() + { + $affectations = Affectation::all(); + + return response()->json([ + 'data' => $affectations + ], 200); + } + + public function delete($id) + { + $affectation = Affectation::find($id); + + if (!$affectation) { + return response()->json(['message' => ''], 404); + } + + $affectation->delete(); + + return response()->json(['message' => ''], 200); + } + } diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 0000000..af71d09 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,74 @@ +render('category/index.html.twig', [ + 'categories' => $categoryRepository->findAll(), + ]); + } + + #[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $category = new Category(); + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($category); + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/new.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + + #[Route('/{id}/edit', name: 'app_category_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/edit.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_category_delete', methods: ['POST'])] + public function delete(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($category); + $entityManager->flush(); + } + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/CategoryType.php b/src/Form/CategoryType.php new file mode 100644 index 0000000..4d68103 --- /dev/null +++ b/src/Form/CategoryType.php @@ -0,0 +1,28 @@ +add('label', TextType::class, [ + 'label' => 'Category label', + ]); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Category::class, + ]); + } +} diff --git a/templates/affectation/index.html.twig b/templates/affectation/index.html.twig new file mode 100644 index 0000000..2db6701 --- /dev/null +++ b/templates/affectation/index.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block title %}Affectation{% endblock %} + + +{% block body %} + +
+

Affectation des utilisateurs.

+ + {% for employee in employees %} + + {{ employee.id }} + + + {% else %} + + no records found + +
+{% endblock %} diff --git a/templates/category/_delete_form.html.twig b/templates/category/_delete_form.html.twig new file mode 100644 index 0000000..535a698 --- /dev/null +++ b/templates/category/_delete_form.html.twig @@ -0,0 +1,4 @@ +
+ + +
diff --git a/templates/category/_form.html.twig b/templates/category/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/templates/category/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/templates/category/edit.html.twig b/templates/category/edit.html.twig new file mode 100644 index 0000000..2be3489 --- /dev/null +++ b/templates/category/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Category{% endblock %} + +{% block body %} +

Edit Category

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

Liste des catégories disponibles :

+ + + + + + + + + + + {% for category in categories %} + + + + + + {% else %} + + + + {% endfor %} + +
IdLabelactions
{{ category.id }}{{ category.label }} + edit +
no records found
+ + Create new +{% endblock %} diff --git a/templates/category/new.html.twig b/templates/category/new.html.twig new file mode 100644 index 0000000..bab5f3c --- /dev/null +++ b/templates/category/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Category{% endblock %} + +{% block body %} +

Create new Category

+ + {{ include('category/_form.html.twig') }} + + back to list +{% endblock %} From ebc0a27382cb5743319d1736566f02ecedfad695 Mon Sep 17 00:00:00 2001 From: ASTIER Yann Date: Fri, 6 Dec 2024 07:57:39 +0100 Subject: [PATCH 2/5] Ongoing - RepresentationController.php Ongoing - AffectationController.php --- config/packages/security.yaml | 3 + src/Controller/RepresentationController.php | 86 ++++++++++++++- src/Controller/RideController.php | 100 ++++++++++++++++++ src/Form/RepresentationType.php | 39 +++++++ src/Form/RideType.php | 37 +++++++ templates/base.html.twig | 12 ++- .../representation/_delete_form.html.twig | 4 + templates/representation/_form.html.twig | 4 + templates/representation/edit.html.twig | 13 +++ templates/representation/index.html.twig | 39 +++++++ templates/representation/new.html.twig | 11 ++ templates/representation/show.html.twig | 34 ++++++ templates/ride/_delete_form.html.twig | 4 + templates/ride/_form.html.twig | 4 + templates/ride/edit.html.twig | 13 +++ templates/ride/index.html.twig | 37 +++++++ templates/ride/new.html.twig | 11 ++ templates/ride/show.html.twig | 36 +++++++ 18 files changed, 479 insertions(+), 8 deletions(-) create mode 100644 src/Controller/RideController.php create mode 100644 src/Form/RepresentationType.php create mode 100644 src/Form/RideType.php create mode 100644 templates/representation/_delete_form.html.twig create mode 100644 templates/representation/_form.html.twig create mode 100644 templates/representation/edit.html.twig create mode 100644 templates/representation/index.html.twig create mode 100644 templates/representation/new.html.twig create mode 100644 templates/representation/show.html.twig create mode 100644 templates/ride/_delete_form.html.twig create mode 100644 templates/ride/_form.html.twig create mode 100644 templates/ride/edit.html.twig create mode 100644 templates/ride/index.html.twig create mode 100644 templates/ride/new.html.twig create mode 100644 templates/ride/show.html.twig diff --git a/config/packages/security.yaml b/config/packages/security.yaml index a764dfa..5ad549c 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -37,6 +37,9 @@ security: # https://symfony.com/doc/current/security/impersonating_user.html # switch_user: true + role_hierarchy: + ROLE_ADMIN: ['ROLE_USER'] + # Easy way to control access for large sections of your site # Note: Only the *first* access control that matches will be used access_control: diff --git a/src/Controller/RepresentationController.php b/src/Controller/RepresentationController.php index af2aac4..7872127 100644 --- a/src/Controller/RepresentationController.php +++ b/src/Controller/RepresentationController.php @@ -2,17 +2,95 @@ namespace App\Controller; +use App\Entity\Employee; +use App\Entity\Representation; +use App\Entity\Ride; +use App\Form\RepresentationType; +use App\Repository\RepresentationRepository; +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; -class RepresentationController extends AbstractController +#[Route('/representation', name: 'representation')] +final class RepresentationController extends AbstractController { - #[Route('/representation', name: 'app_representation')] - public function index(): Response + public function __toString(): string + { + // TODO: Implement __toString() method. + return ""; + } + + #[Route(name: '_index', methods: ['GET'])] + public function index(RepresentationRepository $representationRepository): Response { return $this->render('representation/index.html.twig', [ - 'controller_name' => 'RepresentationController', + 'representations' => $representationRepository->findAll(), ]); } + + #[Route('/new', name: '_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $representation = new Representation(); + $form = $this->createForm(RepresentationType::class, $representation); + $form->handleRequest($request); + +// $employee = $entityManager->getRepository(Employee::class)->findOneBy(['email' => $this->getUser()->getEmail()]); +// $ride = $entityManager->getRepository(Ride::class)->findOneBy(['id' => $form->get('ride')->getData()]); + + + if ($form->isSubmitted() && $form->isValid()) { +// $representation->setEmployee($employee); +// $representation->setRide($ride); + dd($representation); + $entityManager->persist($representation); +// $entityManager->flush(); +// +// return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('representation/new.html.twig', [ + 'representation' => $representation, + 'form' => $form, + ]); + } + + #[Route('/{employee}', name: '_show', methods: ['GET'])] + public function show(Representation $representation): Response + { + return $this->render('representation/show.html.twig', [ + 'representation' => $representation, + ]); + } + + #[Route('/{employee}/edit', name: '_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Representation $representation, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(RepresentationType::class, $representation); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('representation/edit.html.twig', [ + 'representation' => $representation, + 'form' => $form, + ]); + } + + #[Route('/{employee}', name: '_delete', methods: ['POST'])] + public function delete(Request $request, Representation $representation, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$representation->getEmployee(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($representation); + $entityManager->flush(); + } + + return $this->redirectToRoute('representation_index', [], Response::HTTP_SEE_OTHER); + } } diff --git a/src/Controller/RideController.php b/src/Controller/RideController.php new file mode 100644 index 0000000..a954297 --- /dev/null +++ b/src/Controller/RideController.php @@ -0,0 +1,100 @@ +render('ride/index.html.twig', [ + 'rides' => $rideRepository->findAll(), + ]); + } + + #[Route('/new', name: '_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $ride = new Ride(); + $form = $this->createForm(RideType::class, $ride); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($ride); + $entityManager->flush(); + + return $this->redirectToRoute('ride_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('ride/new.html.twig', [ + 'ride' => $ride, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_show', methods: ['GET'])] + public function show(Ride $ride): Response + { + return $this->render('ride/show.html.twig', [ + 'ride' => $ride, + ]); + } + + #[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Ride $ride, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(RideType::class, $ride); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('ride_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('ride/edit.html.twig', [ + 'ride' => $ride, + 'form' => $form, + ]); + } + + #[Route('/{id}/increment', name: '_increment', methods: ['GET', 'POST'])] + public function incrementCount(Request $request, Ride $ride, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(RideType::class); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $ride->setCount($ride->getCount() + 15); + $entityManager->persist($ride); + $entityManager->flush(); + } + + + return $this->redirectToRoute('ride_show', ['id' => $ride->getId()], Response::HTTP_SEE_OTHER); + + } + + #[Route('/{id}', name: '_delete', methods: ['POST'])] + public function delete(Request $request, Ride $ride, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$ride->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($ride); + $entityManager->flush(); + } + + return $this->redirectToRoute('ride_index', [], Response::HTTP_SEE_OTHER); + } + + +} diff --git a/src/Form/RepresentationType.php b/src/Form/RepresentationType.php new file mode 100644 index 0000000..5edc168 --- /dev/null +++ b/src/Form/RepresentationType.php @@ -0,0 +1,39 @@ +add('employee', EntityType::class, [ + 'class' => Employee::class, + 'choice_label' => 'email', + ]) + ->add('ride', EntityType::class, [ + 'class' => Ride::class, + 'choice_label' => 'label', + ]) + ->add('count') + ->add('date', null, [ + 'widget' => 'single_text', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Representation::class, + ]); + } +} diff --git a/src/Form/RideType.php b/src/Form/RideType.php new file mode 100644 index 0000000..3bc014b --- /dev/null +++ b/src/Form/RideType.php @@ -0,0 +1,37 @@ +add('label') + ->add('count') + ->add('incidentTypes', EntityType::class, [ + 'class' => IncidentType::class, + 'choice_label' => 'id', + ]) + ->add('missions', EntityType::class, [ + 'class' => Mission::class, + 'choice_label' => 'id', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Ride::class, + ]); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index ebef434..7b15bc0 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -24,7 +24,7 @@