diff --git a/debug_template_recordatorio.php b/debug_template_recordatorio.php new file mode 100644 index 0000000..1cb466b --- /dev/null +++ b/debug_template_recordatorio.php @@ -0,0 +1,116 @@ +fetch( + "SELECT * FROM message_templates WHERE template_name = 'recordatorio_charla' LIMIT 1" + ); + + if (!$template) { + echo "❌ Plantilla 'recordatorio_charla' no encontrada\n"; + exit; + } + + echo "📋 DATOS DE LA PLANTILLA:\n"; + echo "ID: {$template['id']}\n"; + echo "Nombre: {$template['template_name']}\n"; + echo "Idioma: {$template['language_code']}\n"; + echo "Estado: {$template['status']}\n\n"; + + echo "📝 BODY TEXT:\n"; + echo $template['body_text'] . "\n\n"; + + echo "📦 EXAMPLE_PARAMETERS (raw):\n"; + echo $template['example_parameters'] . "\n\n"; + + if (!empty($template['example_parameters'])) { + $examples = json_decode($template['example_parameters'], true); + + if (isset($examples['variables']) && is_array($examples['variables'])) { + echo "✅ Variables guardadas en BD:\n"; + foreach ($examples['variables'] as $i => $var) { + echo "\n Variable #{$i}:\n"; + echo " index: " . ($var['index'] ?? 'N/A') . "\n"; + echo " name: " . ($var['name'] ?? 'N/A') . "\n"; + echo " placeholder: " . ($var['placeholder'] ?? 'N/A') . "\n"; + echo " example: " . ($var['example'] ?? 'N/A') . "\n"; + } + } else { + echo "⚠️ No hay variables en example_parameters\n"; + } + } else { + echo "⚠️ example_parameters está vacío\n"; + } + + echo "\n\n📤 LO QUE DEVOLVERÍA get_template_details.php:\n"; + + // Simular lo que hace get_template_details.php + $bodyText = $template['body_text'] ?? ''; + $variables = []; + + // Primero intentar usar las variables guardadas + if (!empty($template['example_parameters'])) { + $examples = json_decode($template['example_parameters'], true); + if (isset($examples['variables']) && is_array($examples['variables'])) { + foreach ($examples['variables'] as $var) { + $varName = $var['name'] ?? $var['index']; + $variables[] = [ + 'index' => $var['index'], + 'placeholder' => $var['placeholder'], + 'label' => is_numeric($varName) ? 'Variable ' . $varName : ucfirst(str_replace('_', ' ', $varName)), + 'required' => true, + 'example' => $var['example'] ?? null + ]; + } + } + } + + // Si no hay variables guardadas, extraerlas del body_text (fallback) + if (empty($variables) && preg_match_all('/\{\{([^\}]+)\}\}/', $bodyText, $matches)) { + echo "⚠️ Extrayendo variables del body_text (fallback)\n\n"; + $uniqueVars = array_unique($matches[1]); + + $index = 1; + foreach ($uniqueVars as $varName) { + $varIndex = is_numeric($varName) ? intval($varName) : $index++; + $variables[] = [ + 'index' => $varIndex, + 'placeholder' => '{{' . $varName . '}}', + 'label' => is_numeric($varName) ? 'Variable ' . $varName : ucfirst(str_replace('_', ' ', $varName)), + 'required' => true, + 'example' => null + ]; + } + } + + echo json_encode(['variables' => $variables], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + + echo "\n\n🔍 ANÁLISIS:\n"; + if (!empty($variables)) { + $firstPlaceholder = $variables[0]['placeholder'] ?? ''; + if (preg_match('/^\{\{\d+\}\}$/', $firstPlaceholder)) { + echo "❌ PROBLEMA: Las variables están en formato NUMÉRICO ({{1}}, {{2}}, etc.)\n"; + echo " Esto hace que saveReminder() las guarde como array indexado\n"; + echo " Pero la plantilla usa nombres: {{nombre_tema}}, {{fecha}}, etc.\n\n"; + echo "💡 SOLUCIÓN: Necesitas sincronizar la plantilla desde WhatsApp API\n"; + echo " para actualizar example_parameters con los nombres correctos\n"; + } else { + echo "✅ Variables están en formato con NOMBRES\n"; + echo " saveReminder() las guardará como objeto asociativo\n"; + } + } + +} catch (Exception $e) { + echo "❌ Error: " . $e->getMessage() . "\n"; +}