<?php
namespace App\Services;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use App\Services\ToolService;
/**
* traveller Service
*/
class TravellerService extends AbstractController
{
private $tool;
private $em;
private $session;
public $travellers;
public function __construct(ToolService $tool, EntityManagerInterface $em, SessionInterface $session) {
$this->tool = $tool;
$this->em = $em;
$this->session = $session;
$this->travellers = [
'idResidence' => null,
'nbPax' => 1,
'actnbadultes' => 1,
'actnbenfants' => 0,
'actnbbebes' => 0,
'agesadultes' => [ $this->setBirthday()],
'nomsadultes' => [],
'prenomsadultes' => [],
'agesenfants' => [],
'nomsenfants' => [],
'prenomsenfants' => [],
'agesbebes' => [],
'nomsbebes' => [],
'prenomsbebes' => [],
'actdatearrivee' => null,
'actdatedepart' => null,
'actnbnuits' => null,
];
if ($this->session->has('travellers')) {
$travellersFromSession = $this->session->get('travellers');
$this->travellers['idResidence'] = $travellersFromSession['idResidence'];
$this->travellers['nbPax'] = $travellersFromSession['nbPax'];
$this->travellers['actnbadultes'] = $travellersFromSession['actnbadultes'];
$this->travellers['actnbenfants'] = $travellersFromSession['actnbenfants'];
$this->travellers['actnbbebes'] = $travellersFromSession['actnbbebes'];
$this->travellers['agesadultes'] = $travellersFromSession['agesadultes'];
$this->travellers['nomsadultes'] = $travellersFromSession['nomsadultes'];
$this->travellers['prenomsadultes'] = $travellersFromSession['prenomsadultes'];
$this->travellers['agesenfants'] = $travellersFromSession['agesenfants'];
$this->travellers['nomsenfants'] = $travellersFromSession['nomsenfants'];
$this->travellers['prenomsenfants'] =$travellersFromSession['prenomsenfants'];
$this->travellers['agesbebes'] = $travellersFromSession['agesbebes'];
$this->travellers['nomsbebes'] = $travellersFromSession['nomsbebes'];
$this->travellers['prenomsbebes'] =$travellersFromSession['prenomsbebes'];
$this->travellers['actdatearrivee'] = $travellersFromSession['actdatearrivee'];
$this->travellers['actdatedepart'] = $travellersFromSession['actdatedepart'];
$this->travellers['actnbnuits'] = $travellersFromSession['actnbnuits'];
}
$this->popIfNull('agesadultes');
$this->popIfNull('nomsadultes');
$this->popIfNull('prenomsadultes');
$this->popIfNull('agesenfants');
$this->popIfNull('nomsenfants');
$this->popIfNull('prenomsenfants');
$this->popIfNull('agesbebes');
$this->popIfNull('prenomsbebes');
$this->popIfNull('prenomsbebes');
}
protected function popIfNull($field) {
$array = [];
foreach($this->travellers[$field] as $item) {
if(!is_null($item)) {$array[] = $item;}
}
$this->travellers[$field] = $array;
}
public function init() {
$this->travellers = [
'idResidence' => null,
'nbPax' => 1,
'actnbadultes' => 1,
'actnbenfants' => 0,
'actnbbebes' => 0,
'agesadultes' => [ $this->setBirthday()],
'nomsadultes' => [],
'prenomsadultes' => [],
'agesenfants' => [],
'nomsenfants' => [],
'prenomsenfants' => [],
'agesbebes' => [],
'nomsbebes' => [],
'prenomsbebes' => [],
'actdatearrivee' => null,
'actdatedepart' => null,
'actnbnuits' => null,
'test' => [],
];
$this->save();
}
public function save() {
$this->session->set('travellers', $this->travellers);
return $this->travellers;
}
public function setDates($datearrivee, $datedepart) {
$nbnuits = $this->getNbNuits($datearrivee, $datedepart);
$this->travellers['actdatearrivee'] = $datearrivee;
$this->travellers['actdatedepart'] = $datedepart;
$this->travellers['actnbnuits'] = $nbnuits;
$this->save();
return $travellers;
}
public function setDateDuree($datearrivee, $duree) {
// $travellers['actdatearrivee'] = $datearrivee;
// $travellers['actnbnuits'] = $duree;
if (preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/", $datearrivee)) {
// date FR
$datearrivee = $this->tool->dateFrToEn($datearrivee) ;
} elseif (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $datearrivee)) {
// date EN
} else {
// Format unknown
}
if($duree == "") {
$datedepart = null;
} else {
$datedepart = new \DateTime(date('Y-m-d', strtotime($datearrivee)));
$datedepart->modify('+' . (int) $duree . ' days');
$datedepart = $datedepart->format('Y-m-d');
}
$this->travellers['actdatedepart'] = $datedepart;
$this->travellers['actdatearrivee'] = $datearrivee;
$this->travellers['actnbnuits'] = $duree;
return $this->save();
}
public function getTravellers() {
return $this->travellers;
}
public function setTravellers($requestParams) {
if(empty($requestParams)) {
return $this->travellers;
}
if($requestParams->has("nbadultes") ) {
$nbadultes = $requestParams->get("nbadultes");
$tabAgeAdultes = [];
if($nbadultes > 0) {
for($nb = 0; $nb < $nbadultes; $nb++) {
$tabAgeAdultes[] = $this->setBirthday();
}
}
} else {
$nbadultes = $this->travellers['actnbadultes'];
$tabAgeAdultes = $this->travellers['agesadultes'];
}
if($requestParams->has("nbenfants") ) {
$nbenfants = $requestParams->get("nbenfants");
$tabAgeEnfants = [];
if($nbenfants > 0) {
for($nb = 0; $nb < $nbenfants; $nb++) {
$dateEnfant = $requestParams->get("enfant_datenaissance_".($nb+1));
// Crée un objet DateTime à partir de la date au format YYYY-MM-DD
$dateTime = \DateTime::createFromFormat('Y-m-d', $dateEnfant);
if ($dateTime !== false) {
// Si la date est valide, la convertir au format DD/MM/YYYY
$date = $dateTime->format('d/m/Y');
} else {
// La date n'est pas valide ou n'est pas au format YYYY-MM-DD
// Trouver la date de naissance par défaut d'un enfant de 10 ans par rapport à la date du jour
$date = date('d/m/Y', strtotime('-10 years'));
}
$tabAgeEnfants[] = $date;
}
}
} else {
$nbenfants = $this->travellers['actnbenfants'];
$tabAgeEnfants = $this->travellers['agesenfants'];
}
if($requestParams->has("nbbebes") ) {
$nbbebes = $requestParams->get("nbbebes");
$tabAgeBebes = [];
if($nbbebes > 0) {
for($nb = 0; $nb < $nbbebes; $nb++) {
$dateBebe = $requestParams->get("bebe_datenaissance_".($nb+1));
// Crée un objet DateTime à partir de la date au format YYYY-MM-DD
$dateTime = \DateTime::createFromFormat('Y-m-d', $dateBebe);
if ($dateTime !== false) {
// Si la date est valide, la convertir au format DD/MM/YYYY
$date = $dateTime->format('d/m/Y');
} else {
// La date n'est pas valide ou n'est pas au format YYYY-MM-DD
// Trouver la date de naissance par défaut d'un enfant de 2 ans par rapport à la date du jour
$date = date('d/m/Y', strtotime('-2 years'));
}
$tabAgeBebes[] = $date;
}
}
} else {
$nbbebes = $this->travellers['actnbbebes'];
$tabAgeBebes = $this->travellers['agesbebes'];
}
$nbPax = $nbadultes + $nbenfants;
$this->travellers['nbPax'] = $nbPax;
$this->travellers['actnbadultes'] = $nbadultes;
$this->travellers['actnbenfants'] = $nbenfants;
$this->travellers['actnbbebes'] = $nbbebes;
$this->travellers['agesadultes'] = $tabAgeAdultes;
$this->travellers['agesenfants'] = $tabAgeEnfants;
$this->travellers['agesbebes'] = $tabAgeBebes;
return $this->save();
}
private function setBirthday() {
$date = [];
$now = new \DateTime();
$date = $now->modify('-19 years')->format('d/m/Y');
return $date;
}
public function getNbNuits($arrivee, $depart) {
$nuits = 0;
if($arrivee != null && $depart != null) {
$datearrivee = \DateTime::createFromFormat('d/m/Y', $arrivee);
$datedepart = \DateTime::createFromFormat('d/m/Y', $depart);
if ($datearrivee instanceof \DateTime && $datedepart instanceof \DateTime) {
$interval = $datearrivee->diff($datedepart);
$nuits = $interval->format('%a');
}
}
return $nuits;
}
public function removeTravellersFromSession() {
$this->session->remove('travellers');
}
}