Files
trf_certest/public/userarea/bindings_manage.php
T
2026-06-13 20:19:36 +03:00

247 lines
11 KiB
PHP

<?php
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Filtro opzionale per template.
$templateFilter = isset($_GET['template_id']) ? intval($_GET['template_id']) : 0;
$templates = $pdo->query("SELECT id, name FROM excel_templates ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
$where = '';
$params = [];
if ($templateFilter > 0) {
$where = 'WHERE b.template_id = ?';
$params[] = $templateFilter;
}
$sql = "SELECT b.id, b.template_id, b.mapping_id, b.field_id, b.json_value,
b.lims_value_id, b.lims_value, b.updated_at,
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 t.name, m.field_label, b.json_value";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$bindings = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!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>
.json-value {
font-family: Consolas, Monaco, monospace;
font-weight: 600;
}
td .select2-container {
min-width: 220px;
}
.row-status {
font-size: 12px;
}
</style>
<title>Gestione Binding JSON -> LIMS - <?= 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">
<div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
<h6 class="mb-0">Gestione Binding JSON -> LIMS</h6>
<form method="GET" class="d-flex align-items-center gap-2">
<label class="small text-muted mb-0">Template</label>
<select name="template_id" class="form-select form-select-sm" onchange="this.form.submit()">
<option value="0">Tutti</option>
<?php foreach ($templates as $t): ?>
<option value="<?= (int) $t['id'] ?>" <?= $templateFilter === (int) $t['id'] ? 'selected' : '' ?>>
<?= htmlspecialchars($t['name']) ?>
</option>
<?php endforeach; ?>
</select>
</form>
</div>
</div>
<div class="card-body">
<div id="globalError" class="alert alert-danger" style="display:none;"></div>
<div class="alert alert-secondary small">
Le modifiche ai binding si applicano alle <strong>importazioni future</strong>.
I record gia' importati non vengono ricalcolati.
</div>
<div class="table-responsive">
<table class="table table-striped table-bordered align-middle">
<thead>
<tr>
<th>Template</th>
<th>Campo (template_mapping)</th>
<th>Valore JSON</th>
<th>Valore LIMS</th>
<th style="width:170px;">Azioni</th>
</tr>
</thead>
<tbody>
<?php if (empty($bindings)): ?>
<tr>
<td colspan="5" class="text-center text-muted">Nessun binding presente.</td>
</tr>
<?php else: ?>
<?php foreach ($bindings as $b): ?>
<tr data-id="<?= (int) $b['id'] ?>"
data-mapping-id="<?= (int) $b['mapping_id'] ?>"
data-field-id="<?= (int) $b['field_id'] ?>"
data-template-id="<?= (int) $b['template_id'] ?>"
data-json-value="<?= htmlspecialchars($b['json_value'], ENT_QUOTES) ?>">
<td><?= htmlspecialchars($b['template_name'] ?? ('#' . $b['template_id'])) ?></td>
<td><?= htmlspecialchars($b['field_label'] ?? ('mapping ' . $b['mapping_id'])) ?></td>
<td class="json-value"><?= htmlspecialchars($b['json_value']) ?></td>
<td>
<select class="form-select binding-select" data-field-id="<?= (int) $b['field_id'] ?>">
<?php if ($b['lims_value_id']): ?>
<option value="<?= (int) $b['lims_value_id'] ?>" selected>
<?= htmlspecialchars($b['lims_value']) ?>
</option>
<?php endif; ?>
</select>
<span class="row-status text-muted"></span>
</td>
<td>
<button type="button" class="btn btn-sm btn-success save-binding-btn" disabled>
<i class="fas fa-save"></i> Salva
</button>
<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 endif; ?>
</tbody>
</table>
</div>
</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 $globalError = $('#globalError');
$('.binding-select').each(function() {
const fieldId = $(this).data('field-id');
const initialVal = $(this).val();
$(this).select2({
width: '220px',
ajax: {
url: 'search_customfield_values.php',
dataType: 'json',
delay: 200,
data: params => ({
field_id: fieldId,
q: params.term || '',
limit: 50
}),
processResults: data => ({
results: data.results || []
})
},
minimumInputLength: 0
});
// Abilito Salva solo quando il valore cambia davvero.
$(this).data('original-val', initialVal);
});
$('.binding-select').on('change', function() {
const $row = $(this).closest('tr');
const changed = String($(this).val()) !== String($(this).data('original-val'));
$row.find('.save-binding-btn').prop('disabled', !changed);
});
$('.save-binding-btn').on('click', function() {
const $row = $(this).closest('tr');
const $select = $row.find('.binding-select');
const selectedData = $select.select2('data')[0] || {};
const $status = $row.find('.row-status');
const $btn = $(this);
$globalError.hide();
$btn.prop('disabled', true);
$status.text('Salvataggio...').removeClass('text-success text-danger').addClass('text-muted');
$.post('save_binding.php', {
mapping_id: $row.data('mapping-id'),
field_id: $row.data('field-id'),
template_id: $row.data('template-id'),
json_value: String($row.data('json-value')),
lims_value_id: $select.val(),
lims_value: selectedData.text || ''
}).then(resp => {
if (resp && resp.success) {
$status.text('Salvato').removeClass('text-muted').addClass('text-success');
$select.data('original-val', $select.val());
} else {
throw new Error((resp && resp.error) || 'Errore salvataggio');
}
}).catch(err => {
$status.text('Errore').removeClass('text-muted').addClass('text-danger');
$globalError.text(err.message || 'Errore durante il salvataggio.').show();
$btn.prop('disabled', false);
});
});
$('.delete-binding-btn').on('click', function() {
if (!confirm('Eliminare questo binding?')) return;
const $row = $(this).closest('tr');
const $btn = $(this);
$globalError.hide();
$btn.prop('disabled', true);
$.post('delete_binding.php', {
id: $row.data('id')
}).then(resp => {
if (resp && resp.success) {
$row.fadeOut(200, () => $row.remove());
} else {
throw new Error((resp && resp.error) || 'Errore eliminazione');
}
}).catch(err => {
$globalError.text(err.message || 'Errore durante l\'eliminazione.').show();
$btn.prop('disabled', false);
});
});
});
</script>
</body>
</html>