getConnection(); $pdo->beginTransaction(); // 1. Get source template $stmt = $pdo->prepare(" SELECT name, source_type, header_row, start_column, description, target_table, sample_xlsx, button_size, button_bg_color, button_text_color, button_label, status, client_specific_fields, idclient, clientname, schemaname, idschema, schemajson, xls_headers, api_sample_json, json_nodes, idroutine FROM excel_templates WHERE id = ? LIMIT 1 "); $stmt->execute([$sourceTemplateId]); $template = $stmt->fetch(PDO::FETCH_ASSOC); if (!$template) { throw new Exception("Template not found."); } // 2. Generate unique clone name $baseName = 'Copia di ' . $template['name']; $newName = $baseName; $checkStmt = $pdo->prepare("SELECT COUNT(*) FROM excel_templates WHERE name = ?"); $checkStmt->execute([$newName]); if ((int)$checkStmt->fetchColumn() > 0) { $counter = 2; do { $newName = $baseName . ' (' . $counter . ')'; $checkStmt->execute([$newName]); $exists = (int)$checkStmt->fetchColumn() > 0; $counter++; } while ($exists); } // 3. Insert cloned template $insertTemplate = $pdo->prepare(" INSERT INTO excel_templates ( name, source_type, created_at, updated_at, header_row, start_column, description, target_table, sample_xlsx, button_size, button_bg_color, button_text_color, button_label, status, client_specific_fields, idclient, clientname, schemaname, idschema, schemajson, xls_headers, api_sample_json, json_nodes, idroutine ) VALUES ( :name, :source_type, NOW(), NOW(), :header_row, :start_column, :description, :target_table, :sample_xlsx, :button_size, :button_bg_color, :button_text_color, :button_label, :status, :client_specific_fields, :idclient, :clientname, :schemaname, :idschema, :schemajson, :xls_headers, :api_sample_json, :json_nodes, :idroutine ) "); $insertTemplate->execute([ ':name' => $newName, ':source_type' => $template['source_type'], ':header_row' => $template['header_row'], ':start_column' => $template['start_column'], ':description' => $template['description'], ':target_table' => $template['target_table'], ':sample_xlsx' => $template['sample_xlsx'], ':button_size' => $template['button_size'], ':button_bg_color' => $template['button_bg_color'], ':button_text_color' => $template['button_text_color'], ':button_label' => $template['button_label'], ':status' => $template['status'], ':client_specific_fields' => $template['client_specific_fields'], ':idclient' => $template['idclient'], ':clientname' => $template['clientname'], ':schemaname' => $template['schemaname'], ':idschema' => $template['idschema'], ':schemajson' => $template['schemajson'], ':xls_headers' => $template['xls_headers'], ':api_sample_json' => $template['api_sample_json'], ':json_nodes' => $template['json_nodes'], ':idroutine' => $template['idroutine'] ]); $newTemplateId = (int)$pdo->lastInsertId(); if ($newTemplateId <= 0) { throw new Exception("Unable to create cloned template."); } // 4. Clone template_mapping rows $cloneMappings = $pdo->prepare(" INSERT INTO template_mapping ( template_id, schema_id, field_id, excel_column, json_node, is_manual, manual_default, auto_value, data_type, is_required, default_value, has_list, length, decimals, min_value, max_value, default_curr_date, tablename, field_label, main_field, is_visible_import, is_visible_parts ) SELECT :new_template_id, schema_id, field_id, excel_column, json_node, is_manual, manual_default, auto_value, data_type, is_required, default_value, has_list, length, decimals, min_value, max_value, default_curr_date, tablename, field_label, main_field, is_visible_import, is_visible_parts FROM template_mapping WHERE template_id = :source_template_id "); $cloneMappings->execute([ ':new_template_id' => $newTemplateId, ':source_template_id' => $sourceTemplateId ]); $pdo->commit(); header("Location: templates_dashboard.php?cloned=1&new_id=" . $newTemplateId); exit; } catch (Exception $e) { if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack(); } die("Clone error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')); }