PDO::ERRMODE_EXCEPTION]
);
}
$defaultData = [
'aggiornamento' => 'Aggiornato al seggio / sezione: da compilare',
'totaleVotiSindaco' => 0,
'candidati' => [
['nome' => 'Antonio Piteo detto Tony', 'percentuale' => 0, 'voti' => 0, 'colore' => '#ef4444', 'foto' => 'foto-tony-piteo.jpg'],
['nome' => 'Flavio Fasano', 'percentuale' => 0, 'voti' => 0, 'colore' => '#2563eb', 'foto' => 'foto-flavio-fasano.jpg'],
['nome' => 'Silvia Coronese', 'percentuale' => 0, 'voti' => 0, 'colore' => '#16a34a', 'foto' => 'foto-silvia-coronese.jpg']
],
'schede' => [
'nulle' => ['percentuale' => 0, 'voti' => 0],
'nonValide' => ['percentuale' => 0, 'voti' => 0]
],
'liste' => [
['nome' => 'PD', 'percentuale' => 0, 'voti' => 0],
['nome' => 'AVS', 'percentuale' => 0, 'voti' => 0],
['nome' => 'Tony Piteo Sindaco', 'percentuale' => 0, 'voti' => 0],
['nome' => 'Amare Gallipoli', 'percentuale' => 0, 'voti' => 0],
['nome' => 'Prossima', 'percentuale' => 0, 'voti' => 0]
]
];
function jsonResponse($data): void {
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
$action = $_GET['action'] ?? '';
if ($action === 'get') {
$stmt = db()->query('SELECT payload, updated_at FROM scrutino_live WHERE id = 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$payload = $row && $row['payload'] ? json_decode($row['payload'], true) : $defaultData;
if (!$payload || empty($payload)) $payload = $defaultData;
$payload['_updatedAtDb'] = $row['updated_at'] ?? null;
jsonResponse($payload);
}
if ($action === 'save') {
$body = json_decode(file_get_contents('php://input'), true);
$password = $body['password'] ?? '';
$payload = $body['payload'] ?? null;
if ($password !== ADMIN_PASSWORD) {
http_response_code(403);
jsonResponse(['ok' => false, 'error' => 'Password admin errata']);
}
if (!is_array($payload)) {
http_response_code(400);
jsonResponse(['ok' => false, 'error' => 'Dati non validi']);
}
$stmt = db()->prepare('UPDATE scrutino_live SET payload = :payload WHERE id = 1');
$stmt->execute(['payload' => json_encode($payload, JSON_UNESCAPED_UNICODE)]);
jsonResponse(['ok' => true]);
}
?>
Scrutino Sindaco Live
Scrutino Elezioni Sindaco
Caricamento dati...