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

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.         /*
  99.         if($user !== null) {
  100.             if($user->getTypeAccount() === "candidate") {
  101.                 if($locale != "en") {
  102.                     return $this->redirect($this->generateUrl('locale_cvs_gestion_candidates_dashboard',['_locale' => $locale]));
  103.                 }
  104.                 return $this->redirect($this->generateUrl('cvs_gestion_candidates_dashboard'));
  105.             }
  106.             if ($user->getTypeAccount() === "enterprise") {
  107.                 return $this->redirectToRoute('whileresume_business_'.$locale);
  108.             }
  109.         }
  110.         */
  111.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'resume''locale' => $locale]);
  112.         $form $this->createForm(UsersEmailForm::class);
  113.         $form->handleRequest($request);
  114.         if ($form->isSubmitted() && $form->isValid()) {
  115.             $data $request->request->all();
  116.             $data $data['users_email_form'];
  117.             if($data['acceptTerm'] == "1") {
  118.                 if($data['password']['first'] != $data['password']['second']) {
  119.                     if($locale == "fr") {
  120.                         $session->getFlashBag()->add('danger''Votre second mot de passe n\'est pas identique');
  121.                         return $this->redirectToRoute('whileresume_resume_fr');
  122.                     }
  123.                     $session->getFlashBag()->add('danger''Your second password is not identical');
  124.                     return $this->redirectToRoute('whileresume_resume_en');
  125.                 }
  126.                 $verificationUser $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
  127.                 if ($verificationUser == null) {
  128.                     $candidate = new Candidates();
  129.                     $candidate->setEmail($data['email']);
  130.                     $candidate->setUpdatedAt(new \DateTime("now"));
  131.                     $candidate->setCreatedAt(new \DateTime("now"));
  132.                     $candidate->setOnline(false);
  133.                     $candidate->setAvailability("offline");
  134.                     $this->em->persist($candidate);
  135.                     $this->em->flush();
  136.                     $us1 $this->generateUniqueSlug($candidate->getId(),10);
  137.                     $us2 $this->generateUniqueSlug($candidate->getId(),10);
  138.                     $candidate->setSlug($us1);
  139.                     $candidate->setSlugAnonyme($us2);
  140.                     $this->em->persist($candidate);
  141.                     $this->em->flush();
  142.                     $newUser = new Users();
  143.                     $newUser->setEmail($data['email']);
  144.                     $newUser->setUsername("");
  145.                     $newUser->setVerification(false);
  146.                     $newUser->setNotificationsMessages(true);
  147.                     $newUser->setNotificationsSuivis(true);
  148.                     $newUser->setPremium(false);
  149.                     $newUser->setFirst(false);
  150.                     $newUser->setEnabled(true);
  151.                     $newUser->setPassword($this->passwordEncoder->encodePassword($newUser,$data['password']['first']));
  152.                     $newUser->setRoles(['ROLE_USER']);
  153.                     $newUser->setTypeAccount("candidate");
  154.                     $newUser->setUpdatedAt(new \DateTime("now"));
  155.                     $newUser->setCreatedAt(new \DateTime("now"));
  156.                     $newUser->setCandidate($candidate);
  157.                     $this->em->persist($newUser);
  158.                     $this->em->flush();
  159.                     $title "Welcome to Whileresume - Your account is activated";
  160.                     if($locale == "fr") {
  161.                         $title "Bienvenue sur Whileresume - Votre compte est activée";
  162.                     }
  163.                     $descriptionHTML $this->twig->render("application/whileresume/gestion/emails/"$locale ."/register_candidate.html.twig",[
  164.                         'title' => $title,
  165.                         'email' => $data['email']
  166.                     ]);
  167.                     $this->ms->register($title,$descriptionHTML,null,$data['email'],null,null);
  168.                     $this->userAuthenticator->authenticateUser($newUser$this->authenticator$request);
  169.                     if($locale == "en") {
  170.                         return $this->redirectToRoute('customer_homepage');
  171.                     }
  172.                     return $this->redirectToRoute('locale_customer_homepage',['_locale' => $locale]);
  173.                 }
  174.                 if($locale == "fr") {
  175.                     return $this->redirectToRoute('whileresume_resume_fr');
  176.                 }
  177.                 return $this->redirectToRoute('whileresume_resume_en');
  178.             }
  179.         }
  180.         return $this->render('application/whileresume/website/candidates/resume_'.$locale.'.html.twig',[
  181.             'form' => $form->createView(),
  182.             'page' => $page
  183.         ]);
  184.     }
  185.     private function generateUniqueSlug(int $userIdint $length 8): string
  186.     {
  187.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  188.         $slug '';
  189.         for ($i 0$i $length$i++) {
  190.             $slug .= $characters[random_int(0strlen($characters) - 1)];
  191.         }
  192.         return $userId '-' $slug// Ex: 42-aB3xK9mP
  193.     }
  194. }