routine scripts

This commit is contained in:
2025-09-24 14:18:31 +02:00
parent 33aacfb469
commit 864714d198
4 changed files with 342 additions and 176 deletions
+38 -10
View File
@@ -1,7 +1,7 @@
<?php
// Sopprime eventuali output di errori (li logghiamo invece di mostrarli)
ob_start();
ini_set('display_errors', 0); // Disattiva l'output degli errori a schermo
ini_set('display_errors', 0);
error_reporting(E_ALL);
// Inizia la sessione
@@ -9,9 +9,9 @@ session_start();
// Includi PHPSpreadsheet e la classe DBHandler
require_once '../../vendor/autoload.php';
require_once __DIR__ . '/class/db-functions.php'; // Assumo che DBHandlerSelect sia qui
require_once __DIR__ . '/class/db-functions.php';
$response = ['error' => '', 'rows' => [], 'columns' => [], 'template_id' => 0, 'filename' => ''];
$response = ['error' => '', 'rows' => [], 'columns' => [], 'template_id' => 0, 'filename' => '', 'apply_routine' => false];
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['excel_file'])) {
@@ -29,7 +29,7 @@ try {
if ($fileError === UPLOAD_ERR_OK) {
// Recupera l'ID dell'utente loggato
if (!isset($iduserlogin)) {
$iduserlogin = 1; // Valore di default
$iduserlogin = 1;
error_log("Warning: iduserlogin non definito, usando 1 come default");
}
@@ -69,7 +69,7 @@ try {
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // Corretto
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
$startRow = max(1, $header_row);
$startColumn = max(1, $start_column);
@@ -93,7 +93,7 @@ try {
$headerRowData[] = htmlspecialchars($cellValue ?: '');
}
// Estrai i dati a partire dalla riga successiva
// Estrai i dati a partire dalla riga successiva, includendo excelrow
for ($row = $startRow + 1; $row <= $highestRow; $row++) {
$rowData = [];
for ($col = $startColumn; $col <= $highestColumnIndex; $col++) {
@@ -103,18 +103,46 @@ try {
$rowData[] = htmlspecialchars($cellValue ?: '');
}
if (!empty(array_filter($rowData))) {
$excelData[] = $rowData;
$excelData[] = ['data' => $rowData, 'excelrow' => $row];
}
}
// Recupera routine dal template
$stmt = $pdo->prepare("SELECT idroutine 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 = ?");
$stmtRoutine->execute([$template['idroutine']]);
$routineData = $stmtRoutine->fetch(PDO::FETCH_ASSOC);
if ($routineData) {
$response['apply_routine'] = true;
$response['routine_data'] = [
'name' => $routineData['name'] ?? 'Routine Sconosciuta',
'instruction' => $routineData['instruction'] ?? 'Nessuna descrizione disponibile',
'filename' => $routineData['filename'] ?? '',
'headerrow' => $routineData['headerrow'] ?? $header_row
];
error_log("Routine rilevata per template {$template_id}: " . print_r($routineData, true));
} else {
error_log("Errore: Nessuna routine trovata per idroutine {$template['idroutine']}");
}
} else {
error_log("Nessuna routine associata al template {$template_id}");
}
// Salva i dati in sessione
$_SESSION['excel_data'] = $excelData;
$_SESSION['template_id'] = $template_id;
$_SESSION['headers'] = $headerRowData;
$_SESSION['mappings'] = $mappings; // Salva i mapping per l'importazione
$_SESSION['mappings'] = $mappings;
$response['rows'] = $excelData;
$response['columns'] = $headerRowData; // Usa gli header reali
// Includi excel_data nella risposta JSON in ogni caso
$response['excel_data'] = $excelData;
$response['rows'] = array_column($excelData, 'data');
$response['columns'] = $headerRowData;
$response['template_id'] = $template_id;
$response['filename'] = $newFilename;
}