getConnection(); $iduser = $iduserlogin ?? null; $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true); if (!is_array($input)) { throw new Exception('Payload JSON non valido.'); } $id = (int)($input['id'] ?? 0); if ($id <= 0) { throw new Exception('ID non valido.'); } $roiX = isset($input['roi_x']) ? (float)$input['roi_x'] : null; $roiY = isset($input['roi_y']) ? (float)$input['roi_y'] : null; $roiW = isset($input['roi_width']) ? (float)$input['roi_width'] : null; $roiH = isset($input['roi_height']) ? (float)$input['roi_height'] : null; $roiPage = isset($input['roi_page']) ? (int)$input['roi_page'] : 1; $mode = $input['calculation_mode'] ?? 'auto_roi'; if ($roiX === null || $roiY === null || $roiW === null || $roiH === null) { throw new Exception('ROI non valida.'); } if ($roiW <= 0 || $roiH <= 0) { throw new Exception('Dimensioni ROI non valide.'); } if ($roiX < 0 || $roiY < 0 || $roiX > 1 || $roiY > 1 || $roiW > 1 || $roiH > 1) { throw new Exception('Coordinate ROI fuori scala.'); } $allowedModes = [ 'auto_roi', 'stitch_contour', 'filled_union', 'closed_path' ]; if (!in_array($mode, $allowedModes, true)) { $mode = 'auto_roi'; } if ($iduser === null || $iduser === '') { $stmt = $pdo->prepare(" UPDATE cad_area_jobs SET roi_x = ?, roi_y = ?, roi_width = ?, roi_height = ?, roi_page = ?, calculation_mode = ?, status = 'uploaded', message = NULL WHERE id = ? "); $stmt->execute([ $roiX, $roiY, $roiW, $roiH, $roiPage, $mode, $id ]); } else { $stmt = $pdo->prepare(" UPDATE cad_area_jobs SET roi_x = ?, roi_y = ?, roi_width = ?, roi_height = ?, roi_page = ?, calculation_mode = ?, status = 'uploaded', message = NULL WHERE id = ? AND iduser = ? "); $stmt->execute([ $roiX, $roiY, $roiW, $roiH, $roiPage, $mode, $id, $iduser ]); } if ($stmt->rowCount() === 0) { throw new Exception('Nessun record aggiornato. Controlla ID o utente.'); } echo json_encode([ 'success' => true, 'message' => 'ROI salvata correttamente.' ]); exit; } catch (Throwable $e) { error_log('CAD area save ROI error: ' . $e->getMessage()); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); exit; }