<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Entity\User;
use App\Form\PersonEditType;
use App\Security\EmailVerifier;
use App\Service\SendinBlueManager;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserController extends AbstractController
{
/** @var EmailVerifier */
private $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
/**
* @Route("/utilisateur/rediriger", name="app_user_redirect")
*/
public function redirectUser(ManagerRegistry $registry)
{
$user = $this->getUser();
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('admin');
}
if ($this->isGranted('ROLE_CANDIDATE')) {
return $this->redirectToRoute('app_candidate_index');
}
if ($this->isGranted('ROLE_COMPANY')) {
return $this->redirectToRoute('app_company_profile_index');
}
if ($this->isGranted('ROLE_TERRITORY')) {
return $this->redirectToRoute('app_territory_index');
}
if ($this->isGranted('ROLE_USER_REAL_ESTATE')) {
return $this->redirectToRoute('app_real_estate_profile_index');
}
if ($this->isGranted('ROLE_PROJECT_ESTATE') || $this->isGranted('ROLE_PROJECT_OPPORTUNITY')) {
return $this->redirectToRoute('app_project_leader_index');
}
return $this->redirectToRoute('app_candidate_index');
throw new \Exception();
}
/**
* @Route("/user/edit-profile", name="app_user_edit_profile")
*/
public function editProfile(
Request $request,
ManagerRegistry $registry,
FormFactoryInterface $formFactory,
TranslatorInterface $translator
): Response
{
/** @var User $user */
$user = $this->getUser();
$em = $registry->getManager();
$person = $user->getPerson();
$form = $formFactory->createNamed('form', PersonEditType::class, $person);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!$person->getPhoto() || (!$person->getPhoto()->getDocumentFile() && !$person->getPhoto()->getFileName())) {
$person->setPhoto(null);
}
$em->flush();
$this->addFlash(
'success',
$translator->trans('flash.edit')
);
return $this->redirectToRoute('app_user_redirect');
}
$layout = 'candidate/layout.html.twig';
if ($this->isGranted('ROLE_COMPANY')) {
$layout = 'company_profile/layout.html.twig';
}
if ($this->isGranted('ROLE_TERRITORY')) {
$layout = 'territory/layout.html.twig';
}
return $this->renderForm(
'user/edit_profile.html.twig',
[
'form' => $form,
'person' => $person,
'layout' => $layout,
]
);
}
/**
* @Route("/user/request-email-verifier", name="app_user_request_email_verifier")
*/
public function requestEmailVerifier(
TranslatorInterface $translator
): Response
{
$user = $this->getUser();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('notifications@paris-jetequitte.com', 'Paris Je Te Quitte'))
->to($user->getEmail())
->subject($translator->trans('template_email.request_email_verifier.subject'))
->htmlTemplate('email/security/request_email_verifier.html.twig')
);
$this->addFlash(
'success',
$translator->trans('flash.user.requestemail')
);
return $this->redirectToRoute('app_user_redirect');
}
/**
* @Route("/user/supprimer-compte", name="app_user_delete")
*/
public function delete(
Request $request,
ManagerRegistry $registry,
SendinBlueManager $sendinBlueManager,
TranslatorInterface $translator
): Response
{
/** @var User $user */
$user = $this->getUser();
$em = $registry->getManager();
$form = $this->createFormBuilder($user)
->add('validate', SubmitType::class, ['label' => 'Je veux supprimer mon compte'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$session = new Session();
$session->invalidate();
$sendinBlueManager->deleteContact($user->getEmail());
$em->remove($user);
$em->flush();
$this->addFlash(
'success',
$translator->trans('flash.user.delete')
);
return $this->redirectToRoute('app_logout');
}
return $this->renderForm(
'user/delete.html.twig',
[
'form' => $form,
'user' => $user
]
);
}
public function notificationsNavbar(ManagerRegistry $registry)
{
$user = $this->getUser();
$notifications = $registry->getRepository(Notification::class)
->getUser($user);
$total = $registry->getRepository(Notification::class)
->getTotalUser($user);
return $this->renderForm(
'user/notifications_navbar.html.twig',[
'notifications' => $notifications,
'total' => $total
]);
}
}