added order column

This commit is contained in:
2026-05-25 10:59:58 +02:00
parent 5da37a7836
commit e6820fdb62
3 changed files with 216 additions and 12 deletions
@@ -58,6 +58,7 @@ $stmt = $pdo->prepare("
SELECT
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'] ?? '';
<th style="width:45px; text-align:center;">Import</th>
<th style="width:45px; text-align:center;">Parts</th>
<th style="width:320px;">Title</th>
<th style="width:70px; text-align:center;">Order</th>
<th style="width:120px;">Type</th>
<th><?php echo $sourceType === 'API' ? 'JSON Mapping' : 'Mapping'; ?></th>
<th>Default Value</th>
@@ -383,6 +395,12 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
<?php endif; ?>
</td>
<td class="text-center">
<?php echo (int)($mapping['field_order'] ?? 9999); ?>
</td>
<td><?php echo htmlspecialchars($mapping['data_type'] ?? 'N/A'); ?></td>
<td><?php echo htmlspecialchars($mapping['data_type'] ?? 'N/A'); ?></td>
<td>
@@ -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})` : '';
@@ -0,0 +1,107 @@
<?php
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/class/VisualLimsApiClient.class.php';
require_once dirname(__FILE__) . '/include/headscript.php';
header('Content-Type: application/json');
ini_set('display_errors', '0');
error_reporting(E_ALL);
try {
$db = DBHandlerSelect::getInstance();
$pdo = $db->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);
}
@@ -0,0 +1,79 @@
<?php
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/class/VisualLimsApiClient.class.php';
require_once dirname(__FILE__) . '/include/headscript.php';
header('Content-Type: application/json');
ini_set('display_errors', '0');
error_reporting(E_ALL);
try {
$schemaId = isset($_GET['schema_id']) && is_numeric($_GET['schema_id'])
? intval($_GET['schema_id'])
: 0;
if ($schemaId <= 0) {
throw new Exception('Missing or invalid schema_id');
}
$api = VisualLimsApiClient::getInstance();
$endpoint = "SchemaCustomField($schemaId)?\$expand=SchemiCustomFieldsDettagli(\$expand=CustomField)";
$data = $api->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()
]);
}