diff --git a/Bootstrap_new/js/ux-manager.js b/Bootstrap_new/js/ux-manager.js index 5a6c9e5..915e7c3 100644 --- a/Bootstrap_new/js/ux-manager.js +++ b/Bootstrap_new/js/ux-manager.js @@ -187,6 +187,11 @@ class NavigationManager { // Configuration des comportements this.setupActiveMenu(); + + setTimeout(() => { + this.validateAndFixMenuConsistency(); + }, 300); + this.setupMenuBehavior(); this.setupKeyboardNavigation(); @@ -489,7 +494,55 @@ class ContextPanelManager { this.isOpen = false; this.isInitialized = false; } - + + + /** + * Vérifie et corrige la cohérence menu/page + */ + validateAndFixMenuConsistency() { + const configParentId = window.appConfig?.activeParentId; + const activeLink = window.appConfig?.activeLink; + + if (!configParentId || !activeLink) return; + + const configMenuId = `submenu${configParentId}`; + const configMenu = document.getElementById(configMenuId); + + // Vérification 1: Le menu configuré existe-t-il? + if (!configMenu) { + console.warn(`[Navigation] Menu configuré ${configMenuId} non trouvé, auto-détection...`); + this.autoDetectActiveMenu(); + return; + } + + // Vérification 2: Le menu contient-il un lien vers la page active? + const hasActiveLink = configMenu.querySelector(`a[href*="${activeLink}"]`); + if (!hasActiveLink) { + console.warn(`[Navigation] Menu ${configMenuId} ne contient pas ${activeLink}, recherche alternative...`); + + // Chercher dans tous les menus + document.querySelectorAll('.nav-submenu').forEach(menu => { + if (menu.querySelector(`a[href*="${activeLink}"]`)) { + const correctMenuId = menu.id; + console.log(`[Navigation] ${activeLink} trouvé dans ${correctMenuId}`); + + // Mettre à jour la config + this.activeMenuId = correctMenuId; + if (window.appConfig) { + const menuNum = correctMenuId.replace('submenu', ''); + window.appConfig.activeParentId = menuNum; + } + + // Appliquer + setTimeout(() => { + this.openMenu(correctMenuId); + this.currentOpenMenu = correctMenuId; + }, 100); + } + }); + } + } + /** * Initialiser le panel contextuel * @public - Méthode appelée par UXManager diff --git a/Vue/gabarit.php b/Vue/gabarit.php index 917daed..b582594 100755 --- a/Vue/gabarit.php +++ b/Vue/gabarit.php @@ -10,44 +10,57 @@ $_SESSION['firstLevelMenu'] = $activeLink; $companyDisplayName = htmlspecialchars($_SESSION['nomSociete'], ENT_QUOTES); $imgData = $_SESSION['photoAssureCrypte'] ?? ''; -// Détection automatique des menus actifs - VERSION CORRIGÉE -$activeParentId = null; -$activeChildId = null; +// ============================================ +// FONCTION DE DÉTECTION - À METTRE EN HAUT DU FICHIER +// ============================================ -foreach ($menus as $key0 => $menuParent) { - $menuChildrenLevelOne = $gabary->get_menus_by_parent_code($menuParent['vue']); +/** + * Détecte le menu actif de manière fiable + * @param string $activeLink Lien de la page active (ex: "Accueil") + * @param array $menus Liste des menus parents + * @param object $gabary Instance Gabary pour récupérer les enfants + * @return array ['parent' => string, 'child' => string|null] + */ +function detectActiveMenu($activeLink, $menus, $gabary) { + // LOG SIMPLIFIÉE ET FIABLE : - // 1. Vérifier si c'est un lien DIRECT (comme "Accueil") - if (empty($menuChildrenLevelOne)) { - // Menu sans enfants = lien direct - $parentLink = explode('/', $menuParent['lienMenu'])[0] ?? ''; - if (!empty($parentLink) && $parentLink == $activeLink) { - $activeParentId = $key0; - break; - } - } - // 2. Vérifier les SOUS-MENUS (enfants) - else { - foreach ($menuChildrenLevelOne as $key1 => $menuChild) { + // 1. ACCUEIL = TOUJOURS menu 0 (priorité absolue) + if ($activeLink == 'Accueil') { + return ['parent' => '0', 'child' => null]; + } + + // 2. Recherche dans les autres menus + foreach ($menus as $key0 => $menuParent) { + $menuChildren = $gabary->get_menus_by_parent_code($menuParent['vue']); + + // Vérifier chaque enfant + foreach ($menuChildren as $key1 => $menuChild) { $childLink = explode('/', $menuChild['lienMenu'])[0] ?? ''; if ($childLink == $activeLink) { - $activeParentId = $key0; - $activeChildId = $key1; - break 2; + return ['parent' => (string)$key0, 'child' => (string)$key1]; } } } + + // 3. Fallback : premier menu + return ['parent' => '0', 'child' => null]; } -// CORRECTION IMPORTANTE : Si c'est "Accueil" et pas trouvé, c'est menu 0 -if ($activeParentId === null && $activeLink == 'Accueil') { - $activeParentId = 0; -} +// ============================================ +// UTILISATION - REMPLACE VOTRE CODE ACTUEL +// ============================================ -// Toujours avoir une valeur -if ($activeParentId === null) { - $activeParentId = 0; -} +$menuData = detectActiveMenu($activeLink, $menus, $gabary); +$activeParentId = $menuData['parent']; +$activeChildId = $menuData['child']; + +// Pour le débogage +echo ""; ?>