<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\PostLike;
use App\Entity\Visiteurs;
use App\Entity\VillesFrance;
use App\Entity\Annonces;
use App\Controller\Fonctions\Fonctions;
class ScrollController extends AbstractController
{
#[Route('/scroll/{id}', name: 'app_scroll')]
public function index(ManagerRegistry $doctrine, Request $request, $id): Response
{
$nbr = 0;
$sommetotale = 0;
$fonction = new Fonctions();
$logo = $fonction->FonctionLogo();
// selection de la recherche type
$selectionrecherche = 1;
if ($selectionrecherche == 1) {
$orderBytype = 'u.created_at';
}
if ($selectionrecherche == 2) {
$orderBytype = 'u.prix';
}
if ($selectionrecherche == 3) {
$orderBytype = 'u.superficie';
}
// selection de la recherche asecendant ou decendant
$orderrecherche = 1;
if ($orderrecherche == 1) {
$orderBytypeOrder = 'ASC';
} else {
$orderBytypeOrder = 'DESC';
}
// appartement et maison
$recc = 1;
if ($recc == 1) {
$recherchelog = 2;
$select = 'u.logement <= :logement';
}
// maison
if ($recc == 2) {
$recherchelog = 1;
$select = 'u.logement IN (:logement)';
}
// appartement
if ($recc == 3) {
$recherchelog = 2;
$select = 'u.logement IN (:logement)';
}
$entityManager = $doctrine->getManager();
$userRepo = $entityManager->getRepository(Annonces::class);
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
->from(Annonces::class, 'u')
->where('u.ville_slug IN (:ville_slug)')
->setParameter('ville_slug', [$id])
->orderBy($orderBytype, $orderBytypeOrder)
->setMaxResults(130);
$query = $queryBuilder->getQuery();
$donnees = $query->getResult();
// boucle pour retourner si li y a une reponse
foreach ($donnees as $user) {
$nbr++;
// calcule de la moyenne du marché
$somme = $user->getPrix();
$sommetotale = $sommetotale + $somme;
$moyenne = $sommetotale / $nbr;
$moyenne = floor($moyenne);
}
if ($nbr == 0) {
$message = 'Aucun résultats correspond à votre recherche.';
} else {
$message = ' ' . $nbr . ' résultats correspond à votre recherche. Le prix moyen est de ' . $moyenne . ' Euros.';
}
$this->addFlash('success', $message);
$type = 1;
$ville = $id;
$articles = '';
$date = '';
return $this->render('scroll/liste.html.twig', [
'annonces' => $articles,
'date' => $date,
'ville' => $ville,
'logo' => $logo,
'type' => $type,
'proposition' => $donnees,
]);
}
#[Route('/getrecords/{id}', name: 'getrecords')]
public function scroll(ManagerRegistry $doctrine, Request $request, $id): Response
{
$fonction = new Fonctions();
$url = $fonction->FonctionUrl();
$ip = $fonction->NomIp();
// fonction pour envoyer l'id du journal ou de l'utilisateur
$user = $this->getUser();
$iduser = $user;
$entityManager = $doctrine->getManager();
$userRepo = $entityManager->getRepository(PostLike::class);
$usersByRolepost = $userRepo->findBy(["user" => $iduser]);
$poste = $usersByRolepost;
// recuperation du tableau des visiteurs
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
->from(Visiteurs::class, 'u')
->where('u.user IN (:user)')
->orwhere('u.adresseip IN (:adresseip)')
->setParameter('user', [$iduser])
->setParameter('adresseip', [$ip]);
$query = $queryBuilder->getQuery();
$visiteurs = $query->getResult();
$connection = $doctrine->getConnection();
$ville = $id;
$userRepo = $entityManager->getRepository(VillesFrance::class);
$VilleFrance = $userRepo->findOneBy(["ville_slug" => $ville]);
$ville_slug = $VilleFrance->getVilleSlug($ville);
// $_GET['limit'] = 0; $_GET['offset'] = 0;
$limit = (intval($_GET['limit']) != 0) ? $_GET['limit'] : 5;
$offset = (intval($_GET['offset']) != 0) ? $_GET['offset'] : 0;
$date = $this->date = new \DateTime('now');
$nbr = 0;
$categorie1 = array();
$userRepo = $entityManager->getRepository(Annonces::class);
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('r')
->from(Annonces::class, 'r')
->Where('r.ville_slug LIKE :ville_slug')
->setParameter('ville_slug', '%' . $ville_slug . '%')
->orderBy('r.ville_slug', 'ASC')
->setMaxResults($limit)
->setFirstResult($offset);
$query = $queryBuilder->getQuery();
$donnees = $query->getResult();
foreach ($donnees as $user) {
$id = $user->getId();
$titre = $user->getTitre();
$somme = $user->getPrix();
$image = $user->getImage();
$slug = $user->getSlug();
$createdAt = $user->getcreatedAt();
$nbr++;
$user1[$nbr] = array("id" => $id, "titre" => $titre, "somme" => $somme, "image" => $image, "slug" => $slug, "createdAt" => $createdAt, "likes" => 12);
$categorie1[$nbr] = $user1[$nbr];
}
return $this->render('scroll/scroll.html.twig', [
"categories" => $categorie1,
'date' => $date,
'ville' => $ville,
'post' => $poste,
'visiteurs' => $visiteurs,
'url' => $url,
]);
}
}