Corrige la localisation actuelle des animaux

This commit is contained in:
Alexandre NOEL 2026-09-13 15:57:57 +02:00
commit 68590eddbb
9 changed files with 80 additions and 36 deletions

View file

@ -453,6 +453,35 @@ trait AnimalBrowseActions
echo h(t('error.animal_not_found'));
return;
}
// Les imports historiques peuvent contenir une adresse sans ses coordonnées.
// On complète alors la fiche une seule fois, au premier affichage de la carte.
$coordinateUpdates = [];
foreach ([
'rescue' => trim((string) ($animal['rescue_address'] ?: $animal['rescue_location_name'] ?? '')),
'current' => trim((string) ($animal['current_address'] ?? '')),
] as $kind => $address) {
if (
$address !== '' &&
($animal[$kind . '_lat'] === null || $animal[$kind . '_lng'] === null)
) {
$coordinates = GeoService::geocode($db, $address);
if ($coordinates) {
$coordinateUpdates[$kind . '_lat'] = $coordinates['lat'];
$coordinateUpdates[$kind . '_lng'] = $coordinates['lng'];
}
}
}
if ($coordinateUpdates) {
$assignments = [];
$parameters = [':id' => $id];
foreach ($coordinateUpdates as $column => $value) {
$assignments[] = $column . '=:' . $column;
$parameters[':' . $column] = $value;
$animal[$column] = $value;
}
$db->prepare('UPDATE animals SET ' . implode(',', $assignments) . ' WHERE id=:id')->execute($parameters);
}
if (!empty($animal['intake_owner_care_home'])) {
$animal['intake_reason'] = 'owner_care_home';
}

View file

@ -72,6 +72,15 @@ final class PlacementsController
]),
);
}
$currentLat = null;
$currentLng = null;
if ($currentAddress !== '') {
$coordinates = GeoService::geocode($db, $currentAddress);
if ($coordinates) {
$currentLat = $coordinates['lat'];
$currentLng = $coordinates['lng'];
}
}
$notes = trim((string) ($_POST['notes'] ?? ''));
$db->beginTransaction();
try {
@ -87,8 +96,14 @@ final class PlacementsController
':user' => Auth::id(),
]);
$db->prepare(
'UPDATE animals SET status=:status,refuge_room=NULL,care_box_key=NULL,current_address=:address,current_lat=NULL,current_lng=NULL,updated_at=datetime(\'now\') WHERE id=:id',
)->execute([':status' => $status, ':address' => $currentAddress ?: null, ':id' => $animalId]);
'UPDATE animals SET status=:status,refuge_room=NULL,care_box_key=NULL,current_address=:address,current_lat=:lat,current_lng=:lng,updated_at=datetime(\'now\') WHERE id=:id',
)->execute([
':status' => $status,
':address' => $currentAddress ?: null,
':lat' => $currentLat,
':lng' => $currentLng,
':id' => $animalId,
]);
$after = $animal;
$after['status'] = $status;
$after['refuge_room'] = null;