<?php
namespace App\Repository;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
class OfferRepository
{
private $pr;
private $dir;
private $dir_empty;
public function __construct( PropertyRepository $pr, ParameterBagInterface $params)
{
$this->pr = $pr;
$this->dir = $params->get('images_relative_directory');
$this->dir_empty = $params->get('no_images_directory');
}
public function dataMapper(Array $properties, $isRent, $details=false) : Array
{
$data = [];
foreach ($properties as $property) {
$property = is_array($property) ? $property[0] : $property;
$ligne = [];
$ligne['id'] = $property->getId();
$ligne['status'] = $property->getStatus();
$ligne['ref'] = $property->getReference();
$ligne['title'] = $property->getName();
$ligne['description'] = $property->getAdresse();
$ligne['city'] = $property->getCityFr() ?? "";
$ligne['surface'] = $property->getSurface();
$ligne['address'] = $property->getAdresse();
// rent Address
$rentAddress = $this->formatAddress($property, $isRent);
$ligne['rentAddress'] = $rentAddress;
// buy Address
$buyAddress = $this->formatAddress($property, $isRent);
$ligne['buyAddress'] = $buyAddress;
$ligne['price'] = $property->getPrice();
$ligne['displayAddress'] = true;
$ligne['coordinates'] = [
'lat' => (float)$property->getLatitude(),
'lng' => (float)$property->getLongitude()
];
if (count($property->getVideosObject())>0) {
$ligne['videoUrl'] = $property->getVideosObject()[0]['url'];
}
if (count($property->getVisit3dsObject())>0) {
$ligne['link360'] = $property->getVisit3dsObject()[0]['url'];
}
$ligne['isForRent'] = $isRent;
$ligne['type'] = $property->getType() ? $property->getType()->getTitle() : '';
$ligne['iconOnMap'] = 'office';
$ligne['url'] = $property->getUrl();
if(!empty($property->getPhotos()->toArray())){
foreach ($property->getPhotos() as $photo) {
$ligne['images'][] = $this->dir.'/'.$photo->getPath();
}
}else {
$ligne['images'][] = $this->dir_empty.'/no-photo-available.png';
}
if ($details){
// $fullDescription = preg_replace('/^[ \t]+/m', '', $property->getDescription());
// $fullDescription = str_replace(["\t", "\n"], "", $fullDescription);
// $fullDescription = trim($fullDescription);
// $fullDescription = preg_replace("/\.\s*/", ".\n", $fullDescription);
$fullDescription = nl2br(preg_replace('/^[ \t]+/m', '', $property->getDescription()));
$ligne['fullDescription'] = preg_replace('/<br\s*\/?>\s*(<br\s*\/?>)+/', '<br>', $fullDescription);
$ligne['amenities'] = $resultAme = [];
// $latOffset = 0;
// $lngOffset = 0;
// foreach ($property->getDistances() as $distance) {
// // Convert meters to degrees
// $latOffset = ($distance->getDist() / 111320) * (rand(-100, 100) / 100);
// $lngOffset = ($distance->getDist() / (111320 * cos(deg2rad($property->getLatitude())))) * (rand(-100, 100) / 100);
// $resultAme[] = [
// 'type' => $distance->getTitle(),
// 'coordinates' => [
// 'lat' => floatval($property->getLatitude()) + $latOffset,
// 'lng' => floatval($property->getLongitude()) + $lngOffset,
// ],
// 'distance' => $distance->getDist() . $distance->getUnite(),
// 'title' => $distance->getTitle(),
// ];
// }
// $ligne['amenities'] = $resultAme;
if( $property->getBroker()){
$ligne['contactPersonVisits'] = [
'name' => $property->getBroker()->getFirstName().' '.$property->getBroker()->getLastName(),
'phone' => $property->getBroker()->getPhone(),
'email' => $property->getBroker()->getEmail(),
];
}
if ($property->getDisponibiliteDate()) {
$ligne['availability'] = $property->getDisponibiliteDate()->format('Y-m-d');
}else{
$ligne['availability'] = $property->getDisponibleFr() ?? '';
}
}
$data[] = $ligne;
}
return $data;
}
public function search($request, $id, $rent = true, $similaire=false)
{
$transactionType = $rent ? 1 : 2;
if (!$similaire) {
$properties = $this->pr->search($request, $transactionType);
} else {
$properties = $this->pr->searchSimilaire($id, $transactionType);
}
return $this->dataMapper($properties, $rent);
}
public function findByEncodedTitle($encodedTitle, $rent=true)
{
$pos = strripos($encodedTitle, '-');
$id = (int) substr($encodedTitle, $pos + 1, strlen($encodedTitle) - $pos);
$property = $this->pr->findBy(['id'=>$id]);
$data = $this->dataMapper($property, $rent, true);
if (count($data) === 0) {
return null;
}
return array_shift($data);
}
public function formatAddress($property, $isRent) {
// Récupération des valeurs pour le pays, le canton, la ville et l'adresse
$country = $property->getCountryFr() ?? "";
$canton = $property->getCantonFr() ?? "";
$city = $property->getCityFr() ?? "";
$address = $property->getAdresse() ?? ""; // Si l'adresse est vide, retourne une chaîne vide
// Exclusion du pays si c'est "Suisse"
if ($country === "Suisse") {
$country = "";
}
// Si la propriété est louée (isRent est vrai), inclure l'adresse
if ($isRent) {
$elements = array_filter([$country, $canton, $city, $address]);
} else {
// Sinon, exclure l'adresse
$elements = array_filter([$country, $canton, $city]);
}
// Retourner la chaîne formatée avec des virgules
return implode(", ", $elements);
}
}