Files
Arnaud 5633969d98 Version initiale — carte des établissements scolaires
Carte Leaflet (annuaire Éducation nationale) + API PHP/MySQL :
fiche établissement (effectifs, IPS, population INSEE, DPE ADEME),
GED documents/photos, commentaires par groupe, gestion utilisateurs
et politique de mots de passe. Imports opendata (effectifs, IPS,
population, DPE, IRIS).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 21:35:41 +02:00

87 lines
2.7 KiB
PHP

<?php
/**
* Import de la dernière version de l'opendata (super-admin).
* POST → télécharge l'export de l'annuaire et met à jour la base.
*
* Nécessite un accès Internet sortant côté serveur.
*/
declare(strict_types=1);
require __DIR__ . '/db.php';
const OPENDATA_URL =
'https://data.education.gouv.fr/api/explore/v2.1/catalog/datasets/fr-en-annuaire-education/exports/json'
. '?select=identifiant_de_l_etablissement,nom_etablissement,type_etablissement,statut_public_prive,'
. 'libelle_region,libelle_academie,libelle_departement,code_departement,nom_commune,code_postal,adresse_1,'
. 'telephone,mail,web,etat,nom_circonscription,restauration,hebergement,ulis,segpa,apprentissage,'
. 'latitude,longitude';
try {
require_super();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
json_error('Méthode non supportée.', 405);
}
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Téléchargement.
$raw = false;
if (function_exists('curl_init')) {
$ch = curl_init(OPENDATA_URL);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 600,
CURLOPT_HTTPHEADER => ['Accept: application/json'],
CURLOPT_USERAGENT => 'ecoles-import/1.0',
]);
$raw = curl_exec($ch);
if ($raw === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException('Téléchargement échoué : ' . $err);
}
$code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 200) {
throw new RuntimeException("Réponse HTTP $code de l'opendata.");
}
} else {
$raw = @file_get_contents(OPENDATA_URL);
if ($raw === false) {
throw new RuntimeException('Téléchargement échoué (file_get_contents).');
}
}
$rows = json_decode($raw, true);
unset($raw);
if (!is_array($rows)) {
throw new RuntimeException('Réponse opendata illisible.');
}
// Transformation (clés courtes, géoloc obligatoire).
$clean = [];
$skipped = 0;
foreach ($rows as $r) {
$rec = map_opendata_row($r);
if ($rec === null || empty($rec['id'])) {
$skipped++;
continue;
}
$clean[] = $rec;
}
unset($rows);
$n = import_etablissements(db(), $clean);
$total = (int) db()->query('SELECT COUNT(*) FROM etablissements')->fetchColumn();
json_out([
'ok' => true,
'imported' => $n,
'skipped' => $skipped,
'total' => $total,
]);
} catch (Throwable $e) {
json_error('Import : ' . $e->getMessage(), 500);
}