src/Controller/EspaceAssureController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Assure;
  4. use App\Entity\DevisDocument;
  5. use App\Entity\Sinister;
  6. use App\Entity\SinisterDocument;
  7. use App\Form\AssureType;
  8. use App\Form\AssureUpdateType;
  9. use App\Form\Flow\SinisterFormFlow;
  10. use App\Form\SinisterFormType;
  11. use App\Repository\DocumentRepository;
  12. use App\Service\EmailManager;
  13. use App\Utils\UploadedBase64File;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Pagerfanta\Doctrine\ORM\QueryAdapter;
  16. use Pagerfanta\Pagerfanta;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  22. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  25. /**
  26.  * Class EspaceAssureController
  27.  * @package App\Controller
  28.  *
  29.  * @Route("/espace-assure", name="assure_")
  30.  *
  31.  */
  32. class EspaceAssureController extends AbstractController
  33. {
  34.     private $em;
  35.     private $security;
  36.     public function __construct(EntityManagerInterface $entityManagerInterfaceSecurity $security)
  37.     {
  38.         $this->em $entityManagerInterface;
  39.         $this->security $security;
  40.     }
  41.     /**
  42.      * @Route("/", name="index")
  43.      */
  44.     public function index(): Response
  45.     {
  46.         return $this->redirectToRoute("assure_dashboard");
  47.     }
  48.     /**
  49.      * @Route("/login", name="login")
  50.      */
  51.     public function login(AuthenticationUtils $authenticationUtils): Response
  52.     {
  53.         if ($this->getUser()) {
  54.             return $this->redirectToRoute('assure_dashboard');
  55.         }
  56.         // get the login error if there is one
  57.         $error $authenticationUtils->getLastAuthenticationError();
  58.         // last username entered by the user
  59.         $lastUsername $authenticationUtils->getLastUsername();
  60.         return $this->render('assure/login.html.twig', [
  61.             'last_username' => $lastUsername,
  62.             'error' => $error
  63.         ]);
  64.     }
  65.     /**
  66.      * @Route("/logout", name="logout")
  67.      */
  68.     public function logout(): void
  69.     {
  70.     }
  71.     /**
  72.      * @Route("/register", name="register")
  73.      */
  74.     public function register(Request $requestUserPasswordHasherInterface $passwordEncoder): Response
  75.     {
  76.         $assure = new Assure();
  77.         $form $this->createForm(AssureType::class, $assure);
  78.         $form->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $assure->setPassword($passwordEncoder->hashPassword($assure$assure->getPassword()));
  81.             $this->em->persist($assure);
  82.             $this->em->flush();
  83.             // TODO send notification to Assurboat and send confirmation to user
  84.             return $this->redirectToRoute('assure_login');
  85.         }
  86.         return $this->render('assure/register.html.twig', [
  87.             'registrationForm' => $form->createView(),
  88.         ]);
  89.     }
  90.     /**
  91.      * @Route("/informations-personnelles", name="info")
  92.      */
  93.     public function infos(Request $requestEntityManagerInterface $emUserPasswordHasherInterface $userPasswordHasher): Response
  94.     {
  95.         $user $this->security->getUser();
  96.         $form $this->createForm(AssureUpdateType::class, $user);
  97.         $form->handleRequest($request);
  98.         if ($form->isSubmitted() && $form->isValid()) {
  99.             $assure $form->getData();
  100.             if ($assure->getNewPassword() && $assure->getNewPassword() !== '') {
  101.                 $assure->setPassword(
  102.                     $userPasswordHasher->hashPassword(
  103.                         $assure,
  104.                         $assure->getNewPassword()
  105.                     )
  106.                 );
  107.             }
  108.             $em->persist($user);
  109.             $em->flush();
  110.             $this->addFlash('success''Vos informations ont bien été enregistrés');
  111.         }
  112.         return $this->render('assure/profil.html.twig', [
  113.             'form' => $form->createView(),
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("/dashboard", name="dashboard")
  118.      */
  119.     public function dashboard(DocumentRepository $documentRepository): Response
  120.     {
  121.         $user $this->security->getUser();
  122.         $lastDocuments $documentRepository->findBy(['client' => $user], ['date' => 'DESC'], 5);
  123.         return $this->render('assure/dashboard.html.twig', [
  124.             'lastDocuments' => $lastDocuments,
  125.         ]);
  126.     }
  127.     /**
  128.      * @Route("/documents/{page}", name="documents", requirements={"page"="\d+"}, defaults={"page": 1})
  129.      */
  130.     public function documents(DocumentRepository $documentRepository$page): Response
  131.     {
  132.         $user $this->security->getUser();
  133.         $documents = new Pagerfanta(new QueryAdapter($documentRepository->createQueryBuilder('e')->andWhere('e.client = :user')->setParameter('user'$user)));
  134.         $documents->setMaxPerPage(20);
  135.         $documents->setCurrentPage($page);
  136.         return $this->render('assure/documents.html.twig', [
  137.             'documents' => $documents,
  138.         ]);
  139.     }
  140.     /**
  141.      * @Route("/declarer-un-sinistre", name="sinistre")
  142.      */
  143.     public function sinister(Request $requestEntityManagerInterface $emSinisterFormFlow $flowEmailManager $mailer): Response
  144.     {
  145.         $user $this->security->getUser();
  146.         $sinister = new Sinister();
  147.         $sinister->setUser($user)
  148.             ->setName($user->getName())
  149.             ->setEmail($user->getEmail())
  150.             ->setFirstname($user->getFirstname())
  151.             ->setClientCode($user->getClientCode())
  152.             ->setContractNumber($user->getContractNumber());
  153.         if (!empty($user->getAdress())) {
  154.             $sinister->setAdress($user->getAdress());
  155.         }
  156.         if (!empty($user->getPostalCode())) {
  157.             $sinister->setPostalCode($user->getPostalCode());
  158.         }
  159.         if (!empty($user->getCity())) {
  160.             $sinister->setCity($user->getCity());
  161.         }
  162.         if (!empty($user->getMobilePhone())) {
  163.             $sinister->setMobilePhone($user->getMobilePhone());
  164.         }
  165.         if (!empty($user->getFixedPhone())) {
  166.             $sinister->setFixedPhone($user->getFixedPhone());
  167.         }
  168.         $flow->bind($sinister);
  169.         $form $flow->createForm();
  170. //        $form = $this->createForm(SinisterFormType::class, $sinister);
  171.         $form->handleRequest($request);
  172.         if ($form->isSubmitted() && $form->isValid()) {
  173.             if ($form->has('documents')) {
  174.                 foreach ($form->get('documents')->getData() as $base64Image) {
  175.                     $documentFile = new UploadedBase64File($base64Image["base64"], $base64Image["filename"]);
  176.                     $document = new SinisterDocument();
  177.                     $document->setDocumentFile($documentFile);
  178.                     $sinister->addDocument($document);
  179.                 }
  180.             }
  181.             $flow->saveCurrentStepData($form);
  182.             if ($flow->nextStep()) {
  183.                 // form for the next step
  184.                 $form $flow->createForm();
  185.             } else {
  186.                 $em->persist($sinister);
  187.                 $em->flush();
  188.                 $flow->reset();
  189.                 $this->addFlash('success''Votre sinistre a bien été enregistré, nous vous contacterons dans les plus brefs délais.');
  190.                 $mailer->sendSinisterMailAdmin($sinister);
  191.                 $mailer->sendSinisterMailClient($sinister);
  192.                 return $this->redirectToRoute("assure_dashboard");
  193.             }
  194.         }
  195.         return $this->render('assure/Sinister/create.html.twig', [
  196.             'form' => $form->createView(),
  197.             'flow' => $flow
  198.         ]);
  199.     }
  200. }