<?php
namespace App\Controller\Frontend;
use App\Entity\Activity;
use App\Entity\Contact;
use App\Entity\EntityStatus;
use App\Entity\Entreprise;
use App\Entity\EntrepriseActivity;
use App\Entity\FirmType;
use App\Entity\User;
use App\Form\Common\ChangePasswordType;
use App\Form\Frontend\SignupType;
use App\Repository\UserRepository;
use App\Service\SignupService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'customer_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUserName = $authenticationUtils->getLastUsername();
return $this->render('frontend/security/login.html.twig', [
'controller_name' => 'SecurityController',
'last_username' => $lastUserName,
'error' => $error
]);
}
#[Route(path: '/security/change-password', name: 'security_change_password')]
public function changePassword(
Request $request,
UserPasswordHasherInterface $passwordHasher,
UserRepository $userRepository
): Response
{
$user = $this->getUser();
$form = $this->createForm(ChangePasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$encodedPassword = $passwordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
);
$user
->setPassword($encodedPassword)
->setChangePassword(false)
;
$userRepository->save($user, true);
$this->addFlash(
'success',
'Mot de passe changé avec succès'
);
return $this->redirectToRoute('customer_home');
}
return $this->renderForm('frontend/security/change_password.html.twig', [
'controller_name' => 'SecurityController',
'user' => $user,
'form' => $form
]);
}
#[Route(path: '/logout', name: 'customer_logout')]
public function logout()
{
throw new Exception('FFCB logout');
}
#[Route('/signup', name: 'customer_signup', methods: ['GET', 'POST'])]
public function signup(Request $request, SignupService $signupService, EntityManagerInterface $em): Response
{
$form = $this->createForm(SignupType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contactEmail = $form->get('contact_email')->getData();
$contactUser = $em->getRepository(User::class)->findOneBy(['email' => $contactEmail]);
if(in_array('ROLE_MEMBER', $contactUser->getRoles())) {
$this->addFlash(
'error',
'Email déjà existant, veuillez vous connecter <a href="' . $this->generateUrl('customer_login') .'">ici.</a>'
);
return $this->redirectToRoute('customer_signup');
}
$entreprise = new Entreprise();
$entreprise
->setRaisonSocial($form->get('raisonSocial')->getData())
->setStatus(EntityStatus::STATUS_IN_CREATION)
->setEmail($form->get('firm_email')->getData())
->setTel1($form->get('firm_phone')->getData())
->setCommentaires($form->get('reason')->getData())
->setAdresse($form->get('firm_adresse')->getData())
->setCodePostal($form->get('firm_codePostal')->getData())
->setVille($form->get('firm_ville')->getData())
->setSouhaiteAdherer($form->get('isMember')->getData())
->setFirmType($em->getRepository(FirmType::class)->find(1))
;
$activities = $em->getRepository(Activity::class)->findBy([
'isEnabled' => true
]);
foreach ($activities as $activity) {
$firmActivity = new EntrepriseActivity();
$firmActivity
->setNumber(0)
->setChecked(false)
->setActivity($activity)
->setEntreprise($entreprise)
;
$entreprise->addEntrepriseActivity($firmActivity);
}
$contact = new Contact();
$contact
->setNom($form->get('contact_lastname')->getData())
->setPrenom($form->get('contact_firstname')->getData())
->setTelPortable($form->get('contact_cellphone')->getData())
->setEmailContact($form->get('contact_email')->getData())
->setIsResponsible(true)
;
$entreprise->addContact($contact);
$em->persist($entreprise);
$em->flush();
$signupService->notifySignup($form->getData(), $entreprise);
$this->addFlash(
'success',
'Votre demande d\'inscription à bien été prise en compte. L\'équipe FFCB vous répondra dans les plus brefs délais.'
);
return $this->redirectToRoute('customer_signup');
}
return $this->renderForm('frontend/security/signup.html.twig', [
'controller_name' => 'SecurityController',
'form' => $form
]);
}
}