124 lines
4.1 KiB
PHP
124 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Announcement;
|
|
use App\Entity\InternApplication;
|
|
use App\Form\AnnouncementType;
|
|
use App\Repository\AnnouncementRepository;
|
|
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('/announcement', name: 'app_announcement')]
|
|
class AnnouncementController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly AnnouncementRepository $announcementRepository
|
|
)
|
|
{
|
|
}
|
|
#[Route('/add', name: '_add')]
|
|
public function addAnnouncement(Request $request): Response
|
|
{
|
|
$announcement = new Announcement();
|
|
$form = $this->createForm(AnnouncementType::class, $announcement);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
//met la date de création de l'annonce au moment où le formulaire est envoyé
|
|
$announcement->setCreationDate(new \DateTime());
|
|
|
|
$this->entityManager->persist($announcement);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Annonce créée avec succès.');
|
|
return $this->redirectToRoute('app_announcement_list');
|
|
}
|
|
|
|
return $this->render('announcement/add.html.twig', [
|
|
'announcementForm' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/list', name: '_list')]
|
|
public function list(): Response
|
|
{
|
|
$announcements = $this->announcementRepository->findAll();
|
|
|
|
return $this->render('announcement/list.html.twig', [
|
|
'announcements' => $announcements,
|
|
]);
|
|
}
|
|
|
|
#[Route('/update/{id}', name: '_update')]
|
|
public function update(int $id, Request $request): Response
|
|
{
|
|
$announcement = $this->announcementRepository->find($id);
|
|
$form = $this->createForm(AnnouncementType::class, $announcement);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
$this->entityManager->persist($announcement);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Annonce modifiéé avec succès.');
|
|
return $this->redirectToRoute('app_announcement_list');
|
|
}
|
|
|
|
return $this->render('announcement/add.html.twig', [
|
|
'formAdd' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/delete/{id}', name: '_delete')]
|
|
public function delete(int $id): Response
|
|
{
|
|
$announcement = $this->announcementRepository->find($id);
|
|
$this->entityManager->remove($announcement);
|
|
$this->entityManager->flush();
|
|
|
|
return $this->redirectToRoute('app_announcement_list');
|
|
}
|
|
|
|
#[Route('/apply/{id}', name: '_apply')]
|
|
public function applyToAnnouncement(int $id): Response
|
|
{
|
|
|
|
$announcement = $this->announcementRepository->find($id);
|
|
|
|
$user = $this->getUser();
|
|
|
|
$existingCandidature = $this->entityManager->getRepository(InternApplication::class)->findOneBy([
|
|
'intern' => $user,
|
|
'announcement' => $announcement
|
|
]);
|
|
|
|
if($existingCandidature) {
|
|
$this->addFlash('error', 'Vous avez déjà postulé à cette annonce.');
|
|
return $this->redirectToRoute('app_announcement_list');
|
|
}
|
|
$application = new InternApplication();
|
|
$application->setIntern($user);
|
|
$application->setIntern($announcement);
|
|
$application->setApplicationDate(new \DateTime());
|
|
|
|
$this->entityManager->persist($application);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Votre candidature a été envoyée avec succès.');
|
|
return $this->redirectToRoute('annonce_list', ['id' => $announcement->getId()]);
|
|
}
|
|
|
|
#[Route('/filterByCompany', name: '_filterByCompany')]
|
|
public function filterByCompany(int $id): Response
|
|
{
|
|
return $this->redirectToRoute('annonce_list', ['id' => $id]);
|
|
}
|
|
}
|