update template and edit import
This commit is contained in:
@@ -13,7 +13,7 @@ try {
|
||||
throw new Exception("Invalid request method.");
|
||||
}
|
||||
|
||||
// Recupera i dati dal body JSON (non da $_POST direttamente)
|
||||
// Recupera i dati dal body JSON
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$template_id = isset($input['template_id']) ? intval($input['template_id']) : null;
|
||||
$schemajson = isset($input['schemajson']) ? trim($input['schemajson']) : '';
|
||||
@@ -33,22 +33,86 @@ try {
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
// Aggiorna il database
|
||||
// Recupera il target_table e verifica l'esistenza del template
|
||||
$stmt = $pdo->prepare("SELECT target_table FROM excel_templates WHERE id = ?");
|
||||
$stmt->execute([$template_id]);
|
||||
$template = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$template) {
|
||||
throw new Exception("Template not found.");
|
||||
}
|
||||
$target_table = $template['target_table'];
|
||||
|
||||
// Inizia una transazione
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Aggiorna il schemajson in excel_templates
|
||||
$stmt = $pdo->prepare("UPDATE excel_templates
|
||||
SET schemajson = ?, updated_at = NOW()
|
||||
WHERE id = ?");
|
||||
$stmt->execute([
|
||||
$schemajson,
|
||||
$template_id
|
||||
]);
|
||||
$stmt->execute([$schemajson, $template_id]);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$response["message"] = "Schema JSON updated successfully!";
|
||||
} else {
|
||||
$response["message"] = "No changes detected, but operation completed.";
|
||||
// Estrai i campi dallo schema
|
||||
$schema_id = $decoded_json['IdSchemaCustomFields'] ?? null;
|
||||
$schema_fields = $decoded_json['SchemiCustomFieldsDettagli'] ?? [];
|
||||
if (empty($schema_id)) {
|
||||
throw new Exception("Schema ID not found in schemajson.");
|
||||
}
|
||||
if (empty($schema_fields)) {
|
||||
throw new Exception("No fields found in schema.");
|
||||
}
|
||||
|
||||
// Prepara la query per l'inserimento condizionato in template_mapping
|
||||
$insert_stmt = $pdo->prepare("
|
||||
INSERT INTO template_mapping (
|
||||
template_id, schema_id, field_id, data_type, is_required, default_value,
|
||||
has_list, length, decimals, min_value, max_value, default_curr_date,
|
||||
tablename, field_label
|
||||
)
|
||||
SELECT :template_id, :schema_id, :field_id, :data_type, :is_required, :default_value,
|
||||
:has_list, :length, :decimals, :min_value, :max_value, :default_curr_date,
|
||||
:tablename, :field_label
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM template_mapping
|
||||
WHERE template_id = :template_id_check AND field_id = :field_id_check
|
||||
)
|
||||
");
|
||||
|
||||
// Itera sui campi dello schema e inserisci quelli mancanti
|
||||
foreach ($schema_fields as $field) {
|
||||
$custom_field = $field['CustomField'] ?? [];
|
||||
if (empty($custom_field['IdCustomField'])) {
|
||||
continue; // Salta se manca l'ID del campo
|
||||
}
|
||||
|
||||
$insert_stmt->execute([
|
||||
':template_id' => $template_id,
|
||||
':schema_id' => $schema_id,
|
||||
':field_id' => $custom_field['IdCustomField'],
|
||||
':data_type' => $custom_field['Tipo'] ?? 'Testo',
|
||||
':is_required' => $custom_field['ObbligatorioWeb'] ? 1 : 0,
|
||||
':default_value' => $custom_field['ValoreDefault'] ?? null,
|
||||
':has_list' => $custom_field['Elenco'] ? 1 : 0,
|
||||
':length' => $custom_field['Lunghezza'] ?? 0,
|
||||
':decimals' => $custom_field['Decimali'] ?? 0,
|
||||
':min_value' => $custom_field['Minimo'] ?? null,
|
||||
':max_value' => $custom_field['Massimo'] ?? null,
|
||||
':default_curr_date' => $custom_field['DefaultCurrDate'] ? 1 : 0,
|
||||
':tablename' => $target_table,
|
||||
':field_label' => $custom_field['TitoloTraduzione'] ?? '',
|
||||
':template_id_check' => $template_id,
|
||||
':field_id_check' => $custom_field['IdCustomField']
|
||||
]);
|
||||
}
|
||||
|
||||
// Commit della transazione
|
||||
$pdo->commit();
|
||||
|
||||
$response["success"] = true;
|
||||
$response["message"] = "Schema JSON updated and template mappings created successfully!";
|
||||
} catch (Exception $e) {
|
||||
if (isset($pdo) && $pdo->inTransaction()) {
|
||||
$pdo->rollback();
|
||||
}
|
||||
$response["message"] = $e->getMessage();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user