102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
require_once __DIR__ . '/class/binding-functions.php';
|
|
include dirname(__DIR__) . '/../extra/auth.php';
|
|
|
|
header('Content-Type: application/json');
|
|
ini_set('display_errors', '0');
|
|
error_reporting(E_ALL);
|
|
|
|
if (!Auth::check()) {
|
|
http_response_code(401);
|
|
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
$kind = ($_POST['kind'] ?? 'custom') === 'fixed' ? 'fixed' : 'custom';
|
|
$templateId = intval($_POST['template_id'] ?? 0);
|
|
$mappingId = intval($_POST['mapping_id'] ?? 0);
|
|
$fieldId = intval($_POST['field_id'] ?? 0);
|
|
$sourceValue = (string) ($_POST['source_value'] ?? '');
|
|
$limsValueId = intval($_POST['lims_value_id'] ?? 0);
|
|
$limsValue = (string) ($_POST['lims_value'] ?? '');
|
|
|
|
$datadbIds = [];
|
|
if (isset($_POST['datadb_ids'])) {
|
|
$decoded = json_decode($_POST['datadb_ids'], true);
|
|
if (is_array($decoded)) {
|
|
$datadbIds = $decoded;
|
|
}
|
|
}
|
|
|
|
if ($templateId <= 0 || $mappingId <= 0 || trim($sourceValue) === '' || $limsValueId <= 0) {
|
|
http_response_code(422);
|
|
echo json_encode(['success' => false, 'error' => 'Missing required parameters']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
$createdBy = Auth::user() ? (int) Auth::user()->present()->id : null;
|
|
|
|
if ($kind === 'fixed') {
|
|
$stmt = $pdo->prepare("SELECT fixed_field_key FROM template_fixed_mapping WHERE id = ?");
|
|
$stmt->execute([$mappingId]);
|
|
$fixedKey = (string) ($stmt->fetchColumn() ?: '');
|
|
|
|
if ($fixedKey === '' || !binding_fixed_is_list($fixedKey)) {
|
|
http_response_code(422);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid fixed field']);
|
|
exit;
|
|
}
|
|
|
|
binding_upsert($pdo, $templateId, $mappingId, 0, $sourceValue, $limsValueId, $limsValue, $createdBy, 'fixed');
|
|
|
|
$applied = !empty($datadbIds)
|
|
? binding_apply_to_datadb($pdo, $fixedKey, (string) $limsValueId, $datadbIds)
|
|
: 0;
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'applied_rows' => $applied,
|
|
'kind' => 'fixed',
|
|
'source_value' => $sourceValue,
|
|
'lims_value' => $limsValue,
|
|
'lims_value_id' => $limsValueId,
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
if ($fieldId <= 0) {
|
|
$stmt = $pdo->prepare("SELECT field_id FROM template_mapping WHERE id = ?");
|
|
$stmt->execute([$mappingId]);
|
|
$fieldId = (int) ($stmt->fetchColumn() ?: 0);
|
|
}
|
|
|
|
binding_upsert($pdo, $templateId, $mappingId, $fieldId, $sourceValue, $limsValueId, $limsValue, $createdBy);
|
|
|
|
$applied = !empty($datadbIds)
|
|
? binding_apply_to_details($pdo, $mappingId, (string) $limsValueId, $datadbIds)
|
|
: 0;
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'applied_rows' => $applied,
|
|
'kind' => 'custom',
|
|
'source_value' => $sourceValue,
|
|
'lims_value' => $limsValue,
|
|
'lims_value_id' => $limsValueId,
|
|
]);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|