up edit form

This commit is contained in:
Tykayn 2025-05-26 16:22:01 +02:00 committed by tykayn
parent fd72a1cedc
commit b1965abe06
6 changed files with 325 additions and 49 deletions

View file

@ -46,6 +46,7 @@ class PublicController extends AbstractController
return $this->render('public/edit.html.twig', [
'commerce' => $commerce,
'name' => $name,
'osm_kind' => $place->getOsmKind(),
"mapbox_token" => $_ENV['MAPBOX_TOKEN'],
"maptiler_token" => $_ENV['MAPTILER_TOKEN'],
]);
@ -74,14 +75,31 @@ class PublicController extends AbstractController
if ($request->isMethod('POST')) {
$status = "non modifié";
// Récupérer le type d'objet (node ou way)
$osm_kind = $request->request->get('osm_kind', 'node');
// Récupérer tous les tags du formulaire
$tags = [];
foreach ($request->request->all() as $key => $value) {
var_dump($key, $value);
if (strpos($key, 'commerce_tag_value__') === 0) {
$tagKey = str_replace('commerce_tag_value__', '', $key);
if (!empty($value)) {
// Validation des données selon le type de tag
if ($tagKey === 'addr:postcode') {
// Vérifier que c'est bien un code postal français (5 chiffres)
if (!preg_match('/^\d{5}$/', $value)) {
$status = "Erreur : Le code postal doit être composé de 5 chiffres";
continue;
}
} elseif ($tagKey === 'contact:phone' || $tagKey === 'phone') {
// Nettoyer le numéro de téléphone
$value = preg_replace('/[^0-9+]/', '', $value);
} elseif (strpos($value, 'http://') === 0) {
$value = str_replace('http://', 'https://', $value);
}
$tags[$tagKey] = $value;
}
}
@ -102,7 +120,7 @@ class PublicController extends AbstractController
$tag = $changeset->addChild('tag');
$tag->addAttribute('k', 'comment');
$tag->addAttribute('v', 'Modification des tags via l\'interface web');
$tag->addAttribute('v', 'Modification des tags via l\'interface web #MonCommerceOSM');
$changesetResponse = $client->put('https://api.openstreetmap.org/api/0.6/changeset/create', [
'body' => $changesetXml->asXML(),
@ -114,19 +132,29 @@ class PublicController extends AbstractController
$newChangesetId = $changesetResponse->getBody()->getContents();
// 2. Modifier le nœud avec le nouveau changeset
// Récupérer les données actuelles de l'objet
$currentObjectData = $this->motocultrice->get_osm_object_data($osm_kind, $osm_object_id);
// 2. Modifier l'objet avec le nouveau changeset
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><osm version="0.6"></osm>');
$node = $xml->addChild('node');
$node->addAttribute('id', $osm_object_id);
$node->addAttribute('version', $version);
$node->addAttribute('changeset', $newChangesetId);
$node->addAttribute('lat', '49.6504926');
$node->addAttribute('lon', '-1.5722526');
$object = $xml->addChild($osm_kind);
$object->addAttribute('id', $osm_object_id);
$object->addAttribute('version', $version);
$object->addAttribute('changeset', $newChangesetId);
// Ajouter les coordonnées pour les nodes
if ($osm_kind === 'node') {
if (!isset($currentObjectData['@attributes']['lat']) || !isset($currentObjectData['@attributes']['lon'])) {
throw new \Exception("Impossible de récupérer les coordonnées du nœud");
}
$object->addAttribute('lat', $currentObjectData['@attributes']['lat']);
$object->addAttribute('lon', $currentObjectData['@attributes']['lon']);
}
// Ajouter les tags
foreach ($tags as $key => $value) {
if (!empty($key) && !empty($value)) {
$tag = $node->addChild('tag');
$tag = $object->addChild('tag');
$tag->addAttribute('k', htmlspecialchars($key, ENT_XML1));
$tag->addAttribute('v', htmlspecialchars($value, ENT_XML1));
}
@ -134,9 +162,9 @@ class PublicController extends AbstractController
// Debug du XML généré
$xmlString = $xml->asXML();
var_dump($xmlString);
// echo('xml : <br>'.$xmlString);
$response = $client->put('https://api.openstreetmap.org/api/0.6/node/' . $osm_object_id, [
$response = $client->put("https://api.openstreetmap.org/api/0.6/{$osm_kind}/" . $osm_object_id, [
'body' => $xmlString,
'headers' => [
'Authorization' => 'Bearer ' . $osm_api_token,
@ -166,7 +194,7 @@ class PublicController extends AbstractController
}
// après envoi on récupère les données
$commerce = $this->motocultrice->get_osm_object_data($osm_object_id);
$commerce = $this->motocultrice->get_osm_object_data($osm_kind, $osm_object_id);
return $this->render('public/view.html.twig', [
'controller_name' => 'PublicController',
'commerce' => $commerce,

View file

@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface;
class Motocultrice
{
private $overpassApiUrl = 'https://overpass-api.de/api/interpreter';
private $osmApiUrl = 'https://www.openstreetmap.org/api/0.6';
public function __construct(
private HttpClientInterface $client,
@ -141,4 +142,98 @@ QUERY;
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
public function formatOsmDataForSubmit(array $data): array
{
// Garder uniquement les tags essentiels
$essentialTags = [
'name',
'opening_hours',
'phone',
'contact:email',
'contact:phone',
'website',
'contact:website',
'wheelchair',
'addr:housenumber',
'addr:street',
'addr:city',
'addr:postcode',
'amenity',
'shop',
'tourism',
'source',
'ref:FR:SIRET'
];
$formattedData = [
'node' => [
'@attributes' => [
'id' => $data['@attributes']['id'],
'version' => $data['@attributes']['version'],
'changeset' => $data['@attributes']['changeset'],
'lat' => $data['@attributes']['lat'],
'lon' => $data['@attributes']['lon']
],
'tag' => []
]
];
// Filtrer et ajouter uniquement les tags essentiels
if (isset($data['tag'])) {
foreach ($data['tag'] as $tag) {
if (in_array($tag['@attributes']['k'], $essentialTags)) {
$formattedData['node']['tag'][] = $tag;
}
}
}
return $formattedData;
}
private function arrayToXml(array $data): string
{
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><osm></osm>');
if (isset($data['node'])) {
$node = $xml->addChild('node');
foreach ($data['node']['@attributes'] as $key => $value) {
$node->addAttribute($key, $value);
}
if (isset($data['node']['tag'])) {
foreach ($data['node']['tag'] as $tag) {
$tagElement = $node->addChild('tag');
$tagElement->addAttribute('k', $tag['@attributes']['k']);
$tagElement->addAttribute('v', $tag['@attributes']['v']);
}
}
}
return $xml->asXML();
}
public function submitOsmData(array $data): void
{
$formattedData = $this->formatOsmDataForSubmit($data);
$xmlData = $this->arrayToXml($formattedData);
try {
$response = $this->client->request('PUT',
"{$this->osmApiUrl}/node/{$data['@attributes']['id']}",
[
'body' => $xmlData,
'headers' => [
'Content-Type' => 'application/xml; charset=utf-8'
]
]
);
if ($response->getStatusCode() !== 200) {
throw new \Exception("Erreur lors de la soumission des données : " . $response->getContent());
}
} catch (\Exception $e) {
throw new \Exception("Erreur lors de la communication avec l'API OSM : " . $e->getMessage());
}
}
}