Files
trf_certest/public/userarea/update_fixed_field.php
2026-06-09 09:46:21 +02:00

87 lines
2.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once(__DIR__ . '/include/headscript.php');
try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$input = json_decode(file_get_contents('php://input'), true);
$id = (int)($input['id'] ?? 0);
$field = (string)($input['field'] ?? '');
$value = $input['value'] ?? null;
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid id']);
exit;
}
$allowed = [
'default_value',
'default_source',
'json_node',
'is_visible_import',
'is_required'
];
if (!in_array($field, $allowed, true)) {
echo json_encode(['success' => false, 'message' => 'Invalid field: ' . $field]);
exit;
}
if ($field === 'is_visible_import' || $field === 'is_required') {
$value = ((int)$value === 1) ? 1 : 0;
}
if ($field === 'default_source') {
$value = (string)$value;
if (!in_array($value, ['manual', 'json'], true)) {
echo json_encode(['success' => false, 'message' => 'Invalid default_source']);
exit;
}
// If the user goes back to manual, clear the JSON node
if ($value === 'manual') {
$sql = "
UPDATE template_fixed_mapping
SET default_source = :val, json_node = NULL
WHERE id = :id
";
$stmt = $pdo->prepare($sql);
$ok = $stmt->execute([
':val' => $value,
':id' => $id
]);
echo json_encode(['success' => (bool)$ok]);
exit;
}
}
if ($field === 'json_node') {
$value = trim((string)$value);
if ($value === '') {
$value = null;
}
}
$sql = "UPDATE template_fixed_mapping SET {$field} = :val WHERE id = :id";
$stmt = $pdo->prepare($sql);
$ok = $stmt->execute([
':val' => $value,
':id' => $id
]);
echo json_encode(['success' => (bool)$ok]);
} catch (Throwable $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}