51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
require_once(__DIR__ . '/include/headscript.php');
|
|
|
|
$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', 'is_visible_import', 'is_required', 'default_source', 'excel_column'];
|
|
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;
|
|
}
|
|
|
|
if ($field === 'default_source') {
|
|
$value = in_array($value, ['manual', 'xls', 'json'], true) ? $value : 'manual';
|
|
|
|
if ($value !== 'xls') {
|
|
$pdo->prepare("UPDATE template_fixed_mapping SET excel_column = NULL WHERE id = :id")
|
|
->execute([':id' => $id]);
|
|
}
|
|
}
|
|
|
|
if ($field === 'excel_column') {
|
|
$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]);
|