src/Controller/OffreJobController.php line 20

  1. <?php
  2. namespace App\Controller;
  3. use App\Form\JobFormType;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Address;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Flasher\Toastr\Prime\ToastrFactory;
  12. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. class OffreJobController extends AbstractController
  15. {
  16.     #[Route('/{_locale<%app.supported_locales%>}/jobs/offreJob/'name'app_offreJob'options: ['sitemap' => true], methods: ['GET''POST'])]
  17.     public function index(Request $requestMailerInterface $mailerToastrFactory $toastr): Response
  18.     {
  19.         $type $request->query->get('type''jobUn'); //sujet du mail
  20.         $application = [];
  21.         $form $this->createForm(JobFormType::class, $application);
  22.         //$form = $this->createForm(JobFormType::class);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             $application $form->getData();
  26.             // Définition du sujet du mail selon le poste visé
  27.             $subject = match ($type) {
  28.                 'jobUn' => 'bbcs.be - Candidature – Titre 1',
  29.                 'jobDeux' => 'bbcs.be - Candidature – Titre 2',
  30.                 default => 'bbcs.be - Candidature'
  31.             };
  32.             $jobTitle = match ($type) {
  33.                 'jobUn' => 'Titre 1',
  34.                 'jobDeux' => 'Titre 2',
  35.             };
  36.             $email = (new TemplatedEmail())
  37.                 ->from('no-reply@bbcs-ovh.com')
  38.                 ->to(new Address('info@bbcs.be'))
  39.                 ->subject($subject)
  40.                 ->htmlTemplate('mail/job_application_form.html.twig')
  41.                 ->context([
  42.                     'application' => $application
  43.                 ])
  44.             ;
  45.             // Ajouter la pièce jointe si elle existe
  46.             if (isset($application['cv']) && $application['cv'] instanceof UploadedFile) {
  47.                 $cvFile $application['cv'];
  48.                 $email->attachFromPath(
  49.                     $cvFile->getPathname(),
  50.                     $cvFile->getClientOriginalName(),
  51.                     $cvFile->getMimeType()
  52.                 );
  53.             }
  54.             try {
  55.                 $mailer->send($email);
  56.                 
  57.                 $toastr
  58.                     ->success('<strong>Nous avons bien reçu votre candidature! Nous y répondrons dans les meilleurs délais.</strong>')
  59.                     ->timeOut(5000)
  60.                     ->progressBar()
  61.                     ->closeButton()
  62.                     ->positionClass('toast-top-center')
  63.                     ->flash()
  64.                 ;
  65.                 return $this->redirectToRoute('app_offreJob');
  66.             } catch (TransportExceptionInterface $e) {
  67.                 echo $e->getMessage();
  68.             }
  69.             return $this->redirectToRoute('app_offreJob', [], Response::HTTP_SEE_OTHER);
  70.         }
  71.         return $this->render('jobs/offreJob/index.html.twig', [
  72.             'form' => $form->createView(),
  73.             'active_menu' => 'offreJob',
  74.             'type' => $type
  75.         ]);
  76.     }
  77. }