<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use App\Service\SJira;
use App\Service\SFonction;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class FrontController extends AbstractController
{
/**
* @Route("/", name="front_index")
*/
public function index()
{
return $this->redirectToRoute('app_login');
/*return $this->render('front/index.html.twig', [
]);*/
}
/**
* @Route("/inscription", name="front_connexion_inscription", methods={"GET","POST"})
*/
public function new(SFonction $sFonction, MailerInterface $mailer, Request $request, UserPasswordEncoderInterface $passwordEncoder, SJira $SJira): Response
{
$utilisateur = new User();
$form = $this->createForm(UserType::class, $utilisateur);
$form->handleRequest($request);
$error = '';
if ($form->isSubmitted()) {
$formData = $form->getData();
$email = $formData->getEmail();
$nom = $formData->getName();
$prenom = $formData->getLastName();
$em = $this->getDoctrine()->getManager();
$mailClient = $em->getRepository(User::class)->findByEmail($email);
if (count($mailClient) == 0 && $form->isValid()) {
$customerId = $SJira->setCustomer($nom . ' ' . $prenom, $email); //Création du customer ID pour gerer les tickets support sur Jira
$utilisateur->setPassword($passwordEncoder->encodePassword($utilisateur, $utilisateur->getPassword()));
$utilisateur->setRoles(['ROLE_CLIENT']);
$utilisateur->setEmail($email);
$utilisateur->setName($nom);
$utilisateur->setLastname($prenom);
$utilisateur->setCustomerId($customerId['customerId']);
$utilisateur->setNotificationActive(1);
$utilisateur->setErrorJira($customerId['errorJira']);
$em->persist($utilisateur);
$em->flush();
//Récupération des informations
$client = $em->getRepository(User::class)->findOneById($utilisateur->getId());
//Génération du mail
$email = (new TemplatedEmail()) //https://symfony.com/doc/4.4/mailer.html
->from(new Address('support@voxdia.com', 'Voxdia'))
->to($client->getEmail())
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('support@voxdia.com')
->priority(Email::PRIORITY_HIGH)
->subject('Your account has been successfully created')
->htmlTemplate('__email/client_creation_compte.html.twig')
->context([
'prenomClient' => $client->getName(),
]);
//->html('<p>Votre podcast est disponible</p>Lien ici : ');
$mailer->send($email);
return $this->redirectToRoute('admin_index');
} else {
$error = 'An account already exists with this email<br><br>';
}
}
return $this->render('front/connexion_inscription.html.twig', [
'utilisateur' => $utilisateur,
'form' => $form->createView(),
'error' => $error,
]);
}
/**
* @Route("/reset", name="front_connexion_reset", methods={"GET","POST"})
*/
public function reset(Request $request, UserPasswordEncoderInterface $passwordEncoder, MailerInterface $mailer): Response
{
$request = $request->request;
$resetEmail = $request->get('resetEmail');
if (!is_null($resetEmail)) {
$em = $this->getDoctrine()->getManager();
$utilisateur = $em->getRepository(User::class)->findOneBy(['email' => $resetEmail]);
if (!is_null($utilisateur)) {
$password = md5(uniqid());
$utilisateur->setPassword($passwordEncoder->encodePassword($utilisateur, $password));
$em->persist($utilisateur);
$em->flush();
$email = (new TemplatedEmail()) //https://symfony.com/doc/4.4/mailer.html
->from(new Address('support@voxdia.com', 'Voxdia'))
->to($resetEmail)
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('support@voxdia.com')
//->priority(Email::PRIORITY_HIGH)
->priority(Email::PRIORITY_HIGH)
->subject('Nouveau mot de passe')
->htmlTemplate('__email/nouveau_mdp.html.twig')
->context([
'password' => $password,
]);
$mailer->send($email);
$retour = [ 'success' => true,
'message' => 'Password reset email sent. Please check your inbox.',
];
} else {
$retour = [ 'success' => false,
'message' => 'Wrong email',
];
}
return new JsonResponse($retour, Response::HTTP_OK);
}
return $this->render('front/connexion_reset.html.twig', [
]);
}
}