<?php
namespace App\Controller\ThemesWebsite\Cvs\Website;
use App\Entity\Core\Agencies;
use App\Entity\Core\AgenciesHasUsers;
use App\Entity\Core\Mails;
use App\Entity\Core\Users;
use App\Entity\Cvs\BigCategories;
use App\Entity\Cvs\Candidates;
use App\Entity\Cvs\Categories;
use App\Entity\Pages\Pages;
use App\Form\Core\UserProfileForm;
use App\Form\Core\UsersEmailForm;
use App\Form\Cvs\AgencyForm;
use App\Form\Cvs\SearchCategoryForm;
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;
class HomepageController extends AbstractController
{
private $rd;
private $em;
private $passwordEncoder;
private $ms;
private $us;
private $authenticator;
private $userAuthenticator;
private $paginator;
public function __construct(RequestData $rd,
EntityManagerInterface $em,
UserPasswordEncoderInterface $passwordEncoder,
\App\Services\Mails $ms,
\App\Services\Core\Users $us,
UserAuthenticatorInterface $userAuthenticator,
LoginFormAuthenticator $authenticator,
PaginatorInterface $paginator
) {
$this->rd = $rd;
$this->em = $em;
$this->passwordEncoder = $passwordEncoder;
$this->ms = $ms;
$this->authenticator = $authenticator;
$this->userAuthenticator = $userAuthenticator;
$this->us = $us;
$this->paginator = $paginator;
}
public function homepage(Request $request): Response
{
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() === null) {
return $this->redirect($this->generateUrl('cvs_website_customer_first'));
}
if($user->getTypeAccount() === "enterprise") {
return $this->redirect($this->generateUrl('cvs_website_homepage_recruiter'));
}
}
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage']);
$categories = $this->em->getRepository(Categories::class)->findBy([],['title' => 'ASC']);
return $this->render('themesWebsite/cvs/website/homepage.html.twig',[
'page' => $page,
'categories' => $categories
]);
}
public function homepageRecruiter(Request $request): Response
{
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() === null) {
return $this->redirect($this->generateUrl('cvs_website_customer_first'));
}
if($user->getTypeAccount() === "candidate") {
return $this->redirect($this->generateUrl('cvs_website_homepage'));
}
}
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage']);
$categories = $this->em->getRepository(Categories::class)->findBy([],['title' => 'ASC']);
return $this->render('themesWebsite/cvs/website/homepage_recruiter.html.twig',[
'page' => $page,
'categories' => $categories
]);
}
/**
* Déposer un CV
* @param Request $request
* @return Response
*/
public function depotCV(Request $request): Response
{
$session = $request->getSession();
$typeWebsite = $_ENV['TYPE_WEBSITE'];
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() === "candidate") {
return $this->redirectToRoute('cvs_gestion_candidates_generate_coordinates');
}
return $this->redirectToRoute('homepage');
}
$form = $this->createForm(UsersEmailForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$data = $data['users_email_form'];
$verificationUser = $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
if ($verificationUser == null) {
$newPassword = $this->us->randomPasswordSecurised(5);
$newUser = new Users();
$newUser->setEmail($data['email']);
$newUser->setUsername("");
$newUser->setNotificationsMessages(true);
$newUser->setNotificationsSuivis(true);
$newUser->setPremium(false);
$newUser->setFirst(false);
$newUser->setEnabled(true);
$newUser->setPassword($this->passwordEncoder->encodePassword($newUser,$newPassword));
$newUser->setRoles(['ROLE_USER']);
$newUser->setTypeAccount("candidate");
$newUser->setUpdatedAt(new \DateTime("now"));
$newUser->setCreatedAt(new \DateTime("now"));
$this->em->persist($newUser);
$this->em->flush();
// Envoyer un mail d'inscription à l'utilisateur
$templateEntity = $this->em->getRepository(Mails::class)->findOneBy(['typeWebsite' => $typeWebsite, 'name' => "register"]);
$this->ms->sendUserPassword($newUser, $newPassword, $templateEntity);
$this->userAuthenticator->authenticateUser(
$newUser,
$this->authenticator,
$request
);
$session->getFlashBag()->add('success', 'Merci de votre inscription. Vous êtes maintenant connecté.');
return $this->redirectToRoute('cvs_gestion_candidates_generate_coordinates');
}
return $this->redirectToRoute('cvs_website_customer_deposer_un_cv');
}
return $this->render('themesWebsite/cvs/website/customer/depot_CV.html.twig',[
'form' => $form->createView()
]);
}
/**
* Choix du compte.
* @param Request $request
* @return Response
*/
public function customerFirst(Request $request): Response
{
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() !== null) {
return $this->redirect($this->generateUrl('homepage'));
}
}
$user->setFirst(true);
$this->em->persist($user);
$this->em->flush();
return $this->render('themesWebsite/cvs/website/customer/first.html.twig');
}
/**
* Profil de l'entreprise.
* @param Request $request
* @return Response
*/
public function customerFirstAgency(Request $request): Response
{
$session = $request->getSession();
$user = $this->getUser();
if($user !== null) {
if($user->getFirst() !== true) {
return $this->redirect($this->generateUrl('homepage'));
}
}
$agency = $user->getCurrentAgency();
if($agency == null) {
$agency = new Agencies();
$agency->setValide(false);
$agency->setPremium(false);
$agency->setFirst(true);
$this->em->persist($agency);
$this->em->flush();
$ahu = new AgenciesHasUsers();
$ahu->setUser($user);
$ahu->setAdmin(true);
$ahu->setAgency($agency);
$this->em->persist($agency);
$this->em->flush();
}
$form = $this->createForm(AgencyForm::class, $agency);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$data= $data['agency_form'];
if(empty($data['localisation'])) {
return $this->redirectToRoute('cvs_website_customer_first_agency');
}
$points = $this->rd->getPoints($data['localisation']);
$queryAddress = $this->rd->searchAddressFR($data['localisation']);
$queryAddress = $queryAddress[0];
$agency->setAddress($queryAddress['streetAddress']);
$agency->setCity($queryAddress['city']);
$agency->setCountry($queryAddress['country']);
$agency->setZipcode($queryAddress['postcode']);
$agency->setPointX($points[0]);
$agency->setPointY($points[1]);
$agency->setFirst(false);
$agency->setValide(true);
$this->em->persist($agency);
$this->em->flush();
$user->setCurrentAgency($agency);
$this->em->persist($user);
$this->em->flush();
$session->getFlashBag()->add('success', 'Mise à jour des informations');
return $this->redirectToRoute('cvs_website_customer_first_profile');
}
$queryAddress = "";
if(!empty($agency->getAddress())) {
$queryAddress = $agency->getAddress().", ".$agency->getZipcode().", ".$agency->getCity().", ".$agency->getCountry();
}
return $this->render('themesWebsite/cvs/website/customer/agency.html.twig',[
'form' => $form->createView(),
'queryAddress' => $queryAddress
]);
}
/**
* Profil de l'entreprise.
* @param Request $request
* @return Response
*/
public function customerFirstProfile(Request $request): Response
{
$session = $request->getSession();
$user = $this->getUser();
if($user !== null) {
if($user->getFirst() !== true) {
return $this->redirect($this->generateUrl('homepage'));
}
}
$form = $this->createForm(UserProfileForm::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setFirst(false);
$this->em->persist($user);
$this->em->flush();
$session->getFlashBag()->add('success', 'Mise à jour des informations');
if($user->getTypeAccount() == "enterprise") {
return $this->redirectToRoute('cvs_gestion_enterprises_dashboard');
} elseif($user->getTypeAccount() == "candidate") {
return $this->redirectToRoute('cvs_gestion_candidates_dashboard');
}
return $this->redirectToRoute('homepage');
}
return $this->render('themesWebsite/cvs/website/customer/profile.html.twig',[
'form' => $form->createView()
]);
}
/**
* Création d'un compte entreprise.
* @param Request $request
* @return Response
*/
public function customerRecruiter(Request $request): Response
{
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() !== null) {
return $this->redirect($this->generateUrl('homepage'));
}
}
$user->setTypeAccount("enterprise");
$this->em->persist($user);
$this->em->flush();
return $this->redirect($this->generateUrl('cvs_website_customer_first_agency'));
}
/**
* Création d'un compte candidat.
* @param Request $request
* @return Response
*/
public function customerCandidate(Request $request): Response
{
$user = $this->getUser();
if($user !== null) {
if($user->getTypeAccount() !== null) {
return $this->redirect($this->generateUrl('homepage'));
}
}
$user->setTypeAccount("candidate");
$this->em->persist($user);
$this->em->flush();
return $this->redirect($this->generateUrl('cvs_website_customer_first_profile'));
}
/**
* Explorer une catégorie
* @param Request $request
* @param $slug
* @return Response
*/
public function category(Request $request, $slug): Response
{
$session = $request->getSession();
$category = $this->em->getRepository(Categories::class)->findOneBy(['slug' => $slug]);
$errorLocalisation = $session->get('errorLocalisation');
$queryPointsX = $session->get('queryPointsX');
$queryPointsY = $session->get('queryPointsY');
$queryLocalisation = $session->get('queryLocalisation');
$queryKeyword = $session->get('queryKeyword');
$form = $this->createForm(SearchCategoryForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$search = $data['search_category_form'];
$localisation = $search['localisation'];
$session->set('errorLocalisation',false);
if(empty($search['localisation'])) {
$session->set('errorLocalisation',true);
$session->set('queryLocalisation',"");
return $this->redirect($this->generateUrl('cvs_website_category',['slug' => $slug]));
}
$points = $this->rd->getPoints($localisation);
$session->set('queryLocalisation',$localisation);
$session->set('queryPointsX',$points[0]);
$session->set('queryPointsY',$points[1]);
$session->set('queryKeyword',$search['title']);
return $this->redirect($this->generateUrl('cvs_website_category',['slug' => $slug]));
}
$candidates = $this->em->getRepository(Candidates::class)->searchBy($queryKeyword,$queryPointsX,$queryPointsY,[$category->getId()]);
$pagination = $this->paginator->paginate(
$candidates,
$request->query->getInt('page', 1),
15
);
return $this->render('themesWebsite/cvs/website/category.html.twig',[
'category' => $category,
'form' => $form->createview(),
'candidates' => $pagination,
'errorLocalisation' => $errorLocalisation,
'queryLocalisation' => $queryLocalisation,
'queryKeyword' => $queryKeyword
]);
}
/**
* Explorer les métiers
* @param Request $request
* @param $slug
* @return Response
*/
public function metier(Request $request, $slug): Response
{
$bigCategory = $this->em->getRepository(BigCategories::class)->findOneBy(['slug' => $slug]);
$categories = $this->em->getRepository(Categories::class)->findBy(['bigCategory' => $bigCategory]);
return $this->render('themesWebsite/cvs/website/metier.html.twig',[
'bigCategory' => $bigCategory,
'categories' => $categories
]);
}
/**
* Localisation.
* @param Request $request
* @return JsonResponse
*/
public function localisation(Request $request)
{
$term = $request->query->get('term');
$results = $this->rd->searchAddress($term);
return new JsonResponse($results);
}
}