| Current Path : /home/h/a/p/happyrenas/find.myreco.online/v2/includes/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/h/a/p/happyrenas/find.myreco.online/v2/includes/fonctions_hebergements.php |
<?
/************************************************
* đ RECHERCHER LES HĂBERGEMENTS PROCHES D'UNE VILLE
************************************************/
/************************************************
* DEBUG RECHERCHE HĂBERGEMENTS PROCHES
* MĂȘme logique que la prod, mais avec dĂ©tails
************************************************/
function rechercherHebergementsProches($ville_input, $db, $rayon_km,$nombre_voyageurs=0)
{
$debug = [
'erreur' => '',
'ville' => '',
'pays' => '',
'rayon_km' => (int)$rayon_km,
'nombre_voyageurs' => (int)$nombre_voyageurs,
'nb_supprimes_capacite' => 0,
'point_reference' => null,
'delta_lat' => null,
'delta_lon' => null,
'rectangle_sql' => [],
'nb_ville' => 0,
'nb_rectangle' => 0,
'nb_total_avant' => 0,
'nb_total_apres' => 0,
'lignes_debug' => [],
'resultats_finaux' => [],
];
if (empty($ville_input)) {
$debug['erreur'] = "â Veuillez sĂ©lectionner une ville.";
return $debug;
}
$parts = array_map('trim', explode(',', $ville_input));
$ville = trim($parts[0] ?? '');
$postal_code = '';
$pays = '';
if (count($parts) >= 3) {
$postal_code = trim($parts[1] ?? '');
$pays = strtoupper(trim($parts[2] ?? ''));
} else {
$pays = strtoupper(trim($parts[1] ?? ''));
}
$debug['ville'] = $ville;
$debug['pays'] = $pays;
$debug['postal_code'] = $postal_code;
$condition_postal = '';
if ($postal_code !== '') {
$condition_postal = " AND postal_code = '" . $db->escape($postal_code) . "' ";
}
if ($ville === '' || $pays === '') {
$debug['erreur'] = "â Format ville invalide. Utilisez : Ville, FR";
return $debug;
}
// Point de référence
$ville_ref = $db->get_row("
SELECT id, name, city, postal_code, country_code, latitude, longitude
FROM heb
WHERE city = '" . $db->escape($ville) . "'
AND country_code = '" . $db->escape($pays) . "'
{$condition_postal}
AND name != 'lite'
AND latitude IS NOT NULL
AND longitude IS NOT NULL
AND latitude <> ''
AND longitude <> ''
LIMIT 1
");
if (!$ville_ref) {
$debug['erreur'] = "â Ville non trouvĂ©e ou sans coordonnĂ©es.";
return $debug;
}
$lat_ref = (float)str_replace(',', '.', $ville_ref->latitude);
$lon_ref = (float)str_replace(',', '.', $ville_ref->longitude);
$debug['point_reference'] = [
'id' => $ville_ref->id ?? '',
'name' => $ville_ref->name ?? '',
'city' => $ville_ref->city ?? '',
'country' => $ville_ref->country_code ?? '',
'latitude' => $lat_ref,
'longitude' => $lon_ref,
'lat_brut' => $ville_ref->latitude ?? '',
'lon_brut' => $ville_ref->longitude ?? '',
];
$delta_lat = $rayon_km / 111;
$cos_lat = cos(deg2rad($lat_ref));
if (abs($cos_lat) < 0.00001) {
$cos_lat = 0.00001;
}
$delta_lon = $rayon_km / (111 * $cos_lat);
$debug['delta_lat'] = $delta_lat;
$debug['delta_lon'] = $delta_lon;
$lat_min = $lat_ref - $delta_lat;
$lat_max = $lat_ref + $delta_lat;
$lon_min = $lon_ref - $delta_lon;
$lon_max = $lon_ref + $delta_lon;
$debug['rectangle_sql'] = [
'lat_min' => $lat_min,
'lat_max' => $lat_max,
'lon_min' => $lon_min,
'lon_max' => $lon_max,
];
// Ătape 1 : hĂ©bergements dans la ville
$hebs_ville = $db->get_results("
SELECT *
FROM heb
WHERE city = '" . $db->escape($ville) . "'
AND country_code = '" . $db->escape($pays) . "'
{$condition_postal}
AND name != 'lite'
AND latitude IS NOT NULL
AND longitude IS NOT NULL
AND latitude <> ''
AND longitude <> ''
");
$hebergements = [];
$ids_deja = [];
if ($hebs_ville) {
foreach ($hebs_ville as $h) {
$h->origine = 'ville';
$hebergements[] = $h;
$ids_deja[] = (int)$h->id;
}
}
$debug['nb_ville'] = count($hebs_ville ?: []);
// Ătape 2 : rectangle
$hebs_rect = [];
$exclusion = '';
if (!empty($ids_deja)) {
$exclusion = "AND id NOT IN (" . implode(',', array_map('intval', $ids_deja)) . ")";
}
$hebs_rect = $db->get_results("
SELECT *
FROM heb
WHERE CAST(REPLACE(latitude, ',', '.') AS DECIMAL(10,6)) BETWEEN {$lat_min} AND {$lat_max}
AND CAST(REPLACE(longitude, ',', '.') AS DECIMAL(10,6)) BETWEEN {$lon_min} AND {$lon_max}
AND country_code = '" . $db->escape($pays) . "'
AND name != 'lite'
AND latitude IS NOT NULL
AND longitude IS NOT NULL
AND latitude <> ''
AND longitude <> ''
{$exclusion}
");
if ($hebs_rect) {
foreach ($hebs_rect as $h) {
$h->origine = 'rectangle';
$hebergements[] = $h;
}
}
$debug['nb_rectangle'] = count($hebs_rect ?: []);
$debug['nb_total_avant'] = count($hebergements);
$resultats = [];
$lignes_debug = [];
foreach ($hebergements as $heb) {
$lat = (float)str_replace(',', '.', $heb->latitude);
$lon = (float)str_replace(',', '.', $heb->longitude);
$distance = round(calculerlaDistance($lat_ref, $lon_ref, $lat, $lon), 1);
$dans_rayon = ($distance <= $rayon_km);
$type_hebergement = strtolower(trim((string)($heb->type_hebergement ?? '')));
$capacite_totale = (int)($heb->capacite_totale ?? 0);
if (
$nombre_voyageurs > 0 &&
$type_hebergement === 'location' &&
$capacite_totale < $nombre_voyageurs
) {
$debug['nb_supprimes_capacite']++;
continue;
}
$lignes_debug[] = [
'id' => (int)$heb->id,
'name' => $heb->name ?? '',
'city' => $heb->city ?? '',
'postal_code' => $heb->postal_code ?? '',
'latitude_brut' => $heb->latitude ?? '',
'longitude_brut' => $heb->longitude ?? '',
'latitude' => $lat,
'longitude' => $lon,
'distance' => $distance,
'dans_rayon' => $dans_rayon ? 'OUI' : 'NON',
'origine' => $heb->origine ?? 'inconnue',
'token' => $heb->token ?? '',
'type_hebergement' => $type_hebergement,
];
if (!$dans_rayon) {
continue;
}
$resultats[] = [
'id' => (int)$heb->id,
'token' => htmlspecialchars($heb->token ?? ''),
'name' => htmlspecialchars($heb->name ?? ''),
'postal_code' => htmlspecialchars($heb->postal_code ?? ''),
'capacite_totale' => $capacite_totale,
'ville' => htmlspecialchars($heb->city ?? ''),
'country_code' => strtoupper($heb->country_code ?? ''),
'distance' => $distance,
'reviews' => $heb->reviews ?? '',
'rating' => $heb->rating ?? '',
'photo' => $heb->photo ?? '',
'photo_locale' => $heb->photo_locale ?? '',
'parking' => !empty($heb->parking) ? ucfirst(str_replace('_', ' ', $heb->parking)) : '',
'type_hebergement' => $type_hebergement,
'donnees' => json_decode($heb->donnees_specifiques ?? '[]', true),
'equipements' => json_decode($heb->equipements_json ?? '[]', true),
'activites' => json_decode($heb->activites_json ?? '[]', true),
'tarif_nuit' => $heb->tarif_nuit ?? '',
'category' => $heb->category ?? '',
'latitude' => $lat,
'longitude' => $lon,
'origine' => $heb->origine ?? 'inconnue'
];
}
usort($lignes_debug, function ($a, $b) {
return $a['distance'] <=> $b['distance'];
});
usort($resultats, function ($a, $b) {
return $a['distance'] <=> $b['distance'];
});
$resultats = array_slice($resultats, 0, 500);
$debug['lignes_debug'] = $lignes_debug;
$debug['resultats_finaux'] = $resultats;
$debug['nb_total_apres'] = count($resultats);
return $resultats;
}
/***********************************
* đ CALCULER DISTANCE HEBERGEMENTS
***********************************/
function calculerlaDistance($lat1, $lon1, $lat2, $lon2) {
$R = 6371;
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
$dLat = $lat2 - $lat1;
$dLon = $lon2 - $lon1;
$a = sin($dLat / 2) ** 2 +
cos($lat1) * cos($lat2) * sin($dLon / 2) ** 2;
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $R * $c;
}
/***********************************
* đ METTRE A JOUR VILLES DISTINCT
***********************************/
function miseaJourHeb_villes() {
$db->query("
INSERT IGNORE INTO heb_villes (city, country_code)
SELECT DISTINCT city, country_code FROM heb
WHERE city IS NOT NULL AND city <> ''
");
}
?>