src/Controller/VehicleTipController.php line 21

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Vehicle;
  4. use App\Entity\VehicleTip;
  5. use App\Form\VehicleTipType;
  6. use App\Repository\VehicleTipRepository;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. #[Route('/vehicle/tip')]
  13. class VehicleTipController extends AbstractController
  14. {
  15.     public function tip(VehicleTipRepository $vehicleTipRepository): Response
  16.     {
  17.         $vehicleTips =  $vehicleTipRepository->findRandVisible(4);
  18.         return $this->render('vehicle_tip/tip.html.twig', [
  19.             'vehicleTips'=> $vehicleTips
  20.         ]);
  21.     }
  22.     #[Security('is_granted("ROLE_ADMIN")')]
  23.     #[Route('/'name'app_vehicle_tip_index'methods: ['GET'])]
  24.     public function index(VehicleTipRepository $vehicleTipRepository): Response
  25.     {
  26.         return $this->render('vehicle_tip/index.html.twig', [
  27.             'vehicle_tips' => $vehicleTipRepository->findAll(),
  28.         ]);
  29.     }
  30.     #[Security('is_granted("ROLE_ADMIN")')]
  31.     #[Route('/new/{id}'name'app_vehicle_tip_new'methods: ['GET''POST'])]
  32.     public function new(Request $requestVehicle $vehicleVehicleTipRepository $vehicleTipRepository): Response
  33.     {
  34.         $vehicleTip = new VehicleTip();
  35.         $vehicleTip->setVehicle($vehicle);
  36.         $vehicleTip->setCreateAt(new \DateTimeImmutable());
  37.         $vehicleTip->setEndAt(new \DateTime('+1 month'));
  38.         $form $this->createForm(VehicleTipType::class, $vehicleTip);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted() && $form->isValid()) {
  41.             $vehicleTipRepository->save($vehicleTiptrue);
  42.             return $this->redirectToRoute('app_vehicle_tip_index', [], Response::HTTP_SEE_OTHER);
  43.         }
  44.         return $this->renderForm('vehicle_tip/new.html.twig', [
  45.             'vehicle_tip' => $vehicleTip,
  46.             'form' => $form,
  47.         ]);
  48.     }
  49.     #[Security('is_granted("ROLE_ADMIN")')]
  50.     #[Route('/{id}/edit'name'app_vehicle_tip_edit'methods: ['GET''POST'])]
  51.     public function edit(Request $requestVehicleTip $vehicleTipVehicleTipRepository $vehicleTipRepository): Response
  52.     {
  53.         $form $this->createForm(VehicleTipType::class, $vehicleTip);
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $vehicleTipRepository->save($vehicleTiptrue);
  57.             return $this->redirectToRoute('app_vehicle_tip_index', [], Response::HTTP_SEE_OTHER);
  58.         }
  59.         return $this->renderForm('vehicle_tip/edit.html.twig', [
  60.             'vehicle_tip' => $vehicleTip,
  61.             'form' => $form,
  62.         ]);
  63.     }
  64.     #[Security('is_granted("ROLE_ADMIN")')]
  65.     #[Route('/{id}'name'app_vehicle_tip_delete'methods: ['POST'])]
  66.     public function delete(Request $requestVehicleTip $vehicleTipVehicleTipRepository $vehicleTipRepository): Response
  67.     {
  68.         if ($this->isCsrfTokenValid('delete'.$vehicleTip->getId(), $request->request->get('_token'))) {
  69.             $vehicleTipRepository->remove($vehicleTiptrue);
  70.         }
  71.         return $this->redirectToRoute('app_vehicle_tip_index', [], Response::HTTP_SEE_OTHER);
  72.     }
  73. }