src/Controller/App/SectionController.php line 25

  1. <?php
  2. namespace App\Controller\App;
  3. use App\Entity\Section;
  4. use App\Form\SectionType;
  5. use App\Repository\SectionRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Serializer\SerializerInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. #[Route('/app/section')]
  15. class SectionController extends AbstractController
  16. {
  17.     public function __construct(private TranslatorInterface $translator)
  18.     {
  19.     }
  20.     #[Route('/'name'app_section_index'methods: ['GET''POST'])]
  21.     public function index(Request $requestSectionRepository $sectionRepository): Response
  22.     {
  23.         if ($request->isXmlHttpRequest()) {
  24.             if ($this->isCsrfTokenValid('section'$_POST['section']['_token'])) {
  25.                 $section = new Section();
  26.                 $form $this->createForm(SectionType::class, $section);
  27.                 $section
  28.                     ->setName($_POST['section']['name'])
  29.                     ->setDescription($_POST['section']['description'])
  30.                     ->setAgeRange($_POST['section']['age_range']);
  31.                 $sectionRepository->add($sectiontrue);
  32.                 return new JsonResponse(['status' => 'ok''data' => $section->getId(), 'msg' => $this->translator->trans('Section crée !!')]);
  33.             } else {
  34.                 return new JsonResponse(['status' => 'no''data' => null'msg' => $this->translator->trans('token invalid !')]);
  35.             }
  36.         }
  37.         $section = new Section();
  38.         $form $this->createForm(SectionType::class, $section);
  39.         $edit_form $this->createForm(SectionType::class, $section, [
  40.             'action' => $this->generateUrl('app_section_edit_ajax', ['id' => '1']),
  41.             'method' => 'POST',
  42.             'attr' => ['name' => 'section_type_edit']
  43.         ]);
  44.         return $this->render('app/section/index.html.twig', [
  45.             'sections' => $sectionRepository->findAll(),
  46.             'form' => $form->createView(),
  47.             'edit_form' => $edit_form->createView()
  48.         ]);
  49.     }
  50.     #[Route('/new'name'app_section_new'methods: ['GET''POST'])]
  51.     public function new(Request $requestSectionRepository $sectionRepository): Response
  52.     {
  53.         $section = new Section();
  54.         $form $this->createForm(SectionType::class, $section);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             $sectionRepository->add($sectiontrue);
  58.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  59.             return $this->redirectToRoute('app_section_index', [], Response::HTTP_SEE_OTHER);
  60.         }
  61.         return $this->renderForm('app/section/new.html.twig', [
  62.             'section' => $section,
  63.             'form' => $form,
  64.         ]);
  65.     }
  66.     #[Route('/{id}'name'app_section_show'methods: ['GET'])]
  67.     public function show(Section $section): Response
  68.     {
  69.         return $this->render('app/section/show.html.twig', [
  70.             'section' => $section,
  71.         ]);
  72.     }
  73.     #[Route('/ajax/{id}'name'app_section_show_ajax'methods: ['GET'])]
  74.     public function show_ajax(Section $sectionSerializerInterface $serializer): Response
  75.     {
  76.         $json $serializer->serialize($section'json', ['groups' => ['read:section:basic']]);
  77.         return $this->json(json_decode($json));
  78.     }
  79.     #[Route('/{id}/edit'name'app_section_edit'methods: ['GET''POST'])]
  80.     public function edit(Request $requestSection $sectionSectionRepository $sectionRepository): Response
  81.     {
  82.         $form $this->createForm(SectionType::class, $section);
  83.         $form->handleRequest($request);
  84.         if ($form->isSubmitted() && $form->isValid()) {
  85.             $sectionRepository->add($sectiontrue);
  86.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  87.             return $this->redirectToRoute('app_section_index', [], Response::HTTP_SEE_OTHER);
  88.         }
  89.         return $this->renderForm('app/section/edit.html.twig', [
  90.             'section' => $section,
  91.             'form' => $form,
  92.         ]);
  93.     }
  94.     #[Route('/{id?}/edit/ajax'name'app_section_edit_ajax'methods: ['GET''POST'])]
  95.     public function edit_ajax(Request $requestSection $sectionSectionRepository $sectionRepositoryEntityManagerInterface $manager): Response
  96.     {
  97.         if ($request->isXmlHttpRequest()) {
  98.             if ($this->isCsrfTokenValid('edit_section'$_POST['section']['_token'])) {
  99.                 $section
  100.                     ->setName($_POST['section']['name'])
  101.                     ->setDescription($_POST['section']['description'])
  102.                     ->setAgeRange($_POST['section']['age_range']);
  103.                 $sectionRepository->add($sectiontrue);
  104.                 return new JsonResponse(['status' => 'ok''data' => $section->getId(), 'msg' => $this->translator->trans('Section mis à jour !!')]);
  105.             } else {
  106.                 return new JsonResponse(['status' => 'no''data' => null'msg' => $this->translator->trans('token invalid !')]);
  107.             }
  108.         }
  109.     }
  110.     #[Route('/{id}'name'app_section_delete'methods: ['POST'])]
  111.     public function delete(Request $requestSection $sectionSectionRepository $sectionRepository): Response
  112.     {
  113.         if ($this->isCsrfTokenValid('delete' $section->getId(), $request->request->get('_token'))) {
  114.             try {
  115.                 $sectionRepository->remove($sectiontrue);
  116.                 $this->addFlash('success'$this->translator->trans('Succès !!'));
  117.             } catch (\Exception $e) {
  118.                 $errorMessage $e->getMessage();
  119.                 $result explode(':'$errorMessage);
  120.                 $this->addFlash('error'$result[0]);
  121.             }
  122.         }
  123.         return $this->redirectToRoute('app_section_index', [], Response::HTTP_SEE_OTHER);
  124.     }
  125. }