src/Controller/App/SectionController.php line 25
<?php
namespace App\Controller\App;
use App\Entity\Section;
use App\Form\SectionType;
use App\Repository\SectionRepository;
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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/app/section')]
class SectionController extends AbstractController
{
public function __construct(private TranslatorInterface $translator)
{
}
#[Route('/', name: 'app_section_index', methods: ['GET', 'POST'])]
public function index(Request $request, SectionRepository $sectionRepository): Response
{
if ($request->isXmlHttpRequest()) {
if ($this->isCsrfTokenValid('section', $_POST['section']['_token'])) {
$section = new Section();
$form = $this->createForm(SectionType::class, $section);
$section
->setName($_POST['section']['name'])
->setDescription($_POST['section']['description'])
->setAgeRange($_POST['section']['age_range']);
$sectionRepository->add($section, true);
return new JsonResponse(['status' => 'ok', 'data' => $section->getId(), 'msg' => $this->translator->trans('Section crée !!')]);
} else {
return new JsonResponse(['status' => 'no', 'data' => null, 'msg' => $this->translator->trans('token invalid !')]);
}
}
$section = new Section();
$form = $this->createForm(SectionType::class, $section);
$edit_form = $this->createForm(SectionType::class, $section, [
'action' => $this->generateUrl('app_section_edit_ajax', ['id' => '1']),
'method' => 'POST',
'attr' => ['name' => 'section_type_edit']
]);
return $this->render('app/section/index.html.twig', [
'sections' => $sectionRepository->findAll(),
'form' => $form->createView(),
'edit_form' => $edit_form->createView()
]);
}
#[Route('/new', name: 'app_section_new', methods: ['GET', 'POST'])]
public function new(Request $request, SectionRepository $sectionRepository): Response
{
$section = new Section();
$form = $this->createForm(SectionType::class, $section);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$sectionRepository->add($section, true);
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_section_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('app/section/new.html.twig', [
'section' => $section,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_section_show', methods: ['GET'])]
public function show(Section $section): Response
{
return $this->render('app/section/show.html.twig', [
'section' => $section,
]);
}
#[Route('/ajax/{id}', name: 'app_section_show_ajax', methods: ['GET'])]
public function show_ajax(Section $section, SerializerInterface $serializer): Response
{
$json = $serializer->serialize($section, 'json', ['groups' => ['read:section:basic']]);
return $this->json(json_decode($json));
}
#[Route('/{id}/edit', name: 'app_section_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Section $section, SectionRepository $sectionRepository): Response
{
$form = $this->createForm(SectionType::class, $section);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$sectionRepository->add($section, true);
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_section_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('app/section/edit.html.twig', [
'section' => $section,
'form' => $form,
]);
}
#[Route('/{id?}/edit/ajax', name: 'app_section_edit_ajax', methods: ['GET', 'POST'])]
public function edit_ajax(Request $request, Section $section, SectionRepository $sectionRepository, EntityManagerInterface $manager): Response
{
if ($request->isXmlHttpRequest()) {
if ($this->isCsrfTokenValid('edit_section', $_POST['section']['_token'])) {
$section
->setName($_POST['section']['name'])
->setDescription($_POST['section']['description'])
->setAgeRange($_POST['section']['age_range']);
$sectionRepository->add($section, true);
return new JsonResponse(['status' => 'ok', 'data' => $section->getId(), 'msg' => $this->translator->trans('Section mis à jour !!')]);
} else {
return new JsonResponse(['status' => 'no', 'data' => null, 'msg' => $this->translator->trans('token invalid !')]);
}
}
}
#[Route('/{id}', name: 'app_section_delete', methods: ['POST'])]
public function delete(Request $request, Section $section, SectionRepository $sectionRepository): Response
{
if ($this->isCsrfTokenValid('delete' . $section->getId(), $request->request->get('_token'))) {
try {
$sectionRepository->remove($section, 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_section_index', [], Response::HTTP_SEE_OTHER);
}
}