hegresphere/src/Controller/InternController.php

112 lines
3.7 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Degree;
use App\Entity\Intern;
use App\Entity\InternDegree;
use App\Form\InternType;
use App\Repository\InternRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/intern')]
final class InternController extends AbstractController
{
#[Route(name: 'app_intern_index', methods: ['GET'])]
public function index(InternRepository $internRepository): Response
{
return $this->render('intern/index.html.twig', [
'interns' => $internRepository->findAll(),
]);
}
#[Route('/{id}', name: 'app_intern_show', methods: ['GET'])]
public function show(Intern $intern): Response
{
return $this->render('intern/show.html.twig', [
'intern' => $intern,
]);
}
#[Route('/{id}/edit', name: 'app_intern_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Intern $intern, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(InternType::class, $intern);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_intern_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('intern/edit.html.twig', [
'intern' => $intern,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_intern_delete', methods: ['POST'])]
public function delete(Request $request, Intern $intern, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$intern->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($intern);
$entityManager->flush();
}
return $this->redirectToRoute('app_intern_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/degrees/add', name:'app_intern_add_degrees', methods:['POST'])]
public function addDegrees(Request $request, EntityManagerInterface $entityManager): Response
{
$intern = $this->getUser();
if (!$intern instanceof Intern) {
throw $this->createAccessDeniedException("Seuls les stagiaires peuvent sélectionner des diplômes.");
}
$selectedDegreeIds = $request->request->all('selected_degrees');
if ($selectedDegreeIds == []) {
$this->addFlash('error', "Aucune sélection valide n'a été effectuée.");
return $this->redirectToRoute('app_degree_index');
}
$degreeRepository = $entityManager->getRepository(Degree::class);
$internDegreeRepository = $entityManager->getRepository(InternDegree::class);
foreach ($selectedDegreeIds as $degreeId) {
$degree = $degreeRepository->find($degreeId);
if (!$degree) {
continue;
}
$existingInternDegree = $internDegreeRepository->findOneBy([
'intern' => $intern,
'degree' => $degree
]);
if (!$existingInternDegree) {
$internDegree = new InternDegree();
$internDegree->setIntern($intern);
$internDegree->setDegree($degree);
$internDegree->setGraduationDate(new \DateTime());
$entityManager->persist($internDegree);
}
}
$entityManager->flush();
$this->addFlash('success', 'Les diplômes ont été ajoutés avec succès.');
return $this->redirectToRoute('app_profile');
}
}