added fixed field in json
This commit is contained in:
@@ -3,33 +3,84 @@ header('Content-Type: application/json');
|
||||
|
||||
require_once(__DIR__ . '/include/headscript.php');
|
||||
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
try {
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
$field = (string)($input['field'] ?? '');
|
||||
$value = $input['value'] ?? null;
|
||||
$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;
|
||||
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()
|
||||
]);
|
||||
}
|
||||
|
||||
$allowed = ['default_value', 'is_visible_import', 'is_required'];
|
||||
if (!in_array($field, $allowed, true)) {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid field']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($field === 'is_visible_import' || $field === 'is_required') {
|
||||
$value = ((int)$value === 1) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
$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]);
|
||||
|
||||
Reference in New Issue
Block a user