Menus system: Difference between revisions

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
m (Reverted edits by DAK (talk) to last revision by Eldy)
Tags: content model change Rollback
 
(31 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<!-- BEGIN origin interlang links -->
<!-- You can edit this section but do NOT remove these comments
    Links below will be automatically replicated on translated pages by PolyglotBot -->
[[fr:Système_de_menus]]
[[es:Sistema_de_menús]]
[[de:Menü_System]]
[[zh:菜单]]
<!-- END interlang links -->
[[Category:Core]]
[[Category:Core]]
{{TemplateDocDev}}
{{TemplateDocDev}}


= The system of Dolibarr menus =
= The system of Dolibarr menus =
Dolibarr contains 2 systems to manage menus. One for desktop usage and one for smartphone. A menu system define which entry menu is visible, at which condition, and where (top, left)


{{ToTranslate}}
Changing your menu manager with an existing one can be done from page '''Home -> Menus'''.  
Il y a dans Dolibarr 2 systèmes de menu. Celui du haut et celui de gauche qui peuvent être dépendant l'un de l'autre ou non. On les appelera "menu gauche" et "menu haut".
Take the menu manager you want in the list.
Par défaut, les gestionnaires de ces deux systèmes de menu apportent une approche d'un menu contextuel. Le menu gauche par exemple dépend des éléments métiers que l'on manipule. Cette approche diffère de celle, plus traditionnelle, des applis Windows où le menu déroulant dépend du chemin pris pour atteindre la fonctionnalité. Une sorte d'arbre dans laquelle on ne saute pas d'une branche en changeant de fonction, mais en remontant à la racine de l'arbre (exemple menu "Fichier") pour choisir ensuite la sous branche (exemple "Ouvrir"). Afin de contenter les 2 mondes (questions de choix, de goût ou d'habitudes), Dolibarr offre un système de menu modulaire. Il est en effet possible de développer son propre système de menu, à sa sauce, qui prend complètement l'ascendant sur le menu par défaut. L'autre avantage, pour le développeur, est que toute la logique du menu peut également être centralisée dans un seul et même fichier. Ceci permet d'offrir des systèmes de menu aux comportements complètement différents.
 
= Changer le système de menu =
 
Changer de système de menu est possible via la page Configuration - Interface. Prendre par exemple les gestionnaires de menu "eldybackoffice", fournis en standard (Activer le gestionnaire du menu gauche eldybackoffice en même temps que le gestionnaire du menu haut eldybackoffice, les 2 étant conçus pour fonctionner ensemble). Ces gestionnaires de menu (gauche et haut) sont complètement opérationnels et offrent divers alternatives de menu.
 
