src/Repository/OfferRepository.php line 110

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. class OfferRepository
  6. {
  7.   private $pr;
  8.   private $dir;
  9.   private $dir_empty;
  10.   public function __constructPropertyRepository $prParameterBagInterface $params)
  11.   {
  12.     $this->pr $pr;
  13.     $this->dir $params->get('images_relative_directory');
  14.     $this->dir_empty $params->get('no_images_directory');
  15.   }
  16.   public function dataMapper(Array $properties$isRent$details=false) : Array
  17.   {
  18.     $data = [];
  19.     foreach ($properties as $property) {
  20.       $property is_array($property) ? $property[0] :  $property;
  21.       $ligne = [];
  22.       $ligne['id'] = $property->getId();
  23.       $ligne['status'] = $property->getStatus();
  24.       $ligne['ref'] = $property->getReference();
  25.       $ligne['title'] = $property->getName();
  26.       $ligne['description'] = $property->getAdresse();
  27.       $ligne['city'] =  $property->getCityFr() ?? "";
  28.       $ligne['surface'] = $property->getSurface();
  29.       $ligne['address'] = $property->getAdresse();
  30.       // rent Address
  31.       $rentAddress $this->formatAddress($property$isRent);
  32.       $ligne['rentAddress'] = $rentAddress;
  33.        // buy Address
  34.       $buyAddress $this->formatAddress($property$isRent);
  35.       $ligne['buyAddress'] = $buyAddress;
  36.       $ligne['price'] = $property->getPrice();
  37.       $ligne['displayAddress'] = true;
  38.       $ligne['coordinates'] = [
  39.         'lat' => (float)$property->getLatitude(),
  40.         'lng' => (float)$property->getLongitude()
  41.       ];
  42.       if (count($property->getVideosObject())>0) {
  43.           $ligne['videoUrl'] = $property->getVideosObject()[0]['url'];
  44.       }
  45.       if (count($property->getVisit3dsObject())>0) {
  46.           $ligne['link360'] = $property->getVisit3dsObject()[0]['url'];
  47.       }
  48.       $ligne['isForRent'] = $isRent;
  49.       $ligne['type'] = $property->getType() ? $property->getType()->getTitle() : '';
  50.       $ligne['iconOnMap'] = 'office';
  51.       $ligne['url'] = $property->getUrl();
  52.       if(!empty($property->getPhotos()->toArray())){
  53.         foreach ($property->getPhotos() as $photo) {
  54.           $ligne['images'][] = $this->dir.'/'.$photo->getPath();
  55.         }
  56.       }else {
  57.         $ligne['images'][] = $this->dir_empty.'/no-photo-available.png';
  58.       }
  59.       if ($details){
  60.         // $fullDescription = preg_replace('/^[ \t]+/m', '', $property->getDescription());
  61.         // $fullDescription = str_replace(["\t", "\n"], "", $fullDescription);
  62.         // $fullDescription = trim($fullDescription);
  63.         // $fullDescription = preg_replace("/\.\s*/", ".\n", $fullDescription);
  64.         $fullDescription nl2br(preg_replace('/^[ \t]+/m'''$property->getDescription()));
  65.         $ligne['fullDescription'] = preg_replace('/<br\s*\/?>\s*(<br\s*\/?>)+/''<br>'$fullDescription);
  66.         $ligne['amenities'] = $resultAme = [];
  67.         // $latOffset = 0;
  68.         // $lngOffset = 0;
  69.         // foreach ($property->getDistances() as $distance) {
  70.         //   // Convert meters to degrees
  71.         //   $latOffset = ($distance->getDist() / 111320) * (rand(-100, 100) / 100); 
  72.         //   $lngOffset = ($distance->getDist() / (111320 * cos(deg2rad($property->getLatitude())))) * (rand(-100, 100) / 100);
  73.         //   $resultAme[] = [
  74.         //     'type' => $distance->getTitle(),
  75.         //     'coordinates' => [
  76.         //       'lat' => floatval($property->getLatitude()) + $latOffset,
  77.         //       'lng' => floatval($property->getLongitude()) + $lngOffset,
  78.         //     ],
  79.         //     'distance' => $distance->getDist() . $distance->getUnite(),
  80.         //     'title' => $distance->getTitle(),
  81.         //   ];
  82.         // }
  83.         // $ligne['amenities'] = $resultAme;
  84.         if( $property->getBroker()){
  85.           $ligne['contactPersonVisits'] = [
  86.             'name' => $property->getBroker()->getFirstName().' '.$property->getBroker()->getLastName(),
  87.             'phone' => $property->getBroker()->getPhone(),
  88.             'email' => $property->getBroker()->getEmail(),
  89.           ];
  90.         }
  91.         if ($property->getDisponibiliteDate()) {
  92.             $ligne['availability'] = $property->getDisponibiliteDate()->format('Y-m-d');
  93.         }else{
  94.           $ligne['availability'] = $property->getDisponibleFr() ?? '';
  95.         }
  96.       }
  97.       $data[] = $ligne;
  98.     }
  99.     return $data;
  100.   }
  101.   public function search($request$id$rent true$similaire=false)
  102.   {
  103.     $transactionType $rent 2;
  104.     if (!$similaire) {
  105.       $properties $this->pr->search($request$transactionType);
  106.     } else {
  107.       $properties $this->pr->searchSimilaire($id$transactionType);
  108.     }
  109.     return $this->dataMapper($properties$rent);
  110.   }
  111.   public function findByEncodedTitle($encodedTitle$rent=true)
  112.   {
  113.     $pos strripos($encodedTitle'-');
  114.     $id = (int) substr($encodedTitle$pos 1strlen($encodedTitle) - $pos);
  115.     $property $this->pr->findBy(['id'=>$id]);
  116.     $data $this->dataMapper($property$renttrue);
  117.     if (count($data) === 0) {
  118.       return null;
  119.     }
  120.     return array_shift($data);
  121.   }
  122.   public function formatAddress($property$isRent) {
  123.       // Récupération des valeurs pour le pays, le canton, la ville et l'adresse
  124.       $country $property->getCountryFr() ?? "";
  125.       $canton $property->getCantonFr() ?? "";
  126.       $city $property->getCityFr() ?? "";
  127.       $address $property->getAdresse() ?? ""// Si l'adresse est vide, retourne une chaîne vide
  128.       // Exclusion du pays si c'est "Suisse"
  129.       if ($country === "Suisse") {
  130.           $country "";
  131.       }
  132.       // Si la propriété est louée (isRent est vrai), inclure l'adresse
  133.       if ($isRent) {
  134.           $elements array_filter([$country$canton$city$address]);
  135.       } else {
  136.           // Sinon, exclure l'adresse
  137.           $elements array_filter([$country$canton$city]);
  138.       }
  139.       // Retourner la chaîne formatée avec des virgules
  140.       return implode(", "$elements);
  141.   }
  142. }