api change dahsboard

This commit is contained in:
2026-03-28 10:28:25 +01:00
parent 08b89e01cc
commit b3ce489348
4 changed files with 346 additions and 108 deletions
+59 -6
View File
@@ -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 = ?");