src/Controller/App/SeasonController.php line 92
<?php
namespace App\Controller\App;
use DateTime;
use App\Entity\Season;
use App\Form\SeasonType;
use App\Repository\SeasonRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/app/season')]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class SeasonController extends AbstractController
{
public function __construct(private TranslatorInterface $translator)
{
}
#[Route('/', name: 'app_season_index', methods: ['GET', 'POST'])]
public function index(Request $request, SeasonRepository $seasonRepository): Response
{
if ($request->isXmlHttpRequest()) {
if ($this->isCsrfTokenValid('season', $_POST['season']['_token'])) {
$season = new Season();
$form = $this->createForm(SeasonType::class, $season);
$season
->setName($_POST['season']['name'])
->setDescription($_POST['season']['description'])
->setStartDate(new DateTime($_POST['season']['start_date']))
->setEndDate(new DateTime($_POST['season']['end_date']));
$seasonRepository->add($season, true);
return new JsonResponse(['status' => 'ok', 'data' => $season->getId(), 'msg' => $this->translator->trans('Saison crée !!')]);
} else {
return new JsonResponse(['status' => 'no', 'data' => null, 'msg' => $this->translator->trans('token invalid !')]);
}
}
$season = new Season();
$form = $this->createForm(SeasonType::class, $season);
$edit_form = $this->createForm(SeasonType::class, $season, [
'action' => $this->generateUrl('app_season_edit_ajax', ['id' => '1']),
'method' => 'POST',
'attr' => ['name' => 'season_type_edit']
]);
return $this->render('app/season/index.html.twig', [
'seasons' => $seasonRepository->findAll(),
'form' => $form->createView(),
'edit_form' => $edit_form->createView()
]);
}
#[Route('/new', name: 'app_season_new', methods: ['GET', 'POST'])]
public function new(Request $request, SeasonRepository $seasonRepository): Response
{
$season = new Season();
$form = $this->createForm(SeasonType::class, $season);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$seasonRepository->add($season, true);
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('app/season/new.html.twig', [
'season' => $season,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_season_show', methods: ['GET'])]
public function show(Season $season): Response
{
return $this->render('app/season/show.html.twig', [
'season' => $season,
]);
}
#[Route('/ajax/{id}', name: 'app_season_show_ajax', methods: ['GET'])]
public function show_ajax(Season $season, SerializerInterface $serializer): Response
{
$json = $serializer->serialize($season, 'json', ['groups' => ['read:season:basic']]);
return $this->json(json_decode($json));
}
#[Route('/{id}/edit', name: 'app_season_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Season $season, SeasonRepository $seasonRepository): Response
{
$form = $this->createForm(SeasonType::class, $season);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$seasonRepository->add($season, true);
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('app/season/edit.html.twig', [
'season' => $season,
'form' => $form,
]);
}
#[Route('/{id?}/edit/ajax', name: 'app_season_edit_ajax', methods: ['GET', 'POST'])]
public function edit_ajax(Request $request, Season $season, SeasonRepository $seasonRepository, EntityManagerInterface $manager): Response
{
if ($request->isXmlHttpRequest()) {
if ($this->isCsrfTokenValid('edit_season', $_POST['season']['_token'])) {
$season
->setName($_POST['season']['name'])
->setDescription($_POST['season']['description'])
->setStartDate(new DateTime($_POST['season']['start_date']))
->setEndDate(new DateTime($_POST['season']['end_date']))
;
$seasonRepository->add($season, true);
return new JsonResponse(['status' => 'ok', 'data' => $season->getId(), 'msg' => $this->translator->trans('Saison mis à jour !!')]);
} else {
return new JsonResponse(['status' => 'no', 'data' => null, 'msg' => $this->translator->trans('token invalid !')]);
}
}
}
#[Route('/{id}/activate', name: 'app_season_activate', methods: ['GET', 'POST'])]
public function activate(Request $request, Season $season, SeasonRepository $seasonRepository): Response
{
$seasons = $seasonRepository->findAll();
foreach ($seasons as $ses) {
$ses->setIsActive(false);
$seasonRepository->add($ses, true);
}
$season->setIsActive(true);
$seasonRepository->add($season, true);
return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/{id}', name: 'app_season_delete', methods: ['POST'])]
public function delete(Request $request, Season $season, SeasonRepository $seasonRepository): Response
{
if ($this->isCsrfTokenValid('delete' . $season->getId(), $request->request->get('_token'))) {
try {
$seasonRepository->remove($season, true);
$this->addFlash('success', $this->translator->trans('Succès !!'));
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
$result = explode(':', $errorMessage);
$this->addFlash('error', $result[0]);
}
}
return $this->redirectToRoute('app_season_index', [], Response::HTTP_SEE_OTHER);
}
}