refactor: simplificar Por Categoría — sin toggle, greeting_flow directo

- Eliminar checkbox 'Pedir finca al inicio': innecesario porque el
  dynamic_list ya auto-selecciona cuando solo hay 1 opción (finca, empresa, etc.)
- greeting_flow controla si hay flujo de bienvenida; si está vacío → greeting_menu
- Si greeting_flow tiene valor → greeting='' (dispara el flow) y se oculta greeting_menu
- Hint text explica que con 1 sola opción se auto-selecciona sin preguntar
- save-per-type deriva greeting/greeting_menu del greeting_flow, no de un toggle

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-30 23:19:50 -05:00
co-authored by Claude Sonnet 4.6
parent 909c6be32f
commit fe048f53a3
2 changed files with 28 additions and 51 deletions
+19 -41
View File
@@ -3213,14 +3213,11 @@ HTML;
foreach ($flowKeysList as $fk) $flowOptsBase .= '<option value="' . self::h($fk) . '">🔀 ' . self::h($fk) . '</option>'; foreach ($flowKeysList as $fk) $flowOptsBase .= '<option value="' . self::h($fk) . '">🔀 ' . self::h($fk) . '</option>';
foreach ([1, 2, 3] as $cat) { foreach ([1, 2, 3] as $cat) {
$catCfg = $perTypeConfig[(string)$cat] ?? []; $catCfg = $perTypeConfig[(string)$cat] ?? [];
$m = $catMeta[(string)$cat]; $m = $catMeta[(string)$cat];
$greetingVal = array_key_exists('greeting', $catCfg) ? $catCfg['greeting'] : null; $greetFlow = self::h($catCfg['greeting_flow'] ?? '');
$askFinca = $greetingVal !== null; $fallback = $catCfg['fallback_flow'] ?? '';
$chk = $askFinca ? ' checked' : ''; $selMenu = $catCfg['greeting_menu'] ?? '';
$greetFlow = self::h($catCfg['greeting_flow'] ?? ($askFinca ? 'ask_finca' : ''));
$fallback = $catCfg['fallback_flow'] ?? '';
$selMenu = $catCfg['greeting_menu'] ?? '';
// greeting_menu selector (insert selected attr) // greeting_menu selector (insert selected attr)
$menuOpts = str_replace( $menuOpts = str_replace(
@@ -3235,8 +3232,10 @@ HTML;
$flowOptsBase $flowOptsBase
); );
$greetFlowStyle = $askFinca ? '' : 'display:none'; // Mostrar greeting_menu solo cuando NO hay greeting_flow (cat 1)
$greetMenuStyle = $askFinca ? 'display:none' : ''; $hasGreetFlow = $greetFlow !== '';
$greetFlowStyle = '';
$greetMenuStyle = $hasGreetFlow ? 'display:none' : '';
$label = $m['label']; $icon = $m['icon']; $bg = $m['bg']; $bd = $m['bd']; $clr = $m['clr']; $label = $m['label']; $icon = $m['icon']; $bg = $m['bg']; $bd = $m['bd']; $clr = $m['clr'];
$categoryTabHtml .= <<<CATHTML $categoryTabHtml .= <<<CATHTML
@@ -3245,26 +3244,21 @@ HTML;
<span style="font-size:20px">{$icon}</span> <span style="font-size:20px">{$icon}</span>
<span style="font-weight:700;font-size:13px;color:{$clr}">{$label}</span> <span style="font-weight:700;font-size:13px;color:{$clr}">{$label}</span>
</div> </div>
<div class="form-group" style="margin-bottom:12px"> <div class="form-group" style="{$greetFlowStyle};margin-bottom:12px">
<label style="display:flex;align-items:center;gap:8px;font-weight:500">
<input type="checkbox" name="per_type_ask_finca[{$cat}]" value="1"{$chk} onchange="toggleAskFinca(this,{$cat})">
Pedir selección de finca al inicio
</label>
<div style="font-size:11px;color:#7a8291;margin-top:3px">Al activarlo, el bot solicita la finca antes de mostrar el menú principal</div>
</div>
<div id="cat-greet-flow-{$cat}" style="{$greetFlowStyle}" class="form-group" style="margin-bottom:12px">
<label>Flujo de bienvenida</label> <label>Flujo de bienvenida</label>
<input type="text" name="per_type_greeting_flow[{$cat}]" value="{$greetFlow}" <input type="text" name="per_type_greeting_flow[{$cat}]" value="{$greetFlow}"
list="ptFlowsDatalist" placeholder="ej: ask_finca" list="ptFlowsDatalist" placeholder="ej: ask_finca (se auto-selecciona si hay 1 sola opción)"
style="width:100%;padding:8px 12px;border:1px solid #e2e5ea;border-radius:8px;font-size:13px"> style="width:100%;padding:8px 12px;border:1px solid #e2e5ea;border-radius:8px;font-size:13px">
<div style="font-size:11px;color:#7a8291;margin-top:3px">Flow que se ejecuta en el primer mensaje del usuario</div> <div style="font-size:11px;color:#7a8291;margin-top:3px">
Flow que se ejecuta al primer mensaje. Si el flow retorna 1 sola opción (finca, empresa, etc.) la selecciona automáticamente sin preguntar.
</div>
</div> </div>
<div id="cat-greet-menu-{$cat}" style="{$greetMenuStyle}" class="form-group" style="margin-bottom:12px"> <div id="cat-greet-menu-{$cat}" style="{$greetMenuStyle}" class="form-group" style="margin-bottom:12px">
<label>Menú inicial</label> <label>Menú inicial</label>
<select name="per_type_menu[{$cat}]" style="width:100%;padding:9px 12px;border:1px solid #e2e5ea;border-radius:8px;font-size:13px"> <select name="per_type_menu[{$cat}]" style="width:100%;padding:9px 12px;border:1px solid #e2e5ea;border-radius:8px;font-size:13px">
{$menuOpts} {$menuOpts}
</select> </select>
<div style="font-size:11px;color:#7a8291;margin-top:3px">Menú que se muestra cuando el usuario no tiene contexto activo</div> <div style="font-size:11px;color:#7a8291;margin-top:3px">Menú que se muestra cuando no hay flujo de bienvenida configurado</div>
</div> </div>
<div class="form-group" style="margin-bottom:0"> <div class="form-group" style="margin-bottom:0">
<label>Flujo de respaldo (fallback)</label> <label>Flujo de respaldo (fallback)</label>
@@ -3911,33 +3905,17 @@ HTML;
const CONFIG = {$botConfigJson}; const CONFIG = {$botConfigJson};
const ENDPOINTS = {$botEndpointsJson}; const ENDPOINTS = {$botEndpointsJson};
function toggleAskFinca(chk, cat) {
var flowDiv = document.getElementById('cat-greet-flow-' + cat);
var menuDiv = document.getElementById('cat-greet-menu-' + cat);
if (chk.checked) {
if (flowDiv) flowDiv.style.display = '';
if (menuDiv) menuDiv.style.display = 'none';
var gf = document.querySelector('[name="per_type_greeting_flow[' + cat + ']"]');
if (gf && gf.value === '') gf.value = 'ask_finca';
} else {
if (flowDiv) flowDiv.style.display = 'none';
if (menuDiv) menuDiv.style.display = '';
}
}
function savePerType() { function savePerType() {
const cid = document.querySelector('input[name="company_id"]').value; const cid = document.querySelector('input[name="company_id"]').value;
const fd = new FormData(); const fd = new FormData();
fd.set('company_id', cid); fd.set('company_id', cid);
[1, 2, 3].forEach(function(cat) { [1, 2, 3].forEach(function(cat) {
var chk = document.querySelector('[name="per_type_ask_finca[' + cat + ']"]'); var gf = document.querySelector('[name="per_type_greeting_flow[' + cat + ']"]');
fd.append('per_type_ask_finca[' + cat + ']', chk && chk.checked ? '1' : '0');
var gf = document.querySelector('[name="per_type_greeting_flow[' + cat + ']"]');
if (gf) fd.append('per_type_greeting_flow[' + cat + ']', gf.value);
var sel = document.querySelector('[name="per_type_menu[' + cat + ']"]'); var sel = document.querySelector('[name="per_type_menu[' + cat + ']"]');
var fb = document.querySelector('[name="per_type_fallback[' + cat + ']"]');
if (gf) fd.append('per_type_greeting_flow[' + cat + ']', gf.value);
if (sel) fd.append('per_type_menu[' + cat + ']', sel.value); if (sel) fd.append('per_type_menu[' + cat + ']', sel.value);
var fb = document.querySelector('[name="per_type_fallback[' + cat + ']"]'); if (fb) fd.append('per_type_fallback[' + cat + ']', fb.value);
if (fb) fd.append('per_type_fallback[' + cat + ']', fb.value);
}); });
var btn = document.getElementById('savePerTypeBtn'); var btn = document.getElementById('savePerTypeBtn');
if (btn) { btn.textContent = 'Guardando...'; btn.disabled = true; } if (btn) { btn.textContent = 'Guardando...'; btn.disabled = true; }
+9 -10
View File
@@ -664,24 +664,23 @@ $routes = [
$perType = (is_array($rawPt) && count(array_filter(array_keys($rawPt), 'is_string')) > 0) $perType = (is_array($rawPt) && count(array_filter(array_keys($rawPt), 'is_string')) > 0)
? $rawPt : []; ? $rawPt : [];
$ptAskFinca = $_POST['per_type_ask_finca'] ?? []; $ptGreetFlow = $_POST['per_type_greeting_flow'] ?? [];
$ptGreetFlow = $_POST['per_type_greeting_flow'] ?? []; $ptMenus = $_POST['per_type_menu'] ?? [];
$ptMenus = $_POST['per_type_menu'] ?? []; $ptFallback = $_POST['per_type_fallback'] ?? [];
$ptFallback = $_POST['per_type_fallback'] ?? [];
foreach ([1, 2, 3] as $cat) { foreach ([1, 2, 3] as $cat) {
$key = (string)$cat; $key = (string)$cat;
$current = $perType[$key] ?? []; // preserve menus/flows/commands from seed $current = $perType[$key] ?? []; // preserve menus/flows/commands from seed
$askFinca = ($ptAskFinca[$cat] ?? '0') === '1'; $gf = trim($ptGreetFlow[$cat] ?? '');
if ($gf !== '') {
if ($askFinca) { // Si tiene greeting_flow → greeting='' para que el flow se dispare al primer mensaje
$gf = trim($ptGreetFlow[$cat] ?? 'ask_finca');
$current['greeting'] = ''; $current['greeting'] = '';
$current['greeting_flow'] = $gf !== '' ? $gf : 'ask_finca'; $current['greeting_flow'] = $gf;
unset($current['greeting_menu']); unset($current['greeting_menu']);
} else { } else {
$current['greeting'] = null; // Sin greeting_flow → usa greeting_menu (cat 1)
$current['greeting'] = null;
unset($current['greeting_flow']); unset($current['greeting_flow']);
$mk = trim($ptMenus[$cat] ?? ''); $mk = trim($ptMenus[$cat] ?? '');
if ($mk !== '') { if ($mk !== '') {