From e6820fdb629b5f7e3661fbb20c3bb57298be484f Mon Sep 17 00:00:00 2001 From: solocla Date: Mon, 25 May 2026 10:59:58 +0200 Subject: [PATCH] added order column --- .../userarea/mapping_template_xls_scheme2.php | 42 +++++-- .../update_all_template_mapping_orders.php | 107 ++++++++++++++++++ .../update_template_mapping_order.php | 79 +++++++++++++ 3 files changed, 216 insertions(+), 12 deletions(-) create mode 100644 public/userarea/update_all_template_mapping_orders.php create mode 100644 public/userarea/update_template_mapping_order.php diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php index f0dec9f..8dbd732 100644 --- a/public/userarea/mapping_template_xls_scheme2.php +++ b/public/userarea/mapping_template_xls_scheme2.php @@ -57,7 +57,8 @@ $isSchemajsonEmpty = empty(trim($template['schemajson'] ?? '')); $stmt = $pdo->prepare(" SELECT id, - field_id, + field_id, + field_order, excel_column, json_node, is_manual, @@ -79,6 +80,7 @@ $stmt = $pdo->prepare(" is_visible_parts FROM template_mapping WHERE template_id = ? + ORDER BY field_order ASC, id ASC "); $stmt->execute([$id]); $mappings = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -205,28 +207,37 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; } /* Type */ + /* Order */ #schemaFieldsTable th:nth-child(5), #schemaFieldsTable td:nth-child(5) { + width: 70px; + text-align: center; + white-space: nowrap; + } + + /* Type */ + #schemaFieldsTable th:nth-child(6), + #schemaFieldsTable td:nth-child(6) { width: 120px; white-space: nowrap; } /* Mapping = wide but NOT insane */ - #schemaFieldsTable th:nth-child(6), - #schemaFieldsTable td:nth-child(6) { + #schemaFieldsTable th:nth-child(7), + #schemaFieldsTable td:nth-child(7) { width: 380px; } /* Default Value = wider */ - #schemaFieldsTable th:nth-child(7), - #schemaFieldsTable td:nth-child(7) { + #schemaFieldsTable th:nth-child(8), + #schemaFieldsTable td:nth-child(8) { width: 320px; } /* selects fill the cell */ - #schemaFieldsTable td:nth-child(6) .form-select, - #schemaFieldsTable td:nth-child(7) .form-control, - #schemaFieldsTable td:nth-child(7) .form-select { + #schemaFieldsTable td:nth-child(7) .form-select, + #schemaFieldsTable td:nth-child(8) .form-control, + #schemaFieldsTable td:nth-child(8) .form-select { width: 100% !important; } @@ -347,6 +358,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; Import Parts Title + Order Type Default Value @@ -383,6 +395,12 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; + + + + + + @@ -1737,7 +1755,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; mappedColumn = document.createElement('span'); mappedColumn.className = 'mapped-column'; mappedColumn.style.marginLeft = '5px'; - tr.querySelector('td:nth-child(6)').appendChild(mappedColumn); + tr.querySelector('td:nth-child(7)').appendChild(mappedColumn); } if (!removeBtn) { removeBtn = document.createElement('button'); @@ -1745,7 +1763,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; removeBtn.textContent = 'X'; removeBtn.style.marginLeft = '5px'; removeBtn.setAttribute('data-id', mappingId); - tr.querySelector('td:nth-child(6)').appendChild(removeBtn); + tr.querySelector('td:nth-child(7)').appendChild(removeBtn); removeBtn.addEventListener('click', function(e) { let tr = e.target.closest('tr'); @@ -1791,7 +1809,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; mappedJsonNode = document.createElement('span'); mappedJsonNode.className = 'mapped-json-node'; mappedJsonNode.style.marginLeft = '5px'; - tr.querySelector('td:nth-child(6)').appendChild(mappedJsonNode); + tr.querySelector('td:nth-child(7)').appendChild(mappedJsonNode); } if (!removeJsonBtn) { @@ -1800,7 +1818,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; removeJsonBtn.textContent = 'X'; removeJsonBtn.style.marginLeft = '5px'; removeJsonBtn.setAttribute('data-id', mappingId); - tr.querySelector('td:nth-child(6)').appendChild(removeJsonBtn); + tr.querySelector('td:nth-child(7)').appendChild(removeJsonBtn); } mappedJsonNode.textContent = event.target.value ? `(${event.target.value})` : ''; diff --git a/public/userarea/update_all_template_mapping_orders.php b/public/userarea/update_all_template_mapping_orders.php new file mode 100644 index 0000000..de66dc1 --- /dev/null +++ b/public/userarea/update_all_template_mapping_orders.php @@ -0,0 +1,107 @@ +getConnection(); + + $api = VisualLimsApiClient::getInstance(); + + // Get all schemas currently used in template_mapping + $stmtSchemas = $pdo->query(" + SELECT DISTINCT schema_id + FROM template_mapping + WHERE schema_id IS NOT NULL + AND schema_id > 0 + ORDER BY schema_id ASC + "); + + $schemaIds = $stmtSchemas->fetchAll(PDO::FETCH_COLUMN); + + if (empty($schemaIds)) { + throw new Exception('No schema_id found in template_mapping'); + } + + $stmtUpdate = $pdo->prepare(" + UPDATE template_mapping + SET field_order = ? + WHERE schema_id = ? + AND field_id = ? + "); + + $summary = []; + $totalUpdated = 0; + + foreach ($schemaIds as $schemaId) { + $schemaId = (int)$schemaId; + + $endpoint = "SchemaCustomField($schemaId)?\$expand=SchemiCustomFieldsDettagli(\$expand=CustomField)"; + $data = $api->get($endpoint); + + if (empty($data['SchemiCustomFieldsDettagli']) || !is_array($data['SchemiCustomFieldsDettagli'])) { + $summary[] = [ + 'schema_id' => $schemaId, + 'success' => false, + 'message' => 'No SchemiCustomFieldsDettagli found' + ]; + continue; + } + + $schemaUpdated = 0; + $notFound = []; + + foreach ($data['SchemiCustomFieldsDettagli'] as $detail) { + $order = intval($detail['Ordine'] ?? 9999); + $fieldId = intval($detail['CustomField']['IdCustomField'] ?? 0); + + if ($fieldId <= 0) { + continue; + } + + $stmtUpdate->execute([ + $order, + $schemaId, + $fieldId + ]); + + if ($stmtUpdate->rowCount() > 0) { + $schemaUpdated++; + $totalUpdated++; + } else { + $notFound[] = [ + 'field_id' => $fieldId, + 'order' => $order, + 'label' => $detail['CustomField']['Titolo'] ?? '' + ]; + } + } + + $summary[] = [ + 'schema_id' => $schemaId, + 'success' => true, + 'updated' => $schemaUpdated, + 'not_found_count' => count($notFound), + 'not_found' => $notFound + ]; + } + + echo json_encode([ + 'success' => true, + 'schemas_processed' => count($schemaIds), + 'total_updated' => $totalUpdated, + 'summary' => $summary + ], JSON_PRETTY_PRINT); +} catch (Exception $e) { + http_response_code(500); + + echo json_encode([ + 'success' => false, + 'message' => $e->getMessage() + ], JSON_PRETTY_PRINT); +} diff --git a/public/userarea/update_template_mapping_order.php b/public/userarea/update_template_mapping_order.php new file mode 100644 index 0000000..9212e27 --- /dev/null +++ b/public/userarea/update_template_mapping_order.php @@ -0,0 +1,79 @@ +get($endpoint); + + if (empty($data['SchemiCustomFieldsDettagli']) || !is_array($data['SchemiCustomFieldsDettagli'])) { + throw new Exception('No SchemiCustomFieldsDettagli found'); + } + + $db = DBHandlerSelect::getInstance(); + $pdo = $db->getConnection(); + + $updated = 0; + $notFound = []; + + $stmt = $pdo->prepare(" + UPDATE template_mapping + SET field_order = ? + WHERE schema_id = ? + AND field_id = ? + "); + + foreach ($data['SchemiCustomFieldsDettagli'] as $detail) { + $order = intval($detail['Ordine'] ?? 9999); + $fieldId = intval($detail['CustomField']['IdCustomField'] ?? 0); + + if ($fieldId <= 0) { + continue; + } + + $stmt->execute([ + $order, + $schemaId, + $fieldId + ]); + + if ($stmt->rowCount() > 0) { + $updated++; + } else { + $notFound[] = [ + 'field_id' => $fieldId, + 'order' => $order, + 'label' => $detail['CustomField']['Titolo'] ?? '' + ]; + } + } + + echo json_encode([ + 'success' => true, + 'schema_id' => $schemaId, + 'updated' => $updated, + 'not_found' => $notFound + ]); +} catch (Exception $e) { + http_response_code(500); + + echo json_encode([ + 'success' => false, + 'message' => $e->getMessage() + ]); +}