<?php
namespace App\Controller;
use App\Entity\Assure;
use App\Entity\DevisDocument;
use App\Entity\Sinister;
use App\Entity\SinisterDocument;
use App\Form\AssureType;
use App\Form\AssureUpdateType;
use App\Form\Flow\SinisterFormFlow;
use App\Form\SinisterFormType;
use App\Repository\DocumentRepository;
use App\Service\EmailManager;
use App\Utils\UploadedBase64File;
use Doctrine\ORM\EntityManagerInterface;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Class EspaceAssureController
* @package App\Controller
*
* @Route("/espace-assure", name="assure_")
*
*/
class EspaceAssureController extends AbstractController
{
private $em;
private $security;
public function __construct(EntityManagerInterface $entityManagerInterface, Security $security)
{
$this->em = $entityManagerInterface;
$this->security = $security;
}
/**
* @Route("/", name="index")
*/
public function index(): Response
{
return $this->redirectToRoute("assure_dashboard");
}
/**
* @Route("/login", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('assure_dashboard');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('assure/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout(): void
{
}
/**
* @Route("/register", name="register")
*/
public function register(Request $request, UserPasswordHasherInterface $passwordEncoder): Response
{
$assure = new Assure();
$form = $this->createForm(AssureType::class, $assure);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$assure->setPassword($passwordEncoder->hashPassword($assure, $assure->getPassword()));
$this->em->persist($assure);
$this->em->flush();
// TODO send notification to Assurboat and send confirmation to user
return $this->redirectToRoute('assure_login');
}
return $this->render('assure/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
/**
* @Route("/informations-personnelles", name="info")
*/
public function infos(Request $request, EntityManagerInterface $em, UserPasswordHasherInterface $userPasswordHasher): Response
{
$user = $this->security->getUser();
$form = $this->createForm(AssureUpdateType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$assure = $form->getData();
if ($assure->getNewPassword() && $assure->getNewPassword() !== '') {
$assure->setPassword(
$userPasswordHasher->hashPassword(
$assure,
$assure->getNewPassword()
)
);
}
$em->persist($user);
$em->flush();
$this->addFlash('success', 'Vos informations ont bien été enregistrés');
}
return $this->render('assure/profil.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/dashboard", name="dashboard")
*/
public function dashboard(DocumentRepository $documentRepository): Response
{
$user = $this->security->getUser();
$lastDocuments = $documentRepository->findBy(['client' => $user], ['date' => 'DESC'], 5);
return $this->render('assure/dashboard.html.twig', [
'lastDocuments' => $lastDocuments,
]);
}
/**
* @Route("/documents/{page}", name="documents", requirements={"page"="\d+"}, defaults={"page": 1})
*/
public function documents(DocumentRepository $documentRepository, $page): Response
{
$user = $this->security->getUser();
$documents = new Pagerfanta(new QueryAdapter($documentRepository->createQueryBuilder('e')->andWhere('e.client = :user')->setParameter('user', $user)));
$documents->setMaxPerPage(20);
$documents->setCurrentPage($page);
return $this->render('assure/documents.html.twig', [
'documents' => $documents,
]);
}
/**
* @Route("/declarer-un-sinistre", name="sinistre")
*/
public function sinister(Request $request, EntityManagerInterface $em, SinisterFormFlow $flow, EmailManager $mailer): Response
{
$user = $this->security->getUser();
$sinister = new Sinister();
$sinister->setUser($user)
->setName($user->getName())
->setEmail($user->getEmail())
->setFirstname($user->getFirstname())
->setClientCode($user->getClientCode())
->setContractNumber($user->getContractNumber());
if (!empty($user->getAdress())) {
$sinister->setAdress($user->getAdress());
}
if (!empty($user->getPostalCode())) {
$sinister->setPostalCode($user->getPostalCode());
}
if (!empty($user->getCity())) {
$sinister->setCity($user->getCity());
}
if (!empty($user->getMobilePhone())) {
$sinister->setMobilePhone($user->getMobilePhone());
}
if (!empty($user->getFixedPhone())) {
$sinister->setFixedPhone($user->getFixedPhone());
}
$flow->bind($sinister);
$form = $flow->createForm();
// $form = $this->createForm(SinisterFormType::class, $sinister);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->has('documents')) {
foreach ($form->get('documents')->getData() as $base64Image) {
$documentFile = new UploadedBase64File($base64Image["base64"], $base64Image["filename"]);
$document = new SinisterDocument();
$document->setDocumentFile($documentFile);
$sinister->addDocument($document);
}
}
$flow->saveCurrentStepData($form);
if ($flow->nextStep()) {
// form for the next step
$form = $flow->createForm();
} else {
$em->persist($sinister);
$em->flush();
$flow->reset();
$this->addFlash('success', 'Votre sinistre a bien été enregistré, nous vous contacterons dans les plus brefs délais.');
$mailer->sendSinisterMailAdmin($sinister);
$mailer->sendSinisterMailClient($sinister);
return $this->redirectToRoute("assure_dashboard");
}
}
return $this->render('assure/Sinister/create.html.twig', [
'form' => $form->createView(),
'flow' => $flow
]);
}
}