added fixed field in json
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<!-- <li> <a href="index.php"><i class='bx bx-radio-circle'></i>Default</a>
|
<!-- <li> <a href="index.php"><i class='bx bx-radio-circle'></i>Default</a>
|
||||||
</li> -->
|
</li> -->
|
||||||
<li> <a href="import_dashboard.php"><i class='bx bx-radio-circle'></i>XLS Import</a>
|
<li> <a href="import_dashboard.php"><i class='bx bx-radio-circle'></i>Import AREA</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -87,10 +87,19 @@ $mappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
|
|
||||||
// Recupera i fixed fields dalla tabella template_fixed_mapping
|
// Recupera i fixed fields dalla tabella template_fixed_mapping
|
||||||
$stmt = $pdo->prepare("
|
$stmt = $pdo->prepare("
|
||||||
SELECT id, fixed_field_key, is_manual, data_type, is_required, default_value, is_visible_import
|
SELECT
|
||||||
FROM template_fixed_mapping
|
id,
|
||||||
WHERE template_id = ?
|
fixed_field_key,
|
||||||
ORDER BY id ASC
|
is_manual,
|
||||||
|
data_type,
|
||||||
|
is_required,
|
||||||
|
default_value,
|
||||||
|
default_source,
|
||||||
|
json_node,
|
||||||
|
is_visible_import
|
||||||
|
FROM template_fixed_mapping
|
||||||
|
WHERE template_id = ?
|
||||||
|
ORDER BY id ASC
|
||||||
");
|
");
|
||||||
$stmt->execute([$id]);
|
$stmt->execute([$id]);
|
||||||
$fixedMappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$fixedMappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
@@ -447,19 +456,18 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
<td>
|
<td>
|
||||||
<?php
|
<?php
|
||||||
$isSceltaMultipla = ($mapping['data_type'] === 'SceltaMultipla');
|
$isSceltaMultipla = ($mapping['data_type'] === 'SceltaMultipla');
|
||||||
|
$isApiSceltaMultipla = ($sourceType === 'API' && $isSceltaMultipla);
|
||||||
|
|
||||||
$autoValue = $mapping['auto_value'] ?? 'none';
|
$autoValue = $mapping['auto_value'] ?? 'none';
|
||||||
$hasAuto = ($autoValue && $autoValue !== 'none');
|
$hasAuto = ($autoValue && $autoValue !== 'none');
|
||||||
|
|
||||||
if ($isSceltaMultipla) {
|
if ($sourceType === 'API' && !empty($mapping['json_node'])) {
|
||||||
$mappingValue = 'manual';
|
|
||||||
} elseif ($hasAuto) {
|
|
||||||
$mappingValue = 'auto';
|
|
||||||
} elseif ($sourceType === 'XLS' && !empty($mapping['excel_column'])) {
|
|
||||||
$mappingValue = 'xls';
|
|
||||||
} elseif ($sourceType === 'API' && !empty($mapping['json_node'])) {
|
|
||||||
$mappingValue = 'json';
|
$mappingValue = 'json';
|
||||||
} elseif ((int)$mapping['is_manual'] === 1) {
|
} elseif ($sourceType === 'XLS' && !$isSceltaMultipla && !empty($mapping['excel_column'])) {
|
||||||
|
$mappingValue = 'xls';
|
||||||
|
} elseif (!$isSceltaMultipla && $hasAuto) {
|
||||||
|
$mappingValue = 'auto';
|
||||||
|
} elseif ((int)$mapping['is_manual'] === 1 || $isSceltaMultipla) {
|
||||||
$mappingValue = 'manual';
|
$mappingValue = 'manual';
|
||||||
} else {
|
} else {
|
||||||
$mappingValue = '';
|
$mappingValue = '';
|
||||||
@@ -469,17 +477,18 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
<select class="form-select mapping-select"
|
<select class="form-select mapping-select"
|
||||||
data-id="<?php echo (int)$mapping['id']; ?>"
|
data-id="<?php echo (int)$mapping['id']; ?>"
|
||||||
data-field-id="<?php echo (int)$mapping['field_id']; ?>"
|
data-field-id="<?php echo (int)$mapping['field_id']; ?>"
|
||||||
<?php echo $isSceltaMultipla ? 'disabled' : ''; ?>>
|
data-is-scelta-multipla="<?php echo $isSceltaMultipla ? '1' : '0'; ?>"
|
||||||
|
<?php echo ($isSceltaMultipla && $sourceType !== 'API') ? 'disabled' : ''; ?>>
|
||||||
|
|
||||||
|
<option value="">Select Option</option>
|
||||||
|
|
||||||
|
<?php if ($sourceType === 'XLS' && !$isSceltaMultipla): ?>
|
||||||
|
<option value="xls" <?php echo ($mappingValue === 'xls') ? 'selected' : ''; ?>>Map to XLS Column</option>
|
||||||
|
<?php elseif ($sourceType === 'API'): ?>
|
||||||
|
<option value="json" <?php echo ($mappingValue === 'json') ? 'selected' : ''; ?>>Map to JSON Node</option>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php if (!$isSceltaMultipla): ?>
|
<?php if (!$isSceltaMultipla): ?>
|
||||||
<option value="">Select Option</option>
|
|
||||||
|
|
||||||
<?php if ($sourceType === 'XLS'): ?>
|
|
||||||
<option value="xls" <?php echo ($mappingValue === 'xls') ? 'selected' : ''; ?>>Map to XLS Column</option>
|
|
||||||
<?php elseif ($sourceType === 'API'): ?>
|
|
||||||
<option value="json" <?php echo ($mappingValue === 'json') ? 'selected' : ''; ?>>Map to JSON Node</option>
|
|
||||||
<?php endif; ?>
|
|
||||||
|
|
||||||
<option value="auto" <?php echo ($mappingValue === 'auto') ? 'selected' : ''; ?>>Auto value</option>
|
<option value="auto" <?php echo ($mappingValue === 'auto') ? 'selected' : ''; ?>>Auto value</option>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
@@ -615,39 +624,68 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
<td><?php echo htmlspecialchars($fm['data_type']); ?></td>
|
<td><?php echo htmlspecialchars($fm['data_type']); ?></td>
|
||||||
|
|
||||||
<td>
|
<td>
|
||||||
|
<?php
|
||||||
|
$fixedDefaultSource = $fm['default_source'] ?? 'manual';
|
||||||
|
$fixedJsonNode = $fm['json_node'] ?? '';
|
||||||
|
$showFixedManual = ($fixedDefaultSource !== 'json');
|
||||||
|
$showFixedJson = ($fixedDefaultSource === 'json');
|
||||||
|
?>
|
||||||
|
|
||||||
<?php if ($fm['data_type'] === 'DATE'): ?>
|
<?php if ($sourceType === 'API'): ?>
|
||||||
|
<select class="form-select fixed-source-select"
|
||||||
<input type="date"
|
|
||||||
class="form-control fixed-default-input"
|
|
||||||
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
||||||
value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
style="margin-bottom:6px;">
|
||||||
|
<option value="manual" <?php echo ($fixedDefaultSource === 'manual') ? 'selected' : ''; ?>>
|
||||||
<?php elseif (in_array($fm['fixed_field_key'], [
|
Manual value
|
||||||
'ClienteResponsabile',
|
</option>
|
||||||
'ClienteFornitore',
|
<option value="json" <?php echo ($fixedDefaultSource === 'json') ? 'selected' : ''; ?>>
|
||||||
'ClienteAnalisi',
|
Map to JSON Node
|
||||||
'MoltiplicatorePrezzo',
|
</option>
|
||||||
'AnagraficaCertestObject',
|
|
||||||
'AnagraficaCertestService'
|
|
||||||
])): ?>
|
|
||||||
|
|
||||||
<select class="form-select fixed-default-select"
|
|
||||||
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
|
||||||
data-fixed-key="<?php echo htmlspecialchars($fm['fixed_field_key']); ?>"
|
|
||||||
data-current-value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
|
||||||
<option value="">Loading...</option>
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<?php else: ?>
|
<select class="form-select fixed-json-node-select"
|
||||||
|
|
||||||
<input type="text"
|
|
||||||
class="form-control fixed-default-input"
|
|
||||||
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
||||||
value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
data-current-json="<?php echo htmlspecialchars($fixedJsonNode, ENT_QUOTES, 'UTF-8'); ?>"
|
||||||
|
style="display:<?php echo $showFixedJson ? 'block' : 'none'; ?>; margin-bottom:6px;">
|
||||||
|
</select>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<div class="fixed-manual-wrapper"
|
||||||
|
style="display:<?php echo $showFixedManual ? 'block' : 'none'; ?>;">
|
||||||
|
|
||||||
|
<?php if ($fm['data_type'] === 'DATE'): ?>
|
||||||
|
|
||||||
|
<input type="date"
|
||||||
|
class="form-control fixed-default-input"
|
||||||
|
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
||||||
|
value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
||||||
|
|
||||||
|
<?php elseif (in_array($fm['fixed_field_key'], [
|
||||||
|
'ClienteResponsabile',
|
||||||
|
'ClienteFornitore',
|
||||||
|
'ClienteAnalisi',
|
||||||
|
'MoltiplicatorePrezzo',
|
||||||
|
'AnagraficaCertestObject',
|
||||||
|
'AnagraficaCertestService'
|
||||||
|
])): ?>
|
||||||
|
|
||||||
|
<select class="form-select fixed-default-select"
|
||||||
|
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
||||||
|
data-fixed-key="<?php echo htmlspecialchars($fm['fixed_field_key']); ?>"
|
||||||
|
data-current-value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
||||||
|
<option value="">Loading...</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<?php else: ?>
|
||||||
|
|
||||||
|
<input type="text"
|
||||||
|
class="form-control fixed-default-input"
|
||||||
|
data-fixed-id="<?php echo (int)$fm['id']; ?>"
|
||||||
|
value="<?php echo htmlspecialchars($fm['default_value'] ?? ''); ?>">
|
||||||
|
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
</tr>
|
</tr>
|
||||||
@@ -1183,6 +1221,46 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
initSelect2ForJsonDropdowns();
|
initSelect2ForJsonDropdowns();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateFixedJsonDropdowns() {
|
||||||
|
document.querySelectorAll('select.fixed-json-node-select').forEach(select => {
|
||||||
|
let currentValue = select.value || select.dataset.currentJson || '';
|
||||||
|
|
||||||
|
let options = availableJsonNodes
|
||||||
|
.map(node => {
|
||||||
|
const clean = String(node || '').trim();
|
||||||
|
if (!clean) return '';
|
||||||
|
|
||||||
|
const info = jsonNodeLabels[clean] || {};
|
||||||
|
const shortName = info.shortName || getLastJsonNodeName(clean);
|
||||||
|
const sample = info.sample || '';
|
||||||
|
|
||||||
|
let label = shortName;
|
||||||
|
if (sample) {
|
||||||
|
label += ` — ${sample}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSelected = clean === currentValue ? 'selected' : '';
|
||||||
|
|
||||||
|
return `
|
||||||
|
<option
|
||||||
|
value="${escapeHtmlAttr(clean)}"
|
||||||
|
data-short-name="${escapeHtmlAttr(shortName)}"
|
||||||
|
data-sample="${escapeHtmlAttr(sample)}"
|
||||||
|
data-full-path="${escapeHtmlAttr(clean)}"
|
||||||
|
${isSelected}>
|
||||||
|
${escapeHtmlText(label)}
|
||||||
|
</option>
|
||||||
|
`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
select.innerHTML = '<option value="">Select JSON Node</option>' + options;
|
||||||
|
select.dataset.currentJson = currentValue;
|
||||||
|
});
|
||||||
|
|
||||||
|
initSelect2ForFixedJsonDropdowns();
|
||||||
|
}
|
||||||
|
|
||||||
function initSelect2ForJsonDropdowns() {
|
function initSelect2ForJsonDropdowns() {
|
||||||
if (!(window.jQuery && $.fn.select2)) return;
|
if (!(window.jQuery && $.fn.select2)) return;
|
||||||
|
|
||||||
@@ -1266,7 +1344,81 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initSelect2ForFixedJsonDropdowns() {
|
||||||
|
if (!(window.jQuery && $.fn.select2)) return;
|
||||||
|
|
||||||
|
$('.fixed-json-node-select').each(function() {
|
||||||
|
const $el = $(this);
|
||||||
|
|
||||||
|
if (this.style.display === 'none') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($el.hasClass('select2-hidden-accessible')) {
|
||||||
|
$el.select2('destroy');
|
||||||
|
}
|
||||||
|
|
||||||
|
$el.select2({
|
||||||
|
width: '100%',
|
||||||
|
placeholder: 'Select JSON Node',
|
||||||
|
allowClear: true,
|
||||||
|
|
||||||
|
templateResult: function(data) {
|
||||||
|
if (!data.id) return data.text;
|
||||||
|
|
||||||
|
const option = data.element;
|
||||||
|
const shortName = option.getAttribute('data-short-name') || data.text;
|
||||||
|
const sample = option.getAttribute('data-sample') || '';
|
||||||
|
|
||||||
|
const $row = $('<span class="select2-json-row"></span>');
|
||||||
|
$row.append(`<span class="select2-json-node">${escapeHtmlText(shortName)}</span>`);
|
||||||
|
|
||||||
|
if (sample) {
|
||||||
|
$row.append(`<span class="select2-json-value">${escapeHtmlText(sample)}</span>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $row;
|
||||||
|
},
|
||||||
|
|
||||||
|
templateSelection: function(data) {
|
||||||
|
if (!data.id) return data.text;
|
||||||
|
|
||||||
|
const option = data.element;
|
||||||
|
const shortName = option.getAttribute('data-short-name') || data.text;
|
||||||
|
const sample = option.getAttribute('data-sample') || '';
|
||||||
|
|
||||||
|
if (sample) {
|
||||||
|
return `${shortName} — ${sample}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shortName;
|
||||||
|
},
|
||||||
|
|
||||||
|
matcher: function(params, data) {
|
||||||
|
if ($.trim(params.term) === '') {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
const term = params.term.toLowerCase();
|
||||||
|
const option = data.element;
|
||||||
|
|
||||||
|
const fullPath = option?.getAttribute('data-full-path')?.toLowerCase() || '';
|
||||||
|
const shortName = option?.getAttribute('data-short-name')?.toLowerCase() || '';
|
||||||
|
const sample = option?.getAttribute('data-sample')?.toLowerCase() || '';
|
||||||
|
|
||||||
|
if (
|
||||||
|
fullPath.includes(term) ||
|
||||||
|
shortName.includes(term) ||
|
||||||
|
sample.includes(term)
|
||||||
|
) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function saveJsonNodes(sampleJson, nodes) {
|
function saveJsonNodes(sampleJson, nodes) {
|
||||||
return fetch('update_api_json_nodes.php', {
|
return fetch('update_api_json_nodes.php', {
|
||||||
@@ -1411,6 +1563,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
availableJsonNodes = nodes;
|
availableJsonNodes = nodes;
|
||||||
usedJsonNodesFromDB = [];
|
usedJsonNodesFromDB = [];
|
||||||
updateJsonDropdowns();
|
updateJsonDropdowns();
|
||||||
|
updateFixedJsonDropdowns();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await saveJsonNodes(rawJson, nodes);
|
const data = await saveJsonNodes(rawJson, nodes);
|
||||||
@@ -1556,9 +1709,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mappingSelect = tr?.querySelector('.mapping-select');
|
||||||
|
const currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
|
||||||
|
|
||||||
saveMapping(
|
saveMapping(
|
||||||
mappingId,
|
mappingId,
|
||||||
'manual',
|
currentMappingType || 'manual',
|
||||||
el.value,
|
el.value,
|
||||||
xlsSelect ? xlsSelect.value : null,
|
xlsSelect ? xlsSelect.value : null,
|
||||||
null,
|
null,
|
||||||
@@ -1588,9 +1744,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mappingSelect = tr?.querySelector('.mapping-select');
|
||||||
|
const currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
|
||||||
|
|
||||||
saveMapping(
|
saveMapping(
|
||||||
mappingId,
|
mappingId,
|
||||||
'manual',
|
currentMappingType || 'manual',
|
||||||
el.value,
|
el.value,
|
||||||
xlsSelect ? xlsSelect.value : null,
|
xlsSelect ? xlsSelect.value : null,
|
||||||
null,
|
null,
|
||||||
@@ -1674,6 +1833,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
|
|
||||||
const removeBtn = tr.querySelector('.remove-xls');
|
const removeBtn = tr.querySelector('.remove-xls');
|
||||||
const removeJsonBtn = tr.querySelector('.remove-json');
|
const removeJsonBtn = tr.querySelector('.remove-json');
|
||||||
|
const isSceltaMultipla = mappingSelect.dataset.isSceltaMultipla === '1';
|
||||||
|
|
||||||
function destroyJsonSelect2() {
|
function destroyJsonSelect2() {
|
||||||
if (jsonSelect && window.jQuery && $(jsonSelect).hasClass('select2-hidden-accessible')) {
|
if (jsonSelect && window.jQuery && $(jsonSelect).hasClass('select2-hidden-accessible')) {
|
||||||
@@ -1716,8 +1876,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
if (autoSelect) autoSelect.style.display = 'none';
|
if (autoSelect) autoSelect.style.display = 'none';
|
||||||
|
|
||||||
if (manualInput) {
|
if (manualInput) {
|
||||||
manualInput.style.display = 'none';
|
if (isSceltaMultipla) {
|
||||||
manualInput.value = '';
|
manualInput.style.display = 'block';
|
||||||
|
} else {
|
||||||
|
manualInput.style.display = 'none';
|
||||||
|
manualInput.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedColumn) mappedColumn.style.display = 'none';
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
||||||
@@ -1958,13 +2122,19 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
let mappingId = event.target.getAttribute('data-id');
|
let mappingId = event.target.getAttribute('data-id');
|
||||||
let xlsSelect = tr.querySelector('.xls-columns');
|
let xlsSelect = tr.querySelector('.xls-columns');
|
||||||
let jsonSelect = tr.querySelector('.json-nodes');
|
let jsonSelect = tr.querySelector('.json-nodes');
|
||||||
|
let mappingSelect = tr.querySelector('.mapping-select');
|
||||||
|
|
||||||
|
let currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
|
||||||
|
|
||||||
console.log("Manual default dropdown changed:", {
|
console.log("Manual default dropdown changed:", {
|
||||||
id: mappingId,
|
id: mappingId,
|
||||||
value: event.target.value
|
value: event.target.value,
|
||||||
|
mappingType: currentMappingType
|
||||||
});
|
});
|
||||||
|
|
||||||
saveMapping(
|
saveMapping(
|
||||||
mappingId,
|
mappingId,
|
||||||
'manual',
|
currentMappingType || 'manual',
|
||||||
event.target.value,
|
event.target.value,
|
||||||
xlsSelect ? xlsSelect.value : null,
|
xlsSelect ? xlsSelect.value : null,
|
||||||
null,
|
null,
|
||||||
@@ -2063,7 +2233,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
mapping_type: mappingType,
|
mapping_type: mappingType,
|
||||||
excel_column: mappingType === 'xls' ? excelColumn : null,
|
excel_column: mappingType === 'xls' ? excelColumn : null,
|
||||||
json_node: mappingType === 'json' ? jsonNode : null,
|
json_node: mappingType === 'json' ? jsonNode : null,
|
||||||
manual_default: mappingType === 'manual' ? defaultValue : null,
|
manual_default: defaultValue,
|
||||||
auto_value: mappingType === 'auto' ? (autoValue || 'none') : 'none',
|
auto_value: mappingType === 'auto' ? (autoValue || 'none') : 'none',
|
||||||
tablename: "<?php echo $template['target_table']; ?>"
|
tablename: "<?php echo $template['target_table']; ?>"
|
||||||
})
|
})
|
||||||
@@ -2092,6 +2262,115 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
// FIXED FIELDS (DB autosave)
|
// FIXED FIELDS (DB autosave)
|
||||||
// =======================
|
// =======================
|
||||||
|
|
||||||
|
// =======================
|
||||||
|
// FIXED FIELDS: source manual/json autosave
|
||||||
|
// Only used for API/JSON templates
|
||||||
|
// =======================
|
||||||
|
document.addEventListener('change', function(e) {
|
||||||
|
if (!e.target.classList.contains('fixed-source-select')) return;
|
||||||
|
|
||||||
|
const sourceSelect = e.target;
|
||||||
|
const fixedId = sourceSelect.dataset.fixedId;
|
||||||
|
const tr = sourceSelect.closest('tr');
|
||||||
|
|
||||||
|
const manualWrapper = tr.querySelector('.fixed-manual-wrapper');
|
||||||
|
const jsonSelect = tr.querySelector('.fixed-json-node-select');
|
||||||
|
|
||||||
|
if (sourceSelect.value === 'json') {
|
||||||
|
if (manualWrapper) {
|
||||||
|
manualWrapper.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonSelect) {
|
||||||
|
jsonSelect.style.display = 'block';
|
||||||
|
updateFixedJsonDropdowns();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (jsonSelect) {
|
||||||
|
if (window.jQuery && $(jsonSelect).hasClass('select2-hidden-accessible')) {
|
||||||
|
$(jsonSelect).select2('destroy');
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonSelect.value = '';
|
||||||
|
jsonSelect.dataset.currentJson = '';
|
||||||
|
jsonSelect.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manualWrapper) {
|
||||||
|
manualWrapper.style.display = 'block';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch('update_fixed_field.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
id: fixedId,
|
||||||
|
field: 'default_source',
|
||||||
|
value: sourceSelect.value
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (!data.success) {
|
||||||
|
throw new Error(data.message || 'Update failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
fixedStatus('✅ Saved');
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error(error);
|
||||||
|
fixedStatus('❌ Save error', true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// =======================
|
||||||
|
// FIXED FIELDS: JSON node autosave
|
||||||
|
// =======================
|
||||||
|
function saveFixedJsonNode(selectEl) {
|
||||||
|
const fixedId = selectEl.dataset.fixedId;
|
||||||
|
const jsonNode = selectEl.value || '';
|
||||||
|
|
||||||
|
fetch('update_fixed_field.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
id: fixedId,
|
||||||
|
field: 'json_node',
|
||||||
|
value: jsonNode
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (!data.success) {
|
||||||
|
throw new Error(data.message || 'Update failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
selectEl.dataset.currentJson = jsonNode;
|
||||||
|
fixedStatus('✅ Saved');
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error(error);
|
||||||
|
fixedStatus('❌ Save error', true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('change', function(e) {
|
||||||
|
if (!e.target.classList.contains('fixed-json-node-select')) return;
|
||||||
|
|
||||||
|
saveFixedJsonNode(e.target);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (window.jQuery) {
|
||||||
|
$(document).on('select2:select select2:clear', '.fixed-json-node-select', function() {
|
||||||
|
saveFixedJsonNode(this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function escapeHtml(str) {
|
function escapeHtml(str) {
|
||||||
return String(str ?? '')
|
return String(str ?? '')
|
||||||
.replaceAll('&', '&')
|
.replaceAll('&', '&')
|
||||||
@@ -2328,6 +2607,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateJsonDropdowns();
|
updateJsonDropdowns();
|
||||||
|
updateFixedJsonDropdowns();
|
||||||
}
|
}
|
||||||
|
|
||||||
fillFixedDropdowns().then(() => {
|
fillFixedDropdowns().then(() => {
|
||||||
|
|||||||
@@ -3,33 +3,84 @@ header('Content-Type: application/json');
|
|||||||
|
|
||||||
require_once(__DIR__ . '/include/headscript.php');
|
require_once(__DIR__ . '/include/headscript.php');
|
||||||
|
|
||||||
$db = DBHandlerSelect::getInstance();
|
try {
|
||||||
$pdo = $db->getConnection();
|
$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);
|
$id = (int)($input['id'] ?? 0);
|
||||||
$field = (string)($input['field'] ?? '');
|
$field = (string)($input['field'] ?? '');
|
||||||
$value = $input['value'] ?? null;
|
$value = $input['value'] ?? null;
|
||||||
|
|
||||||
if ($id <= 0) {
|
if ($id <= 0) {
|
||||||
echo json_encode(['success' => false, 'message' => 'Invalid id']);
|
echo json_encode(['success' => false, 'message' => 'Invalid id']);
|
||||||
exit;
|
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