<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Annonces;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Visiteurs;
use App\Entity\User;
use App\Entity\Images;
use App\Entity\VillesFrance;
use App\Entity\Photoindex;
use App\Entity\Event;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Messages;
use App\Form\MessagesType;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use App\Entity\PostLike;
use App\Entity\Financement;
use App\Controller\Fonctions\Fonctions;
use Doctrine\ORM\Query\Expr\Join;
class LogementController extends AbstractController
{
#[Route('/logement/{slug}', name: 'app_logement')]
public function index(Request $request, ManagerRegistry $doctrine, Annonces $annonce, MailerInterface $mailer, $slug): Response
{
$fonction = new Fonctions();
$userid = $this->getUser();
$entityManager = $doctrine->getManager();
// recuperation de 'id de l'annonce
$messageform = 0;
$userRepo = $entityManager->getRepository(Annonces::class);
$id = $userRepo->findOneBy(["slug" => $slug]);
$iduser = $id->getUser();
$etat = $id->getEtat();
$domaine = $id->getDomaine();
$agence = 1;
$codepostale = $id->getCodepostal();
$idagence = 1;
$sejour = 0;
$logosite = $fonction->FonctionLogo();
// recuperation des date den poste
$datedebut = $request->query->get('t-start');
$datefin = $request->query->get('t-end');
// Condition pour detection des dates.
if (($datedebut == true) and ($datefin == true)) {
$post = '?check_in=' . $datedebut . '&check_out=' . $datefin . '';
$datedebutteste = $fonction->Compardates($datedebut);
$datefinteste = $fonction->Compardates($datefin);
// condition pour verivier si les variable dates correspond à une date.
if (($datedebutteste == 1) and ($datefinteste == 1)) {
$queryBuilder = $entityManager->createQueryBuilder();
$datedebut = $fonction->Convertiondate($datedebut);
$datefin = $fonction->Convertiondate($datefin);
$queryBuilder->select('u')
->from(Event::class, 'u')
->where('u.annonce IN (:annonce)')
->andwhere('u.datedebut <= :datedebut')
->andwhere('u.datefin >= :datefin')
->andwhere('u.etat = :etat')
->setParameter('annonce', [$id])
->setParameter('datedebut', [$datedebut])
->setParameter('datefin', [$datefin])
->setParameter('etat', [0]);
$query = $queryBuilder->getQuery();
$donnees = $query->getResult();
$nbr = 0;
foreach ($donnees as $user) {
$nbr++;
}
$sejour = $nbr;
}
} else {
$post = '';
}
// fonction pour envoyer l'id du journal ou de l'utilisateur
$userRepo = $entityManager->getRepository(User::class);
$agence = $userRepo->find($agence);
$like = 0;
$presentation = 0;
$url = $fonction->FonctionUrl();
$usertab = [];
$proposition = '';
$proposition = '';
$poste = '';
$ip = $fonction->NomIp();
$defaut = $etat;
// condition pour que l'auteur de l'annonce puisse voir son annonce même si elle n'est pas validé ou elle est plus en cour.
if ($iduser == $userid) {
$defaut = 1;
}
if ($etat == 3) {
$messageerreur = "L'annonce à était supprimer";
}
if ($etat == 0) {
$messageerreur = "L'annonce n'est pas encore validé.";
}
if ($defaut == 1) {
if ($userid == true) {
// recuperation de l'email.
$sent = $userid->getEmail();
$userRepo = $entityManager->getRepository(User::class);
$user = $userRepo->find($userid);
// déterminer le like
$userRepo = $entityManager->getRepository(PostLike::class);
$like = $userRepo->findOneBy(["annonce" => $id, "user" => $userid]);
if ($like == true) {
$like = 1;
}
// enregistrer les visite
$userRepo = $entityManager->getRepository(Annonces::class);
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
->from(Visiteurs::class, 'u')
->where('u.annonce IN (:annonce)')
->orwhere('u.user IN (:user)')
->orWhere('u.adresseip >= :adresseip')
->setParameter('user', [$user])
->setParameter('annonce', [$id])
->setParameter('adresseip', [$ip]);
$query = $queryBuilder->getQuery();
$userRepo = $entityManager->getRepository(Visiteurs::class);
$visite = $userRepo->findOneBy(["annonce" => $id, "user" => $user]);
// insertion de la visite
if ($visite == false) {
$visite = new Visiteurs();
$visite->setUser($user);
$visite->setAnnonce($id);
$visite->setAdresseip($ip);
$entityManager->persist($visite);
$entityManager->flush();
}
// recherche des messages
$userRepo = $entityManager->getRepository(Messages::class);
$messagenbr = $userRepo->findBy(["annonce" => $id, "sender" => $user]);
$teste = 0;
foreach ($messagenbr as $user) {
$teste++;
}
if ($user == $iduser) {
$messageform = 3;
} else {
if ($teste > 1) {
$messageform = 2;
}
if ($teste == 0) {
$messageform = 1;
}
}
// condition pour valider l'annonce
if ($request->isMethod('post')) {
$userRepo = $entityManager->getRepository(Annonces::class);
// Modification deu crédit
$user = $userRepo->find($id);
$user->setEtat($etat);
$entityManager->flush();
$userRepo = $entityManager->getRepository(Annonces::class);
if ($etat == 0) {
$nouvetat = 1;
} else {
$nouvetat = 0;
}
// Modification deu crédit
$user = $userRepo->find($id);
$user->setEtat($nouvetat);
$entityManager->flush();
}
// condition pour faire apparetre le message ou validation
// création de la gallerie image
}
// mettre l'id de l'annonce
$userRepo = $entityManager->getRepository(Annonces::class);
$userid = $userRepo->findOneBy(['slug' => $slug]);
$id = $userid->getId($slug);
$recipient = $userid->getUser($slug);
$titre = $userid->getTitre($slug);
$slug = $userid->getSlug($slug);
$villeslug = $userid->getVilleSlug($slug);
$somme = $userid->getPrix($slug);
// mettre l'id de l'annonce
$user = $userRepo->find($id);
// mettre l'id du recepteur
$userRepo = $entityManager->getRepository(User::class);
$userrecipient = $userRepo->find($recipient);
// mettre le mail d du recepteur
$emailrecipient = $userrecipient->getEmail();
// recuperation du type de l'annonceur
$type = $userrecipient->getType();
// création de la gallerie image
$userRepo = $entityManager->getRepository(Images::class);
$orderBy = ["id" => "ASC"];
$images = $userRepo->findBy(["annonce" => $id], $orderBy);
if ($images == true) {
$presentation = 1;
}
// coordonnes gps
$zoom = 15;
$longitudelogement = $userid->getLongitude($slug);
$latitudelogement = $userid->getLatitude($slug);
$userRepo = $entityManager->getRepository(VillesFrance::class);
$VilleFrance = $userRepo->findOneBy(["ville_slug" => $villeslug]);
$longitude = $VilleFrance->getVilleLongitudeDeg($villeslug);
$latitude = $VilleFrance->getVilleLatitudeDeg($villeslug);
$ville = $VilleFrance->getVilleSlug($villeslug);
$ville_densite_2010 = $VilleFrance->getVilleDensite2010($villeslug);
if ($ville_densite_2010 > 1000) {
$around = '500';
$zoom = 16;
}
if ($ville_densite_2010 < 1000) {
$zoom = 14;
$around = '5000';
}
// condition pour créer les adresses
$overpass = 0;
if ($overpass == 1) {
$typen = '2';
$array2 = $fonction->Overpass($latitudelogement, $longitudelogement, $around, $typen);
$typen = '4';
$bus = $fonction->Overpass($latitudelogement, $longitudelogement, $around, $typen);
$typen = '5';
$tram = $fonction->Overpass($latitudelogement, $longitudelogement, $around, $typen);
$typen = '6';
$metro = $fonction->Overpass($latitudelogement, $longitudelogement, $around, $typen);
$type = 'bus';
$around = '100';
$lignesbus = $fonction->Transport($latitudelogement, $longitudelogement, $type, $around);
$type = 'tram';
$around = '500';
$lignestrams = $fonction->Transport($latitudelogement, $longitudelogement, $type, $around);
$type = 'subway';
$around = '5000';
$lignesmetro = $fonction->Transport($latitudelogement, $longitudelogement, $type, $around);
} else {
$array2 = [];
$bus = [];
$tram = [];
$metro = [];
$lignesbus = [];
$lignestrams = [];
$lignesmetro = [];
}
$typen = '1';
$type = 'school';
$array1 = $fonction->Openstreetmap($ville, $type, $typen);
$typen = '3';
$type = 'supermarket';
$array3 = $fonction->Openstreetmap($ville, $type, $typen);
$typen = '6';
$type = 'railway=station';
$array6 = $fonction->Openstreetmap($ville, $type, $typen);
$array4 = array_merge($bus, $tram, $metro);
// var_dump($array4);
$result = array_merge($array1, $array2, $array3, $array4);
$transport = array_merge($lignesbus, $lignestrams, $lignesmetro);
$nbr = 0;
// tableau pour faire le nombrepar categorie
$entie1 = 0;
$entie2 = 0;
$entie3 = 0;
$entie4 = 0;
$logo = 0;
$taillelogo = 0;
$typesensei = 0;
foreach ($result as $customer) {
if ($customer['type'] > 3) {
// tableaiu pour tester les lignes
$lignes = [];
foreach ($transport as $lignebus) {
// var_dump($lignebus);
$distanceligne = $fonction->Distance($lignebus['lat'], $lignebus['lon'], $customer['lat'], $customer['lon']);
$distanceselect = 1;
if ($distanceligne < $distanceselect) {
$lignes[] = array('colour' => $lignebus['colour'], 'network:short' => $lignebus['network:short'], 'network' => $lignebus['network'], 'operator:short' => $lignebus['operator:short'], 'from' => $lignebus['from'], 'name' => $lignebus['name'], 'ligne' => $lignebus['ligne'], 'type' => $lignebus['type'], 'highway' => $customer['highway'], "trolley" => $customer['trolley']);
}
}
}
$nbr++;
$customer = json_decode(json_encode($customer), true);
$distance = $fonction->Distance($customer['lat'], $customer['lon'], $latitudelogement, $longitudelogement);
$distanceselect = 1.5;
if ($distance < $distanceselect) {
if ($customer['typenbr'] == 1) {
$entie1++;
if (isset($customer['typesensei']) == 1) {
$typesensei = $customer['typesensei'];
} else {
$typesensei = 0;
}
}
if ($customer['typenbr'] == 2) {
$entie2++;
}
if ($customer['typenbr'] == 3) {
$entie3++;
}
if ($customer['typenbr'] == 4) {
$entie4++;
$logo = 0;
$taillelogo = 0;
if ($customer['brand'] != 0) {
$userRepoLogo = $entityManager->getRepository(Photoindex::class);
$usersByRolelogo = $userRepoLogo->findBy(["type" => $customer['brand']]);
foreach ($usersByRolelogo as $user) {
$logo = $user->getImages();
$taillelogo = $user->getNbr();
}
}
}
$distance = round($distance, 2); // 1.96
$tabligne[$nbr] = array("type" => $customer['nomfr'], "nom" => $customer['name'], "lat" => $customer['lat'], "lon" => $customer['lon'], "typenbr" => $customer['typenbr'], "nbr" => $nbr, "distance" => $distance, "logo" => $logo, "taillelogo" => $taillelogo, "typesensei" => $typesensei, 'highway' => $customer['highway'], 'lignes' => $lignes, 'amenity' => $customer['amenity']);
}
}
$entite = array('entit1' => $entie1, 'entit2' => $entie2, 'entit3' => $entie3, 'entit4' => $entie4);
$adresse = $tabligne;
$message = new Messages;
$form = $this->createForm(MessagesType::class, $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message->setSender($this->getUser());
$em = $this->getDoctrine()->getManager();
$message->setTitle($titre);
$message->setAnnonce($user);
$message->setAgence($agence);
$message->setRecipient($userrecipient);
// recuperation du message pour le mail
$messageenvoie = $message->getMessage();
$titre = $message->getTitle();
$em->persist($message);
$em->flush();
$email = (new Email())
->from($sent)
->to($emailrecipient)
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject($titre)
->text('Sending emails is fun again!')
->html($messageenvoie);
$mailer->send($email);
$message = "Message envoyé avec succès.";
// message
$session = $request->getSession();
$session->set('message', 1);
$this->addFlash('success', $message);
}
// condition pour faire les proposition de financement
$nbr = 0;
$nbr1 = 0;
$userRepo = $entityManager->getRepository(Financement::class);
$financement = $userRepo->findAll();
$tabparligne = array();
$tabparbanque = array();
foreach ($financement as $user) {
$nbr++;
$taux = $user->getTaux();
$etablissement = $user->getEtablissement();
$pourcent_annuel = 1.22;
$nbr1 = 0;
$duree = 120;
for ($i = 1; $i <= 3; $i++) {
$ans = $duree / 12;
$nbr1++;
$mensualite = $fonction->CalculVPM($duree, $pourcent_annuel, $somme);
$mensualite = round($mensualite, 2);
$tabparligne[$nbr1] = array("mensualite" => $mensualite, "duree" => $ans);
$duree = $duree * 2;
}
$tabparbanque = $tabparligne;
$usertab[$nbr] = array("etablissement" => $etablissement, "mensualite" => $mensualite, "user" => $tabparbanque);
// condition pour les proposition
$recherchelog = 2;
$select = 'u.logement <= :logement';
$orderBytype = 'u.id';
$orderBytypeOrder = 'ASC';
$userRepo = $entityManager->getRepository(Annonces::class);
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
->from(Annonces::class, 'u')
->where('u.codepostale IN (:codepostale)')
->orwhere('u.titre IN (:titre)')
->andWhere($select)
->andWhere('u.prix <= :prix')
->setParameter('codepostale', [$codepostale])
->setParameter('titre', ['essai'])
->setParameter('prix', [$somme])
->setParameter('logement', [$recherchelog])
->orderBy($orderBytype, $orderBytypeOrder)
->setMaxResults(5);
$query = $queryBuilder->getQuery();
$proposition = $query->getResult();
$userRepo = $entityManager->getRepository(PostLike::class);
$usersByRolepost = $userRepo->findBy(["user" => $iduser]);
$poste = $usersByRolepost;
}
}
if ($defaut == 1) {
return $this->render('logement/index.html.twig', [
"form" => $form->createView(),
'annonce' => $annonce,
'logo' => $logosite,
'image' => $images,
'auteur' => $userrecipient,
'messageform' => $messageform,
'like' => $like,
'zoom' => $zoom,
'latitude' => $latitude,
'longitude' => $longitude,
'latitudelogement' => $latitudelogement,
'longitudelogement' => $longitudelogement,
'presentation' => $presentation,
'interet' => $usertab,
'proposition' => $proposition,
'post' => $poste,
'adresse' => $adresse,
'entite' => $entite,
'url' => $url,
'sejour' => $sejour,
'post' => $post,
]);
} else {
return $this->render('logement/defaut.html.twig', [
'message' => $messageerreur,
'logo' => $logosite,
]);
}
}
}