721 lines
38 KiB
PHP
721 lines
38 KiB
PHP
<?php include('include/headscript.php');
|
|
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
die("Invalid template ID");
|
|
}
|
|
|
|
$id = intval($_GET['id']);
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
$stmt = $pdo->prepare("SELECT name, header_row, start_column, target_table, sample_xlsx, idclient, clientname, idschema, schemaname, schemajson, xls_headers FROM excel_templates WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$template = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$template) {
|
|
die("Template not found");
|
|
}
|
|
|
|
$clientName = $template['clientname'] ?: '';
|
|
$schemaName = $template['schemaname'] ?: '';
|
|
$schemajson = $template['schemajson'] ? json_decode($template['schemajson'], true) : [];
|
|
$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, is_visible_import FROM template_mapping WHERE template_id = ?");
|
|
$stmt->execute([$id]);
|
|
$mappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Recupera le colonne già associate nel database
|
|
$usedColumnsFromDB = array_filter(array_column($mappings, 'excel_column'));
|
|
|
|
// Decodifica l'header XLS salvato, se presente
|
|
$xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], true) : [];
|
|
?>
|
|
|
|
<!doctype html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
|
<?php include('cssinclude.php'); ?>
|
|
<title>Configure Template <?= htmlspecialchars($template['name'], ENT_QUOTES, 'UTF-8'); ?></title>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
|
|
<style>
|
|
.dropdown-select {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
border: 1px solid #ced4da;
|
|
border-radius: 4px;
|
|
padding: 5px;
|
|
font-size: 14px;
|
|
appearance: none;
|
|
background: white url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="%23333"><path d="M7.293 4.293a1 1 0 011.414 0L10 6.586l1.293-1.293a1 1 0 111.414 1.414l-2 2a1 1 0 01-1.414 0l-2-2a1 1 0 010-1.414z"/></svg>') no-repeat right 5px center;
|
|
}
|
|
|
|
.dropdown-select:focus {
|
|
outline: none;
|
|
border-color: #80bdff;
|
|
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
|
|
}
|
|
|
|
#loading-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.7);
|
|
z-index: 9999;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
#loading-overlay div {
|
|
color: white;
|
|
font-size: 24px;
|
|
padding: 20px;
|
|
background: #333;
|
|
border-radius: 10px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
|
|
}
|
|
|
|
#schemaFieldsTable th:first-child,
|
|
#schemaFieldsTable td:first-child,
|
|
#schemaFieldsTable th:nth-child(2),
|
|
#schemaFieldsTable td:nth-child(2) {
|
|
width: 100px;
|
|
text-align: center;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="wrapper">
|
|
<?php include('include/navbar.php'); ?>
|
|
<?php include('include/topbar.php'); ?>
|
|
<div class="page-wrapper">
|
|
<div class="page-content">
|
|
<?php include('top_stat_widget.php'); ?>
|
|
|
|
<div class="card radius-10">
|
|
<div class="card-header">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<h6 class="mb-0">Configure Template: <span id="templateName"><?php echo htmlspecialchars($template['name']); ?></span></h6>
|
|
<p>
|
|
Client: <span id="clientName"><?php echo htmlspecialchars($clientName); ?></span> |
|
|
Schema: <span id="schemaName"><?php echo htmlspecialchars($schemaName); ?></span> |
|
|
Header Row: <span id="headerRow"><?php echo $template['header_row']; ?></span> |
|
|
Start Column: <span id="startColumn"><?php echo htmlspecialchars($template['start_column']); ?></span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="mb-4">
|
|
<label class="form-label">Upload XLS Example:</label>
|
|
<input type="file" id="xlsUpload" class="form-control">
|
|
<small id="uploadStatus">
|
|
<?php if (!empty($template['sample_xlsx'])): ?>
|
|
✅ Current file: <a href="xlstemplates/<?php echo htmlspecialchars($template['sample_xlsx']); ?>" target="_blank"><?php echo htmlspecialchars($template['sample_xlsx']); ?></a>
|
|
<?php else: ?>
|
|
No file uploaded yet.
|
|
<?php endif; ?>
|
|
</small>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<div class="d-flex align-items-center mb-2">
|
|
<h5>Schema Fields Configuration</h5>
|
|
<button id="updateSchemaButton" class="btn btn-primary ms-2" data-empty="<?php echo $isSchemajsonEmpty ? 'true' : 'false'; ?>">
|
|
<?php echo $isSchemajsonEmpty ? 'Load Schema Details' : 'Update Schema Details'; ?>
|
|
</button>
|
|
</div>
|
|
<table id="schemaFieldsTable" class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Main</th>
|
|
<th>Visible on Import</th>
|
|
<th>Title</th>
|
|
<th>Type</th>
|
|
<th>Mapping</th>
|
|
<th>Default Value</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="schemaFieldsBody">
|
|
<?php foreach ($mappings as $mapping): ?>
|
|
<tr>
|
|
<td>
|
|
<input type="checkbox" class="main-field-checkbox" data-mapping-id="<?php echo $mapping['id']; ?>" <?php echo $mapping['main_field'] == 1 ? 'checked' : ''; ?>>
|
|
</td>
|
|
<td>
|
|
<input type="checkbox" class="visible-import-checkbox" data-mapping-id="<?php echo $mapping['id']; ?>" <?php echo (isset($mapping['is_visible_import']) ? $mapping['is_visible_import'] : 1) == 1 ? 'checked' : ''; ?>>
|
|
</td>
|
|
<td>
|
|
<?php echo htmlspecialchars($mapping['field_label'] ?? 'N/A'); ?>
|
|
<?php if ($mapping['is_required'] == 1): ?>
|
|
<span class="badge bg-danger ms-2">Required</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?php echo htmlspecialchars($mapping['data_type'] ?? 'N/A'); ?></td>
|
|
<td>
|
|
<?php
|
|
$isSceltaMultipla = $mapping['data_type'] === 'SceltaMultipla';
|
|
$mappingValue = $isSceltaMultipla ? 'manual' : ($mapping['excel_column'] ? 'xls' : ($mapping['is_manual'] ? 'manual' : ''));
|
|
?>
|
|
<select class="form-select mapping-select" data-id="<?php echo $mapping['id']; ?>" data-field-id="<?php echo $mapping['field_id']; ?>" <?php echo $isSceltaMultipla ? 'disabled' : ''; ?>>
|
|
<?php if (!$isSceltaMultipla): ?>
|
|
<option value="">Select Option</option>
|
|
<option value="xls" <?php echo $mappingValue === 'xls' ? 'selected' : ''; ?>>Map to XLS Column</option>
|
|
<?php endif; ?>
|
|
<option value="manual" <?php echo $mappingValue === 'manual' ? 'selected' : ''; ?>>Manual Entry</option>
|
|
</select>
|
|
<select class="form-select xls-columns" style="display:<?php echo $mappingValue === 'xls' ? 'block' : 'none'; ?>" data-id="<?php echo $mapping['id']; ?>" <?php echo $mapping['excel_column'] ? 'data-current-xls="' . htmlspecialchars($mapping['excel_column']) . '"' : ''; ?>></select>
|
|
<?php if ($mapping['excel_column']): ?>
|
|
<span class="mapped-column" style="margin-left: 5px;"><?php echo htmlspecialchars($mapping['excel_column']); ?></span>
|
|
<button class="btn btn-danger btn-sm remove-xls" data-id="<?php echo $mapping['id']; ?>" style="margin-left: 5px;">X</button>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<?php if ($mapping['data_type'] === 'SceltaMultipla'): ?>
|
|
<select class="form-select dropdown-select manual-default" data-id="<?php echo $mapping['id']; ?>" data-field-id="<?php echo $mapping['field_id']; ?>" data-manual-default="<?php echo htmlspecialchars($mapping['manual_default'] ?? ''); ?>" style="display:block;">
|
|
<option value="">Seleziona...</option>
|
|
</select>
|
|
<?php else: ?>
|
|
<input type="text" class="form-control manual-default" placeholder="Default value" value="<?php echo htmlspecialchars($mapping['manual_default'] ?? ''); ?>" style="display:<?php echo $mapping['is_manual'] ? 'block' : 'none'; ?>" data-field-id="<?php echo $mapping['field_id']; ?>">
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-4 text-end">
|
|
<a href="templates_dashboard.php" class="btn btn-primary">⬅ Back to Template Dashboard</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="overlay toggle-icon"></div>
|
|
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
|
|
<?php include('jsinclude.php'); ?>
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
<script>
|
|
let availableXlsColumns = <?php echo json_encode($xlsHeaders); ?> || [];
|
|
let usedColumnsFromDB = <?php echo json_encode($usedColumnsFromDB); ?> || [];
|
|
|
|
document.getElementById('xlsUpload').addEventListener('change', function(event) {
|
|
let file = event.target.files[0];
|
|
if (!file) return;
|
|
|
|
let formData = new FormData();
|
|
formData.append("xls_file", file);
|
|
formData.append("template_id", <?php echo $id; ?>);
|
|
|
|
let statusText = document.getElementById('uploadStatus');
|
|
statusText.innerText = "Uploading...";
|
|
|
|
fetch('upload_xls_example.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (!data.success) {
|
|
statusText.innerText = "❌ Upload failed: " + data.message;
|
|
return;
|
|
}
|
|
statusText.innerHTML = `✅ File uploaded: <a href="xlstemplates/${data.filename}" target="_blank">${data.filename}</a>`;
|
|
processXLSX(file);
|
|
})
|
|
.catch(error => {
|
|
statusText.innerText = "❌ Upload failed. Check console.";
|
|
console.error(error);
|
|
});
|
|
});
|
|
|
|
function processXLSX(file) {
|
|
let reader = new FileReader();
|
|
reader.onload = function(e) {
|
|
let data = new Uint8Array(e.target.result);
|
|
let workbook = XLSX.read(data, {
|
|
type: 'array'
|
|
});
|
|
let sheet = workbook.Sheets[workbook.SheetNames[0]];
|
|
let rowIndex = parseInt(document.getElementById('headerRow').textContent) || 1;
|
|
let startColumn = parseInt(document.getElementById('startColumn').textContent) || 1;
|
|
|
|
let sheetData = XLSX.utils.sheet_to_json(sheet, {
|
|
header: 1,
|
|
defval: "",
|
|
raw: false,
|
|
range: 0
|
|
});
|
|
console.log("Dati della riga " + rowIndex + ":", sheetData[rowIndex - 1]);
|
|
if (!sheetData[rowIndex - 1]) {
|
|
document.getElementById('schemaFieldsBody').querySelectorAll('select.xls-columns').forEach(select => {
|
|
select.innerHTML = '<option value="">Nessuna intestazione trovata</option>';
|
|
});
|
|
return;
|
|
}
|
|
|
|
let headers = sheetData[rowIndex - 1].slice(startColumn - 1).map(header => header === undefined ? "" : header);
|
|
console.log("Intestazioni estratte:", headers);
|
|
availableXlsColumns = [...headers];
|
|
usedColumnsFromDB = [];
|
|
saveXlsHeaders(headers);
|
|
updateXlsDropdowns();
|
|
};
|
|
reader.readAsArrayBuffer(file);
|
|
}
|
|
|
|
function saveXlsHeaders(headers) {
|
|
fetch('update_xls_headers.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
template_id: <?php echo $id; ?>,
|
|
xls_headers: JSON.stringify(headers)
|
|
})
|
|
}).then(response => response.json())
|
|
.then(data => {
|
|
if (!data.success) console.error("❌ Error saving XLS headers:", data.message);
|
|
})
|
|
.catch(error => console.error("❌ Fetch error:", error));
|
|
}
|
|
|
|
function updateXlsDropdowns() {
|
|
let usedColumns = Array.from(document.querySelectorAll('select.xls-columns'))
|
|
.filter(select => select.style.display === 'block' && select.value)
|
|
.map(select => select.value)
|
|
.concat(usedColumnsFromDB);
|
|
|
|
document.querySelectorAll('select.xls-columns').forEach(select => {
|
|
let currentValue = select.value || select.dataset.currentXls || '';
|
|
let options = availableXlsColumns
|
|
.filter(col => !usedColumns.includes(col) || col === currentValue)
|
|
.map(col => `<option value="${col}" ${col === currentValue ? 'selected' : ''}>${col}</option>`)
|
|
.join('');
|
|
select.innerHTML = '<option value="">Select XLS Column</option>' + options;
|
|
select.dataset.currentXls = currentValue;
|
|
if (currentValue && !options.includes(currentValue)) {
|
|
select.value = '';
|
|
}
|
|
});
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
let templateId = <?php echo $id; ?>;
|
|
let schemaId = <?php echo json_encode($template['idschema'] ?? 0); ?>;
|
|
let isSchemajsonEmpty = <?php echo json_encode($isSchemajsonEmpty); ?>;
|
|
|
|
// Crea l'overlay di caricamento
|
|
const loadingOverlay = document.createElement('div');
|
|
loadingOverlay.id = 'loading-overlay';
|
|
loadingOverlay.innerHTML = '<div>Loading Dropdown Options...</div>';
|
|
document.body.appendChild(loadingOverlay);
|
|
|
|
async function populateDropdowns() {
|
|
const dropdowns = document.querySelectorAll('.dropdown-select');
|
|
if (dropdowns.length === 0) {
|
|
console.warn('Nessun dropdown di tipo SceltaMultipla trovato.');
|
|
return;
|
|
}
|
|
|
|
console.log(`Trovati ${dropdowns.length} dropdown da popolare.`);
|
|
|
|
const uniqueFieldIds = [...new Set(Array.from(dropdowns).map(d => d.getAttribute('data-field-id')))].filter(fieldId => fieldId);
|
|
|
|
if (uniqueFieldIds.length === 0) {
|
|
console.warn('Nessun field_id valido trovato per i dropdown.');
|
|
dropdowns.forEach(dropdown => {
|
|
dropdown.innerHTML = '<option value="">Nessun field_id valido</option>';
|
|
dropdown.disabled = true;
|
|
});
|
|
return;
|
|
}
|
|
|
|
console.log('Field IDs univoci:', uniqueFieldIds);
|
|
|
|
// Mostra l'overlay di caricamento
|
|
const loadingOverlay = document.getElementById('loading-overlay');
|
|
loadingOverlay.style.display = 'flex';
|
|
|
|
let dropdownData = {};
|
|
|
|
try {
|
|
// Usa field_ids come previsto dal backend
|
|
const fieldIdsQuery = uniqueFieldIds.join(',');
|
|
console.log(`Recupero dati per field_ids: ${fieldIdsQuery}`);
|
|
const response = await fetch(`get_customfield_values.php?field_ids=${fieldIdsQuery}`);
|
|
if (!response.ok) {
|
|
throw new Error(`Errore HTTP: ${response.status} ${response.statusText}`);
|
|
}
|
|
const data = await response.json();
|
|
if (data.error) {
|
|
throw new Error(`Errore API: ${data.error}`);
|
|
}
|
|
dropdownData = data; // data è un oggetto come { "146": [], "156": [...] }
|
|
console.log('Dati totali restituiti:', dropdownData);
|
|
|
|
// Popola i dropdown
|
|
dropdowns.forEach(dropdown => {
|
|
const fieldId = dropdown.getAttribute('data-field-id');
|
|
const manualDefault = dropdown.getAttribute('data-manual-default') || '';
|
|
console.log(`Popolamento dropdown per field_id: ${fieldId}, manual_default: ${manualDefault}`);
|
|
|
|
dropdown.innerHTML = '<option value="">Seleziona...</option>';
|
|
|
|
if (!fieldId || !dropdownData[fieldId] || dropdownData[fieldId].length === 0) {
|
|
console.warn(`Nessun dato disponibile per field_id ${fieldId}`);
|
|
dropdown.innerHTML = `<option value="">Nessun valore (field_id ${fieldId} vuoto)</option>`;
|
|
dropdown.disabled = true; // Disabilita per evitare selezioni inutili
|
|
return;
|
|
}
|
|
|
|
dropdownData[fieldId].forEach(value => {
|
|
const option = document.createElement('option');
|
|
option.value = value.IdCustomFieldsValue;
|
|
option.textContent = value.Valore;
|
|
if (manualDefault && String(value.IdCustomFieldsValue) === String(manualDefault)) {
|
|
option.selected = true;
|
|
}
|
|
dropdown.appendChild(option);
|
|
});
|
|
dropdown.disabled = false;
|
|
});
|
|
} catch (error) {
|
|
console.error('Errore generale nel caricamento dei dropdown:', error);
|
|
dropdowns.forEach(dropdown => {
|
|
dropdown.innerHTML = '<option value="">Errore nel caricamento</option>';
|
|
dropdown.disabled = true;
|
|
});
|
|
} finally {
|
|
console.log('Nascondo overlay di caricamento.');
|
|
loadingOverlay.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
// Carica i dropdown con overlay
|
|
async function loadDropdownsWithOverlay() {
|
|
console.log('Inizio caricamento tendine');
|
|
loadingOverlay.style.display = 'flex';
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
await populateDropdowns();
|
|
console.log('Caricamento tendine completato');
|
|
loadingOverlay.style.display = 'none';
|
|
}
|
|
|
|
loadDropdownsWithOverlay();
|
|
|
|
async function loadClientAndSchemaNames() {
|
|
if (<?php echo json_encode($template['idclient'] ?? 0); ?> > 0) {
|
|
let response = await fetch(`get_clienti.php?id=<?php echo $template['idclient']; ?>`);
|
|
let data = await response.json();
|
|
if (response.ok && data.value?.length) document.getElementById('clientName').textContent = data.value[0].Nominativo || 'N/A';
|
|
}
|
|
if (schemaId > 0) {
|
|
let response = await fetch(`get_schemi.php?id=${schemaId}`);
|
|
let data = await response.json();
|
|
if (response.ok && data.value?.length) document.getElementById('schemaName').textContent = data.value[0].Nome || 'N/A';
|
|
}
|
|
}
|
|
|
|
loadClientAndSchemaNames();
|
|
|
|
async function updateSchemaDetails() {
|
|
if (!schemaId) {
|
|
document.getElementById('schemaFieldsBody').innerHTML = '<tr><td colspan="6" class="text-warning">No schema associated.</td></tr>';
|
|
return;
|
|
}
|
|
|
|
let updateSchemaButton = document.getElementById('updateSchemaButton');
|
|
updateSchemaButton.disabled = true;
|
|
updateSchemaButton.textContent = 'Loading...';
|
|
|
|
try {
|
|
let response = await fetch(`get_schema_details.php?id=${schemaId}`);
|
|
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
|
let data = JSON.parse(await response.text());
|
|
if (!data.SchemiCustomFieldsDettagli) throw new Error('Missing "SchemiCustomFieldsDettagli"');
|
|
|
|
await saveSchemaJson(templateId, JSON.stringify(data));
|
|
alert('Schema updated successfully. Refresh the page to see changes.');
|
|
} catch (error) {
|
|
document.getElementById('schemaFieldsBody').innerHTML = '<tr><td colspan="6" class="text-danger">Error: ' + error.message + '</td></tr>';
|
|
} finally {
|
|
updateSchemaButton.disabled = false;
|
|
updateSchemaButton.textContent = 'Update Schema Details';
|
|
}
|
|
}
|
|
|
|
async function saveSchemaJson(templateId, schemaJson) {
|
|
let response = await fetch('update_schemajson.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
template_id: templateId,
|
|
schemajson: schemaJson
|
|
})
|
|
});
|
|
let data = await response.json();
|
|
if (!data.success) throw new Error(data.message || 'Failed to save schema JSON');
|
|
}
|
|
|
|
document.getElementById('updateSchemaButton').addEventListener('click', updateSchemaDetails);
|
|
|
|
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
|
if (event.target.classList.contains('mapping-select')) {
|
|
let tr = event.target.closest('tr');
|
|
let mappingId = event.target.getAttribute('data-id');
|
|
let xlsSelect = tr.querySelector('.xls-columns');
|
|
let manualInput = tr.querySelector('.manual-default');
|
|
let mappedColumn = tr.querySelector('.mapped-column');
|
|
let removeBtn = tr.querySelector('.remove-xls');
|
|
if (event.target.value === 'xls') {
|
|
xlsSelect.style.display = 'block';
|
|
manualInput.style.display = 'none';
|
|
manualInput.value = '';
|
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
|
if (removeBtn) removeBtn.style.display = xlsSelect.value ? 'inline-block' : 'none';
|
|
} else if (event.target.value === 'manual') {
|
|
xlsSelect.style.display = 'none';
|
|
manualInput.style.display = 'block';
|
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
|
if (removeBtn) removeBtn.style.display = 'none';
|
|
} else {
|
|
xlsSelect.style.display = 'none';
|
|
manualInput.style.display = 'none';
|
|
manualInput.value = '';
|
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
|
if (removeBtn) removeBtn.style.display = 'none';
|
|
}
|
|
saveMapping(mappingId, event.target.value, manualInput.value, xlsSelect.value);
|
|
updateXlsDropdowns();
|
|
} else if (event.target.classList.contains('main-field-checkbox')) {
|
|
const checkbox = event.target;
|
|
const mappingId = checkbox.dataset.mappingId;
|
|
const value = checkbox.checked ? 1 : 0;
|
|
|
|
// Se checked, deseleziona tutti gli altri visivamente
|
|
if (checkbox.checked) {
|
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
|
if (cb !== checkbox) cb.checked = false;
|
|
});
|
|
}
|
|
|
|
// Salva l'aggiornamento
|
|
fetch('update_main_field.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
template_id: <?php echo $id; ?>,
|
|
mapping_id: mappingId,
|
|
value: value
|
|
})
|
|
}).then(response => response.json())
|
|
.then(data => {
|
|
if (!data.success) {
|
|
console.error("❌ Error updating main_field:", data.message);
|
|
checkbox.checked = !checkbox.checked;
|
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
|
cb.checked = cb.dataset.originalChecked === 'true';
|
|
});
|
|
} else {
|
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
|
cb.dataset.originalChecked = cb.checked;
|
|
});
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error("❌ Fetch error:", error);
|
|
checkbox.checked = !checkbox.checked;
|
|
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: <?php echo $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;
|
|
});
|
|
}
|
|
});
|
|
|
|
// Salva lo stato originale dei checkbox al caricamento
|
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
|
cb.dataset.originalChecked = cb.checked;
|
|
});
|
|
|
|
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
|
if (event.target.classList.contains('xls-columns')) {
|
|
let tr = event.target.closest('tr');
|
|
let mappingId = event.target.getAttribute('data-id');
|
|
let manualInput = tr.querySelector('.manual-default');
|
|
let mappedColumn = tr.querySelector('.mapped-column');
|
|
let removeBtn = tr.querySelector('.remove-xls');
|
|
|
|
if (!mappedColumn) {
|
|
mappedColumn = document.createElement('span');
|
|
mappedColumn.className = 'mapped-column';
|
|
mappedColumn.style.marginLeft = '5px';
|
|
tr.querySelector('td:nth-child(5)').appendChild(mappedColumn);
|
|
}
|
|
if (!removeBtn) {
|
|
removeBtn = document.createElement('button');
|
|
removeBtn.className = 'btn btn-danger btn-sm remove-xls';
|
|
removeBtn.textContent = 'X';
|
|
removeBtn.style.marginLeft = '5px';
|
|
removeBtn.setAttribute('data-id', mappingId);
|
|
tr.querySelector('td:nth-child(5)').appendChild(removeBtn);
|
|
|
|
removeBtn.addEventListener('click', function(e) {
|
|
let tr = e.target.closest('tr');
|
|
let xlsSelect = tr.querySelector('.xls-columns');
|
|
let mappingSelect = tr.querySelector('.mapping-select');
|
|
xlsSelect.value = '';
|
|
xlsSelect.style.display = 'none';
|
|
mappingSelect.value = '';
|
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
|
e.target.style.display = 'none';
|
|
saveMapping(mappingId, '', '', null);
|
|
updateXlsDropdowns();
|
|
});
|
|
}
|
|
|
|
console.log("XLS Column changed:", {
|
|
id: mappingId,
|
|
value: event.target.value
|
|
});
|
|
if (mappedColumn) {
|
|
mappedColumn.textContent = event.target.value ? `(${event.target.value})` : '';
|
|
mappedColumn.style.display = event.target.value ? 'inline' : 'none';
|
|
}
|
|
if (removeBtn) removeBtn.style.display = event.target.value ? 'inline-block' : 'none';
|
|
saveMapping(mappingId, 'xls', manualInput.value, event.target.value);
|
|
updateXlsDropdowns();
|
|
}
|
|
});
|
|
|
|
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
|
if (event.target.classList.contains('manual-default') && event.target.tagName === 'SELECT') {
|
|
let tr = event.target.closest('tr');
|
|
let mappingId = event.target.getAttribute('data-id');
|
|
let xlsSelect = tr.querySelector('.xls-columns');
|
|
console.log("Manual default dropdown changed:", {
|
|
id: mappingId,
|
|
value: event.target.value
|
|
});
|
|
saveMapping(mappingId, 'manual', event.target.value, xlsSelect.value);
|
|
}
|
|
});
|
|
|
|
document.getElementById('schemaFieldsBody').addEventListener('input', function(event) {
|
|
if (event.target.classList.contains('manual-default') && event.target.tagName === 'INPUT') {
|
|
let tr = event.target.closest('tr');
|
|
let mappingId = tr.querySelector('.mapping-select').getAttribute('data-id');
|
|
let xlsSelect = tr.querySelector('.xls-columns');
|
|
console.log("Manual default input changed:", {
|
|
id: mappingId,
|
|
value: event.target.value
|
|
});
|
|
saveMapping(mappingId, 'manual', event.target.value, xlsSelect.value);
|
|
}
|
|
});
|
|
|
|
document.getElementById('schemaFieldsBody').addEventListener('click', function(event) {
|
|
if (event.target.classList.contains('remove-xls')) {
|
|
let mappingId = event.target.getAttribute('data-id');
|
|
let tr = event.target.closest('tr');
|
|
let xlsSelect = tr.querySelector('.xls-columns');
|
|
let mappingSelect = tr.querySelector('.mapping-select');
|
|
let mappedColumn = tr.querySelector('.mapped-column');
|
|
xlsSelect.value = '';
|
|
xlsSelect.style.display = 'none';
|
|
mappingSelect.value = '';
|
|
if (mappedColumn) mappedColumn.style.display = 'none';
|
|
event.target.style.display = 'none';
|
|
console.log("Removing XLS mapping:", {
|
|
id: mappingId
|
|
});
|
|
saveMapping(mappingId, '', '', null);
|
|
updateXlsDropdowns();
|
|
}
|
|
});
|
|
|
|
function saveMapping(mappingId, mappingType, defaultValue, excelColumn) {
|
|
console.log("Saving mapping:", {
|
|
id: mappingId,
|
|
mapping_type: mappingType,
|
|
excel_column: excelColumn,
|
|
manual_default: defaultValue
|
|
});
|
|
fetch('save_mapping_json.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
id: mappingId,
|
|
mapping_type: mappingType,
|
|
excel_column: mappingType === 'xls' ? excelColumn : null,
|
|
manual_default: mappingType === 'manual' ? defaultValue : null,
|
|
tablename: "<?php echo $template['target_table']; ?>"
|
|
})
|
|
}).then(response => response.json())
|
|
.then(data => {
|
|
console.log("Save response:", data);
|
|
if (!data.success) console.error("❌ Error saving mapping:", data.message);
|
|
if (data.success && excelColumn) {
|
|
usedColumnsFromDB = usedColumnsFromDB.filter(col => col !== excelColumn);
|
|
usedColumnsFromDB.push(excelColumn);
|
|
updateXlsDropdowns();
|
|
}
|
|
})
|
|
.catch(error => console.error("❌ Fetch error:", error));
|
|
}
|
|
|
|
if (availableXlsColumns.length) updateXlsDropdowns();
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|