trf_certest/public/userarea/update_schemajson.php
2025-07-05 20:31:44 +02:00

57 lines
1.7 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'class/db-functions.php';
// Disabilita l'output di errori HTML
ini_set('display_errors', '0');
error_reporting(E_ALL);
$response = ["success" => false, "message" => ""];
try {
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
throw new Exception("Invalid request method.");
}
// Recupera i dati dal body JSON (non da $_POST direttamente)
$input = json_decode(file_get_contents('php://input'), true);
$template_id = isset($input['template_id']) ? intval($input['template_id']) : null;
$schemajson = isset($input['schemajson']) ? trim($input['schemajson']) : '';
// Controllo sui campi obbligatori
if (empty($template_id) || empty($schemajson)) {
throw new Exception("All fields marked with * are required, including schemajson.");
}
// Validazione del JSON
$decoded_json = json_decode($schemajson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Invalid JSON format for schemajson.");
}
// Connessione al database
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Aggiorna il database
$stmt = $pdo->prepare("UPDATE excel_templates
SET schemajson = ?, updated_at = NOW()
WHERE id = ?");
$stmt->execute([
$schemajson,
$template_id
]);
if ($stmt->rowCount() > 0) {
$response["message"] = "Schema JSON updated successfully!";
} else {
$response["message"] = "No changes detected, but operation completed.";
}
$response["success"] = true;
} catch (Exception $e) {
$response["message"] = $e->getMessage();
}
// Restituisce un JSON per il fetch
echo json_encode($response);