Et si cela ne vous convient toujours pas, rien ne vous empêche de développer le vôtre. Voir paragraphe suivant...'''''


= Développer son propre système de menu =
If you want to personalize the menu, you can add you own menu entry or develop your own menu manager, that will replace completely the default one. You will decide how you store your menu definition (database like with the menu manager '''auguria''', centralized hard coded menu entries like in the menu '''eldy''', xml files, ...). Like you see, you can offer a menu manager with the behavior you need and the technology you want. See next chapters for that.


Le but de cet article est de décrire comment créer un nouveau système de menu dans son intégralité (remplacement de l'intégralité des entrées menus, chamboulement complet du concept de navigation).
= Add a menu entry =
Imagine you want to add a new menu entry.
Go on '''Home - Menus''' then choose '''Menu editor'''. Add an entry.
For example to add a menu entry to switch to a page that list opportunities with filters already set, you will add and entry that will looks like this:


Cela ne concerne pas le simple ajout d'entrées de menus. Si tel et le cas et que vous désirez juste savoir comment ajouter des entrées menus au sein d'un gestionnaire de menu existant, dans le cadre du développement d'une extension ou d'un nouveau module par exemple, reporter vous plutôt à la page [[Developpement module]].
<div class="divforimgcentpercent">
[[File:Menu editor.png]]
</div>


Si vous voulez remplacer intégralement un systeme de menu par le votre, le plus simple est de prendre exemple sur le gestionnaire de menu "eldy_backoffice".
= Develop you own menu manager =
The aim of this paper is to describe how to create a new menu system in its entirety (to replace the entire menu entries, complete upheaval of the concept of navigation).
This does not relate how to add menu entries. If you just want to know how to add menu entries within an existing menu manager, when  developing an extension or a new module for example, you should look page [[Module development]] instead.
If you want to completely replace a system menu with your own, the easiest way is to use the menu Manager "eldy_backoffice" as an example:


* Pour développer son '''système de menu haut''', il suffit de
== To develop your own '''top menu''' ==
# Copier le fichier htdocs/includes/menus/barre_top/eldybackoffice.php sous un autre nom comme htdocs/includes/menus/barre_top/monmenu.php
# Copy file htdocs/core/menus/standard/eldy_backoffice.php into a file with a name like htdocs/core/menus/standard/mymenu.php
# Editer ensuite le fichier monmenu.php. La fonction showmenu() est la fonction appelée par Dolibarr lorsqu'il génère une page pour afficher ce menu haut. On peut y mettre le code que l'on veut, cette fonction ne modifie aucune variable extérieur et doit juste afficher par des "print" le menu que l'on veut voir. On peut ainsi récupérer le menu à afficher depuis un fichier de configuration, une base de donnée et le personnaliser par rapport à l'environnement. L'environnement Dolibarr étant stocké dans les 3 objets global suivant: $user, $conf, $langs.
# Edit this file mymenu.php. Function showmenu() of class MenuTop is the function called by Dolibarr, when generating a page, to show the top menu. You can put here the code you want, this function does not change any external variable and must show using "print" the menu to show. So you can get menu to show from a configuration file, a database and personalized it according to environment. Environment Dolibarr is stored into global variables: {{TemplateGlobalVariables}}.
$user contient les informations propres à l'utilisateur.
$user contains information about current user.
$conf contient les informations propres à la configuration (modules actifs, permissions actives, etc...)
$conf contains information about setup (activated modules, permissions, options, etc...)
$langs contient les informations propres à la langue active.
$langs contains information about active language.


Exemple de fonction showmenu() du fichier htdocs/includes/menus/barre_top/monmenu.php
Example of a function showmenu() into file htdocs/core/menus/standard/mymenu.php
<source lang="php">
<source lang="php">
     function showmenu()
     function showmenu()
     {
     {
         global $user,$conf,$langs,$dolibarr_main_db_name;;
         global $user, $conf, $langs;
        global $dolibarr_main_db_name;
          
          
         print '<table class="tmenu"><tr class="tmenu">';
         print '<table class="tmenu"><tr class="tmenu">';
Line 47: Line 59:
</source>
</source>


== To develop your own '''left menu''' ==
Principle is same than for top menu.
# You must edit function showmenu() of class MenuLeft to build your left menu. If you want to show standard menu with just minor changes without redoing everything, you have to loop on content of array $menu->liste. If you want to build a menu completely different and controled only by yourself, you must, into the method showmenu(), create an object $newmenu=new Menu() and use methods $newmenu->add and $newmenu->add_submenu to defined list of left menu entries to show. Once this is done, you show those entries using some "print" to show content of array $newmenu->liste (that we have just defined) instead of printing content of $menu->liste.


* Pour développer son '''système de menu gauche''', le principe est aussi simple.
Example of file htdocs/core/menus/standard/mymenu.php
# Copier le fichier htdocs/includes/menus/barre_left/eldybackoffice.php sous un autre nom comme htdocs/includes/menus/barre_left/monmenu.php.
# Il faut ensuite modifier la fonction showmenu() afin de fabriquer son menu gauche. Si on veut afficher le menu standard ou juste le modifier légèrement sans recréer le sien complètement, il suffit de boucler sur le contenu du tableau $menu->liste, initialisé par Dolibarr "par défaut" grace au fichier pre.inc.php stocké dans le même répertoire que la page appelée. Si vous voulez afficher un menu complètement différent et totalement controlé par vous (et donc ignorer les fichiers pre.inc.php), il vous faut, dans la fonction showmenu(), créer un objet $newmenu=new Menu() et utiliser les méthodes $newmenu->add et $newmenu->add_submenu pour définir la liste des entrées menu gauche à faire apparaitre. En fin de code, on affiche par des print le contenu du tableau $newmenu->liste (qui vient d'etre construit) au lieu de $menu->liste.
 
Exemple de fichier htdocs/includes/menus/barre_left/monmenu.php
<source lang="php">
<source lang="php">
     function showmenu()
     function showmenu()
Line 128: Line 139:
     }
     }
</source>
</source>
= To force usage of your menu manager =
You can force usage of your own menu manager by creating a module.
For this , all you have to do is to declare the 4 following constants into the array $this->const of your module descriptor file (See page [[Module development]]).
<source lang="php">
* 1=>array('MAIN_MENU_STANDARD_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 2=>array('MAIN_MENUFRONT_STANDARD_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 3=>array('MAIN_MENU_SMARTPHONE_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 4=>array('MAIN_MENUFRONT_SMARTPHONE_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
</source>
Your menu manager will be used once the module is activated.

Latest revision as of 14:56, 17 July 2025

The system of Dolibarr menus

Dolibarr contains 2 systems to manage menus. One for desktop usage and one for smartphone. A menu system define which entry menu is visible, at which condition, and where (top, left)

Changing your menu manager with an existing one can be done from page Home -> Menus. Take the menu manager you want in the list.

If you want to personalize the menu, you can add you own menu entry or develop your own menu manager, that will replace completely the default one. You will decide how you store your menu definition (database like with the menu manager auguria, centralized hard coded menu entries like in the menu eldy, xml files, ...). Like you see, you can offer a menu manager with the behavior you need and the technology you want. See next chapters for that.

Add a menu entry

Imagine you want to add a new menu entry. Go on Home - Menus then choose Menu editor. Add an entry. For example to add a menu entry to switch to a page that list opportunities with filters already set, you will add and entry that will looks like this:

Menu editor.png

Develop you own menu manager

The aim of this paper is to describe how to create a new menu system in its entirety (to replace the entire menu entries, complete upheaval of the concept of navigation). This does not relate how to add menu entries. If you just want to know how to add menu entries within an existing menu manager, when developing an extension or a new module for example, you should look page Module development instead. If you want to completely replace a system menu with your own, the easiest way is to use the menu Manager "eldy_backoffice" as an example:

To develop your own top menu

  1. Copy file htdocs/core/menus/standard/eldy_backoffice.php into a file with a name like htdocs/core/menus/standard/mymenu.php
  2. Edit this file mymenu.php. Function showmenu() of class MenuTop is the function called by Dolibarr, when generating a page, to show the top menu. You can put here the code you want, this function does not change any external variable and must show using "print" the menu to show. So you can get menu to show from a configuration file, a database and personalized it according to environment. Environment Dolibarr is stored into global variables: $user, $conf, $db, $langs, $mysoc, $hookmanager, $extrafields.
$user contains information about current user.
$conf contains information about setup (activated modules, permissions, options, etc...)
$langs contains information about active language.

Example of a function showmenu() into file htdocs/core/menus/standard/mymenu.php

    function showmenu()
    {
        global $user, $conf, $langs;
        global $dolibarr_main_db_name;
        
        print '<table class="tmenu"><tr class="tmenu">';

	    // Menu Home
	    print '<td class="tmenu"><a '.$class.' href="'.DOL_URL_ROOT.'/index.php?mainmenu=home&leftmenu="'.($this->atarget?" target=".$this->atarget:"").'>'.$langs->trans("Home").'</a></td>';
	    
	    // Put here other entries
	    // ...
	    
        print '</tr></table>';
    }

To develop your own left menu

Principle is same than for top menu.

  1. You must edit function showmenu() of class MenuLeft to build your left menu. If you want to show standard menu with just minor changes without redoing everything, you have to loop on content of array $menu->liste. If you want to build a menu completely different and controled only by yourself, you must, into the method showmenu(), create an object $newmenu=new Menu() and use methods $newmenu->add and $newmenu->add_submenu to defined list of left menu entries to show. Once this is done, you show those entries using some "print" to show content of array $newmenu->liste (that we have just defined) instead of printing content of $menu->liste.

Example of file htdocs/core/menus/standard/mymenu.php

    function showmenu()
    {
        global $user,$conf,$langs,$dolibarr_main_db_name;
        $newmenu = new Menu();
        
	// Put here left menu entries
	// ***** START *****

	$langs->load("admin");	// Load translation file admin.lang
	$newmenu->add(DOL_URL_ROOT."/admin/index.php?leftmenu=setup", $langs->trans("Setup"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/company.php", $langs->trans("MenuCompanySetup"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/modules.php", $langs->trans("Modules"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/ihm.php", $langs->trans("GUISetup"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/boxes.php", $langs->trans("Boxes"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/delais.php",$langs->trans("Alerts"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/triggers.php", $langs->trans("Triggers"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/perms.php", $langs->trans("Security"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/dict.php", $langs->trans("DictionnarySetup"));
	$newmenu->add_submenu(DOL_URL_ROOT."/admin/const.php", $langs->trans("OtherSetup"));
		
	// ***** END *****
		
        // do not change code after this

        // override menu_array by value array in $newmenu
	$this->menu_array=$newmenu->liste;

        $alt=0;
        for ($i = 0 ; $i < sizeof($this->menu_array) ; $i++) 
        {
            $alt++;
            if ($this->menu_array[$i]['level']==0) {
                if (($alt%2==0))
                {
                    print '<div class="blockvmenuimpair">'."\n";
                }
                else
                {
                    print '<div class="blockvmenupair">'."\n";
                }
            }

            if ($this->menu_array[$i]['level']==0) {
                if ($this->menu_array[$i]['enabled'])
                    print '<a class="vmenu" href="'.$this->menu_array[$i]['url'].'">'.$this->menu_array[$i]['titre'].'</a><br>';
                else 
                    print '<font class="vmenudisabled">'.$this->menu_array[$i]['titre'].'</font><br>';
            }
            if ($this->menu_array[$i]['level']==1) {
                if ($this->menu_array[$i]['enabled'])
                    print '<a class="vsmenu" href="'.$this->menu_array[$i]['url'].'">'.$this->menu_array[$i]['titre'].'</a><br>';
                else 
                    print '<font class="vsmenudisabled">'.$this->menu_array[$i]['titre'].'</font><br>';
            }
            if ($this->menu_array[$i]['level']==2) {
                if ($this->menu_array[$i]['enabled'])
                    print '&nbsp; &nbsp; <a class="vsmenu" href="'.$this->menu_array[$i]['url'].'">'.$this->menu_array[$i]['titre'].'</a><br>';
                else 
                    print '&nbsp; &nbsp; <font class="vsmenudisabled">'.$this->menu_array[$i]['titre'].'</font><br>';
            }
            if ($this->menu_array[$i]['level']==3) {
                if ($this->menu_array[$i]['enabled'])
                    print '&nbsp; &nbsp; &nbsp; &nbsp; <a class="vsmenu" href="'.$this->menu_array[$i]['url'].'">'.$this->menu_array[$i]['titre'].'</a><br>';
                else 
                    print '&nbsp; &nbsp; &nbsp; &nbsp; <font class="vsmenudisabled">'.$this->menu_array[$i]['titre'].'</font><br>';
            }
            
            if ($i == (sizeof($this->menu_array)-1) || $this->menu_array[$i+1]['level']==0)  {
                print "</div>\n";
            }
        }

    }

To force usage of your menu manager

You can force usage of your own menu manager by creating a module. For this , all you have to do is to declare the 4 following constants into the array $this->const of your module descriptor file (See page Module development).

* 1=>array('MAIN_MENU_STANDARD_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 2=>array('MAIN_MENUFRONT_STANDARD_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 3=>array('MAIN_MENU_SMARTPHONE_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),
* 4=>array('MAIN_MENUFRONT_SMARTPHONE_FORCED','chaine','mymodule.php','Force menu handler to this value',1,'current',1),

Your menu manager will be used once the module is activated.