src/Controller/ThemesWebsite/Cvs/Website/HomepageController.php line 110

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Cvs\Website;
  3. use App\Entity\Core\Agencies;
  4. use App\Entity\Core\AgenciesHasUsers;
  5. use App\Entity\Core\Mails;
  6. use App\Entity\Core\Users;
  7. use App\Entity\Cvs\BigCategories;
  8. use App\Entity\Cvs\Candidates;
  9. use App\Entity\Cvs\Categories;
  10. use App\Entity\Pages\Pages;
  11. use App\Form\Core\UserProfileForm;
  12. use App\Form\Core\UsersEmailForm;
  13. use App\Form\Cvs\AgencyForm;
  14. use App\Form\Cvs\SearchCategoryForm;
  15. use App\Security\LoginFormAuthenticator;
  16. use App\Services\Core\RequestData;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  24. use Symfony\Component\HttpFoundation\Cookie;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  31. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  32. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  33. use Symfony\Component\Security\Core\User\UserInterface;
  34. use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
  35. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  36. use Symfony\Component\Security\Http\Authentication\AuthenticatorManagerInterface;
  37. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  38. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  39. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordCredentialsBadge;
  40. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  41. class HomepageController extends AbstractController
  42. {
  43.     private $rd;
  44.     private $em;
  45.     private $passwordEncoder;
  46.     private $ms;
  47.     private $us;
  48.     private $authenticator;
  49.     private $userAuthenticator;
  50.     private $paginator;
  51.     public function __construct(RequestData                  $rd,
  52.                                 EntityManagerInterface       $em,
  53.                                 UserPasswordEncoderInterface $passwordEncoder,
  54.                                 \App\Services\Mails          $ms,
  55.                                 \App\Services\Core\Users     $us,
  56.                                 UserAuthenticatorInterface   $userAuthenticator,
  57.                                 LoginFormAuthenticator       $authenticator,
  58.                                 PaginatorInterface           $paginator
  59.     ) {
  60.         $this->rd $rd;
  61.         $this->em $em;
  62.         $this->passwordEncoder $passwordEncoder;
  63.         $this->ms $ms;
  64.         $this->authenticator $authenticator;
  65.         $this->userAuthenticator $userAuthenticator;
  66.         $this->us $us;
  67.         $this->paginator $paginator;
  68.     }
  69.     public function homepage(Request $request): Response
  70.     {
  71.         $user $this->getUser();
  72.         if($user !== null) {
  73.             if($user->getTypeAccount() === null) {
  74.                 return $this->redirect($this->generateUrl('cvs_website_customer_first'));
  75.             }
  76.             if($user->getTypeAccount() === "enterprise") {
  77.                 return $this->redirect($this->generateUrl('cvs_website_homepage_recruiter'));
  78.             }
  79.         }
  80.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage']);
  81.         $categories $this->em->getRepository(Categories::class)->findBy([],['title' => 'ASC']);
  82.         return $this->render('themesWebsite/cvs/website/homepage.html.twig',[
  83.             'page' => $page,
  84.             'categories' => $categories
  85.         ]);
  86.     }
  87.     public function homepageRecruiter(Request $request): Response
  88.     {
  89.         $user $this->getUser();
  90.         if($user !== null) {
  91.             if($user->getTypeAccount() === null) {
  92.                 return $this->redirect($this->generateUrl('cvs_website_customer_first'));
  93.             }
  94.             if($user->getTypeAccount() === "candidate") {
  95.                 return $this->redirect($this->generateUrl('cvs_website_homepage'));
  96.             }
  97.         }
  98.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage']);
  99.         $categories $this->em->getRepository(Categories::class)->findBy([],['title' => 'ASC']);
  100.         return $this->render('themesWebsite/cvs/website/homepage_recruiter.html.twig',[
  101.             'page' => $page,
  102.             'categories' => $categories
  103.         ]);
  104.     }
  105.     /**
  106.      * Déposer un CV
  107.      * @param Request $request
  108.      * @return Response
  109.      */
  110.     public function depotCV(Request $request): Response
  111.     {
  112.         $session $request->getSession();
  113.         $typeWebsite $_ENV['TYPE_WEBSITE'];
  114.         $user $this->getUser();
  115.         if($user !== null) {
  116.             if($user->getTypeAccount() === "candidate") {
  117.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_coordinates');
  118.             }
  119.             return $this->redirectToRoute('homepage');
  120.         }
  121.         $form $this->createForm(UsersEmailForm::class);
  122.         $form->handleRequest($request);
  123.         if ($form->isSubmitted() && $form->isValid()) {
  124.             $data $request->request->all();
  125.             $data $data['users_email_form'];
  126.             $verificationUser $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
  127.             if ($verificationUser == null) {
  128.                 $newPassword $this->us->randomPasswordSecurised(5);
  129.                 $newUser = new Users();
  130.                 $newUser->setEmail($data['email']);
  131.                 $newUser->setUsername("");
  132.                 $newUser->setNotificationsMessages(true);
  133.                 $newUser->setNotificationsSuivis(true);
  134.                 $newUser->setPremium(false);
  135.                 $newUser->setFirst(false);
  136.                 $newUser->setEnabled(true);
  137.                 $newUser->setPassword($this->passwordEncoder->encodePassword($newUser,$newPassword));
  138.                 $newUser->setRoles(['ROLE_USER']);
  139.                 $newUser->setTypeAccount("candidate");
  140.                 $newUser->setUpdatedAt(new \DateTime("now"));
  141.                 $newUser->setCreatedAt(new \DateTime("now"));
  142.                 $this->em->persist($newUser);
  143.                 $this->em->flush();
  144.                 // Envoyer un mail d'inscription à l'utilisateur
  145.                 $templateEntity $this->em->getRepository(Mails::class)->findOneBy(['typeWebsite' => $typeWebsite'name' => "register"]);
  146.                 $this->ms->sendUserPassword($newUser$newPassword$templateEntity);
  147.                 $this->userAuthenticator->authenticateUser(
  148.                     $newUser,
  149.                     $this->authenticator,
  150.                     $request
  151.                 );
  152.                 $session->getFlashBag()->add('success''Merci de votre inscription. Vous êtes maintenant connecté.');
  153.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_coordinates');
  154.             }
  155.             return $this->redirectToRoute('cvs_website_customer_deposer_un_cv');
  156.         }
  157.         return $this->render('themesWebsite/cvs/website/customer/depot_CV.html.twig',[
  158.             'form' => $form->createView()
  159.         ]);
  160.     }
  161.     /**
  162.      * Choix du compte.
  163.      * @param Request $request
  164.      * @return Response
  165.      */
  166.     public function customerFirst(Request $request): Response
  167.     {
  168.         $user $this->getUser();
  169.         if($user !== null) {
  170.             if($user->getTypeAccount() !== null) {
  171.                 return $this->redirect($this->generateUrl('homepage'));
  172.             }
  173.         }
  174.         $user->setFirst(true);
  175.         $this->em->persist($user);
  176.         $this->em->flush();
  177.         return $this->render('themesWebsite/cvs/website/customer/first.html.twig');
  178.     }
  179.     /**
  180.      * Profil de l'entreprise.
  181.      * @param Request $request
  182.      * @return Response
  183.      */
  184.     public function customerFirstAgency(Request $request): Response
  185.     {
  186.         $session $request->getSession();
  187.         $user $this->getUser();
  188.         if($user !== null) {
  189.             if($user->getFirst() !== true) {
  190.                 return $this->redirect($this->generateUrl('homepage'));
  191.             }
  192.         }
  193.         $agency $user->getCurrentAgency();
  194.         if($agency == null) {
  195.             $agency = new Agencies();
  196.             $agency->setValide(false);
  197.             $agency->setPremium(false);
  198.             $agency->setFirst(true);
  199.             $this->em->persist($agency);
  200.             $this->em->flush();
  201.             $ahu = new AgenciesHasUsers();
  202.             $ahu->setUser($user);
  203.             $ahu->setAdmin(true);
  204.             $ahu->setAgency($agency);
  205.             $this->em->persist($agency);
  206.             $this->em->flush();
  207.         }
  208.         $form $this->createForm(AgencyForm::class, $agency);
  209.         $form->handleRequest($request);
  210.         if ($form->isSubmitted() && $form->isValid()) {
  211.             $data $request->request->all();
  212.             $data$data['agency_form'];
  213.             if(empty($data['localisation'])) {
  214.                 return $this->redirectToRoute('cvs_website_customer_first_agency');
  215.             }
  216.             $points $this->rd->getPoints($data['localisation']);
  217.             $queryAddress $this->rd->searchAddressFR($data['localisation']);
  218.             $queryAddress $queryAddress[0];
  219.             $agency->setAddress($queryAddress['streetAddress']);
  220.             $agency->setCity($queryAddress['city']);
  221.             $agency->setCountry($queryAddress['country']);
  222.             $agency->setZipcode($queryAddress['postcode']);
  223.             $agency->setPointX($points[0]);
  224.             $agency->setPointY($points[1]);
  225.             $agency->setFirst(false);
  226.             $agency->setValide(true);
  227.             $this->em->persist($agency);
  228.             $this->em->flush();
  229.             $user->setCurrentAgency($agency);
  230.             $this->em->persist($user);
  231.             $this->em->flush();
  232.             $session->getFlashBag()->add('success''Mise à jour des informations');
  233.             return $this->redirectToRoute('cvs_website_customer_first_profile');
  234.         }
  235.         $queryAddress "";
  236.         if(!empty($agency->getAddress())) {
  237.             $queryAddress $agency->getAddress().", ".$agency->getZipcode().", ".$agency->getCity().", ".$agency->getCountry();
  238.         }
  239.         return $this->render('themesWebsite/cvs/website/customer/agency.html.twig',[
  240.             'form' => $form->createView(),
  241.             'queryAddress' => $queryAddress
  242.         ]);
  243.     }
  244.     /**
  245.      * Profil de l'entreprise.
  246.      * @param Request $request
  247.      * @return Response
  248.      */
  249.     public function customerFirstProfile(Request $request): Response
  250.     {
  251.         $session $request->getSession();
  252.         $user $this->getUser();
  253.         if($user !== null) {
  254.             if($user->getFirst() !== true) {
  255.                 return $this->redirect($this->generateUrl('homepage'));
  256.             }
  257.         }
  258.         $form $this->createForm(UserProfileForm::class, $user);
  259.         $form->handleRequest($request);
  260.         if ($form->isSubmitted() && $form->isValid()) {
  261.             $user->setFirst(false);
  262.             $this->em->persist($user);
  263.             $this->em->flush();
  264.             $session->getFlashBag()->add('success''Mise à jour des informations');
  265.             if($user->getTypeAccount() == "enterprise") {
  266.                 return $this->redirectToRoute('cvs_gestion_enterprises_dashboard');
  267.             } elseif($user->getTypeAccount() == "candidate") {
  268.                 return $this->redirectToRoute('cvs_gestion_candidates_dashboard');
  269.             }
  270.             return $this->redirectToRoute('homepage');
  271.         }
  272.         return $this->render('themesWebsite/cvs/website/customer/profile.html.twig',[
  273.             'form' => $form->createView()
  274.         ]);
  275.     }
  276.     /**
  277.      * Création d'un compte entreprise.
  278.      * @param Request $request
  279.      * @return Response
  280.      */
  281.     public function customerRecruiter(Request $request): Response
  282.     {
  283.         $user $this->getUser();
  284.         if($user !== null) {
  285.             if($user->getTypeAccount() !== null) {
  286.                 return $this->redirect($this->generateUrl('homepage'));
  287.             }
  288.         }
  289.         $user->setTypeAccount("enterprise");
  290.         $this->em->persist($user);
  291.         $this->em->flush();
  292.         return $this->redirect($this->generateUrl('cvs_website_customer_first_agency'));
  293.     }
  294.     /**
  295.      * Création d'un compte candidat.
  296.      * @param Request $request
  297.      * @return Response
  298.      */
  299.     public function customerCandidate(Request $request): Response
  300.     {
  301.         $user $this->getUser();
  302.         if($user !== null) {
  303.             if($user->getTypeAccount() !== null) {
  304.                 return $this->redirect($this->generateUrl('homepage'));
  305.             }
  306.         }
  307.         $user->setTypeAccount("candidate");
  308.         $this->em->persist($user);
  309.         $this->em->flush();
  310.         return $this->redirect($this->generateUrl('cvs_website_customer_first_profile'));
  311.     }
  312.     /**
  313.      * Explorer une catégorie
  314.      * @param Request $request
  315.      * @param $slug
  316.      * @return Response
  317.      */
  318.     public function category(Request $request$slug): Response
  319.     {
  320.         $session $request->getSession();
  321.         $category $this->em->getRepository(Categories::class)->findOneBy(['slug' => $slug]);
  322.         $errorLocalisation $session->get('errorLocalisation');
  323.         $queryPointsX $session->get('queryPointsX');
  324.         $queryPointsY $session->get('queryPointsY');
  325.         $queryLocalisation $session->get('queryLocalisation');
  326.         $queryKeyword $session->get('queryKeyword');
  327.         $form $this->createForm(SearchCategoryForm::class);
  328.         $form->handleRequest($request);
  329.         if ($form->isSubmitted() && $form->isValid()) {
  330.             $data $request->request->all();
  331.             $search $data['search_category_form'];
  332.             $localisation $search['localisation'];
  333.             $session->set('errorLocalisation',false);
  334.             if(empty($search['localisation'])) {
  335.                 $session->set('errorLocalisation',true);
  336.                 $session->set('queryLocalisation',"");
  337.                 return $this->redirect($this->generateUrl('cvs_website_category',['slug' => $slug]));
  338.             }
  339.             $points $this->rd->getPoints($localisation);
  340.             $session->set('queryLocalisation',$localisation);
  341.             $session->set('queryPointsX',$points[0]);
  342.             $session->set('queryPointsY',$points[1]);
  343.             $session->set('queryKeyword',$search['title']);
  344.             return $this->redirect($this->generateUrl('cvs_website_category',['slug' => $slug]));
  345.         }
  346.         $candidates $this->em->getRepository(Candidates::class)->searchBy($queryKeyword,$queryPointsX,$queryPointsY,[$category->getId()]);
  347.         $pagination $this->paginator->paginate(
  348.             $candidates,
  349.             $request->query->getInt('page'1),
  350.             15
  351.         );
  352.         return $this->render('themesWebsite/cvs/website/category.html.twig',[
  353.             'category' => $category,
  354.             'form' => $form->createview(),
  355.             'candidates' => $pagination,
  356.             'errorLocalisation' => $errorLocalisation,
  357.             'queryLocalisation' => $queryLocalisation,
  358.             'queryKeyword' => $queryKeyword
  359.         ]);
  360.     }
  361.     /**
  362.      * Explorer les métiers
  363.      * @param Request $request
  364.      * @param $slug
  365.      * @return Response
  366.      */
  367.     public function metier(Request $request$slug): Response
  368.     {
  369.         $bigCategory $this->em->getRepository(BigCategories::class)->findOneBy(['slug' => $slug]);
  370.         $categories $this->em->getRepository(Categories::class)->findBy(['bigCategory' => $bigCategory]);
  371.         return $this->render('themesWebsite/cvs/website/metier.html.twig',[
  372.             'bigCategory' => $bigCategory,
  373.             'categories' => $categories
  374.         ]);
  375.     }
  376.     /**
  377.      * Localisation.
  378.      * @param Request $request
  379.      * @return JsonResponse
  380.      */
  381.     public function localisation(Request $request)
  382.     {
  383.         $term $request->query->get('term');
  384.         $results $this->rd->searchAddress($term);
  385.         return new JsonResponse($results);
  386.     }
  387. }