fixed columns bind

This commit is contained in:
2026-06-17 21:14:20 +03:00
parent 25c3990753
commit 2ad2e06dc2
7 changed files with 973 additions and 207 deletions
+35 -23
View File
@@ -1,6 +1,6 @@
<?php
// Salva un binding JSON -> LIMS e lo applica ai record appena importati. Ritorna JSON.
// Salva un binding JSON -> LIMS (custom o fixed) e lo applica ai record appena importati. Ritorna JSON.
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once __DIR__ . '/class/db-functions.php';
@@ -23,8 +23,7 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
exit;
}
$mappingId = intval($_POST['mapping_id'] ?? 0);
$fieldId = intval($_POST['field_id'] ?? 0);
$kind = ($_POST['kind'] ?? 'custom') === 'fixed' ? 'fixed' : 'custom';
$templateId = intval($_POST['template_id'] ?? 0);
$jsonValue = (string) ($_POST['json_value'] ?? '');
$limsValueId = intval($_POST['lims_value_id'] ?? 0);
@@ -38,7 +37,7 @@ if (isset($_POST['datadb_ids'])) {
}
}
if ($mappingId <= 0 || $templateId <= 0 || $jsonValue === '' || $limsValueId <= 0) {
if ($templateId <= 0 || $jsonValue === '' || $limsValueId <= 0) {
http_response_code(422);
echo json_encode(['success' => false, 'error' => 'Missing required parameters']);
exit;
@@ -47,31 +46,44 @@ if ($mappingId <= 0 || $templateId <= 0 || $jsonValue === '' || $limsValueId <=
try {
$pdo = DBHandlerSelect::getInstance()->getConnection();
$createdBy = null;
if (Auth::user()) {
$createdBy = (int) Auth::user()->present()->id;
}
$createdBy = Auth::user() ? (int) Auth::user()->present()->id : null;
if ($fieldId <= 0) {
$stmt = $pdo->prepare("SELECT field_id FROM template_mapping WHERE id = ?");
$stmt->execute([$mappingId]);
$fieldId = (int) ($stmt->fetchColumn() ?: 0);
}
if ($kind === 'fixed') {
$fixedKey = trim($_POST['fixed_field_key'] ?? '');
$column = binding_fixed_column($fixedKey);
if ($fixedKey === '' || !binding_fixed_is_list($fixedKey) || !$column) {
http_response_code(422);
echo json_encode(['success' => false, 'error' => 'Invalid fixed field']);
exit;
}
binding_upsert($pdo, $templateId, $mappingId, $fieldId, $jsonValue, $limsValueId, $limsValue, $createdBy);
binding_upsert_fixed($pdo, $templateId, $fixedKey, $jsonValue, $limsValueId, $limsValue, $createdBy);
$applied = !empty($datadbIds) ? binding_apply_to_datadb($pdo, $column, $limsValueId, $datadbIds) : 0;
} else {
$mappingId = intval($_POST['mapping_id'] ?? 0);
$fieldId = intval($_POST['field_id'] ?? 0);
if ($mappingId <= 0) {
http_response_code(422);
echo json_encode(['success' => false, 'error' => 'Missing mapping_id']);
exit;
}
if ($fieldId <= 0) {
$stmt = $pdo->prepare("SELECT field_id FROM template_mapping WHERE id = ?");
$stmt->execute([$mappingId]);
$fieldId = (int) ($stmt->fetchColumn() ?: 0);
}
$applied = 0;
if (!empty($datadbIds)) {
$applied = binding_apply_to_details($pdo, $mappingId, $limsValue, $datadbIds);
binding_upsert($pdo, $templateId, $mappingId, $fieldId, $jsonValue, $limsValueId, $limsValue, $createdBy);
$applied = !empty($datadbIds) ? binding_apply_to_details($pdo, $mappingId, $limsValue, $datadbIds) : 0;
}
echo json_encode([
'success' => true,
'applied_rows' => $applied,
'mapping_id' => $mappingId,
'json_value' => $jsonValue,
'lims_value' => $limsValue,
'lims_value_id' => $limsValueId,
'success' => true,
'applied_rows' => $applied,
'kind' => $kind,
'json_value' => $jsonValue,
'lims_value' => $limsValue,
'lims_value_id' => $limsValueId,
]);
} catch (Exception $e) {
http_response_code(500);