diff --git a/db/migrations/20261026101717_create_lims_value_binding.php b/db/migrations/20261026101717_create_lims_value_binding.php new file mode 100644 index 00000000..3b3f4925 --- /dev/null +++ b/db/migrations/20261026101717_create_lims_value_binding.php @@ -0,0 +1,43 @@ +hasTable('lims_value_binding')) { + return; + } + + $this->execute(" + CREATE TABLE `lims_value_binding` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `template_id` int(11) NOT NULL, + `mapping_id` int(11) NOT NULL, + `field_id` int(11) NOT NULL, + `source_value` varchar(500) NOT NULL, + `source_key` varchar(500) NOT NULL, + `lims_value_id` int(11) NOT NULL, + `lims_value` varchar(500) DEFAULT NULL, + `created_by` int(11) DEFAULT NULL, + `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_mapping_source` (`mapping_id`, `source_key`), + KEY `idx_template` (`template_id`), + KEY `idx_field_source` (`field_id`, `source_key`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci + "); + } + + public function down(): void + { + $this->execute("DROP TABLE IF EXISTS `lims_value_binding`"); + } +} diff --git a/db/migrations/20261026101723_xls_fixed_field_binding.php b/db/migrations/20261026101723_xls_fixed_field_binding.php new file mode 100644 index 00000000..95c13804 --- /dev/null +++ b/db/migrations/20261026101723_xls_fixed_field_binding.php @@ -0,0 +1,81 @@ +table('template_fixed_mapping'); + + if (!$tfm->hasColumn('json_node')) { + $this->execute("ALTER TABLE `template_fixed_mapping` + ADD COLUMN `json_node` varchar(500) DEFAULT NULL"); + } + + if (!$tfm->hasColumn('default_source')) { + $this->execute("ALTER TABLE `template_fixed_mapping` + ADD COLUMN `default_source` enum('manual','xls','json') NOT NULL DEFAULT 'manual'"); + } else { + $this->execute("ALTER TABLE `template_fixed_mapping` + MODIFY COLUMN `default_source` enum('manual','xls','json') NOT NULL DEFAULT 'manual'"); + } + + if (!$tfm->hasColumn('excel_column')) { + $this->execute("ALTER TABLE `template_fixed_mapping` + ADD COLUMN `excel_column` varchar(255) DEFAULT NULL AFTER `json_node`"); + } + + // --- lims_value_binding --- + $lvb = $this->table('lims_value_binding'); + + if (!$lvb->hasColumn('binding_kind')) { + $this->execute("ALTER TABLE `lims_value_binding` + ADD COLUMN `binding_kind` enum('custom','fixed') NOT NULL DEFAULT 'custom' AFTER `template_id`"); + } + + $this->execute("ALTER TABLE `lims_value_binding` + MODIFY COLUMN `field_id` int(11) NOT NULL DEFAULT 0"); + + if ($lvb->hasIndexByName('uq_mapping_source')) { + $this->execute("ALTER TABLE `lims_value_binding` DROP INDEX `uq_mapping_source`"); + } + + if (!$lvb->hasIndexByName('uq_kind_mapping_source')) { + $this->execute("ALTER TABLE `lims_value_binding` + ADD UNIQUE KEY `uq_kind_mapping_source` (`binding_kind`, `mapping_id`, `source_key`)"); + } + } + + public function down(): void + { + $lvb = $this->table('lims_value_binding'); + + if ($lvb->hasIndexByName('uq_kind_mapping_source')) { + $this->execute("ALTER TABLE `lims_value_binding` DROP INDEX `uq_kind_mapping_source`"); + } + if (!$lvb->hasIndexByName('uq_mapping_source')) { + $this->execute("ALTER TABLE `lims_value_binding` + ADD UNIQUE KEY `uq_mapping_source` (`mapping_id`, `source_key`)"); + } + if ($lvb->hasColumn('binding_kind')) { + $this->execute("ALTER TABLE `lims_value_binding` DROP COLUMN `binding_kind`"); + } + $this->execute("ALTER TABLE `lims_value_binding` + MODIFY COLUMN `field_id` int(11) NOT NULL"); + + $tfm = $this->table('template_fixed_mapping'); + foreach (['excel_column', 'default_source', 'json_node'] as $col) { + if ($tfm->hasColumn($col)) { + $this->execute("ALTER TABLE `template_fixed_mapping` DROP COLUMN `$col`"); + } + } + } +} diff --git a/public/userarea/filter_records.php b/public/userarea/filter_records.php index 6c47bdab..6534c850 100644 --- a/public/userarea/filter_records.php +++ b/public/userarea/filter_records.php @@ -39,8 +39,14 @@ if (!empty($explicitIds)) { // Scoping di sicurezza: solo record del template/utente/import corretti. $conds = ['d.templateid = ?', 'd.status = ?']; $params = [$templateId, $status]; - if (!$showAll) { $conds[] = 'd.user_id = ?'; $params[] = $userId; } - if ($importref !== '') { $conds[] = 'd.importreferencecode = ?'; $params[] = $importref; } + if (!$showAll) { + $conds[] = 'd.user_id = ?'; + $params[] = $userId; + } + if ($importref !== '') { + $conds[] = 'd.importreferencecode = ?'; + $params[] = $importref; + } $ph = implode(',', array_fill(0, count($explicitIds), '?')); $conds[] = "d.iddatadb IN ($ph)"; foreach ($explicitIds as $id) $params[] = $id; @@ -57,7 +63,7 @@ if (!empty($explicitIds)) { $idStmt = $pdo->prepare(" SELECT d.iddatadb FROM datadb d WHERE {$whereSql} - ORDER BY d.excelrow ASC, d.iddatadb ASC + ORDER BY CAST(d.excelrow AS UNSIGNED) ASC, d.excelrow ASC, d.iddatadb ASC LIMIT {$perPage} OFFSET {$offset} "); $idStmt->execute($params);