<?phpnamespace App\Controller\ThemesWebsite\Whileresume\Website;use App\Entity\Articles\Articles;use App\Entity\Core\Users;use App\Entity\Cvs\Candidates;use App\Entity\Pages\Pages;use App\Form\Core\UsersEmailForm;use App\Security\LoginFormAuthenticator;use App\Services\Core\RequestData;use Doctrine\ORM\EntityManagerInterface;use Knp\Component\Pager\PaginatorInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Cookie;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;use Symfony\Component\Security\Http\Authentication\AuthenticatorManagerInterface;use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordCredentialsBadge;use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;use Twig\Environment;class CandidatesController extends AbstractController{ private $rd; private $em; private $passwordEncoder; private $ms; private $us; private $authenticator; private $userAuthenticator; private $paginator; private $twig; public function __construct(RequestData $rd, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, \App\Services\Mails $ms, \App\Services\Core\Users $us, UserAuthenticatorInterface $userAuthenticator, LoginFormAuthenticator $authenticator, PaginatorInterface $paginator, Environment $twig, ) { $this->rd = $rd; $this->em = $em; $this->passwordEncoder = $passwordEncoder; $this->ms = $ms; $this->authenticator = $authenticator; $this->userAuthenticator = $userAuthenticator; $this->us = $us; $this->paginator = $paginator; $this->twig = $twig; } /** * Homepage principal * @param Request $request * @return Response */ public function homepage(Request $request): Response { $locale = $request->getLocale(); $user = $this->getUser(); /* if($user !== null) { if ($user->getTypeAccount() === "enterprise") { return $this->redirectToRoute('whileresume_business_'.$locale); } }*/ $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage', 'locale' => $locale]); $articles = $this->em->getRepository(Articles::class)->getArticles(6,$locale); return $this->render('application/whileresume/website/candidates/homepage_'.$locale.'.html.twig',[ 'page' => $page, 'articles' => $articles ]); } /** * Déposer un CV * @param Request $request * @return Response */ public function resume(Request $request): Response { $session = $request->getSession(); $locale = $request->getLocale(); $user = $this->getUser(); /* if($user !== null) { if($user->getTypeAccount() === "candidate") { if($locale != "en") { return $this->redirect($this->generateUrl('locale_cvs_gestion_candidates_dashboard',['_locale' => $locale])); } return $this->redirect($this->generateUrl('cvs_gestion_candidates_dashboard')); } if ($user->getTypeAccount() === "enterprise") { return $this->redirectToRoute('whileresume_business_'.$locale); } } */ $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'resume', 'locale' => $locale]); $form = $this->createForm(UsersEmailForm::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $request->request->all(); $data = $data['users_email_form']; if($data['acceptTerm'] == "1") { if($data['password']['first'] != $data['password']['second']) { if($locale == "fr") { $session->getFlashBag()->add('danger', 'Votre second mot de passe n\'est pas identique'); return $this->redirectToRoute('whileresume_resume_fr'); } $session->getFlashBag()->add('danger', 'Your second password is not identical'); return $this->redirectToRoute('whileresume_resume_en'); } $verificationUser = $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]); if ($verificationUser == null) { $candidate = new Candidates(); $candidate->setEmail($data['email']); $candidate->setUpdatedAt(new \DateTime("now")); $candidate->setCreatedAt(new \DateTime("now")); $candidate->setOnline(false); $candidate->setAvailability("offline"); $this->em->persist($candidate); $this->em->flush(); $us1 = $this->generateUniqueSlug($candidate->getId(),10); $us2 = $this->generateUniqueSlug($candidate->getId(),10); $candidate->setSlug($us1); $candidate->setSlugAnonyme($us2); $this->em->persist($candidate); $this->em->flush(); $newUser = new Users(); $newUser->setEmail($data['email']); $newUser->setUsername(""); $newUser->setVerification(false); $newUser->setNotificationsMessages(true); $newUser->setNotificationsSuivis(true); $newUser->setPremium(false); $newUser->setFirst(false); $newUser->setEnabled(true); $newUser->setPassword($this->passwordEncoder->encodePassword($newUser,$data['password']['first'])); $newUser->setRoles(['ROLE_USER']); $newUser->setTypeAccount("candidate"); $newUser->setUpdatedAt(new \DateTime("now")); $newUser->setCreatedAt(new \DateTime("now")); $newUser->setCandidate($candidate); $this->em->persist($newUser); $this->em->flush(); $title = "Welcome to Whileresume - Your account is activated"; if($locale == "fr") { $title = "Bienvenue sur Whileresume - Votre compte est activée"; } $descriptionHTML = $this->twig->render("application/whileresume/gestion/emails/". $locale ."/register_candidate.html.twig",[ 'title' => $title, 'email' => $data['email'] ]); $this->ms->register($title,$descriptionHTML,null,$data['email'],null,null); $this->userAuthenticator->authenticateUser($newUser, $this->authenticator, $request); if($locale == "en") { return $this->redirectToRoute('customer_homepage'); } return $this->redirectToRoute('locale_customer_homepage',['_locale' => $locale]); } if($locale == "fr") { return $this->redirectToRoute('whileresume_resume_fr'); } return $this->redirectToRoute('whileresume_resume_en'); } } return $this->render('application/whileresume/website/candidates/resume_'.$locale.'.html.twig',[ 'form' => $form->createView(), 'page' => $page ]); } private function generateUniqueSlug(int $userId, int $length = 8): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $slug = ''; for ($i = 0; $i < $length; $i++) { $slug .= $characters[random_int(0, strlen($characters) - 1)]; } return $userId . '-' . $slug; // Ex: 42-aB3xK9mP }}