added order column
This commit is contained in:
@@ -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()
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user