From 92c27b1732a21c301bafb2acc5d50c208bc71790 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 31 Dec 2011 01:33:12 +0100
Subject: [PATCH] New: Can add a left menu (first level) into an existing top
 menu.

---
 ChangeLog                                     | 17 +++--
 dev/skeletons/modMyModule.class.php           | 15 ++--
 htdocs/core/class/menubase.class.php          | 76 +++++++++++++------
 htdocs/core/modules/DolibarrModules.class.php |  7 ++
 4 files changed, 75 insertions(+), 40 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f0a0b1264da..ee63510305b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -35,22 +35,23 @@ For users:
 - Fix: Can use POS module with several concurrent users.
 
 For developers:
-- New: Add webservice to get or create a product, service.
-- New: Add webservice to get a user.
-- New: Add hooks to change way of showing/editing lines into dictionnaries.
+- New: Can add a left menu (first level) into an existing top menu.
 - New: Add webservice to get or create a product or service.
+- New: Add webservice to get a user.
+- New: Add more "hooks" (like hooks to change way of showing/editing lines into dictionnaries).
 - New: Log module outputs can be setup with "or" rule (not only "xor").
 - New: Add FirePHP output for logging module.
-- New: Add trigger ACTION_DELETE and ACTION_MODIFY
+- New: Add trigger ACTION_DELETE and ACTION_MODIFY.
 - New: Can define different requests according to database type into migration files.
+- New: Add "canvas" feature to overwrite page of thirdparty, contact, product with yours.
+- Qual: Add a lot of more PHPUnit tests.
 - Qual: Data structure for supplier prices is simpler.
 - Qual: Removed no more used external libraries.
 - Qual: Cleaned a lot of dead code.
-- Qual: Add more "hooks".
-- Qual: Add "canvas" feature to overwrite page of thirdparty, contact, product with yours.
 - Qual: More OOP (usage of "abstract", "static", ...), uniformize constructors.
-- Qual: task #216 : Move /lib in /core/lib directory
-- Qual: task #217 : Move core files in core directory (login, menus, triggers, boxes, modules)
+- Qual: Fix a lot of checkstyle warnings.
+- Qual: task #216 : Move /lib into /core/lib directory
+- Qual: task #217 : Move core files into core directory (login, menus, triggers, boxes, modules)
 
 
 ***** ChangeLog for 3.1 compared to 3.0 *****
diff --git a/dev/skeletons/modMyModule.class.php b/dev/skeletons/modMyModule.class.php
index f0973d60c03..d0c9daabeb5 100644
--- a/dev/skeletons/modMyModule.class.php
+++ b/dev/skeletons/modMyModule.class.php
@@ -169,7 +169,8 @@ class modMyModule extends DolibarrModules
 		$r=0;
 
 		// Add here entries to declare new menus
-		// Example to declare the Top Menu entry:
+		//
+		// Example to declare a new Top Menu entry and its Left menu entry:
 		// $this->menu[$r]=array(	'fk_menu'=>0,			// Put 0 if this is a top menu
 		//							'type'=>'top',			// This is a Top menu entry
 		//							'titre'=>'MyModule top menu',
@@ -182,11 +183,9 @@ class modMyModule extends DolibarrModules
 		//							'target'=>'',
 		//							'user'=>2);				// 0=Menu for internal users, 1=external users, 2=both
 		// $r++;
-		//
-		// Example to declare a Left Menu entry:
-		// $this->menu[$r]=array(	'fk_menu'=>'r=0',		// Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry) or use 'fk_mainmenu=xxx,fk_leftmenu=yyy'
+		// $this->menu[$r]=array(	'fk_menu'=>'r=0',		// Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
 		//							'type'=>'left',			// This is a Left menu entry
-		//							'titre'=>'MyModule left menu 1',
+		//							'titre'=>'MyModule left menu',
 		//							'mainmenu'=>'mymodule',
 		//							'url'=>'/mymodule/pagelevel1.php',
 		//							'langs'=>'mylangfile',	// Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
