Files
trf_certest/public/userarea/bindings_manage.php
T
2026-09-23 15:50:03 +03:00

300 lines
14 KiB
PHP

<?php
include('include/headscript.php');
require_once __DIR__ . '/class/binding-functions.php';
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$templateFilter = isset($_GET['template_id']) ? (int) $_GET['template_id'] : 0;
$search = trim($_GET['q'] ?? '');
$templates = $pdo->query("SELECT id, name FROM excel_templates ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
$conds = [];
$params = [];
if ($templateFilter > 0) {
$conds[] = 'b.template_id = ?';
$params[] = $templateFilter;
}
if ($search !== '') {
$conds[] = '(b.source_value LIKE ? OR b.lims_value LIKE ? OR m.field_label LIKE ? OR f.fixed_field_key LIKE ?)';
$like = '%' . $search . '%';
array_push($params, $like, $like, $like, $like);
}
$where = $conds ? ('WHERE ' . implode(' AND ', $conds)) : '';
$stmt = $pdo->prepare(
"SELECT b.id, b.template_id, b.binding_kind, b.mapping_id, b.field_id, b.source_value,
b.lims_value_id, b.lims_value, b.updated_at,
t.name AS template_name,
COALESCE(m.field_label, f.fixed_field_key) AS field_label,
COALESCE(m.excel_column, f.excel_column) AS excel_column,
f.fixed_field_key
FROM lims_value_binding b
LEFT JOIN excel_templates t ON t.id = b.template_id
LEFT JOIN template_mapping m ON m.id = b.mapping_id AND b.binding_kind = 'custom'
LEFT JOIN template_fixed_mapping f ON f.id = b.mapping_id AND b.binding_kind = 'fixed'
$where
ORDER BY t.name ASC, field_label ASC, b.source_value ASC"
);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$groups = [];
foreach ($rows as $r) {
$kind = $r['binding_kind'] ?? 'custom';
$gkey = $kind . ':' . (int) $r['mapping_id'];
if (!isset($groups[$gkey])) {
$groups[$gkey] = [
'kind' => $kind,
'label' => $kind === 'fixed'
? binding_fixed_label((string) $r['fixed_field_key'])
: ($r['field_label'] ?: ('Mapping ' . $gkey)),
'excel_column' => $r['excel_column'] ?: '',
'template_name' => $r['template_name'] ?: ('Template ' . $r['template_id']),
'template_id' => (int) $r['template_id'],
'rows' => [],
];
}
$groups[$gkey]['rows'][] = $r;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet">
<style>
.source-value {
font-family: Consolas, Monaco, monospace;
font-weight: 600;
}
td .select2-container {
min-width: 240px;
}
.select2-container--default .select2-selection--single {
height: 38px;
border-color: #ced4da;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 36px;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 36px;
}
</style>
<title>Gestione binding - <?= htmlspecialchars($titlewebsite ?? '', ENT_QUOTES, 'UTF-8'); ?></title>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card radius-10">
<div class="card-header">
<h6 class="mb-0">Binding valori importati &rarr; LIMS</h6>
<small class="text-muted">
Le modifiche valgono dalla prossima importazione: i record gia' importati non vengono aggiornati.
</small>
</div>
<div class="card-body">
<form class="row g-2 align-items-end mb-3" method="get">
<div class="col-md-4">
<label class="form-label mb-1">Template</label>
<select class="form-select" name="template_id" onchange="this.form.submit()">
<option value="">Tutti</option>
<?php foreach ($templates as $t): ?>
<option value="<?= (int) $t['id'] ?>" <?= $templateFilter === (int) $t['id'] ? 'selected' : '' ?>>
<?= htmlspecialchars($t['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4">
<label class="form-label mb-1">Cerca</label>
<input type="text" class="form-control" name="q" value="<?= htmlspecialchars($search) ?>"
placeholder="valore importato, valore LIMS, campo...">
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-primary">Filtra</button>
<a href="bindings_manage.php" class="btn btn-outline-secondary">Azzera</a>
</div>
</form>
<div id="bindingError" class="alert alert-danger" style="display:none;"></div>
<?php if (empty($rows)): ?>
<div class="alert alert-info mb-0">Nessun binding salvato per i filtri selezionati.</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-bordered align-middle">
<thead>
<tr>
<th style="width:32px;"></th>
<th>Valore importato</th>
<th>Valore LIMS</th>
<th style="width:150px;">Aggiornato</th>
<th style="width:110px;">Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($groups as $gkey => $g): ?>
<tr class="table-light">
<td colspan="5">
<strong><?= htmlspecialchars($g['label']) ?></strong>
<?php if (($g['kind'] ?? 'custom') === 'fixed'): ?><span class="badge bg-light text-dark border">campo fisso</span><?php endif; ?>
<span class="text-muted">
&middot; <?= htmlspecialchars($g['template_name']) ?>
<?php if ($g['excel_column'] !== ''): ?>
&middot; colonna &laquo;<?= htmlspecialchars($g['excel_column']) ?>&raquo;
<?php endif; ?>
&middot; <?= count($g['rows']) ?> valore/i
</span>
</td>
</tr>
<?php foreach ($g['rows'] as $r): ?>
<tr class="binding-row"
data-binding-id="<?= (int) $r['id'] ?>"
data-kind="<?= htmlspecialchars($r['binding_kind'] ?? 'custom', ENT_QUOTES) ?>"
data-fixed-key="<?= htmlspecialchars($r['fixed_field_key'] ?? '', ENT_QUOTES) ?>"
data-template-id="<?= (int) $r['template_id'] ?>"
data-mapping-id="<?= (int) $r['mapping_id'] ?>"
data-field-id="<?= (int) $r['field_id'] ?>"
data-source-value="<?= htmlspecialchars($r['source_value'], ENT_QUOTES) ?>">
<td class="text-muted ps-4">&rsaquo;</td>
<td class="source-value"><?= htmlspecialchars($r['source_value']) ?></td>
<td>
<select class="form-select binding-select">
<option value="<?= (int) $r['lims_value_id'] ?>" selected>
<?= htmlspecialchars($r['lims_value'] ?: ('#' . $r['lims_value_id'])) ?>
</option>
</select>
<div class="binding-status small mt-1"></div>
</td>
<td class="small text-muted"><?= htmlspecialchars((string) $r['updated_at']) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-danger delete-binding-btn">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="overlay toggle-icon"></div>
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(function() {
const $error = $('#bindingError');
$('.binding-select').each(function() {
const $row = $(this).closest('.binding-row');
const isFixed = $row.data('kind') === 'fixed';
$(this).select2({
placeholder: 'Seleziona valore LIMS...',
width: '100%',
ajax: {
url: isFixed ? 'search_fixed_field_values.php' : 'search_customfield_values.php',
dataType: 'json',
delay: 200,
data: params => isFixed ? {
field_key: $row.data('fixed-key'),
template_id: $row.data('template-id'),
q: params.term || '',
limit: 50
} : {
field_id: $row.data('field-id'),
q: params.term || '',
limit: 50
},
processResults: data => ({
results: data.results || []
})
},
minimumInputLength: 0
});
});
$('.binding-select').on('change', function() {
const $select = $(this);
const $row = $select.closest('.binding-row');
const $status = $row.find('.binding-status');
const selected = $select.select2('data')[0] || {};
if (!$select.val()) {
return;
}
$status.removeClass('text-success text-danger').addClass('text-muted').text('Salvataggio...');
$.post('save_binding.php', {
kind: $row.data('kind') || 'custom',
template_id: $row.data('template-id'),
mapping_id: $row.data('mapping-id'),
field_id: $row.data('field-id'),
source_value: String($row.data('source-value')),
lims_value_id: $select.val(),
lims_value: selected.text || ''
}).then(resp => {
if (resp && resp.success) {
$status.removeClass('text-muted').addClass('text-success').text('Salvato');
} else {
$status.removeClass('text-muted').addClass('text-danger').text((resp && resp.error) || 'Errore');
}
}).fail(() => {
$status.removeClass('text-muted').addClass('text-danger').text('Errore di rete');
});
});
$('.delete-binding-btn').on('click', function() {
const $row = $(this).closest('.binding-row');
$error.hide();
$.post('delete_binding.php', {
binding_id: $row.data('binding-id')
}).then(resp => {
if (resp && resp.success) {
$row.remove();
} else {
$error.text((resp && resp.error) || 'Errore durante l\'eliminazione.').show();
}
}).fail(() => {
$error.text('Errore di rete durante l\'eliminazione.').show();
});
});
});
</script>
</body>
</html>