Files
yogibook_aury_new/public/update_reschedule.php
2026-09-24 11:20:05 +02:00

66 lines
2.3 KiB
PHP

<?php
/**
* Aggiorna maxreschedule o reprogrammed di un ordine (chiamato via AJAX dal riepilogo ordini).
* Risponde sempre in JSON: {status, message, maxreschedule, reprogrammed}
*/
ob_start();
require_once('include/headscript.php');
ob_end_clean(); // scarta eventuale output di headscript, così il JSON resta pulito
header('Content-Type: application/json; charset=utf-8');
function jsonOut(array $data, int $code = 200): void
{
http_response_code($code);
echo json_encode($data);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
jsonOut(['status' => 'error', 'message' => 'Metodo non consentito.'], 405);
}
$allowedFields = ['maxreschedule', 'reprogrammed'];
$field = $_POST['field'] ?? '';
$orderId = (int) ($_POST['order_id'] ?? 0);
$value = (string) ($_POST['value'] ?? '');
if (!in_array($field, $allowedFields, true)) {
jsonOut(['status' => 'error', 'message' => 'Campo non valido.'], 400);
}
if ($orderId <= 0) {
jsonOut(['status' => 'error', 'message' => 'ID ordine non valido.'], 400);
}
if (!preg_match('/^\d{1,3}$/', $value)) {
jsonOut(['status' => 'error', 'message' => 'Inserisci un numero intero da 0 a 999.'], 400);
}
try {
$pdo = DBHandlerSelect::getInstance()->getConnection();
$check = $pdo->prepare("SELECT idorderbook FROM orderbook WHERE idorderbook = :id");
$check->execute([':id' => $orderId]);
if (!$check->fetch()) {
jsonOut(['status' => 'error', 'message' => 'Ordine non trovato.'], 404);
}
// $field è da whitelist, quindi sicuro da inserire nella query
$upd = $pdo->prepare("UPDATE orderbook SET `$field` = :v WHERE idorderbook = :id");
$upd->execute([':v' => (int) $value, ':id' => $orderId]);
$row = $pdo->prepare("SELECT maxreschedule, reprogrammed FROM orderbook WHERE idorderbook = :id");
$row->execute([':id' => $orderId]);
$r = $row->fetch();
jsonOut([
'status' => 'success',
'message' => 'Valore aggiornato.',
'maxreschedule' => $r['maxreschedule'] === null ? null : (int) $r['maxreschedule'],
'reprogrammed' => (int) $r['reprogrammed'],
]);
} catch (Throwable $e) {
error_log('update_reschedule: ' . $e->getMessage());
jsonOut(['status' => 'error', 'message' => 'Errore durante il salvataggio.'], 500);
}