fixed columns bind
This commit is contained in:
@@ -84,10 +84,22 @@ foreach ($allMappings as $mapping) {
|
||||
}
|
||||
}
|
||||
|
||||
// Campi fixed mappati da JSON (default_source='json') per questo template.
|
||||
$fixedJsonFields = [];
|
||||
if ($source_type === 'json') {
|
||||
$fxStmt = $pdo->prepare("
|
||||
SELECT fixed_field_key, json_node, data_type
|
||||
FROM template_fixed_mapping
|
||||
WHERE template_id = ? AND default_source = 'json' AND json_node IS NOT NULL AND json_node <> ''
|
||||
");
|
||||
$fxStmt->execute([$template_id]);
|
||||
$fixedJsonFields = $fxStmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// Inserisci le righe selezionate in datadb
|
||||
$insertedIds = [];
|
||||
|
||||
// Binding JSON -> LIMS senza corrispondenza salvata, per "mapping_id|json_value".
|
||||
// Binding JSON -> LIMS senza corrispondenza salvata, per "kind:key|json_value".
|
||||
$pendingBindings = [];
|
||||
// Binding risolti in automatico durante questo import (solo per visualizzazione).
|
||||
$autoBindings = [];
|
||||
@@ -258,9 +270,10 @@ foreach ($selected_rows as $loopIndex => $rowIndex) {
|
||||
if ($existing) {
|
||||
$fieldValue = $existing['lims_value'];
|
||||
|
||||
$key = $mapping['id'] . '|' . $jsonValue;
|
||||
$key = 'cf:' . $mapping['id'] . '|' . $jsonValue;
|
||||
if (!isset($savedBindings[$key])) {
|
||||
$savedBindings[$key] = [
|
||||
'kind' => 'custom',
|
||||
'mapping_id' => (int) $mapping['id'],
|
||||
'field_id' => (int) $mapping['field_id'],
|
||||
'field_label' => $mapping['field_label'],
|
||||
@@ -289,9 +302,10 @@ foreach ($selected_rows as $loopIndex => $rowIndex) {
|
||||
);
|
||||
$fieldValue = (string) $autoMatch['Valore'];
|
||||
|
||||
$key = $mapping['id'] . '|' . $jsonValue;
|
||||
$key = 'cf:' . $mapping['id'] . '|' . $jsonValue;
|
||||
if (!isset($autoBindings[$key])) {
|
||||
$autoBindings[$key] = [
|
||||
'kind' => 'custom',
|
||||
'mapping_id' => (int) $mapping['id'],
|
||||
'field_id' => (int) $mapping['field_id'],
|
||||
'field_label' => $mapping['field_label'],
|
||||
@@ -303,9 +317,10 @@ foreach ($selected_rows as $loopIndex => $rowIndex) {
|
||||
}
|
||||
$autoBindings[$key]['datadb_ids'][] = (int) $iddatadb;
|
||||
} else {
|
||||
$key = $mapping['id'] . '|' . $jsonValue;
|
||||
$key = 'cf:' . $mapping['id'] . '|' . $jsonValue;
|
||||
if (!isset($pendingBindings[$key])) {
|
||||
$pendingBindings[$key] = [
|
||||
'kind' => 'custom',
|
||||
'mapping_id' => (int) $mapping['id'],
|
||||
'field_id' => (int) $mapping['field_id'],
|
||||
'field_label' => $mapping['field_label'],
|
||||
@@ -326,6 +341,104 @@ foreach ($selected_rows as $loopIndex => $rowIndex) {
|
||||
$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));
|
||||
}
|
||||
|
||||
// ---- Fixed fields mappati da JSON (scrivono colonne datadb) ----
|
||||
if ($source_type === 'json' && !empty($fixedJsonFields)) {
|
||||
$fixedUpdates = []; // colonna datadb => valore (id LIMS o data)
|
||||
|
||||
foreach ($fixedJsonFields as $fx) {
|
||||
$fixedKey = $fx['fixed_field_key'];
|
||||
$column = binding_fixed_column($fixedKey);
|
||||
if (!$column) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$idx = binding_find_column_index((string) $fx['json_node'], $columns);
|
||||
$raw = ($idx >= 0 && isset($row[$idx])) ? trim((string) $row[$idx]) : '';
|
||||
if ($raw === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Campo non a lista (es. ConsegnaRichiesta DATE): scrivo il valore direttamente.
|
||||
if (!binding_fixed_is_list($fixedKey)) {
|
||||
if (($fx['data_type'] ?? '') === 'DATE') {
|
||||
$fixedUpdates[$column] = date('Y-m-d', strtotime($raw));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Binding gia' salvato.
|
||||
$existing = binding_lookup_fixed($pdo, (int) $template_id, $fixedKey, $raw);
|
||||
if ($existing) {
|
||||
$fixedUpdates[$column] = (int) $existing['lims_value_id'];
|
||||
$key = 'fx:' . $fixedKey . '|' . $raw;
|
||||
if (!isset($savedBindings[$key])) {
|
||||
$savedBindings[$key] = [
|
||||
'kind' => 'fixed',
|
||||
'fixed_field_key' => $fixedKey,
|
||||
'field_label' => binding_fixed_label($fixedKey),
|
||||
'json_value' => $raw,
|
||||
'lims_value' => (string) $existing['lims_value'],
|
||||
'lims_value_id' => (int) $existing['lims_value_id'],
|
||||
'datadb_ids' => [],
|
||||
];
|
||||
}
|
||||
$savedBindings[$key]['datadb_ids'][] = (int) $iddatadb;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Auto-match 1-a-1 (solo per le liste globali piccole).
|
||||
$autoMatch = null;
|
||||
if (binding_fixed_auto_matchable($fixedKey)) {
|
||||
$fixedValues = binding_get_fixed_values($pdo, $fixedKey, (int) $template_id);
|
||||
$autoMatch = binding_auto_match_fixed($fixedValues, $raw);
|
||||
}
|
||||
|
||||
if ($autoMatch) {
|
||||
binding_upsert_fixed($pdo, (int) $template_id, $fixedKey, $raw, (int) $autoMatch['id'], (string) $autoMatch['text'], $user_id);
|
||||
$fixedUpdates[$column] = (int) $autoMatch['id'];
|
||||
$key = 'fx:' . $fixedKey . '|' . $raw;
|
||||
if (!isset($autoBindings[$key])) {
|
||||
$autoBindings[$key] = [
|
||||
'kind' => 'fixed',
|
||||
'fixed_field_key' => $fixedKey,
|
||||
'field_label' => binding_fixed_label($fixedKey),
|
||||
'json_value' => $raw,
|
||||
'lims_value' => (string) $autoMatch['text'],
|
||||
'lims_value_id' => (int) $autoMatch['id'],
|
||||
'datadb_ids' => [],
|
||||
];
|
||||
}
|
||||
$autoBindings[$key]['datadb_ids'][] = (int) $iddatadb;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Nessuna corrispondenza: colonna lasciata vuota, segnalo come pending.
|
||||
$key = 'fx:' . $fixedKey . '|' . $raw;
|
||||
if (!isset($pendingBindings[$key])) {
|
||||
$pendingBindings[$key] = [
|
||||
'kind' => 'fixed',
|
||||
'fixed_field_key' => $fixedKey,
|
||||
'field_label' => binding_fixed_label($fixedKey),
|
||||
'json_value' => $raw,
|
||||
'datadb_ids' => [],
|
||||
];
|
||||
}
|
||||
$pendingBindings[$key]['datadb_ids'][] = (int) $iddatadb;
|
||||
}
|
||||
|
||||
if (!empty($fixedUpdates)) {
|
||||
$setParts = [];
|
||||
$params = [];
|
||||
foreach ($fixedUpdates as $col => $val) {
|
||||
$setParts[] = "`$col` = ?";
|
||||
$params[] = $val;
|
||||
}
|
||||
$params[] = (int) $iddatadb;
|
||||
$upd = $pdo->prepare("UPDATE datadb SET " . implode(', ', $setParts) . " WHERE iddatadb = ?");
|
||||
$upd->execute($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$_SESSION['inserted_ids'] = $insertedIds;
|
||||
|
||||
Reference in New Issue
Block a user