Files
yogibook_aury_new/public/event_locations_save.php
T
2026-09-17 10:22:30 +02:00

68 lines
2.2 KiB
PHP

<?php
require_once('include/headscript.php');
/**
* Salvataggio sede (insert/update) da event_locations.php.
* Se "predefinita" è spuntata, la imposta come unica default.
*/
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: event_locations.php');
exit();
}
$pdo = DBHandlerSelect::getInstance()->getConnection();
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$name = trim($_POST['name'] ?? '');
$address = trim($_POST['address'] ?? '');
$isDefault = (isset($_POST['is_default']) && $_POST['is_default'] === 'Y') ? 'Y' : 'N';
$lat = ($_POST['lat'] ?? '') !== '' ? (float) $_POST['lat'] : null;
$lng = ($_POST['lng'] ?? '') !== '' ? (float) $_POST['lng'] : null;
if ($name === '' || $address === '') {
header('Location: event_locations.php?message=error');
exit();
}
try {
if ($id > 0) {
$stmt = $pdo->prepare("UPDATE event_locations
SET name = :name, address = :address, lat = :lat, lng = :lng,
is_default = :is_default, updated_at = NOW()
WHERE id = :id");
$stmt->execute([
':name' => $name,
':address' => $address,
':lat' => $lat,
':lng' => $lng,
':is_default' => $isDefault,
':id' => $id,
]);
$savedId = $id;
} else {
$stmt = $pdo->prepare("INSERT INTO event_locations (name, address, lat, lng, is_default, created_at, updated_at)
VALUES (:name, :address, :lat, :lng, :is_default, NOW(), NOW())");
$stmt->execute([
':name' => $name,
':address' => $address,
':lat' => $lat,
':lng' => $lng,
':is_default' => $isDefault,
]);
$savedId = (int) $pdo->lastInsertId();
}
// Una sola predefinita
if ($isDefault === 'Y') {
$pdo->prepare("UPDATE event_locations SET is_default = IF(id = ?, 'Y', 'N'), updated_at = NOW()")->execute([$savedId]);
}
header('Location: event_locations.php?message=success');
exit();
} catch (Throwable $ex) {
error_log('[event_locations_save] ' . $ex->getMessage());
header('Location: event_locations.php?message=error');
exit();
}