src/Controller/OfferController.php line 145

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Application;
  4. use App\Entity\Attachments;
  5. use App\Entity\Commitment;
  6. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  7. use App\Entity\BookCv;
  8. use App\Entity\Offer;
  9. use App\Entity\Training;
  10. use App\Entity\User;
  11. use App\Form\OfferType;
  12. use App\Manager\SendInBlue;
  13. use App\Entity\HistoryOffers;
  14. use App\Form\SendEmailOfferType;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use SendinBlue\Client\Configuration;
  21. use App\Entity\HistoryUser;
  22. class OfferController extends AbstractController
  23. {
  24.     private EntityManagerInterface $entityManager;
  25.     public function __construct(EntityManagerInterface $entityManager)
  26.     {
  27.         $this->entityManager $entityManager;
  28.     }
  29.     #[Route('/offre/voir/{id}'name'offer_show_user')]
  30.     public function show(Request $requestOffer $offer$id): Response
  31.     {
  32.         if (!$this->getUser()) {
  33.             return $this->redirectToRoute('home');
  34.         }
  35.         $user $this->entityManager->getRepository(User::class)->find($this->getUser());
  36.         $offerId $this->entityManager->getRepository(Offer::class)->findOneBy(['id'  => $id]);
  37.         if ($offerId == null) {
  38.             return $this->redirectToRoute('home');
  39.         }
  40.         // On vérifie si le candidat a déjà candidaté à cette offre
  41.         $applicationCheck $this->entityManager->getRepository(Application::class)->findOneBy(
  42.             ['user'  => $user'offer' => $offer]
  43.         );
  44.         if (!$applicationCheck) {
  45.             $form $this->createFormBuilder()
  46.                 ->add('apply'SubmitType::class, [
  47.                     'label' => 'Je candidate'
  48.                 ])
  49.                 ->getForm();
  50.             $form->handleRequest($request);
  51.             if ($form->isSubmitted() && $form->isValid()) {
  52.                 // La candidature se fait ici
  53.                 $application = new Application();
  54.                 $application->setUser($user);
  55.                 $application->setOffer($offer);
  56.                 $cvAttachment $this->entityManager->getRepository(Attachments::class)->findBy(
  57.                     ['User' => $this->getUser(), 'type' => 'cv'],
  58.                     ['id' => 'DESC']
  59.                 );
  60.                 $cv "";
  61.                 foreach ($cvAttachment as $attachment) {
  62.                     if ($attachment->getAttachmentState()->getId() == 3) {
  63.                         $cv $attachment->getPath();
  64.                     } else {
  65.                         $this->addFlash('danger''Vous devez avoir envoyé votre CV pour postuler à une offre.');
  66.                         return $this->redirectToRoute('search_company');
  67.                     }
  68.                 }
  69.                 $fullName $user->getLastname() . " " $user->getFirstname();
  70.                 $application->setCvLink($cv);
  71.                 $application->setDate(new \DateTime());
  72.                 // Envoi du mail à l'entreprise
  73.                 $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  74.                 $url $baseurl '/uploads/cv/' $cv;
  75.                 $history = new HistoryOffers();
  76.                 $history->setMember(null);
  77.                 $history->setCreatedAt(new \DateTimeImmutable());
  78.                 $history->setOffer($offer);
  79.                 $history->setText("Le candidat " $fullName " à postulé a l'offre");
  80.                 $this->entityManager->persist($history);
  81.                 // history User
  82.                 $history_user = new HistoryUser();
  83.                 $history_user->setUser($user);
  84.                 $history_user->setDescription(
  85.                     'Candidature pour l\'offre :' $offer->getJobTitle() . " - " $offer->getCompanyName()
  86.                 );
  87.                 $history_user->setCreatedAt(new \DateTimeImmutable());
  88.                 $this->entityManager->persist($history_user);
  89.                 $this->entityManager->persist($application);
  90.                 $this->entityManager->flush();
  91.                 return $this->redirectToRoute('search_company');
  92.             }
  93.             return $this->render('user/company/show.html.twig', [
  94.                 'form' => $form->createView(),
  95.                 'offer' => $offer,
  96.             ]);
  97.         } else {
  98.             return $this->render('user/company/show.html.twig', [
  99.                 'offer' => $offer
  100.             ]);
  101.         }
  102.     }
  103.     #[Route('/book-cv/{id}'name'book_cv')]
  104.     public function bookCV(Offer $offerEntityManagerInterface $entityManager)
  105.     {
  106.         $bookCvs $entityManager->getRepository(BookCv::class)->getBookCvs($offer);
  107.         return $this->render('user/company/book-cv.html.twig', [
  108.             'bookCvs' => $bookCvs,
  109.             'offer' => $offer,
  110.         ]);
  111.     }
  112.     #[Route('/book-cv/{title}/{id}'name'book_cv_public')]
  113.     public function bookCVBis(Offer $offer$titleEntityManagerInterface $entityManager)
  114.     {
  115.         $bookCvs $entityManager->getRepository(BookCv::class)->getBookCvs($offer);
  116.         return $this->render('user/company/book-cv.html.twig', [
  117.             'bookCvs' => $bookCvs,
  118.             'offer' => $offer,
  119.         ]);
  120.     }
  121.     #[Route('/offre/ajouter'name'offer_add')]
  122.     public function add(Request $requestSendInBlue $sendInBlueEntityManagerInterface $em)
  123.     {
  124.         $offer = new Offer();
  125.         $trainings $em->getRepository(Training::class)->findBy(['enabled' => 1], ['levelName' => 'DESC']);
  126.         $form $this->createForm(OfferType::class, $offer);
  127.         $form->handleRequest($request);
  128.         if ($form->isSubmitted() && $form->isValid()) {
  129.             $trainingRequest $request->request->all("trainings");
  130.             if ($request->request->get('recaptchaResponse') && $request->request->get('recaptchaResponse') != null) {
  131.                 $recaptcha_url 'https://www.google.com/recaptcha/api/siteverify'// URL to the reCAPTCHA server
  132.                 $recaptcha_secret '6Lf982YpAAAAADyzWvftfXuFWHEJHPUHbqmSs54D'// Secret key
  133.                 $recaptcha_response $request->request->get('recaptchaResponse'); // Response from reCAPTCHA server, added to the form during processing
  134.                 $recaptcha file_get_contents($recaptcha_url '?secret=' $recaptcha_secret '&response=' $recaptcha_response); // Send request to the server
  135.                 $recaptcha json_decode($recaptcha); // Decode the JSON response
  136.                 if ($recaptcha->success == false || $recaptcha->action != "submit") { // If the response is valid
  137.                     $this->addFlash('error''Votre demande n\'a pas pu être envoyée, veuillez réessayer.');
  138.                     return $this->redirectToRoute("offer_add");
  139.                 } elseif ($recaptcha->score <= 0.5) {
  140.                     $this->addFlash('error''Votre demande n\'a pas pu être envoyée, veuillez réessayer.');
  141.                     return $this->redirectToRoute("offer_add");
  142.                 }
  143.             } else {
  144.                 $this->addFlash('error''Votre demande n\'a pas pu être envoyée, veuillez réessayer.');
  145.                 return $this->redirectToRoute("offer_add");
  146.             }
  147.             foreach ($trainingRequest as $tr) {
  148.                 $training $em->getRepository(Training::class)->findOneBy(['id' => $tr]);
  149.                 $offer->addTraining($training);
  150.             }
  151.             $offer->setDate(new \DateTime());
  152.             $jobDescription $form->get('job_description')->getData();
  153.             $newDescr trim(preg_replace('/\r\n+/''<br>'$jobDescription));
  154.             $offer->setCompany(true);
  155.             $offer->setState(false);
  156.             $offer->setJobDescription($newDescr);
  157.             $em->persist($offer);
  158.             $em->flush();
  159.             $history = new HistoryOffers();
  160.             $history->setMember(null);
  161.             $history->setCreatedAt(new \DateTimeImmutable());
  162.             $history->setOffer($offer);
  163.             $history->setText("Création de l'offre -- via formulaire pour entreprises");
  164.             $em->persist($history);
  165.             $em->flush();
  166.             $email "maxime.diho@INTERFOR-SIA.COM";
  167.             if ($_ENV['APP_ENV'] == 'dev') {
  168.                 $email "alice@awelty.com";
  169.             }
  170.             $payload = [
  171.                 'to' => [
  172.                     ['email' => $email]
  173.                 ],
  174.                 'templateId' => $sendInBlue->getTemplateId('offer_commercial'),
  175.                 'params' => [
  176.                     'url_button' =>
  177.                         $this->getParameter('url')  .
  178.                         $this->generateUrl(
  179.                             'admin_offer_show',
  180.                             ['id' => $offer->getId()]
  181.                         ),
  182.                 ]
  183.             ];
  184.             $sendInBlue->sendMail($payload);
  185.             $payload = [
  186.                 'to' => [
  187.                     ['email' => $email]
  188.                 ],
  189.                 'templateId' => $sendInBlue->getTemplateId('offer_send_to_company'),
  190.             ];
  191.             $sendInBlue->sendMail($payload);
  192.             $this->addFlash(
  193.                 'success',
  194.                 'Merci d\'avoir publié une offre,
  195.                 celle-ci sera validée par Interfor avant d\'être visible par les candidats.'
  196.             );
  197.             return $this->redirectToRoute('company');
  198.         }
  199.         return $this->render('company/offer_add.html.twig', [
  200.             'form' => $form->createView(),
  201.             'trainings' => $trainings
  202.         ]);
  203.     }
  204.     #[Route('/company/renvoi-d-offres'name'company_send_mail')]
  205.     public function sendOffers(Request $requestSendInBlue $sendInBlueEntityManagerInterface $em)
  206.     {
  207.         $form $this->createForm(SendEmailOfferType::class);
  208.         $form->handleRequest($request);
  209.         if ($form->isSubmitted() && $form->isValid()) {
  210.             $data $form->getData();
  211.             /*url pour modifier offre d'emploi */
  212.             $company =  $em->getRepository(Offer::class)->findBy(['company_email' => $data['company_email']]);
  213.             if ($company != null) {
  214.                 foreach ($company as $companie) {
  215.                     $url $this->getParameter('url') . $this->generateUrl('book_cv', ['id' => $companie->getId()]);
  216.                     $email $companie->getCompanyEmail();
  217.                     if ($_ENV['APP_ENV'] == 'dev') {
  218.                         $email "alice@awelty.com";
  219.                     }
  220.                     $payload = [
  221.                         'to' => [
  222.                             ['email' => "alice@awelty.com"]
  223.                         ],
  224.                         'templateId' => $sendInBlue->getTemplateId('resend_offer_to_company'),
  225.                         'params' => [
  226.                             'company_name' => $companie->getCompanyName(),
  227.                             'offer_title' => $companie->getJobTitle(),
  228.                             'url_button' => $url,
  229.                             'company_address' => $this->getParameter('COMPANY_ADDRESS'),
  230.                             'company_phone' => $this->getParameter('COMPANY_PHONE'),
  231.                         ],
  232.                         'subject' => 'Vous avez demandé l\'accès à votre annonce ' $companie->getJobTitle()
  233.                     ];
  234.                     $sendInBlue->sendMail($payload);
  235.                 }
  236.                 $this->addFlash('success''Veuillez consulter votre boîte mail, pour accéder aux offres');
  237.             }
  238.             if (empty($company) || $company == null) {
  239.                 $this->addFlash('warning''Vous n\'avez publié aucune offre d\'emploi.');
  240.             }
  241.         }
  242.         $history = new HistoryOffers();
  243.         $history->setMember(null);
  244.         $history->setCreatedAt(new \DateTimeImmutable());
  245.         $history->setText("L'entreprise a demandé à accéder à l'offre");
  246.         $em->persist($history);
  247.         return $this->render('company/send.html.twig', [
  248.             'form' => $form->createView(),
  249.         ]);
  250.     }
  251.     #[Route('/offre/modifier/{id}'name'offer_edit_offer'methods: ['POST''GET'"PUT"])]
  252.     public function offerCompanyEdit(Request $request$idEntityManagerInterface $emSendInBlue $sendInBlue)
  253.     {
  254.         $offer $em->getRepository(Offer::class)->find($id);
  255.         $trainings $em->getRepository(Training::class)->findBy([], ['levelName' => 'DESC']);
  256.         $form $this->createForm(OfferType::class, $offer);
  257.         $form->handleRequest($request);
  258.         if ($form->isSubmitted() && $form->isValid()) {
  259.             $trainingRequest $request->request->all("trainings");
  260.             foreach ($trainingRequest as $tr) {
  261.                 $training $em->getRepository(Training::class)->findOneBy(['id' => $tr]);
  262.                 $offer->addTraining($training);
  263.             }
  264.             $offer->setDate(new \DateTime());
  265.             $jobDescription $form->get('job_description')->getData();
  266.             $newDescr trim(preg_replace('/\r\n+/''<br>'$jobDescription));
  267.             $offer->setJobDescription($newDescr);
  268.             $history = new HistoryOffers();
  269.             $history->setMember(null);
  270.             $history->setCreatedAt(new \DateTimeImmutable());
  271.             $history->setOffer($offer);
  272.             $history->setText("Modification de l'offre -- via formulaire pour entreprises");
  273.             $em->persist($history);
  274.             $em->persist($offer);
  275.             $em->flush();
  276.             $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  277.             $payload = [
  278.                 'to' => [
  279. //                    ['email' => "alternance@interfor-sia.com"]
  280.                     ['email' => "alice@awelty.com"]
  281.                 ],
  282.                 'templateId' => $sendInBlue->getTemplateId('offer_commercial_update'),
  283.                 'params' => [
  284.                     'url_button' =>   $baseurl $this->generateUrl('admin_offer_show', ['id' => $offer->getId()]),
  285.                     'company_address' => $this->getParameter('COMPANY_ADDRESS'),
  286.                     'company_phone' => $this->getParameter('COMPANY_PHONE'),
  287.                 ]
  288.             ];
  289.             $sendInBlue->sendMail($payload);
  290.             $this->addFlash('success''Modifications ont été effectuées avec succès !');
  291.         }
  292.         return $this->render('company/offer_edit.html.twig', [
  293.             'form' => $form->createView(),
  294.             'trainings' => $trainings,
  295.             'offer' => $offer,
  296.         ]);
  297.     }
  298.     #[Route('/company/engagement'name'company_send_mail_commitment')]
  299.     public function sendOffersCommitment(Request $requestEntityManagerInterface $em)
  300.     {
  301.         $form $this->createForm(SendEmailOfferType::class);
  302.         $form->handleRequest($request);
  303.         if ($form->isSubmitted() && $form->isValid()) {
  304.             $data $form->getData();
  305.             $phone $request->request->get('phone');
  306.             /*url pour modifier offre d'emploi */
  307.             $company $em->getRepository(Commitment::class)->findOneBy(['company_email' => $data['company_email']]);
  308.             $user $em->getRepository(User::class)->findOneBy(['phone' => $phone]);
  309.             $history = new HistoryOffers();
  310.             $history->setMember(null);
  311.             $history->setCreatedAt(new \DateTimeImmutable());
  312.             $history->setText("Entreprise -- Demande d'accès a l'engagement");
  313.             $em->persist($history);
  314.             $em->flush();
  315.             $hasCommitment false;
  316.             if ($company) {
  317.                 if ($user) {
  318.                     return $this->redirectToRoute('company_commitment', [ 'id' => $company->getId()]);
  319.                 } else {
  320.                     $this->addFlash(
  321.                         'warning',
  322.                         'Nous n\'avons pas pu trouver d\'étudiant avec ce numéro de téléphone.' .
  323.                         ' Rapprochez-vous du service recrutement alternance@interfor-sia.com et au 03 22 82 00 70.'
  324.                     );
  325.                 }
  326.             } else {
  327.                 $this->addFlash('warning''Nous n\'avons pas pu trouver d\'offre comprenant ce mail.');
  328.             }
  329.         }
  330.         return $this->render('company/sendEmailComitment.html.twig', [
  331.             'form' => $form->createView(),
  332.         ]);
  333.     }
  334. }