getConnection();
// Filtri comuni.
$templateFilter = isset($_GET['template_id']) ? intval($_GET['template_id']) : 0;
$search = trim($_GET['q'] ?? '');
$perPage = 50;
$page = max(1, intval($_GET['page'] ?? 1));
$dir = (strtolower($_GET['dir'] ?? 'asc') === 'desc') ? 'DESC' : 'ASC';
// Modalita': overview (gruppi per campo) oppure detail (valori di un campo).
$focusTarget = trim($_GET['target'] ?? '');
$mode = $focusTarget !== '' ? 'detail' : 'overview';
$templates = $pdo->query("SELECT id, name FROM excel_templates ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
// Helper: URL che preserva i filtri correnti.
$bmUrl = function (array $ov) use ($templateFilter, $search, $dir, $page, $focusTarget) {
$base = [
'template_id' => $templateFilter ?: null,
'q' => $search !== '' ? $search : null,
'order' => $_GET['order'] ?? null,
'dir' => strtolower($dir),
'page' => $page,
'target' => $focusTarget !== '' ? $focusTarget : null,
];
$p = array_filter(array_merge($base, $ov), fn($v) => $v !== null && $v !== '');
return 'bindings_manage.php?' . http_build_query($p);
};
// Filtri WHERE comuni (template/kind/search).
$conds = [];
$params = [];
if ($templateFilter > 0) {
$conds[] = 'b.template_id = ?';
$params[] = $templateFilter;
}
if ($mode === 'detail') {
// ---- DETAIL: valori (json_value -> lims) di un singolo campo (target_key) ----
$conds[] = 'b.target_key = ?';
$params[] = $focusTarget;
if ($search !== '') {
$conds[] = '(b.json_value LIKE ? OR b.lims_value LIKE ?)';
$like = '%' . $search . '%';
array_push($params, $like, $like);
}
$where = 'WHERE ' . implode(' AND ', $conds);
$orderCols = ['json' => 'b.json_value', 'lims' => 'b.lims_value'];
$order = array_key_exists($_GET['order'] ?? '', $orderCols) ? $_GET['order'] : 'json';
$orderSql = $orderCols[$order] . ' ' . $dir;
$countStmt = $pdo->prepare("SELECT COUNT(*) FROM json_lims_binding b $where");
$countStmt->execute($params);
$total = (int) $countStmt->fetchColumn();
$totalPages = max(1, (int) ceil($total / $perPage));
if ($page > $totalPages) $page = $totalPages;
$offset = ($page - 1) * $perPage;
$sql = "SELECT b.id, b.template_id, b.binding_kind, b.mapping_id, b.fixed_field_key, b.field_id,
b.json_value, b.lims_value_id, b.lims_value,
t.name AS template_name, m.field_label
FROM json_lims_binding b
LEFT JOIN excel_templates t ON t.id = b.template_id
LEFT JOIN template_mapping m ON m.id = b.mapping_id
$where
ORDER BY $orderSql, b.json_value ASC
LIMIT $perPage OFFSET $offset";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Intestazione del gruppo (tutte le righe condividono campo/template/kind).
$head = $rows[0] ?? null;
if (!$head) {
// Target vuoto (es. dopo aver svuotato il campo): ricavo i meta dal target_key.
$head = ['binding_kind' => str_starts_with($focusTarget, 'fx:') ? 'fixed' : 'custom'];
}
$headKind = $head['binding_kind'] ?? 'custom';
if ($headKind === 'fixed') {
$headFixedKey = $head['fixed_field_key'] ?? (explode(':', $focusTarget)[2] ?? '');
$headLabel = binding_fixed_label((string) $headFixedKey);
$headTpl = (int) ($head['template_id'] ?? (explode(':', $focusTarget)[1] ?? 0));
} else {
$headLabel = $head['field_label'] ?? ('mapping ' . ($head['mapping_id'] ?? ''));
$headTpl = (int) ($head['template_id'] ?? 0);
}
$headTplName = $headTpl ? ($pdo->query("SELECT name FROM excel_templates WHERE id=" . $headTpl)->fetchColumn() ?: ('#' . $headTpl)) : '';
} else {
// ---- OVERVIEW: un gruppo per campo (target_key) con il conteggio ----
if ($search !== '') {
$conds[] = '(t.name LIKE ? OR m.field_label LIKE ? OR b.fixed_field_key LIKE ?)';
$like = '%' . $search . '%';
array_push($params, $like, $like, $like);
}
$where = $conds ? ('WHERE ' . implode(' AND ', $conds)) : '';
$orderCols = ['template' => 'template_name', 'field' => 'field_label', 'count' => 'cnt'];
$order = array_key_exists($_GET['order'] ?? '', $orderCols) ? $_GET['order'] : 'template';
$orderSql = $orderCols[$order] . ' ' . $dir;
$countStmt = $pdo->prepare("SELECT COUNT(DISTINCT b.target_key)
FROM json_lims_binding b
LEFT JOIN excel_templates t ON t.id = b.template_id
LEFT JOIN template_mapping m ON m.id = b.mapping_id $where");
$countStmt->execute($params);
$total = (int) $countStmt->fetchColumn();
$totalPages = max(1, (int) ceil($total / $perPage));
if ($page > $totalPages) $page = $totalPages;
$offset = ($page - 1) * $perPage;
$sql = "SELECT b.target_key, b.template_id, b.binding_kind, b.mapping_id, b.fixed_field_key,
MAX(t.name) AS template_name, MAX(m.field_label) AS field_label,
COUNT(*) AS cnt, MAX(b.updated_at) AS last_updated
FROM json_lims_binding b
LEFT JOIN excel_templates t ON t.id = b.template_id
LEFT JOIN template_mapping m ON m.id = b.mapping_id
$where
GROUP BY b.target_key, b.template_id, b.binding_kind, b.mapping_id, b.fixed_field_key
ORDER BY $orderSql, template_name ASC
LIMIT $perPage OFFSET $offset";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$groups = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$sortLink = function (string $col, string $label) use ($bmUrl, $order, $dir) {
$nextDir = ($order === $col && $dir === 'ASC') ? 'desc' : 'asc';
$caret = $order === $col ? ($dir === 'ASC' ? ' ▲' : ' ▼') : '';
return '' . $label . $caret . '';
};
?>
Gestione Binding JSON → LIMS - = htmlspecialchars($titlewebsite ?? '', ENT_QUOTES, 'UTF-8'); ?>
Tutti i campi
= htmlspecialchars($headLabel) ?>
fixed
· = htmlspecialchars((string) $headTplName) ?>
= $total ?> valore/i · pagina = $page ?>/= $totalPages ?>
| = $sortLink('json', 'Valore JSON') ?> |
= $sortLink('lims', 'Valore LIMS') ?> |
Azioni |
| Nessun valore per questo campo. |
| = htmlspecialchars($b['json_value']) ?> |
|
|
Scegli un campo per gestirne i valori. Le modifiche valgono per le importazioni future.
= $total ?> camp= $total === 1 ? 'o' : 'i' ?> con binding · pagina = $page ?>/= $totalPages ?>
| = $sortLink('template', 'Template') ?> |
= $sortLink('field', 'Campo') ?> |
= $sortLink('count', '# Binding') ?> |
|
| Nessun binding presente. |
$g['target_key'], 'page' => 1, 'order' => null, 'q' => null]);
?>
| = htmlspecialchars($g['template_name'] ?? ('#' . $g['template_id'])) ?> |
= htmlspecialchars($gLabel) ?>
fixed
campo rimosso
|
= (int) $g['cnt'] ?> |
Apri |
1): ?>