<?php
namespace App\Controller\ThemesWebsite\Blog;
use App\Entity\Core\Mails;
use App\Entity\Core\Users;
use App\Entity\Cvs\Shares;
use App\Entity\Pages\Pages;
use App\Entity\Pages\PagesHasBlocks;
use App\Form\Core\UsersType;
use Doctrine\ORM\EntityManagerInterface;
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\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\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Inscription
*/
class RegisterController extends AbstractController
{
private $passwordEncoder;
private $ms;
private $em;
public function __construct(UserPasswordEncoderInterface $passwordEncoder,
EntityManagerInterface $em,
\App\Services\Mails $ms
) {
$this->passwordEncoder = $passwordEncoder;
$this->em = $em;
$this->ms = $ms;
}
/**
* Page d'inscription
* @param Request $request
* @param \App\Services\Mails $ms
* @return Response
*/
public function register(Request $request): Response
{
$session = $request->getSession();
$themeSelection = $_ENV['THEME_SELECTION'];
$registerOnline = $_ENV['APPLICATION_REFISTER'];
if($registerOnline == "offline") {
return $this->redirectToRoute('homepage');
}
$user = $this->getUser();
if($user != null) {
return $this->redirectToRoute('homepage');
}
$sharingCode = $session->get("sharing-code");
$shareInvitation = null;
if(!empty($sharingCode)) {
$shareInvitation = $this->em->getRepository(Shares::class)->findOneBy(['code' => $sharingCode]);
}
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'register_nopassword']);
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
$newUser = new Users();
$newUser->setPremium(false);
$form = $this->createForm(UsersType::class, $newUser);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$data = $data['users'];
if($data['password']['first'] != $data['password']['second']) {
$session->getFlashBag()->add('danger', 'Votre second mot de passe n\'est pas identique');
return $this->redirectToRoute('app_register');
}
$verificationUser = $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
if ($verificationUser == null) {
$newPassword = $newUser->getPassword();
$newUser->setFirst(false);
$newUser->setEnabled(true);
$newUser->setPassword($this->passwordEncoder->encodePassword($newUser, $newUser->getPassword()));
$newUser->setRoles(['ROLE_USER']);
$newUser->setUpdatedAt(new \DateTime("now"));
$newUser->setCreatedAt(new \DateTime("now"));
$newUser->setShare($shareInvitation);
$this->em->persist($newUser);
$this->em->flush();
if($shareInvitation !== null) {
if($shareInvitation->isMultiple() === false) {
$shareInvitation->setUserTarget($newUser);
$this->em->persist($shareInvitation);
$this->em->flush();
}
}
// Envoyer un mail d'inscription à l'utilisateur
$templateEntity = $this->em->getRepository(Mails::class)->findOneBy(['name' => "register"]);
$this->ms->sendUserPassword($newUser,$newPassword,$templateEntity);
$session->getFlashBag()->add('success', 'Merci de votre inscription');
return $this->redirectToRoute('app_login');
}
$session->getFlashBag()->add('danger', 'L\'adresse mail est déjà dans notre base de données');
return $this->redirectToRoute('app_register');
}
$response = $this->render('themesWebsite/blog'.$themeSelection.'/register.html.twig', [
'page' => $page,
'blocks' => $blocks,
'form' => $form->createView(),
'invitation' => $shareInvitation
]);
//$response->setSharedMaxAge(3600);
return $response;
}
}