| Current Path : /home/h/a/p/happyrenas/find.myreco.online/tools/ |
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/tools/generer_villes_prefix.php |
<?php
declare(strict_types=1);
require_once __DIR__ . '/../configuration.php';
require_once __DIR__ . '/../includes/fonctions.php';
$dir = __DIR__ . '/../includes/data/villes_prefix_prod';
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
/**
* Normalise une ville pour créer le préfixe fichier : ab, ac, etc.
*/
function prefixVille(string $city): string
{
$city = trim($city);
$city = mb_strtolower($city, 'UTF-8');
if (function_exists('iconv')) {
$tmp = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $city);
if ($tmp !== false) {
$city = $tmp;
}
}
$city = preg_replace('/[^a-z]/', '', $city);
if (strlen($city) < 2) {
return 'xx';
}
return substr($city, 0, 2);
}
/**
* Nettoyage anciens fichiers
*/
foreach (glob($dir . '/villes_*.json') as $file) {
unlink($file);
}
$rows = $db->get_results("
SELECT DISTINCT
city,
postal_code,
country_code
FROM heb
WHERE city IS NOT NULL
AND city <> ''
AND country_code IS NOT NULL
AND country_code <> ''
AND postal_code IS NOT NULL
AND postal_code <> ''
AND name != 'lite'
ORDER BY country_code ASC, city ASC, postal_code ASC
");
$groupes = [];
if ($rows) {
foreach ($rows as $r) {
$city = trim((string)$r->city);
$postalCode = trim((string)$r->postal_code);
$countryCode = strtoupper(trim((string)$r->country_code));
if ($city === '' || $postalCode === '' || $countryCode === '') {
continue;
}
$prefix = prefixVille($city);
$groupes[$prefix][] = [
'city' => $city,
'postal_code' => $postalCode,
'country_code' => $countryCode,
'label' => $city . ' (' . $postalCode . '), ' . $countryCode,
'value' => $city . ', ' . $postalCode . ', ' . $countryCode,
];
}
}
foreach ($groupes as $prefix => $items) {
$items = array_values(array_unique($items, SORT_REGULAR));
file_put_contents(
$dir . '/villes_' . $prefix . '.json',
json_encode($items, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
);
}
echo '✅ JSON générés : ' . count($groupes) . " fichiers\n";