diff --git a/public/userarea/include/navbar.php b/public/userarea/include/navbar.php
index 5df1b53..128e77b 100644
--- a/public/userarea/include/navbar.php
+++ b/public/userarea/include/navbar.php
@@ -22,7 +22,7 @@
- - XLS Import
+
- Import AREA
diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php
index f36ca48..eb25441 100644
--- a/public/userarea/mapping_template_xls_scheme2.php
+++ b/public/userarea/mapping_template_xls_scheme2.php
@@ -87,10 +87,19 @@ $mappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Recupera i fixed fields dalla tabella template_fixed_mapping
$stmt = $pdo->prepare("
- SELECT id, fixed_field_key, is_manual, data_type, is_required, default_value, is_visible_import
- FROM template_fixed_mapping
- WHERE template_id = ?
- ORDER BY id ASC
+ SELECT
+ id,
+ fixed_field_key,
+ 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]);
$fixedMappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -447,19 +456,18 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
"
data-field-id=""
- >
+ data-is-scelta-multipla=""
+ >
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
@@ -615,39 +624,68 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
| |
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
@@ -1183,6 +1221,46 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
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 `
+
+ `;
+ })
+ .join('');
+
+ select.innerHTML = '' + options;
+ select.dataset.currentJson = currentValue;
+ });
+
+ initSelect2ForFixedJsonDropdowns();
+ }
+
function initSelect2ForJsonDropdowns() {
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 = $('');
+ $row.append(`${escapeHtmlText(shortName)}`);
+
+ if (sample) {
+ $row.append(`${escapeHtmlText(sample)}`);
+ }
+
+ 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) {
return fetch('update_api_json_nodes.php', {
@@ -1411,6 +1563,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
availableJsonNodes = nodes;
usedJsonNodesFromDB = [];
updateJsonDropdowns();
+ updateFixedJsonDropdowns();
try {
const data = await saveJsonNodes(rawJson, nodes);
@@ -1556,9 +1709,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
return;
}
+ const mappingSelect = tr?.querySelector('.mapping-select');
+ const currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
+
saveMapping(
mappingId,
- 'manual',
+ currentMappingType || 'manual',
el.value,
xlsSelect ? xlsSelect.value : null,
null,
@@ -1588,9 +1744,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
return;
}
+ const mappingSelect = tr?.querySelector('.mapping-select');
+ const currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
+
saveMapping(
mappingId,
- 'manual',
+ currentMappingType || 'manual',
el.value,
xlsSelect ? xlsSelect.value : null,
null,
@@ -1674,6 +1833,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
const removeBtn = tr.querySelector('.remove-xls');
const removeJsonBtn = tr.querySelector('.remove-json');
+ const isSceltaMultipla = mappingSelect.dataset.isSceltaMultipla === '1';
function destroyJsonSelect2() {
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 (manualInput) {
- manualInput.style.display = 'none';
- manualInput.value = '';
+ if (isSceltaMultipla) {
+ manualInput.style.display = 'block';
+ } else {
+ manualInput.style.display = 'none';
+ manualInput.value = '';
+ }
}
if (mappedColumn) mappedColumn.style.display = 'none';
@@ -1958,13 +2122,19 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
let mappingId = event.target.getAttribute('data-id');
let xlsSelect = tr.querySelector('.xls-columns');
let jsonSelect = tr.querySelector('.json-nodes');
+ let mappingSelect = tr.querySelector('.mapping-select');
+
+ let currentMappingType = mappingSelect ? mappingSelect.value : 'manual';
+
console.log("Manual default dropdown changed:", {
id: mappingId,
- value: event.target.value
+ value: event.target.value,
+ mappingType: currentMappingType
});
+
saveMapping(
mappingId,
- 'manual',
+ currentMappingType || 'manual',
event.target.value,
xlsSelect ? xlsSelect.value : null,
null,
@@ -2063,7 +2233,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
mapping_type: mappingType,
excel_column: mappingType === 'xls' ? excelColumn : null,
json_node: mappingType === 'json' ? jsonNode : null,
- manual_default: mappingType === 'manual' ? defaultValue : null,
+ manual_default: defaultValue,
auto_value: mappingType === 'auto' ? (autoValue || 'none') : 'none',
tablename: ""
})
@@ -2092,6 +2262,115 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
// 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) {
return String(str ?? '')
.replaceAll('&', '&')
@@ -2328,6 +2607,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
}
updateJsonDropdowns();
+ updateFixedJsonDropdowns();
}
fillFixedDropdowns().then(() => {
diff --git a/public/userarea/update_fixed_field.php b/public/userarea/update_fixed_field.php
index 71b6c97..023cdf7 100644
--- a/public/userarea/update_fixed_field.php
+++ b/public/userarea/update_fixed_field.php
@@ -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]);