<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\compteType;
use App\Form\coordonneeType;
use App\Form\optinType;
use Doctrine\ORM\EntityManagerInterface;
use App\Form\ChangePasswordType;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\commande;
use App\Entity\Language;
use App\Entity\Residence;
use App\Entity\Typologie;
use App\Entity\InfosResidence;
use App\Services\iresaService;
use App\Utils\Residences;
/**
* @Route("/{_locale}/mon-compte", requirements={"_locale": "en|fr"})
*/
class CompteController extends AbstractController
{
/**
* @var EntityManager
*/
private $em;
private $iresaService;
private $translator;
private $residenceService;
private $passwordEncoder;
public function __construct(EntityManagerInterface $em, iresaService $iresaService, TranslatorInterface $translator, Residences $residenceService, UserPasswordEncoderInterface $passwordEncoder)
{
$this->em = $em;
$this->iresaService = $iresaService;
$this->translator = $translator;
$this->residenceService = $residenceService;
$this->passwordEncoder = $passwordEncoder;
}
/**
* @Route("/", name="mon_compte")
*/
public function indexAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_CUSTOMER');
return $this->render('/front/Compte/accueil.html.twig');
}
/**
* @Route("/mes-infos", name="mon_compte_infos")
*/
public function accountDetailsAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_CUSTOMER');
$user = $this->getUser();
$form = $this->createForm(coordonneeType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->iresaService->iresaXFT("Create", "Customer", $user);
$this->em->persist($user);
$this->em->flush();
}
return $this->render('/front/Compte/infos.html.twig',[
'form' => $form->createView(),
]);
}
/**
* @Route("/reservations", name="mon_compte_reservations_liste")
*/
public function reservationAction(Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$this->denyAccessUnlessGranted('ROLE_CUSTOMER');
$commandes = $this->em->getRepository(commande::class)->getCurrentStays($this->getUser());
$currentStays = $this->parseOrder($commandes, $lang);
$commandes = $this->em->getRepository(commande::class)->findBy(['compte' => $this->getUser(), 'etat' => [1, 4, 6]], ['datecrea'=> 'DESC']);
$archives = $this->parseOrder($commandes, $lang);
return $this->render('/front/Compte/sejours.html.twig', [
'currentStays' => $currentStays,
'archives' => $archives
]);
}
/**
* @Route("/reservations/{id_sejour}", name="mon_compte_reservation_detail")
*/
public function sejourAction(Request $request, $id_sejour)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$this->residenceService->setLocale($locale);
$idiresa = str_replace("-","/", $id_sejour);
$vars["idiresa"] = $idiresa;
$vars["iresaService"] = $this->iresaService;
$obj = new \stdClass;
$obj->idAvailability = $idiresa;
$obj->debug = ['User-agent' => $request->headers->get('User-Agent'), "script" => "CompteController:sejourAction:340"];
$iresa = $this->iresaService->iresaXFT("Get", "Booking", $obj);
if(isset($iresa->Booking)) {
$vars["booking"] = $iresa->Booking;
}
$fiche = [];
$commande = $this->em->getRepository(commande::class)->findOneByDossiersIresa($idiresa);
$vars["commande"] = $commande;
if($commande) {
$fiche = $this->residenceService->getFicheHebergement($commande->getDetail());
}
$vars = array_merge($vars, $fiche);
return $this->render('/front/Compte/sejour.html.twig', $vars);
}
/**
* @Route("/reintitialiser-mot-de-passe", name="mon_compte_changer_motdepasse")
*/
public function changePasswordAction(Request $request)
{
$session = $request->getSession();
$this->denyAccessUnlessGranted('ROLE_CUSTOMER');
$user = $this->getUser();
$form = $this->createForm(ChangePasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$oldPassword = $form->getData()['oldPassword'];
if( password_verify($oldPassword, $user->getPassword()) == false ) {
$title="";
$message = $this->translator->trans("compte.connexion.reset_password.wrong_old_password" , [], 'app');
$session->getFlashBag()->add('warning', ['type' => 'warning', 'title' => $title, 'message' => $message]);
return $this->redirectToRoute('mon_compte_changer_motdepasse');
}
$password = $this->passwordEncoder->encodePassword($user, $form->getData()['plainPassword']);
$user->setPassword($password);
$this->em->persist($user);
$this->em->flush();
$title="";
$message = $this->translator->trans("compte.connexion.reset_password.change_success" , [], 'app');
$session->getFlashBag()->add('success', ['type' => 'success', 'title' => $title, 'message' => $message]);
return $this->redirectToRoute('mon_compte');
}
return $this->render('/security/change_password.html.twig',[
'form' => $form->createView(),
]);
}
/**
* @Route("/newsletter", name="mon_compte_newsletter")
*/
public function newsletterAction(Request $request)
{
$session = $request->getSession();
$this->denyAccessUnlessGranted('ROLE_CUSTOMER');
$user = $this->getUser();
$form = $this->createForm(optinType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$optin = $form->getData()->getOptin();
$user->setOptin($optin);
$this->em->persist($user);
$this->em->flush();
$message = $optin == 1 ? 'footer.social.newsletter.inscription_ok' :'footer.social.newsletter.desinscription';
$message = $this->translator->trans($message , [], 'app');
$title = "";
$session->getFlashBag()->add('success', ['type' => 'success', 'title' => $title, 'message' => $message]);
return $this->redirectToRoute('mon_compte');
}
return $this->render('/front/Compte/newsletter.html.twig',[
'form' => $form->createView(),
]);
}
public function logoutAction(Request $request)
{
$session = $request->getSession();
$session->remove("compte");
return $this->redirect($this->generateUrl('ac_platform_homepage'));
}
private function parseOrder($commandes, $lang) {
$orders = [];
foreach ($commandes as $commande) {
$resas = json_decode($commande->getDetail(), true);
$formule = null;
$residence = null;
$typologie = null;
if($resas !== null) {
$idPackage = array_key_first($resas['reservation']);
$resa = $resas['reservation'][$idPackage];
$residence = $this->em->getRepository(Residence::class)->find($resas['residence']);
$typologie = $this->em->getRepository(Typologie::class)->findOneBy([
'lang' => $lang,
'idiresa' => $resas['typologie']
]);
if ($resas['formule']['className'] != "unknown") {
$formule = $this->em->getRepository("App\\Entity\\{$resas['formule']['className']}")->findOneBy([
'lang' => $lang,
'id' => $resas['formule']['idResidence'],
'idIresa' => $resas['formule']['idIresa']
]);
}
$orders[] = [
"dossier_iresa" => $commande->getDossiersIresa(),
"montant" => $commande->getMontant(),
"resa" => $resa,
"residence" => $residence,
"typologie" => $typologie,
"formule" => $formule
];
}
}
return $orders;
}
}