From 8d83186a68102483d860cc00f2b27b55f6fcdb67 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Thu, 5 Jun 2025 15:09:28 +0200 Subject: [PATCH] search by insee code --- assets/app.js | 77 ++++++++++++++++++++++++++++ src/Controller/AdminController.php | 1 + src/Controller/PublicController.php | 2 +- src/Service/Motocultrice.php | 46 +++++------------ templates/admin/stats.html.twig | 4 +- templates/public/dashboard.html.twig | 33 +++++------- templates/public/edit.html.twig | 45 ++++++++-------- 7 files changed, 130 insertions(+), 78 deletions(-) diff --git a/assets/app.js b/assets/app.js index aeff00b..1388037 100644 --- a/assets/app.js +++ b/assets/app.js @@ -633,5 +633,82 @@ document.addEventListener('DOMContentLoaded', () => { table.classList.add('js-sort-table'); }); + async function searchInseeCode(query) { + try { + // Afficher l'indicateur de chargement + document.querySelector('#loading_search_insee').classList.remove('d-none'); + const response = await fetch(`https://geo.api.gouv.fr/communes?nom=${query}&fields=nom,code,codesPostaux&limit=10`); + const data = await response.json(); + return data.map(commune => ({ + label: `${commune.nom} (code insee ${commune.code})`, + insee: commune.code, + postcodes: commune.codesPostaux + })); + } catch (error) { + console.error('Erreur lors de la recherche du code INSEE:', error); + return []; + } + } + + // Modifier la fonction de recherche existante + const searchInput = document.getElementById('app_admin_labourer'); + const suggestionList = document.getElementById('suggestionList'); + + if (searchInput && suggestionList) { + let timeoutId; + + searchInput.addEventListener('input', () => { + clearTimeout(timeoutId); + const query = searchInput.value.trim(); + + if (query.length < 2) { + suggestionList.innerHTML = ''; + return; + } + + timeoutId = setTimeout(async () => { + const suggestions = await searchInseeCode(query); + suggestionList.innerHTML = ''; + + if (suggestions.length === 0) { + const li = document.createElement('li'); + li.style.cssText = ` + padding: 8px 12px; + color: #666; + font-style: italic; + `; + li.textContent = 'Aucun résultat trouvé'; + suggestionList.appendChild(li); + return; + } + + suggestions.forEach(suggestion => { + const li = document.createElement('li'); + li.style.cssText = ` + padding: 8px 12px; + cursor: pointer; + border-bottom: 1px solid #eee; + `; + li.textContent = suggestion.label; + + li.addEventListener('mouseenter', () => { + li.style.backgroundColor = '#f0f0f0'; + }); + + li.addEventListener('mouseleave', () => { + li.style.backgroundColor = 'white'; + }); + + li.addEventListener('click', () => { + searchInput.value = suggestion.insee; + suggestionList.innerHTML = ''; + labourer(); + }); + + suggestionList.appendChild(li); + }); + }, 300); + }); + } }); diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index 2a6d6c5..41fff9e 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -1,5 +1,6 @@ entityManager->getRepository(Stats::class)->findAll(); diff --git a/src/Service/Motocultrice.php b/src/Service/Motocultrice.php index c38a790..f38ab20 100644 --- a/src/Service/Motocultrice.php +++ b/src/Service/Motocultrice.php @@ -41,7 +41,8 @@ class Motocultrice ]; // quand un commerce a fermé, on peut supprimer ces tags - public $obsolete_tags = ["phone", "website", "email", "description", "brand", "opening_hours", + public $obsolete_tags = [ + "phone", "website", "email", "description", "brand", "opening_hours", "check_date:opening_hours", "internet_access", "indoor_seating", "takeaway", "female", "male", "unisex", "ref:FR:NAF", "ref:FR:FINESS", "ref:FR:SIRET", "ref:FR:SIREN", "ref:vatin", @@ -52,10 +53,12 @@ class Motocultrice "mobile", "sport", "produce", "lottery", "supermarket", "information", "tourism", "government", "brewery" ]; - public $tags_to_remove = ["diet:", "contact:", "name:", "payment:", "delivery:", "type:FR:", "ref:FR:SDIS:", + public $tags_to_remove = [ + "diet:", "contact:", "name:", "payment:", "delivery:", "type:FR:", "ref:FR:SDIS:", "brand:", "fuel:", "service:", "description:", "operator:", "tickets:", "healthcare:" ]; - public $tags_to_convert = ["shop" => "was:shop", + public $tags_to_convert = [ + "shop" => "was:shop", "information" => "was:information", "office" => "was:office", "amenity" => "was:amenity", @@ -98,7 +101,7 @@ QUERY; public function get_query_places($zone) { return '[out:json][timeout:25]; -area["postal_code"="'.$zone.'"]->.searchArea; +area["ref:INSEE"="'.$zone.'"]->.searchArea; '.$this->overpass_base_places.' out center tags;'; } @@ -150,20 +153,9 @@ out center tags;'; public function labourer(string $zone): array { - - $use_places_without_email_to_reference = $_ENV['USE_PLACES_WITHOUT_EMAIL_TO_REFERENCE'] ?? false; - - if (!$zone) { - throw new \InvalidArgumentException("La zone ne peut pas être vide"); - } - - // Nettoyer et échapper la zone pour la requête - $zone = addslashes(trim($zone)); - $query = $this->get_query_places($zone); - try { $response = $this->client->request('POST', $this->overpassApiUrl, [ - 'body' => ['data' => $query] + 'body' => ['data' => $this->get_query_places($zone)] ]); $data = json_decode($response->getContent(), true); @@ -174,14 +166,6 @@ out center tags;'; if (isset($element['tags'])) { $email = ""; - if( ! $use_places_without_email_to_reference){ - - $email = $element['tags']['contact:email'] ?? $element['tags']['email'] ?? null; - // On passe si pas d'email - if (!$email) { - continue; - } - } $places[] = [ 'id' => $element['id'], @@ -198,9 +182,6 @@ out center tags;'; return $places; } catch (\Exception $e) { - var_dump($query); - var_dump($e->getMessage()); - die(); throw new \Exception("Erreur lors de la requête Overpass : " . $e->getMessage()); } } @@ -208,14 +189,14 @@ out center tags;'; public function get_city_osm_from_zip_code($zip_code) { // Requête Overpass pour obtenir la zone administrative de niveau 8 avec un nom $query = "[out:json][timeout:25]; - area[\"postal_code\"=\"{$zip_code}\"]->.searchArea; + area[\"ref:INSEE\"=\"{$zip_code}\"]->.searchArea; ( relation[\"admin_level\"=\"8\"][\"name\"][\"type\"=\"boundary\"][\"boundary\"=\"administrative\"](area.searchArea); ); out body; >; out skel qt;"; - // $query = "area[\"postal_code\"=\"{$zip_code}\"]->.searchArea;"; + $response = $this->client->request('POST', $this->overpassApiUrl, [ 'body' => ['data' => $query] ]); @@ -225,14 +206,12 @@ out center tags;'; return $city; } return null; - } public function get_osm_object_data($osm_kind = 'node', $osm_object_id = 12855459190) { $object_id = "https://www.openstreetmap.org/api/0.6/".$osm_kind."/".$osm_object_id; - // dump($object_id); - // die(); + try { $response = $this->client->request('GET', $object_id); @@ -308,6 +287,7 @@ out center tags;'; } return null; } + public function migrate_tags($osm_object_data) { // migrer email vers contact:email @@ -373,7 +353,7 @@ out center tags;'; $essentialTags = [ 'name', 'opening_hours', - // 'phone', + // 'phone', 'contact:email', 'contact:phone', 'website', diff --git a/templates/admin/stats.html.twig b/templates/admin/stats.html.twig index b0396b8..989b82e 100644 --- a/templates/admin/stats.html.twig +++ b/templates/admin/stats.html.twig @@ -92,9 +92,9 @@ async function fetchland() { // Requête pour obtenir le contour administratif const boundaryQuery = `[out:json][timeout:25]; -area["postal_code"="${zip_code}"]->.searchArea; +area["ref:INSEE"="${zip_code}"]->.searchArea; ( - relation["boundary"="postal_code"]["postal_code"="${zip_code}"](area.searchArea); + relation["boundary"="postal_code"]["ref:INSEE"="${zip_code}"](area.searchArea); ); out body; >; diff --git a/templates/public/dashboard.html.twig b/templates/public/dashboard.html.twig index b01043d..2df89b5 100644 --- a/templates/public/dashboard.html.twig +++ b/templates/public/dashboard.html.twig @@ -25,20 +25,7 @@ // Fonction pour rechercher avec Addok - async function searchPostalCode(query) { - try { - const response = await fetch(`https://api-adresse.data.gouv.fr/search/?q=${query}&type=municipality&autocomplete=1`); - const data = await response.json(); - return data.features.map(feature => ({ - label: `${feature.properties.city} (${feature.properties.postcode})`, - postcode: feature.properties.postcode, - city: feature.properties.city - })); - } catch (error) { - console.error('Erreur lors de la recherche:', error); - return []; - } - } + // Créer et configurer la liste de suggestions const suggestionList = document.createElement('ul'); @@ -79,7 +66,7 @@ return; } - const suggestions = await searchPostalCode(query); + const suggestions = await searchInseeCode(query); suggestionList.innerHTML = ''; if (suggestions.length === 0) { @@ -135,12 +122,12 @@ } // Créer une carte des villes avec les codes postaux - let map = new maplibregl.Map({ + {# let map = new maplibregl.Map({ container: 'mapDashboard', style: 'https://api.maptiler.com/maps/basic-v2/style.json?key={{ maptiler_token }}', center: [2.3488, 48.8534], // Paris zoom: 10 - }); + }); #} // Fonction pour obtenir la couleur selon le pourcentage function getColorFromPercent(percent) { @@ -232,9 +219,15 @@ out skel qt;`;

{{ places_count }} Lieux

- - - + + +
diff --git a/templates/public/edit.html.twig b/templates/public/edit.html.twig index af7bd91..55e436b 100644 --- a/templates/public/edit.html.twig +++ b/templates/public/edit.html.twig @@ -170,27 +170,28 @@ -{% block javascripts %} - {{ parent() }} - - -{% endblock %} {% endblock %} + {% block javascripts %} + {{ parent() }} + + + {% endblock %} \ No newline at end of file