trf_certest/public/userarea/update_api_json_nodes.php

64 lines
1.5 KiB
PHP

<?php
include('include/headscript.php');
header('Content-Type: application/json');
try {
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
throw new Exception('Invalid JSON payload');
}
$templateId = isset($input['template_id']) ? (int)$input['template_id'] : 0;
$apiSampleJson = $input['api_sample_json'] ?? '';
$jsonNodes = $input['json_nodes'] ?? '';
if ($templateId <= 0) {
throw new Exception('Invalid template ID');
}
if (trim($apiSampleJson) === '') {
throw new Exception('API sample JSON is empty');
}
json_decode($apiSampleJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Invalid API sample JSON: ' . json_last_error_msg());
}
$decodedNodes = json_decode($jsonNodes, true);
if (!is_array($decodedNodes)) {
throw new Exception('Invalid JSON nodes array');
}
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$stmt = $pdo->prepare("
UPDATE excel_templates
SET
api_sample_json = ?,
json_nodes = ?
WHERE id = ?
");
$stmt->execute([
$apiSampleJson,
json_encode($decodedNodes, JSON_UNESCAPED_UNICODE),
$templateId
]);
echo json_encode([
'success' => true,
'nodes_count' => count($decodedNodes)
]);
} catch (Throwable $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}