trf_certest/public/userarea/mapping_template_xls.php
2025-02-26 08:57:46 +01:00

169 lines
6.7 KiB
PHP

<?php include('include/headscript.php'); ?>
<?php
require_once 'class/db-functions.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 FROM excel_templates WHERE id = ?");
$stmt->execute([$id]);
$template = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$template) {
die("Template not found");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Associate Columns</title>
<link rel="shortcut icon" href="assets/images/logo/favicon.png">
<?php include('cssinclude.php'); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
</head>
<body>
<!-- Preloader -->
<div class="preloader">
<div class="loader"></div>
</div>
<!-- Sidebar -->
<?php include('include/navbar.php'); ?>
<div class="dashboard-main-wrapper">
<?php include('include/topbar.php'); ?>
<div class="dashboard-body">
<div class="row gy-4">
<div class="col-lg-12">
<div class="card mt-24">
<div class="card-body">
<div class="mb-20 flex-between flex-wrap gap-8">
<h4 class="mb-0">Associate Columns - Template: <span id="templateName"><?php echo htmlspecialchars($template['name']); ?></span></h4>
<p>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>
<!-- Upload Section -->
<div class="mb-4">
<label class="form-label">Upload XLS Example:</label>
<input type="file" id="xlsUpload" class="form-control">
</div>
<!-- Association Section -->
<div class="row">
<div class="col-md-5">
<h5>XLS Column</h5>
<ul id="xlsColumns" class="list-group border p-3" style="height: 300px; overflow-y: auto;"></ul>
</div>
<div class="col-md-2 text-center d-flex align-items-center justify-content-center">
<button class="btn btn-dark" id="addAssociation">➝ Add</button>
</div>
<div class="col-md-5">
<h5>Table Column</h5>
<ul id="tableColumns" class="list-group border p-3" style="height: 300px; overflow-y: auto;"></ul>
</div>
</div>
<!-- Associations List -->
<div class="mt-4">
<h5>Current Associations</h5>
<ul id="associationsList" class="list-group border p-3"></ul>
</div>
<!-- Save Button -->
<div class="mt-4 text-end">
<button class="btn btn-success" id="saveAssociations">Save Associations</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
document.getElementById('xlsUpload').addEventListener('change', function(event) {
let file = event.target.files[0];
if (!file) return;
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 headers = XLSX.utils.sheet_to_json(sheet, {
header: 1
})[rowIndex - 1];
if (!headers || headers.length === 0) {
console.error("No headers found in row:", rowIndex);
return;
}
// Rimuove le colonne prima di startColumn
let adjustedHeaders = headers.slice(startColumn - 1);
let xlsColumns = document.getElementById('xlsColumns');
xlsColumns.innerHTML = '';
adjustedHeaders.forEach(header => {
let li = document.createElement('li');
li.className = 'list-group-item';
li.textContent = header;
xlsColumns.appendChild(li);
});
};
reader.readAsArrayBuffer(file);
});
document.addEventListener("DOMContentLoaded", function() {
fetch('load_table_columns.php?table=<?php echo urlencode($template['target_table']); ?>')
.then(response => response.json())
.then(data => {
if (!data.success) {
console.error("Error:", data.message);
return;
}
let tableColumns = document.getElementById('tableColumns');
tableColumns.innerHTML = '';
if (Array.isArray(data.columns)) {
data.columns.forEach(column => {
let li = document.createElement('li');
li.className = 'list-group-item';
li.textContent = column;
tableColumns.appendChild(li);
});
} else {
console.error("Unexpected data format:", data);
}
})
.catch(error => console.error('Error loading table columns:', error));
});
</script>
</body>
</html>