getConnection(); // Genera un UUID univoco per importreferencecode $importReferenceCode = date('YmdHis') . '-' . uniqid(); // Recupera tutti i mapping dal template $stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field FROM template_mapping WHERE template_id = ?"); $stmt->execute([$template_id]); $allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC); if (empty($allMappings)) { header("Location: import_xls.php?id=$template_id&status=error&message=" . urlencode("Nessun mapping trovato per il template")); exit; } // Trova il campo main_field $mainFieldMapping = null; foreach ($allMappings as $mapping) { if ($mapping['main_field'] == 1) { $mainFieldMapping = $mapping; break; } } // Inserisci le righe selezionate in datadb $insertedIds = []; foreach ($selected_rows as $rowIndex) { $row = $rows[$rowIndex] ?? null; $excelrow = $excelrows[$rowIndex] ?? null; if ($row === null || $excelrow === null) { error_log("Errore: riga o excelrow mancante per rowIndex $rowIndex"); continue; } $values = [ $template_id, $importReferenceCode, $newFilename, 'i', $user_id, null, date('Y-m-d'), $excelrow // Aggiunto excelrow per la colonna excelrow ]; $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate, excelrow) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute($values); $iddatadb = $pdo->lastInsertId(); $insertedIds[] = $iddatadb; // Inserisci tutti i campi in import_data_details foreach ($allMappings as $mapping) { $fieldValue = null; if (!$mapping['is_manual']) { $excelColumn = trim($mapping['excel_column']); $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { $fieldValue = $row[$excelColumnIndex]; error_log("Found Excel column '$excelColumn' at index $excelColumnIndex, value: " . var_export($fieldValue, true)); } else { $fieldValue = $mapping['manual_default'] ?? ''; error_log("Excel column '$excelColumn' not found or empty, using default: " . var_export($fieldValue, true)); } switch ($mapping['data_type']) { case 'INT': $fieldValue = is_numeric($fieldValue) ? (int)$fieldValue : ($mapping['manual_default'] ?? 0); break; case 'DATE': $fieldValue = !empty($fieldValue) ? date('Y-m-d', strtotime($fieldValue)) : ($mapping['manual_default'] === 'today' ? date('Y-m-d') : ($mapping['manual_default'] ?? '')); break; case 'CHAR': $fieldValue = !empty($fieldValue) ? substr((string)$fieldValue, 0, 1) : ($mapping['manual_default'] ?? ''); break; case 'Testo': case 'VARCHAR': default: $fieldValue = !empty($fieldValue) ? htmlspecialchars((string)$fieldValue) : ($mapping['manual_default'] ?? ''); break; } } else { $fieldValue = $mapping['manual_default'] ?? ''; if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') { $fieldValue = date('Y-m-d'); } } if ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) { error_log("Required field missing for mapping ID: " . $mapping['id'] . ", field: " . $mapping['field_label']); } error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Excel Column: " . ($mapping['excel_column'] ?? 'N/A') . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); $stmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); $stmt->execute([$iddatadb, $mapping['id'], $fieldValue]); error_log("Inserted into import_data_details for ID $iddatadb, Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true)); } } $_SESSION['inserted_ids'] = $insertedIds; $params = [ 'template_id' => $template_id, 'filename' => $newFilename, ]; ?>