src/Controller/ThemesWebsite/Whileresume/Website/CandidatesController.php line 125

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Whileresume\Website;
  3. use App\Entity\Articles\Articles;
  4. use App\Entity\Core\Users;
  5. use App\Entity\Cvs\Candidates;
  6. use App\Entity\Pages\Pages;
  7. use App\Form\Core\UsersEmailForm;
  8. use App\Security\LoginFormAuthenticator;
  9. use App\Services\Core\RequestData;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Cookie;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  25. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  26. use Symfony\Component\Security\Core\User\UserInterface;
  27. use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
  28. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  29. use Symfony\Component\Security\Http\Authentication\AuthenticatorManagerInterface;
  30. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  31. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  32. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordCredentialsBadge;
  33. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  34. use Twig\Environment;
  35. class CandidatesController extends AbstractController
  36. {
  37.     private $rd;
  38.     private $em;
  39.     private $passwordEncoder;
  40.     private $ms;
  41.     private $us;
  42.     private $authenticator;
  43.     private $userAuthenticator;
  44.     private $paginator;
  45.     private $twig;
  46.     public function __construct(RequestData                  $rd,
  47.                                 EntityManagerInterface       $em,
  48.                                 UserPasswordEncoderInterface $passwordEncoder,
  49.                                 \App\Services\Mails          $ms,
  50.                                 \App\Services\Core\Users     $us,
  51.                                 UserAuthenticatorInterface   $userAuthenticator,
  52.                                 LoginFormAuthenticator       $authenticator,
  53.                                 PaginatorInterface           $paginator,
  54.                                 Environment $twig,
  55.     ) {
  56.         $this->rd $rd;
  57.         $this->em $em;
  58.         $this->passwordEncoder $passwordEncoder;
  59.         $this->ms $ms;
  60.         $this->authenticator $authenticator;
  61.         $this->userAuthenticator $userAuthenticator;
  62.         $this->us $us;
  63.         $this->paginator $paginator;
  64.         $this->twig $twig;
  65.     }
  66.     /**
  67.      * Homepage principal
  68.      * @param Request $request
  69.      * @return Response
  70.      */
  71.     public function homepage(Request $request): Response
  72.     {
  73.         $locale $request->getLocale();
  74.         $user $this->getUser();
  75.         /*
  76.         if($user !== null) {
  77.             if ($user->getTypeAccount() === "enterprise") {
  78.                 return $this->redirectToRoute('whileresume_business_'.$locale);
  79.             }
  80.         }*/
  81.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage''locale' => $locale]);
  82.         $articles $this->em->getRepository(Articles::class)->getArticles(6,$locale);
  83.         return $this->render('application/whileresume/website/candidates/homepage_'.$locale.'.html.twig',[
  84.             'page' => $page,
  85.             'articles' => $articles
  86.         ]);
  87.     }
  88.     /**
  89.      * Déposer un CV
  90.      * @param Request $request
  91.      * @return Response
  92.      */
  93.     public function resume(Request $request): Response
  94.     {
  95.         $session $request->getSession();
  96.         $locale $request->getLocale();
  97.         $user $this->getUser();
  98.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'resume''locale' => $locale]);
  99.         $form $this->createForm(UsersEmailForm::class);
  100.         $form->handleRequest($request);
  101.         if ($form->isSubmitted() && $form->isValid()) {
  102.             $data $request->request->all();
  103.             $data $data['users_email_form'];
  104.             if($data['acceptTerm'] == "1") {
  105.                 if($data['password']['first'] != $data['password']['second']) {
  106.                     if($locale == "fr") {
  107.                         $session->getFlashBag()->add('danger''Votre second mot de passe n\'est pas identique');
  108.                         return $this->redirectToRoute('whileresume_resume_fr');
  109.                     }
  110.                     $session->getFlashBag()->add('danger''Your second password is not identical');
  111.                     return $this->redirectToRoute('whileresume_resume_en');
  112.                 }
  113.                 $verificationUser $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
  114.                 if ($verificationUser == null) {
  115.                     $candidate = new Candidates();
  116.                     $candidate->setEmail($data['email']);
  117.                     $candidate->setUpdatedAt(new \DateTime("now"));
  118.                     $candidate->setCreatedAt(new \DateTime("now"));
  119.                     $candidate->setOnline(false);
  120.                     $candidate->setAvailability("offline");
  121.                     $this->em->persist($candidate);
  122.                     $this->em->flush();
  123.                     $us1 $this->generateUniqueSlug($candidate->getId(),10);
  124.                     $us2 $this->generateUniqueSlug($candidate->getId(),10);
  125.                     $candidate->setSlug($us1);
  126.                     $candidate->setSlugAnonyme($us2);
  127.                     $this->em->persist($candidate);
  128.                     $this->em->flush();
  129.                     $newUser = new Users();
  130.                     $newUser->setEmail($data['email']);
  131.                     $newUser->setUsername("");
  132.                     $newUser->setVerification(false);
  133.                     $newUser->setNotificationsMessages(true);
  134.                     $newUser->setNotificationsSuivis(true);
  135.                     $newUser->setPremium(false);
  136.                     $newUser->setFirst(false);
  137.                     $newUser->setEnabled(true);
  138.                     $newUser->setPassword($this->passwordEncoder->encodePassword($newUser,$data['password']['first']));
  139.                     $newUser->setRoles(['ROLE_USER']);
  140.                     $newUser->setTypeAccount("candidate");
  141.                     $newUser->setUpdatedAt(new \DateTime("now"));
  142.                     $newUser->setCreatedAt(new \DateTime("now"));
  143.                     $newUser->setCandidate($candidate);
  144.                     $this->em->persist($newUser);
  145.                     $this->em->flush();
  146.                     $title "Find your next tech talents in just a few minutes!";
  147.                     if($locale == "fr") {
  148.                         $title "🎉 Bienvenue ! Votre profil peut déjà attirer des recruteurs !";
  149.                     }
  150.                     $descriptionHTML $this->twig->render("application/whileresume/gestion/emails/"$locale ."/register_candidate.html.twig",[
  151.                         'title' => $title,
  152.                         'email' => $data['email']
  153.                     ]);
  154.                     $this->ms->webhook($title,$descriptionHTMLnull$data['email'], nullnull);
  155.                     $this->userAuthenticator->authenticateUser($newUser$this->authenticator$request);
  156.                     if($locale == "en") {
  157.                         return $this->redirectToRoute('customer_homepage');
  158.                     }
  159.                     return $this->redirectToRoute('locale_customer_homepage',['_locale' => $locale]);
  160.                 }
  161.                 if($locale == "fr") {
  162.                     return $this->redirectToRoute('whileresume_resume_fr');
  163.                 }
  164.                 return $this->redirectToRoute('whileresume_resume_en');
  165.             }
  166.         }
  167.         return $this->render('application/whileresume/website/candidates/resume_'.$locale.'.html.twig',[
  168.             'form' => $form->createView(),
  169.             'page' => $page
  170.         ]);
  171.     }
  172.     private function generateUniqueSlug(int $userIdint $length 8): string
  173.     {
  174.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  175.         $slug '';
  176.         for ($i 0$i $length$i++) {
  177.             $slug .= $characters[random_int(0strlen($characters) - 1)];
  178.         }
  179.         return $userId '-' $slug// Ex: 42-aB3xK9mP
  180.     }
  181. }