@@ -197,10 +196,10 @@ class modMyModule extends DolibarrModules
 		//							'user'=>2);				// 0=Menu for internal users, 1=external users, 2=both
 		// $r++;
 		//
-		// Example to declare another Left Menu entry:
-		// $this->menu[$r]=array(	'fk_menu'=>'r=1',		// Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
+		// Example to declare a Left Menu entry into an existing Top menu entry:
+		// $this->menu[$r]=array(	'fk_menu'=>'fk_mainmenu=mainmenucode',		// Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy'
 		//							'type'=>'left',			// This is a Left menu entry
-		//							'titre'=>'MyModule left menu 2',
+		//							'titre'=>'MyModule left menu',
 		//							'mainmenu'=>'mymodule',
 		//							'url'=>'/mymodule/pagelevel2.php',
 		//							'langs'=>'mylangfile',	// Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
diff --git a/htdocs/core/class/menubase.class.php b/htdocs/core/class/menubase.class.php
index ea1a1db783e..0c2b9804600 100644
--- a/htdocs/core/class/menubase.class.php
+++ b/htdocs/core/class/menubase.class.php
@@ -392,24 +392,11 @@ class Menubase
         for ($x = 0; $x < $num; $x++)
         {
             //si un element a pour pere : $pere
-            if ($tab[$x]['fk_menu'] == $pere)
+            if ($tab[$x]['fk_menu'] == $pere && $tab[$x]['enabled'])
             {
-                if ($tab[$x]['enabled'])
-                {
-                    $leftmenuConstraint = true;
-                    if ($tab[$x]['leftmenu'])
-                    {
-                        $leftmenuConstraint = verifCond($tab[$x]['leftmenu']);
-                    }
-
-                    if ($leftmenuConstraint)
-                    {
-                        //print 'name='.$tab[$x][3].' pere='.$pere." ".$tab[$x][6];
-
-                        $this->newmenu->add((! preg_match("/^(http:\/\/|https:\/\/)/i",$tab[$x]['url'])) ? $tab[$x]['url'] : $tab[$x]['url'], $tab[$x]['titre'], $rang -1, $tab[$x]['perms'], $tab[$x]['atarget'], $tab[$x]['mainmenu']);
-                        $this->recur($tab, $tab[$x]['rowid'], $rang +1, $leftmenu);
-                    }
-                }
+                //print 'mainmenu='.$tab[$x]['mainmenu'];
+                $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], $rang - 1, $tab[$x]['perms'], $tab[$x]['atarget'], $tab[$x]['mainmenu']);
+                $this->recur($tab, $tab[$x]['rowid'], $rang + 1, $leftmenu);
             }
         }
     }
@@ -483,23 +470,23 @@ class Menubase
 
         $leftmenu=$myleftmenu;  // To export to dol_eval function
 
-        // We initialize newmenu to return with first already found menu entries
+        // We initialize newmenu with first already found menu entries
         $this->newmenu = $newmenu;
 
-        // Load datas from database into $tabMenu
+        // Load datas from database into $tabMenu, then we will complete this->newmenu with values into $tabMenu
         if (count($tabMenu) == 0)
         {
             $this->menuLoad($leftmenu, $type_user, $menu_handler, $tabMenu);
         }
         //var_dump($tabMenu);
 
