fix: preserve per_type from DB on main form save
The main save rebuilt config from scratch — per_type always started empty, so clicking Guardar wiped whatever savePerType() had just stored. Now: load existing DB config first, use its per_type as the base, apply POST per_type_menu on top only when it is present. Same for welcome_menu. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
8f51659f1e
commit
3c79fac8d7
+24
-12
@@ -320,6 +320,10 @@ $routes = [
|
||||
$companyId = (int)($_POST['company_id'] ?? 0);
|
||||
if ($companyId === 0) { header('Location: /admin/bot-config?msg=error'); exit; }
|
||||
|
||||
// Load existing DB config so keys not in the form (per_type, welcome_menu, etc.) are preserved
|
||||
$existingCompany = CompanyRepository::findById($companyId);
|
||||
$existingConfig = json_decode($existingCompany['config_json'] ?? '{}', true) ?: [];
|
||||
|
||||
$config = [];
|
||||
// Commands
|
||||
$keywords = $_POST['cmd_keyword'] ?? [];
|
||||
@@ -536,20 +540,24 @@ $routes = [
|
||||
$geminiModel = trim($_POST['gemini_model'] ?? '');
|
||||
if ($geminiModel !== '') $config['gemini_model'] = $geminiModel;
|
||||
|
||||
// Per-category menus — merge sobre la config existente para no perder flows/commands por categoría
|
||||
// Per-category menus — base on DB value so form size limits can't wipe it
|
||||
$perTypeMenus = $_POST['per_type_menu'] ?? [];
|
||||
$perType = $config['per_type'] ?? [];
|
||||
foreach ([1, 2, 3] as $cat) {
|
||||
$mk = trim($perTypeMenus[$cat] ?? '');
|
||||
$key = (string)$cat;
|
||||
if ($mk !== '') {
|
||||
$perType[$key] = array_merge($perType[$key] ?? [], ['greeting_menu' => $mk]);
|
||||
} elseif (isset($perType[$key]['greeting_menu'])) {
|
||||
unset($perType[$key]['greeting_menu']);
|
||||
if (empty($perType[$key])) unset($perType[$key]);
|
||||
$dbPerType = $existingConfig['per_type'] ?? [];
|
||||
$perType = (is_array($dbPerType) && $dbPerType !== [] && !isset($dbPerType[0])) ? $dbPerType : [];
|
||||
if (!empty($perTypeMenus)) {
|
||||
// POST has per_type data — apply it on top of DB value
|
||||
foreach ([1, 2, 3] as $cat) {
|
||||
$mk = trim($perTypeMenus[$cat] ?? '');
|
||||
$key = (string)$cat;
|
||||
if ($mk !== '') {
|
||||
$perType[$key] = array_merge($perType[$key] ?? [], ['greeting_menu' => $mk]);
|
||||
} else {
|
||||
unset($perType[$key]['greeting_menu']);
|
||||
if (empty($perType[$key])) unset($perType[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$config['per_type'] = $perType;
|
||||
$config['per_type'] = empty($perType) ? new stdClass() : $perType;
|
||||
|
||||
// General
|
||||
$greeting = trim($_POST['greeting'] ?? '');
|
||||
@@ -563,7 +571,11 @@ $routes = [
|
||||
$approvalWebhook = trim($_POST['approval_webhook'] ?? '');
|
||||
if ($approvalWebhook !== '') $config['approval_webhook'] = $approvalWebhook;
|
||||
$welcomeMenu = trim($_POST['welcome_menu'] ?? '');
|
||||
if ($welcomeMenu !== '') $config['welcome_menu'] = $welcomeMenu;
|
||||
if ($welcomeMenu !== '') {
|
||||
$config['welcome_menu'] = $welcomeMenu;
|
||||
} elseif (!empty($existingConfig['welcome_menu'])) {
|
||||
$config['welcome_menu'] = $existingConfig['welcome_menu'];
|
||||
}
|
||||
|
||||
CompanyRepository::save(['id' => $companyId, 'config_json' => json_encode($config, JSON_UNESCAPED_UNICODE)]);
|
||||
header('Location: /admin/bot-config?id=' . $companyId . '&msg=saved');
|
||||
|
||||
Reference in New Issue
Block a user