src/Controller/App/SeasonController.php line 92

  1. <?php
  2. namespace App\Controller\App;
  3. use DateTime;
  4. use App\Entity\Season;
  5. use App\Form\SeasonType;
  6. use App\Repository\SeasonRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. #[Route('/app/season')]
  17. #[IsGranted('IS_AUTHENTICATED_FULLY')]
  18. class SeasonController extends AbstractController
  19. {
  20.     public function __construct(private TranslatorInterface $translator)
  21.     {
  22.     }
  23.     #[Route('/'name'app_season_index'methods: ['GET''POST'])]
  24.     public function index(Request $requestSeasonRepository $seasonRepository): Response
  25.     {
  26.         if ($request->isXmlHttpRequest()) {
  27.             if ($this->isCsrfTokenValid('season'$_POST['season']['_token'])) {
  28.                 $season = new Season();
  29.                 $form $this->createForm(SeasonType::class, $season);
  30.                 $season
  31.                     ->setName($_POST['season']['name'])
  32.                     ->setDescription($_POST['season']['description'])
  33.                     ->setStartDate(new DateTime($_POST['season']['start_date']))
  34.                     ->setEndDate(new DateTime($_POST['season']['end_date']));
  35.                 $seasonRepository->add($seasontrue);
  36.                 return new JsonResponse(['status' => 'ok''data' => $season->getId(), 'msg' => $this->translator->trans('Saison crée !!')]);
  37.             } else {
  38.                 return new JsonResponse(['status' => 'no''data' => null'msg' => $this->translator->trans('token invalid !')]);
  39.             }
  40.         }
  41.         $season = new Season();
  42.         $form $this->createForm(SeasonType::class, $season);
  43.         $edit_form $this->createForm(SeasonType::class, $season, [
  44.             'action' => $this->generateUrl('app_season_edit_ajax', ['id' => '1']),
  45.             'method' => 'POST',
  46.             'attr' => ['name' => 'season_type_edit']
  47.         ]);
  48.         return $this->render('app/season/index.html.twig', [
  49.             'seasons' => $seasonRepository->findAll(),
  50.             'form' => $form->createView(),
  51.             'edit_form' => $edit_form->createView()
  52.         ]);
  53.     }
  54.     #[Route('/new'name'app_season_new'methods: ['GET''POST'])]
  55.     public function new(Request $requestSeasonRepository $seasonRepository): Response
  56.     {
  57.         $season = new Season();
  58.         $form $this->createForm(SeasonType::class, $season);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $seasonRepository->add($seasontrue);
  62.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  63.             return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
  64.         }
  65.         return $this->renderForm('app/season/new.html.twig', [
  66.             'season' => $season,
  67.             'form' => $form,
  68.         ]);
  69.     }
  70.     #[Route('/{id}'name'app_season_show'methods: ['GET'])]
  71.     public function show(Season $season): Response
  72.     {
  73.         return $this->render('app/season/show.html.twig', [
  74.             'season' => $season,
  75.         ]);
  76.     }
  77.     #[Route('/ajax/{id}'name'app_season_show_ajax'methods: ['GET'])]
  78.     public function show_ajax(Season $seasonSerializerInterface $serializer): Response
  79.     {
  80.         $json $serializer->serialize($season'json', ['groups' => ['read:season:basic']]);
  81.         return $this->json(json_decode($json));
  82.     }
  83.     #[Route('/{id}/edit'name'app_season_edit'methods: ['GET''POST'])]
  84.     public function edit(Request $requestSeason $seasonSeasonRepository $seasonRepository): Response
  85.     {
  86.         $form $this->createForm(SeasonType::class, $season);
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $seasonRepository->add($seasontrue);
  90.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  91.             return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
  92.         }
  93.         return $this->renderForm('app/season/edit.html.twig', [
  94.             'season' => $season,
  95.             'form' => $form,
  96.         ]);
  97.     }
  98.     #[Route('/{id?}/edit/ajax'name'app_season_edit_ajax'methods: ['GET''POST'])]
  99.     public function edit_ajax(Request $requestSeason $seasonSeasonRepository $seasonRepositoryEntityManagerInterface $manager): Response
  100.     {
  101.         if ($request->isXmlHttpRequest()) {
  102.             if ($this->isCsrfTokenValid('edit_season'$_POST['season']['_token'])) {
  103.                 $season
  104.                     ->setName($_POST['season']['name'])
  105.                     ->setDescription($_POST['season']['description'])
  106.                     ->setStartDate(new DateTime($_POST['season']['start_date']))
  107.                     ->setEndDate(new DateTime($_POST['season']['end_date']))
  108.                 ;
  109.                 $seasonRepository->add($seasontrue);
  110.                 return new JsonResponse(['status' => 'ok''data' => $season->getId(), 'msg' => $this->translator->trans('Saison mis à jour !!')]);
  111.             } else {
  112.                 return new JsonResponse(['status' => 'no''data' => null'msg' => $this->translator->trans('token invalid !')]);
  113.             }
  114.         }
  115.     }
  116.     #[Route('/{id}/activate'name'app_season_activate'methods: ['GET''POST'])]
  117.     public function activate(Request $requestSeason $seasonSeasonRepository $seasonRepository): Response
  118.     {
  119.         $seasons $seasonRepository->findAll();
  120.         foreach ($seasons as $ses) {
  121.             $ses->setIsActive(false);
  122.             $seasonRepository->add($sestrue);
  123.         }
  124.         $season->setIsActive(true);
  125.         $seasonRepository->add($seasontrue);
  126.         return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
  127.     }
  128.     #[Route('/{id}'name'app_season_delete'methods: ['POST'])]
  129.     public function delete(Request $requestSeason $seasonSeasonRepository $seasonRepository): Response
  130.     {
  131.         if ($this->isCsrfTokenValid('delete' $season->getId(), $request->request->get('_token'))) {
  132.             try {
  133.                 $seasonRepository->remove($seasontrue);
  134.                 $this->addFlash('success'$this->translator->trans('Succès !!'));
  135.             } catch (\Exception $e) {
  136.                 $errorMessage $e->getMessage();
  137.                 $result explode(':'$errorMessage);
  138.                 $this->addFlash('error'$result[0]);
  139.             }
  140.         }
  141.         return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
  142.     }
  143. }