From ebc0a27382cb5743319d1736566f02ecedfad695 Mon Sep 17 00:00:00 2001 From: ASTIER Yann Date: Fri, 6 Dec 2024 07:57:39 +0100 Subject: [PATCH] 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 @@