-        // Define menutopid
         $menutopid='';
         if (is_array($tabMenu))
         {
-            foreach($tabMenu as $val)
+            foreach($tabMenu as $key => $val)
             {
-                if ($val['type'] == 'top' && $val['mainmenu'] == $mainmenu)    // 9 is type, 8 is mainmenu
+                // Define menutopid of mainmenu
+                if (empty($menutopid) && $val['type'] == 'top' && $val['mainmenu'] == $mainmenu)    // 9 is type, 8 is mainmenu
                 {
                     $menutopid=$val['rowid'];
                     break;
@@ -507,8 +494,48 @@ class Menubase
             }
         }
 
-        // Now edit this->newmenu->list to add entries found into tabMenu that are in childs of mainmenu claimed
+        // Update fk_menu when value is -1 (left menu added by modules with no top menu)
+        if (is_array($tabMenu))
+        {
+            foreach($tabMenu as $key => $val)
+            {
+                if ($val['fk_menu'] == -1 && $val['fk_mainmenu'] == $mainmenu)
+                {
+                    if (empty($val['fk_leftmenu']))
+                    {
+                        //print 'Try to find fk_menu for '.join(',',$val);
+                        //var_dump($this->newmenu->liste);exit;
+                        $tabMenu[$key]['fk_menu']=$menutopid;
+                    }
+                    else if ($val['fk_leftmenu'] == $fk_leftmenu)
+                    {
+                        // TODO
+                        /*
+                        foreach($this->newmenu as $keyparent => $valparent)
+                        {
+                            if (empty($val['fk_leftmenu']) && $valparent['type'] == 'top' && $valparent['mainmenu'] == $val['fk_mainmenu'])
+                            {
+                                $tabMenu[$key]['fk_menu']=$valparent['rowid'];
+                                break;
+                            }
+                            //var_dump($tabMenu);exit;
+                            if (! empty($val['fk_leftmenu']) && $valparent['type'] == 'left' && $valparent['mainmenu'] == $val['fk_mainmenu'] && $valparent['leftmenu'] == $val['fk_leftmenu'])
+                            {
+                                print 'eeee';
+                                $tabMenu[$key]['fk_menu']=$valparent['rowid'];
+                                break;
+                            }
+                        }
+                        */
+                        //exit;
+                    }
+                }
+            }
+        }
+
+        // Now edit this->newmenu->list to add entries found into tabMenu that are childs of mainmenu claimed
         $this->recur($tabMenu, $menutopid, 1, $leftmenu);
+        //var_dump($this->newmenu->liste);exit;
 
         return $this->newmenu;
     }
@@ -596,6 +623,7 @@ class Menubase
                     //print "verifCond chaine=".$chaine." rowid=".$menu['rowid']." ".$menu['enabled'].":".$enabled."<br>\n";
                 }
 
+/*
                 // 0=rowid, 1=fk_menu, 2=url, 3=text, 4=perms, 5=target, 8=mainmenu
                 $tabMenu[$b][0] = $menu['rowid'];
                 $tabMenu[$b][1] = $menu['fk_menu'];
@@ -615,7 +643,7 @@ class Menubase
                 $tabMenu[$b][8] = $menu['mainmenu'];
                 $tabMenu[$b][9] = $menu['type'];
                 $tabMenu[$b][10] = $menu['langs'];
-
+*/
                 // We complete tabMenu
                 $tabMenu[$b]['rowid']       = $menu['rowid'];
                 $tabMenu[$b]['fk_menu']     = $menu['fk_menu'];
diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php
index 3f90ddaf06b..dff911efe48 100644
--- a/htdocs/core/modules/DolibarrModules.class.php
+++ b/htdocs/core/modules/DolibarrModules.class.php
@@ -1129,6 +1129,13 @@ abstract class DolibarrModules
                     $menu->fk_leftmenu=$reg[2];
                     $foundparent=1;
                 }
+                elseif (preg_match('/fk_mainmenu=(.*)/',$fk_parent,$reg))
+                {
+                    $menu->fk_menu=-1;
+                    $menu->fk_mainmenu=$reg[1];
+                    $menu->fk_leftmenu='';
+                    $foundparent=1;
+                }
                 if (! $foundparent)
                 {
                     $this->error="ErrorBadDefinitionOfMenuArrayInModuleDescriptor (bad value for key fk_menu)";
-- 
GitLab