pdf test routine
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
include('include/headscript.php');
|
||||
|
||||
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
||||
header("Location: template_dashboard.php?status=error&message=" . urlencode("Invalid ID"));
|
||||
exit;
|
||||
}
|
||||
|
||||
$id = intval($_GET['id']);
|
||||
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
$stmt = $pdo->prepare("SELECT id, name, source_type, idclient, sample_pdf FROM excel_templates WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$template = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$template) {
|
||||
header("Location: template_dashboard.php?status=error&message=" . urlencode("Template not found"));
|
||||
exit;
|
||||
}
|
||||
|
||||
$sourceType = strtoupper($template['source_type'] ?? '');
|
||||
if ($sourceType !== 'PDF') {
|
||||
// Non è un template PDF: rimanda al flusso corretto
|
||||
header("Location: import_xls2.php?id=" . $id);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Conta i mapping con riquadri, per avvisare se non è configurato
|
||||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM template_mapping WHERE template_id = ? AND pdf_regions IS NOT NULL AND pdf_regions <> ''");
|
||||
$stmt->execute([$id]);
|
||||
$mappedRegions = (int)$stmt->fetchColumn();
|
||||
?>
|
||||
<!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><?= htmlspecialchars($template['name']) ?> - PDF Import</title>
|
||||
<style>
|
||||
#dropZone {
|
||||
border: 2px dashed #adb5bd;
|
||||
border-radius: 10px;
|
||||
padding: 40px 20px;
|
||||
text-align: center;
|
||||
color: #6c757d;
|
||||
background: #f8f9fa;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s, border-color 0.15s;
|
||||
}
|
||||
|
||||
#dropZone.dragover {
|
||||
background: #e7f1ff;
|
||||
border-color: #0d6efd;
|
||||
color: #0d6efd;
|
||||
}
|
||||
|
||||
#dropZone .big {
|
||||
font-size: 42px;
|
||||
line-height: 1;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.file-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 12px;
|
||||
border: 1px solid #e5e5e5;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 8px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.file-row .status-icon {
|
||||
font-size: 18px;
|
||||
width: 24px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.file-row .fname {
|
||||
font-weight: 600;
|
||||
flex: 0 0 260px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.file-row .fvals {
|
||||
flex: 1;
|
||||
color: #555;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.file-row.ok {
|
||||
background: #eaffea;
|
||||
}
|
||||
|
||||
.file-row.err {
|
||||
background: #ffecec;
|
||||
}
|
||||
|
||||
.file-row.pending {
|
||||
background: #fff8e1;
|
||||
}
|
||||
|
||||
.val-pair {
|
||||
display: inline-block;
|
||||
margin-right: 12px;
|
||||
}
|
||||
|
||||
.val-pair b {
|
||||
color: #333;
|
||||
}
|
||||
</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">
|
||||
<h6 class="mb-0"><?= htmlspecialchars($template['name']) ?> <span class="badge bg-danger">PDF</span></h6>
|
||||
<small>Template ID: <?= $id ?> — Ogni PDF diventa una riga importata.</small>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
|
||||
<?php if ($mappedRegions === 0): ?>
|
||||
<div class="alert alert-warning">
|
||||
Questo template non ha ancora riquadri PDF configurati. Configura il mapping prima di importare.
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div id="dropZone">
|
||||
<div class="big">📄⬇</div>
|
||||
<div><strong>Trascina qui i PDF</strong> oppure clicca per selezionarli</div>
|
||||
<div style="font-size:13px; margin-top:6px;">Puoi caricarne più di uno insieme</div>
|
||||
<input type="file" id="pdfInput" accept="application/pdf" multiple style="display:none;">
|
||||
</div>
|
||||
|
||||
<div class="d-flex align-items-center justify-content-between mt-3">
|
||||
<span class="badge bg-secondary" id="fileCount">0 file</span>
|
||||
<button id="startImportBtn" class="btn btn-primary" disabled>Importa PDF</button>
|
||||
</div>
|
||||
|
||||
<div id="fileList" class="mt-3"></div>
|
||||
|
||||
<div class="mt-4">
|
||||
<a href="imported.php?id=<?= $id ?>" class="btn btn-warning">Vai agli Imported</a>
|
||||
<a href="template_dashboard.php" class="btn btn-secondary">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>
|
||||
(function() {
|
||||
const TEMPLATE_ID = <?= (int)$id ?>;
|
||||
|
||||
const dropZone = document.getElementById('dropZone');
|
||||
const pdfInput = document.getElementById('pdfInput');
|
||||
const fileList = document.getElementById('fileList');
|
||||
const fileCount = document.getElementById('fileCount');
|
||||
const startBtn = document.getElementById('startImportBtn');
|
||||
|
||||
let selectedFiles = [];
|
||||
|
||||
function updateCount() {
|
||||
fileCount.textContent = selectedFiles.length + ' file';
|
||||
startBtn.disabled = selectedFiles.length === 0;
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
fileList.innerHTML = '';
|
||||
selectedFiles.forEach((f, idx) => {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'file-row pending';
|
||||
row.id = 'file-row-' + idx;
|
||||
row.innerHTML = `
|
||||
<span class="status-icon">⏳</span>
|
||||
<span class="fname">${escapeHtml(f.name)}</span>
|
||||
<span class="fvals">In attesa...</span>
|
||||
`;
|
||||
fileList.appendChild(row);
|
||||
});
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
const d = document.createElement('div');
|
||||
d.textContent = s;
|
||||
return d.innerHTML;
|
||||
}
|
||||
|
||||
function addFiles(files) {
|
||||
for (const f of files) {
|
||||
if (f.type === 'application/pdf' || f.name.toLowerCase().endsWith('.pdf')) {
|
||||
selectedFiles.push(f);
|
||||
}
|
||||
}
|
||||
updateCount();
|
||||
renderList();
|
||||
}
|
||||
|
||||
// Click per selezionare
|
||||
dropZone.addEventListener('click', () => pdfInput.click());
|
||||
pdfInput.addEventListener('change', (e) => addFiles(e.target.files));
|
||||
|
||||
// Drag & drop
|
||||
['dragenter', 'dragover'].forEach(ev => {
|
||||
dropZone.addEventListener(ev, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dropZone.classList.add('dragover');
|
||||
});
|
||||
});
|
||||
['dragleave', 'drop'].forEach(ev => {
|
||||
dropZone.addEventListener(ev, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dropZone.classList.remove('dragover');
|
||||
});
|
||||
});
|
||||
dropZone.addEventListener('drop', (e) => {
|
||||
addFiles(e.dataTransfer.files);
|
||||
});
|
||||
|
||||
// Genera un importreferencecode condiviso da tutto il gruppo
|
||||
function makeImportRef() {
|
||||
return new Date().toISOString().replace(/[-:.TZ]/g, '').slice(0, 14) +
|
||||
'-' + Math.random().toString(36).slice(2, 10);
|
||||
}
|
||||
|
||||
startBtn.addEventListener('click', async () => {
|
||||
if (selectedFiles.length === 0) return;
|
||||
|
||||
startBtn.disabled = true;
|
||||
startBtn.textContent = 'Importazione...';
|
||||
|
||||
const importRef = makeImportRef();
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('template_id', TEMPLATE_ID);
|
||||
formData.append('importreferencecode', importRef);
|
||||
selectedFiles.forEach(f => formData.append('pdf_files[]', f));
|
||||
|
||||
// segna tutte le righe come "in corso"
|
||||
selectedFiles.forEach((f, idx) => {
|
||||
const row = document.getElementById('file-row-' + idx);
|
||||
if (row) row.querySelector('.fvals').textContent = 'Elaborazione...';
|
||||
});
|
||||
|
||||
try {
|
||||
const resp = await fetch('process_import_pdf.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
const data = await resp.json();
|
||||
|
||||
if (!data.ok) {
|
||||
alert('Errore: ' + (data.error || 'sconosciuto'));
|
||||
startBtn.disabled = false;
|
||||
startBtn.textContent = 'Importa PDF';
|
||||
return;
|
||||
}
|
||||
|
||||
// Aggiorna ogni riga con l'esito (l'ordine dei results segue quello inviato)
|
||||
data.results.forEach((res, idx) => {
|
||||
const row = document.getElementById('file-row-' + idx);
|
||||
if (!row) return;
|
||||
|
||||
const icon = row.querySelector('.status-icon');
|
||||
const vals = row.querySelector('.fvals');
|
||||
|
||||
if (res.ok) {
|
||||
row.className = 'file-row ok';
|
||||
icon.textContent = '✅';
|
||||
const pairs = Object.entries(res.values || {})
|
||||
.map(([k, v]) => `<span class="val-pair"><b>${escapeHtml(k)}:</b> ${escapeHtml(v || '—')}</span>`)
|
||||
.join('');
|
||||
vals.innerHTML = pairs || 'Importato (nessun valore estratto)';
|
||||
} else {
|
||||
row.className = 'file-row err';
|
||||
icon.textContent = '❌';
|
||||
vals.textContent = res.error || 'Errore sconosciuto';
|
||||
}
|
||||
});
|
||||
|
||||
startBtn.textContent = 'Importa altri PDF';
|
||||
startBtn.disabled = false;
|
||||
// svuota la selezione (i file sono stati processati)
|
||||
selectedFiles = [];
|
||||
pdfInput.value = '';
|
||||
updateCount();
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
alert('Errore di rete durante l\'importazione.');
|
||||
startBtn.disabled = false;
|
||||
startBtn.textContent = 'Importa PDF';
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user