xls binding
This commit is contained in:
@@ -5,6 +5,7 @@ ini_set('log_errors', 1);
|
||||
ini_set('error_log', __DIR__ . '/import_debug.log');
|
||||
|
||||
include('include/headscript.php');
|
||||
require_once __DIR__ . '/class/binding-functions.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
@@ -49,7 +50,7 @@ $db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
// Recupera i mapping
|
||||
$stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field, auto_value FROM template_mapping WHERE template_id = ?");
|
||||
$stmt = $pdo->prepare("SELECT id, excel_column, data_type, has_list, is_required, manual_default, is_manual, field_label, field_id, main_field, auto_value FROM template_mapping WHERE template_id = ?");
|
||||
$stmt->execute([$template_id]);
|
||||
$allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
@@ -76,6 +77,23 @@ $mappingId244 = (int)$stmtMap->fetchColumn();
|
||||
|
||||
$insertedIds = [];
|
||||
|
||||
$fixedMappings = [];
|
||||
try {
|
||||
$fxStmt = $pdo->prepare(
|
||||
"SELECT id, fixed_field_key, data_type, default_value, default_source, excel_column
|
||||
FROM template_fixed_mapping
|
||||
WHERE template_id = ?"
|
||||
);
|
||||
$fxStmt->execute([$template_id]);
|
||||
$fixedMappings = $fxStmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
error_log("[FIXED] lettura template_fixed_mapping fallita: " . $e->getMessage());
|
||||
}
|
||||
|
||||
$savedBindings = [];
|
||||
$autoBindings = [];
|
||||
$pendingBindings = [];
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
@@ -146,9 +164,65 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
binding_is_list_field($mapping)
|
||||
&& $fieldValue !== null
|
||||
&& trim((string) $fieldValue) !== ''
|
||||
) {
|
||||
$fieldValue = binding_resolve_for_import(
|
||||
$pdo,
|
||||
$mapping,
|
||||
$template_id,
|
||||
(string) $fieldValue,
|
||||
(int) $iddatadb,
|
||||
(int) $user_id,
|
||||
$savedBindings,
|
||||
$autoBindings,
|
||||
$pendingBindings
|
||||
);
|
||||
}
|
||||
|
||||
$insDetail->execute([$iddatadb, $mapping['id'], $fieldValue]);
|
||||
}
|
||||
|
||||
$fixedValues = [];
|
||||
foreach ($fixedMappings as $fm) {
|
||||
$fixedKey = (string) $fm['fixed_field_key'];
|
||||
$value = null;
|
||||
|
||||
if (($fm['default_source'] ?? 'manual') === 'xls' && trim((string) ($fm['excel_column'] ?? '')) !== '') {
|
||||
$idx = binding_find_column_index((string) $fm['excel_column'], $columns);
|
||||
$raw = ($idx !== false && isset($row[$idx])) ? trim((string) $row[$idx]) : '';
|
||||
|
||||
if ($raw !== '') {
|
||||
$value = binding_resolve_fixed_for_import(
|
||||
$pdo,
|
||||
$fm,
|
||||
(int) $template_id,
|
||||
$raw,
|
||||
(int) $iddatadb,
|
||||
(int) $user_id,
|
||||
$savedBindings,
|
||||
$autoBindings,
|
||||
$pendingBindings
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($value === null || $value === '') {
|
||||
$default = binding_fixed_default($fm);
|
||||
$value = $default !== '' ? $default : null;
|
||||
}
|
||||
|
||||
if ($value !== null && $value !== '') {
|
||||
$fixedValues[$fixedKey] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($fixedValues)) {
|
||||
binding_write_fixed_columns($pdo, (int) $iddatadb, $fixedValues);
|
||||
}
|
||||
|
||||
// Campo 244 (Accettatore) con lims_user_id
|
||||
if ($limsUserId !== '' && $mappingId244 > 0) {
|
||||
$insDetail->execute([$iddatadb, $mappingId244, $limsUserId]);
|
||||
@@ -157,9 +231,27 @@ try {
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
$state = $_SESSION['pending_bindings'] ?? null;
|
||||
if (!is_array($state) || ($state['importref'] ?? null) !== $importReferenceCode) {
|
||||
$state = [
|
||||
'template_id' => $template_id,
|
||||
'importref' => $importReferenceCode,
|
||||
'items' => [],
|
||||
'auto' => [],
|
||||
'saved' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$state['items'] = binding_merge_buckets($state['items'], $pendingBindings);
|
||||
$state['auto'] = binding_merge_buckets($state['auto'], $autoBindings);
|
||||
$state['saved'] = binding_merge_buckets($state['saved'], $savedBindings);
|
||||
|
||||
$_SESSION['pending_bindings'] = $state;
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'inserted' => count($insertedIds)
|
||||
'ok' => true,
|
||||
'inserted' => count($insertedIds),
|
||||
'pending_bindings' => count($state['items'])
|
||||
]);
|
||||
exit;
|
||||
} catch (Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user