trf_certest/public/userarea/validate_export.php

121 lines
4.1 KiB
PHP

<?php
/**
* Validate rows before export to LIMS.
*
* Expects POST JSON: { rows: [ { iddatadb: int, index: int }, ... ] }
* Returns JSON:
* {
* success: true,
* results: {
* "<index>": {
* valid: bool,
* iddatadb: int,
* errors: [ { field: "<data-col>", message: "..." }, ... ]
* }
* }
* }
*
* Validators are closures registered in $validators[].
* Each receives ($iddatadb, $ctx) and returns an array of errors (or empty []).
* $ctx holds all prefetched data (record, parts, field values, mappings, etc.).
* To add a new rule — just append another closure to $validators.
*/
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
header("Content-Type: application/json");
// ── Validators ──────────────────────────────────────────────────────────────
// Each validator is a closure: fn(int $iddatadb, array $ctx): array<{field,message}>
// $ctx keys: record, parts, fieldValues, requiredFixed, requiredMappings, fixedAliasMap
$validators = [];
// 1. Every part must have a valid Matrice
// Without Matrice the LIMS API rejects the Campione creation.
$validators[] = function (int $iddatadb, array $ctx): array {
$parts = $ctx['parts'];
if (empty($parts)) {
return [];
}
$missing = [];
foreach ($parts as $p) {
$matrice = (int)($p['idmatrice'] ?? 0);
if ($matrice <= 0) {
$label = $p['part_number'] ?: ($p['part_description'] ?: '(senza nome)');
$missing[] = $label;
}
}
if (!empty($missing)) {
return [[
'field' => 'parts',
'message' => 'Matrice mancante — il LIMS rifiuterà l\'esportazione. '
. 'Part senza matrice: ' . implode(', ', $missing)
. '. Aprire la sezione Parts e assegnare la matrice.',
]];
}
return [];
};
// ── Main ────────────────────────────────────────────────────────────────────
try {
$input = json_decode(file_get_contents('php://input'), true);
$rows = $input['rows'] ?? [];
if (empty($rows)) {
echo json_encode(['success' => false, 'message' => 'No rows provided']);
exit;
}
// ── Prefetch all data in bulk ───────────────────────────────────────────
$iddatadbList = array_column($rows, 'iddatadb');
$placeholders = implode(',', array_fill(0, count($iddatadbList), '?'));
// Parts (full rows, so validators can report which parts have issues)
$stmt = $pdo->prepare("
SELECT iddatadb, part_number, part_description, idmatrice
FROM identification_parts
WHERE iddatadb IN ($placeholders)
");
$stmt->execute($iddatadbList);
$partsInfo = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
$partsInfo[(int)$r['iddatadb']][] = $r;
}
// ── Run validators per row ──────────────────────────────────────────────
$results = [];
foreach ($rows as $rowInfo) {
$iddatadb = (int)$rowInfo['iddatadb'];
$index = $rowInfo['index'];
// Build context for validators
$ctx = [
'parts' => $partsInfo[$iddatadb] ?? [],
];
$errors = [];
foreach ($validators as $validator) {
$errors = array_merge($errors, $validator($iddatadb, $ctx));
}
$results[$index] = [
'valid' => empty($errors),
'iddatadb' => $iddatadb,
'errors' => $errors,
];
}
echo json_encode(['success' => true, 'results' => $results]);
} catch (Exception $e) {
error_log("Validation error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}