diff --git a/public/userarea/insert_template_xls.php b/public/userarea/insert_template_xls.php
index 32b6c8d..f3f34dd 100644
--- a/public/userarea/insert_template_xls.php
+++ b/public/userarea/insert_template_xls.php
@@ -1,6 +1,6 @@
getConnection();
$stmt = $pdo->prepare("SELECT * FROM routine");
@@ -18,26 +18,29 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
-
+
@@ -167,6 +184,12 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
const routineAction2 = document.getElementById("routineAction2");
const routineAction3 = document.getElementById("routineAction3");
+ const sourceType = document.getElementById("sourceType");
+ const headerRowWrapper = document.getElementById("headerRowWrapper");
+ const startColumnWrapper = document.getElementById("startColumnWrapper");
+ const headerRow = document.getElementById("headerRow");
+ const startColumn = document.getElementById("startColumn");
+
if (!form || !clientLoadingStatus || !schemaLoadingStatus || !routineSelect || !routineDetails) {
alert("Errore: Uno o più elementi della pagina non sono stati trovati. Contatta l'amministratore.");
return;
@@ -187,30 +210,66 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
allowClear: true
});
+ function updateSourceFields() {
+ const selectedSource = sourceType.value;
+
+ if (selectedSource === 'API') {
+ headerRowWrapper.style.opacity = '0.6';
+ startColumnWrapper.style.opacity = '0.6';
+
+ headerRow.required = false;
+ startColumn.required = false;
+
+ headerRow.disabled = true;
+ startColumn.disabled = true;
+ } else {
+ headerRowWrapper.style.opacity = '1';
+ startColumnWrapper.style.opacity = '1';
+
+ headerRow.required = true;
+ startColumn.required = true;
+
+ headerRow.disabled = false;
+ startColumn.disabled = false;
+ }
+ }
+
+ sourceType.addEventListener('change', updateSourceFields);
+ updateSourceFields();
+
async function loadClients() {
try {
clientLoadingStatus.style.display = 'inline';
clientLoadingStatus.textContent = 'Recupero clienti in corso...';
+
const response = await fetch("get_clienti.php", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});
+
const data = await response.json();
- if (!response.ok) throw new Error(data.error || `Errore HTTP: ${response.status}`);
+
+ if (!response.ok) {
+ throw new Error(data.error || `Errore HTTP: ${response.status}`);
+ }
+
const select = document.getElementById("clientSelect");
select.innerHTML = '
';
+
data.value.forEach(client => {
const nome = client.Nominativo || "Nome non disponibile";
const id = client.IdCliente || "ID non disponibile";
const option = new Option(`${nome.trim()} (ID: ${id})`, id);
select.add(option);
});
+
$(select).trigger('change');
clientLoadingStatus.textContent = "Clienti caricati.";
} catch (error) {
clientLoadingStatus.textContent = "Errore nel caricamento.";
+
Swal.fire({
title: "Errore!",
text: "Impossibile caricare i clienti: " + error.message,
@@ -226,16 +285,23 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
try {
schemaLoadingStatus.style.display = 'inline';
schemaLoadingStatus.textContent = 'Caricamento schemi in corso...';
+
const response = await fetch("get_schemi.php", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});
+
const data = await response.json();
- if (!response.ok) throw new Error(data.error || `Errore HTTP: ${response.status}`);
+
+ if (!response.ok) {
+ throw new Error(data.error || `Errore HTTP: ${response.status}`);
+ }
+
const select = document.getElementById("schemaSelect");
select.innerHTML = '
';
+
const sortedSchemas = [...data.value].sort((a, b) => {
const nomeA = (a.Nome || "").trim().toLowerCase();
const nomeB = (b.Nome || "").trim().toLowerCase();
@@ -250,10 +316,12 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
const option = new Option(`${nome.trim()} (ID: ${id})`, id);
select.add(option);
});
+
$(select).trigger('change');
schemaLoadingStatus.textContent = "Schemi caricati.";
} catch (error) {
schemaLoadingStatus.textContent = "Errore nel caricamento.";
+
Swal.fire({
title: "Errore!",
text: "Impossibile caricare gli schemi: " + error.message,
@@ -278,6 +346,7 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
});
}
}
+
loadData();
const routines = ;
@@ -285,8 +354,10 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
function updateRoutineDetails() {
const selectedId = routineSelect.value;
routineDetails.style.display = selectedId ? 'block' : 'none';
+
if (selectedId) {
const routine = routines.find(r => r.idroutine == selectedId);
+
if (routine) {
routineName.textContent = routine.name || 'N/A';
routineDescription.textContent = routine.description || 'N/A';
@@ -308,6 +379,7 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
routineAction3.textContent = '';
}
}
+
routineSelect.addEventListener('change', updateRoutineDetails);
updateRoutineDetails();
@@ -358,6 +430,7 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
schemaName = nameMatch ? nameMatch[1].trim() : optionText;
}
+
formData.append("idschema", schemaId);
formData.append("schemaname", schemaName);
diff --git a/public/userarea/process_import_xls2.php b/public/userarea/process_import_xls2.php
index 652a15b..27ed5d5 100644
--- a/public/userarea/process_import_xls2.php
+++ b/public/userarea/process_import_xls2.php
@@ -11,6 +11,36 @@ session_start();
require_once '../../vendor/autoload.php';
require_once __DIR__ . '/class/db-functions.php';
+function findHeaderRow(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $worksheet, array $expectedHeaders, int $startCol, int $highestColIndex, int $maxRowsToScan = 20): ?int
+{
+ $normalizedExpected = array_filter(array_map(function ($h) {
+ return strtolower(trim(str_replace(['\\r\\n', '\r\n', "\r\n", "\n", "\r"], ' ', $h)));
+ }, $expectedHeaders));
+ $normalizedExpected = array_values($normalizedExpected);
+ sort($normalizedExpected);
+
+ for ($row = 1; $row <= $maxRowsToScan; $row++) {
+ $rowHeaders = [];
+ for ($col = $startCol; $col <= $highestColIndex; $col++) {
+ $colLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col);
+ $cell = $worksheet->getCell($colLetter . $row);
+ $val = $cell ? trim((string)$cell->getCalculatedValue()) : '';
+ if ($val !== '') {
+ $rowHeaders[] = strtolower(trim(str_replace(["\r\n", "\n", "\r"], ' ', $val)));
+ }
+ }
+ $normalizedRow = $rowHeaders;
+ sort($normalizedRow);
+
+ $matches = count(array_intersect($normalizedExpected, $normalizedRow));
+ $threshold = (int) ceil(count($normalizedExpected) * 0.6);
+ if ($matches >= $threshold) {
+ return $row;
+ }
+ }
+ return null;
+}
+
$response = ['error' => '', 'rows' => [], 'columns' => [], 'template_id' => 0, 'filename' => '', 'apply_routine' => false];
try {
@@ -71,9 +101,35 @@ try {
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
- $startRow = max(1, $header_row);
$startColumn = max(1, $start_column);
+ // Recupera routine e headers dal template — DEVE essere prima dell'auto-detect
+ $stmt = $pdo->prepare("SELECT idroutine, idclient, xls_headers FROM excel_templates WHERE id = ?");
+ $stmt->execute([$template_id]);
+ $template = $stmt->fetch(PDO::FETCH_ASSOC);
+ error_log("=== DEBUG TEMPLATE ===");
+ error_log("template raw: " . print_r($template, true));
+ error_log("xls_headers value: " . var_export($template['xls_headers'] ?? 'KEY NON ESISTE', true));
+ error_log("xls_headers empty check: " . var_export(empty($template['xls_headers']), true));
+ // Auto-detect della riga header se xls_headers è disponibile
+ $detectedHeaderRow = $header_row;
+ if (!empty($template['xls_headers'])) {
+ $expectedHeaders = json_decode($template['xls_headers'], true);
+ if (is_array($expectedHeaders) && !empty($expectedHeaders)) {
+ error_log("Expected headers from DB: " . print_r($expectedHeaders, true));
+ $found = findHeaderRow($worksheet, $expectedHeaders, $startColumn, $highestColumnIndex);
+ error_log("findHeaderRow result: " . var_export($found, true));
+ if ($found !== null) {
+ $detectedHeaderRow = $found;
+ error_log("Header row auto-detected at row: $detectedHeaderRow (was: $header_row)");
+ } else {
+ error_log("Header row auto-detection failed, using provided header_row: $header_row");
+ }
+ }
+ }
+ $startRow = max(1, $detectedHeaderRow);
+ $header_row = $detectedHeaderRow;
+
// Debug dei parametri
error_log("Processing - template_id: $template_id, startRow: $startRow, startColumn: $startColumn, highestRow: $highestRow, highestColumn: $highestColumn, highestColumnIndex: $highestColumnIndex");
@@ -94,7 +150,7 @@ try {
}
// Estrai i dati a partire dalla riga successiva, includendo excelrow
- for ($row = $startRow + 1; $row <= $highestRow; $row++) {
+ for ($row = $header_row + 1; $row <= $highestRow; $row++) {
$rowData = [];
for ($col = $startColumn; $col <= $highestColumnIndex; $col++) {
$columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col);
@@ -107,10 +163,7 @@ try {
}
}
- // Recupera routine dal template
- $stmt = $pdo->prepare("SELECT idroutine, idclient FROM excel_templates WHERE id = ?");
- $stmt->execute([$template_id]);
- $template = $stmt->fetch(PDO::FETCH_ASSOC);
+
if ($template && $template['idroutine']) {
$stmtRoutine = $pdo->prepare("SELECT idroutine, name, filename, headerrow, instruction FROM routine WHERE idroutine = ?");
diff --git a/public/userarea/process_insert_template_xls.php b/public/userarea/process_insert_template_xls.php
index fda7a61..ddc0a5b 100644
--- a/public/userarea/process_insert_template_xls.php
+++ b/public/userarea/process_insert_template_xls.php
@@ -9,12 +9,13 @@ try {
throw new Exception("Invalid request method.");
}
- // Recupera e sanifica i dati
- $name = trim($_POST['name']);
- $header_row = intval($_POST['header_row']);
- $start_column = trim($_POST['start_column']);
+ // Retrieve and sanitize form data
+ $name = trim($_POST['name'] ?? '');
+ $source_type = strtoupper(trim($_POST['source_type'] ?? 'XLS'));
+ $header_row = isset($_POST['header_row']) && $_POST['header_row'] !== '' ? intval($_POST['header_row']) : null;
+ $start_column = trim($_POST['start_column'] ?? '');
$description = trim($_POST['description'] ?? '');
- $target_table = trim($_POST['target_table']);
+ $target_table = trim($_POST['target_table'] ?? 'datadb');
$idclient = intval($_POST['client_id'] ?? 0);
$clientname = trim($_POST['client_name'] ?? '');
$idschema = intval($_POST['idschema'] ?? 0);
@@ -25,24 +26,61 @@ try {
$button_text_color = trim($_POST['button_text_color'] ?? '#ffffff');
$button_label = trim($_POST['button_label'] ?? 'Click Me');
- // Controllo sui campi obbligatori
- if (empty($name) || empty($header_row) || empty($start_column) || empty($target_table) || $idclient <= 0 || $idschema <= 0) {
+ // Normalize source type
+ if (!in_array($source_type, ['XLS', 'API'], true)) {
+ $source_type = 'XLS';
+ }
+
+ // Required fields validation
+ if ($name === '' || $target_table === '' || $idclient <= 0 || $idschema <= 0) {
throw new Exception("All fields marked with * are required, including client and schema.");
}
- // Connessione al database
+ // XLS-only validation
+ if ($source_type === 'XLS') {
+ if ($header_row === null || $header_row <= 0 || $start_column === '') {
+ throw new Exception("Header Row and Start Column are required for XLS templates.");
+ }
+ }
+
+ // API templates do not require XLS coordinates
+ if ($source_type === 'API') {
+ $header_row = null;
+ $start_column = null;
+ }
+
+ // Database connection
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
- // Inserisci il nuovo template
+ // Insert the new template
$stmt = $pdo->prepare("
INSERT INTO excel_templates
- (name, header_row, start_column, description, target_table, idclient, clientname, idschema, schemaname, idroutine,
- button_size, button_bg_color, button_text_color, button_label, created_at, updated_at)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
+ (
+ name,
+ source_type,
+ header_row,
+ start_column,
+ description,
+ target_table,
+ idclient,
+ clientname,
+ idschema,
+ schemaname,
+ idroutine,
+ button_size,
+ button_bg_color,
+ button_text_color,
+ button_label,
+ created_at,
+ updated_at
+ )
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
");
+
$stmt->execute([
$name,
+ $source_type,
$header_row,
$start_column,
$description,
diff --git a/public/userarea/templates_dashboard.php b/public/userarea/templates_dashboard.php
index 635bafc..15cc5de 100644
--- a/public/userarea/templates_dashboard.php
+++ b/public/userarea/templates_dashboard.php
@@ -56,6 +56,56 @@
input:checked+.slider:before {
transform: translateX(14px);
}
+
+ .badge-source {
+ font-size: 11px;
+ padding: 0.30rem 0.55rem;
+ border-radius: 999px;
+ font-weight: 600;
+ display: inline-block;
+ min-width: 50px;
+ text-align: center;
+ line-height: 1.2;
+ }
+
+ .badge-source-xls {
+ background-color: #e7f1ff;
+ color: #0d6efd;
+ }
+
+ .badge-source-api {
+ background-color: #e8fff1;
+ color: #198754;
+ }
+
+ #xlsTemplatesTable {
+ font-size: 13px;
+ }
+
+ #xlsTemplatesTable th,
+ #xlsTemplatesTable td {
+ vertical-align: middle;
+ white-space: nowrap;
+ }
+
+ #xlsTemplatesTable td.description-cell,
+ #xlsTemplatesTable td.client-cell,
+ #xlsTemplatesTable td.name-cell,
+ #xlsTemplatesTable td.button-cell {
+ white-space: normal;
+ }
+
+ .table-actions {
+ min-width: 120px;
+ }
+
+ .table-actions .btn {
+ padding: 0.25rem 0.45rem;
+ }
+
+ .compact-card .card-body {
+ padding: 1rem;
+ }
@@ -65,18 +115,20 @@
+
+