<?php
namespace App\Controller;
use App\Entity\Offre;
use App\Form\PostulerType;
use Symfony\Component\Mime\Email;
use App\Repository\LabelRepository;
use App\Repository\OffreRepository;
use Symfony\Component\Mime\Address;
use App\Repository\OfficeRepository;
use App\Repository\SettingRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class OffreController extends AbstractController
{
/**
* @Route("/offres", name="app_offres")
*/
public function index(
SettingRepository $settingRepository,
LabelRepository $labelRepository,
OfficeRepository $officeRepository,
OffreRepository $offreRepository
): Response
{
return $this->render('offre/index.html.twig', [
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'offices' => $officeRepository->findBy([], ['titre' => 'ASC']),
'offres' => $offreRepository->findBy(['isEnabled' => true], ['createdAt' => 'DESC']),
'pageTitle' => 'offres'
]);
}
/**
* @Route("/offres/{slug}/{id}", name="app_offre_show")
*/
public function show(
Offre $offre,
SettingRepository $settingRepository,
LabelRepository $labelRepository,
OfficeRepository $officeRepository,
OffreRepository $offreRepository
): Response {
return $this->render('offre/show.html.twig', [
'offre' => $offre,
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'offices' => $officeRepository->findBy([], ['titre' => 'ASC']),
'offres' => $offreRepository->findBy(['isEnabled' => true], ['createdAt' => 'DESC']),
'pageTitle' => $offre->getTitle()
]);
}
/**
* @Route("/offres/postuler/{slug}/{id}", name="app_offre_postuler")
*/
public function postuler(
Offre $offre,
SettingRepository $settingRepository,
LabelRepository $labelRepository,
OfficeRepository $officeRepository,
OffreRepository $offreRepository,
Request $request,
MailerInterface $mailer
): Response {
$form = $this->createForm(PostulerType::class);
$form->handleRequest($request);
// On recherche l'email et le nom du site dans Setting
// pour injecter dans le mail
$dataMail = $settingRepository->findAll();
$siteEmail = $dataMail[0]->getSiteEmail();
$siteName = $dataMail[0]->getSiteName();
if ($form->isSubmitted() && $form->isValid()) {
$contactFormData = $form->getData();
$email = (new Email())
->from($contactFormData['email'])
->to(new Address($siteEmail, $siteName))
->subject('Candidature de ' . $contactFormData['prenom'] . ' ' . $contactFormData['nom'] . ' pour l\'offre : ' . $offre)
->html(
'<h4 style="color: #0b3a62; font-weight: bold;">Candidature pour l\'offre : ' . $offre . '</h4>' .
// '<span style="color: #bbd57e; font-weight: bold;">Type de candidature :</span> ' . $offre . '<br>' .
'<span style="color: #2dafe6; font-weight: bold;">De :</span> ' . $contactFormData['prenom'] . ' ' . $contactFormData['nom'] . '<br>' .
'<span style="color: #2dafe6; font-weight: bold;">E-mail :</span> ' . $contactFormData['email'] . '<br><br>' .
'<span style="color: #2dafe6; font-weight: bold;">Message</span> : <br>' . trim(nl2br($contactFormData['message'])) . '<br>',
'text/plain'
);
// Attacher les fichiers joints CV et Autre doc
$email->attachFromPath($contactFormData['cv'], 'Curriculum-Vitae.pdf', 'application/pdf');
if ($contactFormData['autredoc'] != null) {
$email->attachFromPath($contactFormData['autredoc'], 'autre-document.pdf', 'application/pdf');
}
// Envoi du mail
$mailer->send($email);
//Message flash de succès d'envoi du mail
$this->addFlash('success', 'Votre candidature a bien été envoyée.');
return $this->redirect($this->generateUrl('app_offre_postuler', ['slug' => $offre->getSlug(), 'id' => $offre->getId()]) . '#message-success');
}
return $this->render('offre/postuler.html.twig', [
'offre' => $offre,
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'offices' => $officeRepository->findBy([], ['titre' => 'ASC']),
'offres' => $offreRepository->findBy(['isEnabled' => true], ['createdAt' => 'DESC']),
'form' => $form->createView()
]);
}
/**
* @Route("/postuler/candidature-spontanee", name="app_offre_postuler_spontanee")
*/
public function postulerSpontanee(
// Offre $offre,
SettingRepository $settingRepository,
LabelRepository $labelRepository,
OfficeRepository $officeRepository,
// OffreRepository $offreRepository,
Request $request,
MailerInterface $mailer
)
{
$form = $this->createForm(PostulerType::class);
$form->handleRequest($request);
// On recherche l'email et le nom du site dans Setting
// pour injecter dans le mail
$dataMail = $settingRepository->findAll();
$siteEmail = $dataMail[0]->getSiteEmail();
$siteName = $dataMail[0]->getSiteName();
if ($form->isSubmitted() && $form->isValid()) {
$contactFormData = $form->getData();
$email = (new Email())
->from($contactFormData['email'])
->to(new Address($siteEmail, $siteName))
->subject('Candidature spontanée de ' . $contactFormData['prenom'] . ' ' . $contactFormData['nom'])
->html(
'<h4 style="color: #0b3a62; font-weight: bold;">Candidature spontanée</h4>' .
'<span style="color: #2dafe6; font-weight: bold;">De :</span> ' . $contactFormData['prenom'] . ' ' . $contactFormData['nom'] . '<br>' .
'<span style="color: #2dafe6; font-weight: bold;">E-mail :</span> ' . $contactFormData['email'] . '<br><br>' .
'<span style="color: #2dafe6; font-weight: bold;">Message</span> : <br>' . trim(nl2br($contactFormData['message'])) . '<br>',
'text/plain'
);
// Attacher les fichiers joints CV et Autre doc
$email->attachFromPath($contactFormData['cv'], 'Curriculum-Vitae.pdf', 'application/pdf');
if ($contactFormData['autredoc'] != null) {
$email->attachFromPath($contactFormData['autredoc'], 'autre-document.pdf', 'application/pdf');
}
// Envoi du mail
$mailer->send($email);
//Message flash de succès d'envoi du mail
$this->addFlash('success', 'Votre candidature spontanée a bien été envoyée.');
//return $this->redirect($this->generateUrl('app_offre_postuler', ['slug' => $offre->getSlug(), 'id' => $offre->getId()]) . '#message-success');
return $this->redirectToRoute('app_offres');
}
return $this->render('offre/postuler-spontanee.html.twig', [
// 'offre' => $offre,
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'offices' => $officeRepository->findBy([], ['titre' => 'ASC']),
// 'offres' => $offreRepository->findBy(['isEnabled' => true], ['createdAt' => 'DESC']),
'form' => $form->createView()
]);
}
}