From 296143016a2c0bf683973c10a36fe29d22e3eec9 Mon Sep 17 00:00:00 2001 From: Claudio Date: Fri, 12 Sep 2025 10:01:19 +0200 Subject: [PATCH] template mapping addes is visible as checkbox and is required as badge --- .../userarea/mapping_template_xls_scheme2.php | 74 +++++++++++++------ public/userarea/update_visible_import.php | 26 +++++++ 2 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 public/userarea/update_visible_import.php diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php index 0436acd..37186bf 100644 --- a/public/userarea/mapping_template_xls_scheme2.php +++ b/public/userarea/mapping_template_xls_scheme2.php @@ -21,7 +21,7 @@ $schemajson = $template['schemajson'] ? json_decode($template['schemajson'], tru $isSchemajsonEmpty = empty(trim($template['schemajson'])); // Recupera i campi dalla tabella template_mapping -$stmt = $pdo->prepare("SELECT id, field_id, excel_column, is_manual, manual_default, data_type, is_required, default_value, has_list, length, decimals, min_value, max_value, default_curr_date, tablename, field_label, main_field FROM template_mapping WHERE template_id = ?"); +$stmt = $pdo->prepare("SELECT id, field_id, excel_column, is_manual, manual_default, data_type, is_required, default_value, has_list, length, decimals, min_value, max_value, default_curr_date, tablename, field_label, main_field, is_visible_import FROM template_mapping WHERE template_id = ?"); $stmt->execute([$id]); $mappings = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -83,8 +83,10 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t } #schemaFieldsTable th:first-child, - #schemaFieldsTable td:first-child { - width: 50px; + #schemaFieldsTable td:first-child, + #schemaFieldsTable th:nth-child(2), + #schemaFieldsTable td:nth-child(2) { + width: 100px; text-align: center; } @@ -137,8 +139,8 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t Main + Visible on Import Title - ID Type Mapping Default Value @@ -150,8 +152,15 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t > - - + + > + + + + + Required + + No schema associated.'; // Aggiornato colspan a 6 + document.getElementById('schemaFieldsBody').innerHTML = 'No schema associated.'; return; } @@ -445,7 +454,7 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t await saveSchemaJson(templateId, JSON.stringify(data)); alert('Schema updated successfully. Refresh the page to see changes.'); } catch (error) { - document.getElementById('schemaFieldsBody').innerHTML = 'Error: ' + error.message + ''; // Aggiornato colspan a 6 + document.getElementById('schemaFieldsBody').innerHTML = 'Error: ' + error.message + ''; } finally { updateSchemaButton.disabled = false; updateSchemaButton.textContent = 'Update Schema Details'; @@ -524,16 +533,11 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t .then(data => { if (!data.success) { console.error("❌ Error updating main_field:", data.message); - // Revert checkbox state on error checkbox.checked = !checkbox.checked; - // Riselezione visiva degli altri se errore - if (value === 1) { - document.querySelectorAll('.main-field-checkbox').forEach(cb => { - cb.checked = cb.dataset.originalChecked === 'true'; - }); - } + document.querySelectorAll('.main-field-checkbox').forEach(cb => { + cb.checked = cb.dataset.originalChecked === 'true'; + }); } else { - // Aggiorna lo stato originale dopo successo document.querySelectorAll('.main-field-checkbox').forEach(cb => { cb.dataset.originalChecked = cb.checked; }); @@ -542,12 +546,36 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t .catch(error => { console.error("❌ Fetch error:", error); checkbox.checked = !checkbox.checked; - // Riselezione visiva degli altri se errore - if (value === 1) { - document.querySelectorAll('.main-field-checkbox').forEach(cb => { - cb.checked = cb.dataset.originalChecked === 'true'; - }); + document.querySelectorAll('.main-field-checkbox').forEach(cb => { + cb.checked = cb.dataset.originalChecked === 'true'; + }); + }); + } else if (event.target.classList.contains('visible-import-checkbox')) { + const checkbox = event.target; + const mappingId = checkbox.dataset.mappingId; + const value = checkbox.checked ? 1 : 0; + + fetch('update_visible_import.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + template_id: , + mapping_id: mappingId, + value: value + }) + }) + .then(response => response.json()) + .then(data => { + if (!data.success) { + console.error("❌ Error updating is_visible_import:", data.message); + checkbox.checked = !checkbox.checked; } + }) + .catch(error => { + console.error("❌ Fetch error:", error); + checkbox.checked = !checkbox.checked; }); } }); @@ -569,7 +597,7 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t mappedColumn = document.createElement('span'); mappedColumn.className = 'mapped-column'; mappedColumn.style.marginLeft = '5px'; - tr.querySelector('td:nth-child(5)').appendChild(mappedColumn); // Aggiornato a nth-child(5) per la nuova colonna + tr.querySelector('td:nth-child(5)').appendChild(mappedColumn); } if (!removeBtn) { removeBtn = document.createElement('button'); @@ -577,7 +605,7 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t removeBtn.textContent = 'X'; removeBtn.style.marginLeft = '5px'; removeBtn.setAttribute('data-id', mappingId); - tr.querySelector('td:nth-child(5)').appendChild(removeBtn); // Aggiornato a nth-child(5) + tr.querySelector('td:nth-child(5)').appendChild(removeBtn); removeBtn.addEventListener('click', function(e) { let tr = e.target.closest('tr'); diff --git a/public/userarea/update_visible_import.php b/public/userarea/update_visible_import.php new file mode 100644 index 0000000..75651ad --- /dev/null +++ b/public/userarea/update_visible_import.php @@ -0,0 +1,26 @@ + false, 'message' => 'Invalid input']); + exit; +} + +$db = DBHandlerSelect::getInstance(); +$pdo = $db->getConnection(); + +$stmt = $pdo->prepare("UPDATE template_mapping SET is_visible_import = ? WHERE id = ? AND template_id = ?"); +$result = $stmt->execute([$value, $mapping_id, $template_id]); + +if ($result) { + echo json_encode(['success' => true]); +} else { + echo json_encode(['success' => false, 'message' => 'Failed to update is_visible_import']); +}