src/Controller/InstructorController.php line 55

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Instructor;
  4. use App\Form\InstructorType;
  5. use App\Repository\InstructorRepository;
  6. use App\Services\FileUploader;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  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('/instructor')]
  13. class InstructorController extends AbstractController
  14. {
  15.     #[IsGranted('ROLE_USER_ADMIN', statusCode: '403', message: "Vous n'avez pas accès à cette ressource")]
  16.     #[Route('/', name: 'app_instructor_index', methods: ['GET'])]
  17.     public function index(InstructorRepository $instructorRepository): Response
  18.     {
  19.         return $this->render('instructor/index.html.twig', [
  20.             'instructors' => $instructorRepository->findAll(),
  21.         ]);
  22.     }
  23.     #[IsGranted('ROLE_USER_ADMIN', statusCode: '403', message: "Vous n'avez pas accès à cette ressource")]
  24.     #[Route('/new', name: 'app_instructor_new', methods: ['GET', 'POST'])]
  25.     public function new(Request $request, InstructorRepository $instructorRepository, FileUploader $fileUploader): Response
  26.     {
  27.         $instructor = new Instructor();
  28.         $form = $this->createForm(InstructorType::class, $instructor);
  29.         $form->handleRequest($request);
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             // Gestion de l'upload de la photo
  32.             $photoFile = $form->get('photo')->getData();
  33.             if ($photoFile) {
  34.                 $photoName = $fileUploader->up($photoFile, $this->getParameter('image_directory'));
  35.                 $instructor->setPhoto($photoName);
  36.             }
  37.             $instructorRepository->save($instructor, true);
  38.             return $this->redirectToRoute('app_instructor_index', [], Response::HTTP_SEE_OTHER);
  39.         }
  40.         return $this->renderForm('instructor/new.html.twig', [
  41.             'instructor' => $instructor,
  42.             'form' => $form,
  43.         ]);
  44.     }
  45.     #[Route('/profile/{id}', name: 'app_instructor_profile', methods: ['GET'])]
  46.     public function profile(Instructor $instructor): Response
  47.     {
  48.         return $this->render('instructor/show.html.twig', [
  49.             'instructor' => $instructor,
  50.         ]);
  51.     }
  52.     #[IsGranted('ROLE_USER_ADMIN', statusCode: '403', message: "Vous n'avez pas accès à cette ressource")]
  53.     #[Route('/{id}', name: 'app_instructor_show', methods: ['GET'])]
  54.     public function show(Instructor $instructor): Response
  55.     {
  56.         return $this->render('instructor/show.html.twig', [
  57.             'instructor' => $instructor,
  58.         ]);
  59.     }
  60.     #[IsGranted('ROLE_USER_ADMIN', statusCode: '403', message: "Vous n'avez pas accès à cette ressource")]
  61.     #[Route('/{id}/edit', name: 'app_instructor_edit', methods: ['GET', 'POST'])]
  62.     public function edit(Request $request, Instructor $instructor, InstructorRepository $instructorRepository, FileUploader $fileUploader): Response
  63.     {
  64.         $form = $this->createForm(InstructorType::class, $instructor);
  65.         $form->handleRequest($request);
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             // Gestion de l'upload de la photo
  68.             $photoFile = $form->get('photo')->getData();
  69.             if ($photoFile) {
  70.                 $photoName = $fileUploader->up($photoFile, $this->getParameter('image_directory'));
  71.                 $instructor->setPhoto($photoName);
  72.             }
  73.             $instructorRepository->save($instructor, true);
  74.             return $this->redirectToRoute('app_instructor_index', [], Response::HTTP_SEE_OTHER);
  75.         }
  76.         return $this->renderForm('instructor/edit.html.twig', [
  77.             'instructor' => $instructor,
  78.             'form' => $form,
  79.         ]);
  80.     }
  81.     #[IsGranted('ROLE_USER_ADMIN', statusCode: '403', message: "Vous n'avez pas accès à cette ressource")]
  82.     #[Route('/{id}', name: 'app_instructor_delete', methods: ['POST'])]
  83.     public function delete(Request $request, Instructor $instructor, InstructorRepository $instructorRepository): Response
  84.     {
  85.         if ($this->isCsrfTokenValid('delete'.$instructor->getId(), $request->request->get('_token'))) {
  86.             $instructorRepository->remove($instructor, true);
  87.         }
  88.         return $this->redirectToRoute('app_instructor_index', [], Response::HTTP_SEE_OTHER);
  89.     }
  90. }