print_left_auguria_menu()   F
last analyzed

Complexity

Conditions 105
Paths > 20000

Size

Total Lines 362
Code Lines 222

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 105
eloc 222
nc 102400
nop 10
dl 0
loc 362
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* Copyright (C) 2010-2022  Laurent Destailleur <[email protected]>
4
 * Copyright (C) 2010-2012	Regis Houssin		<[email protected]>
5
 * Copyright (C) 2024		MDW							<[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 * or see https://www.gnu.org/
21
 */
22
23
use Dolibarr\Lib\ViewMain;
24
25
/**
26
 *  \file       htdocs/core/menus/standard/auguria.lib.php
27
 *  \brief      Library for file auguria menus
28
 */
29
30
31
/**
32
 * Core function to output top menu auguria
33
 *
34
 * @param DoliDB $db Database handler
35
 * @param string $atarget Target (Example: '' or '_top')
36
 * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
37
 * @param array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,level:int,prefix:string}> $tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{rowid:string...vel:int,prefix:string}> at position 18 could not be parsed: Expected '}' at position 18, but found 'int'.
Loading history...
38
 * @param Menu $menu Object Menu to return back list of menu entries
39
 * @param int $noout 1=Disable output (Initialise &$menu only).
40
 * @param string $mode 'top', 'topnb', 'left', 'jmobile'
41
 * @return  int                     0
42
 */
43
function print_auguria_menu($db, $atarget, $type_user, &$tabMenu, &$menu, $noout = 0, $mode = '')
44
{
45
    global $user, $conf, $langs, $mysoc;
46
    global $dolibarr_main_db_name;
47
48
    $mainmenu = (empty($_SESSION["mainmenu"]) ? '' : $_SESSION["mainmenu"]);
49
    $leftmenu = (empty($_SESSION["leftmenu"]) ? '' : $_SESSION["leftmenu"]);
50
51
    $id = 'mainmenu';
52
    $listofmodulesforexternal = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
53
54
    // Show personalized menus
55
    $menuArbo = new Menubase($db, 'auguria');
56
    $newTabMenu = $menuArbo->menuTopCharger('', '', $type_user, 'auguria', $tabMenu);
57
    '@phan-var-force array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,prefix:string}> $newTabMenu';
58
59
    $substitarray = getCommonSubstitutionArray($langs, 0, null, null);
60
61
    if (empty($noout)) {
62
        print_start_menu_array_auguria();
63
    }
64
65
    global $usemenuhider;
66
    $usemenuhider = 1;
67
68
    // Show/Hide vertical menu. The hamburger icon for .menuhider action.
69
    if ($mode != 'jmobile' && $mode != 'topnb' && $usemenuhider && !getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
70
        $showmode = 1;
71
        $classname = 'class="tmenu menuhider nohover"';
72
        $idsel = 'menu';
73
74
        $menu->add('#', (getDolGlobalInt('THEME_TOPMENU_DISABLE_IMAGE') == 1 ? '<span class="fa fa-bars"></span>' : ''), 0, $showmode, $atarget, "xxx", '', 0, $id, $idsel, $classname);
75
    }
76
77
    $num = count($newTabMenu);
78
    for ($i = 0; $i < $num; $i++) {
79
        //var_dump($type_user.' '.$newTabMenu[$i]['url'].' '.$showmode.' '.$newTabMenu[$i]['perms']);
80
        $idsel = (empty($newTabMenu[$i]['mainmenu']) ? 'none' : $newTabMenu[$i]['mainmenu']);
81
82
        $shorturl = '';
83
84
        $showmode = dol_auguria_showmenu($type_user, $newTabMenu[$i], $listofmodulesforexternal);
85
        if ($showmode == 1) {
86
            $newTabMenu[$i]['url'] = make_substitutions($newTabMenu[$i]['url'], $substitarray);
87
88
            // url = url from host, shorturl = relative path into dolibarr sources
89
            $url = $shorturl = $newTabMenu[$i]['url'];
90
            if (!preg_match("/^(http:\/\/|https:\/\/)/i", $newTabMenu[$i]['url'])) {    // Do not change url content for external links
91
                $tmp = explode('?', $newTabMenu[$i]['url'], 2);
92
                $url = $shorturl = $tmp[0];
93
                $param = (isset($tmp[1]) ? $tmp[1] : '');
94
95
                // Complete param to force leftmenu to '' to close open menu when we click on a link with no leftmenu defined.
96
                if ((!preg_match('/mainmenu/i', $param)) && (!preg_match('/leftmenu/i', $param)) && !empty($newTabMenu[$i]['url'])) {
97
                    // @phan-suppress-next-line PhanTypeSuspiciousStringExpression
98
                    $param .= ($param ? '&' : '') . 'mainmenu=' . $newTabMenu[$i]['mainmenu'] . '&leftmenu=';
99
                }
100
                if ((!preg_match('/mainmenu/i', $param)) && (!preg_match('/leftmenu/i', $param)) && empty($newTabMenu[$i]['url'])) {
101
                    $param .= ($param ? '&' : '') . 'leftmenu=';
102
                }
103
                //$url.="idmenu=".$newTabMenu[$i]['rowid'];    // Already done by menuLoad
104
                $url = dol_buildpath($url, 1) . ($param ? '?' . $param : '');
105
                //$shorturl = $shorturl.($param?'?'.$param:'');
106
                $shorturl = $url;
107
108
                if (DOL_URL_ROOT) {
109
                    $shorturl = preg_replace('/^' . preg_quote(DOL_URL_ROOT, '/') . '/', '', $shorturl);
110
                }
111
            }
112
113
            // TODO Find a generic solution
114
            if (preg_match('/search_project_user=__search_project_user__/', $shorturl)) {
115
                $search_project_user = GETPOSTINT('search_project_user');
116
                if ($search_project_user) {
117
                    $shorturl = preg_replace('/search_project_user=__search_project_user__/', 'search_project_user=' . $search_project_user, $shorturl);
118
                } else {
119
                    $shorturl = preg_replace('/search_project_user=__search_project_user__/', '', $shorturl);
120
                }
121
            }
122
123
            // Define the class (top menu selected or not)
124
            if (!empty($_SESSION['idmenu']) && $newTabMenu[$i]['rowid'] == $_SESSION['idmenu']) {
125
                $classname = 'class="tmenusel"';
126
            } elseif (!empty($_SESSION["mainmenu"]) && $newTabMenu[$i]['mainmenu'] == $_SESSION["mainmenu"]) {
127
                $classname = 'class="tmenusel"';
128
            } else {
129
                $classname = 'class="tmenu"';
130
            }
131
        } elseif ($showmode == 2) {
132
            $classname = 'class="tmenu"';
133
        } else {
134
            $classname = '';
135
        }
136
137
        $menu->add($shorturl, $newTabMenu[$i]['titre'], 0, $showmode, ($newTabMenu[$i]['target'] ? $newTabMenu[$i]['target'] : $atarget), ($newTabMenu[$i]['mainmenu'] ? $newTabMenu[$i]['mainmenu'] : $newTabMenu[$i]['rowid']), ($newTabMenu[$i]['leftmenu'] ? $newTabMenu[$i]['leftmenu'] : ''), $newTabMenu[$i]['position'], $id, $idsel, $classname, $newTabMenu[$i]['prefix']);
138
    }
139
140
    // Sort on position
141
    $menu->liste = dol_sort_array($menu->liste, 'position');
142
143
    // Output menu entries
144
    // Show logo company
145
    if (!getDolGlobalString('MAIN_MENU_INVERT') && empty($noout) && getDolGlobalString('MAIN_SHOW_LOGO') && !getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
146
        //$mysoc->logo_mini=(empty($conf->global->MAIN_INFO_SOCIETE_LOGO_MINI)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO_MINI);
147
        $mysoc->logo_squarred_mini = (!getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_SQUARRED_MINI') ? '' : $conf->global->MAIN_INFO_SOCIETE_LOGO_SQUARRED_MINI);
148
149
        $logoContainerAdditionalClass = 'backgroundforcompanylogo';
150
        if (getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_NO_BACKGROUND')) {
151
            $logoContainerAdditionalClass = '';
152
        }
153
154
        /**
155
         * TODO: See if the getUrl function can be used, or a new function.
156
         */
157
        if (!empty($mysoc->logo_squarred_mini) && is_readable($conf->mycompany->dir_output . '/logos/thumbs/' . $mysoc->logo_squarred_mini)) {
158
            $urllogo = constant('BASE_URL') . '/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file=' . urlencode('logos/thumbs/' . $mysoc->logo_squarred_mini);
159
            /*} elseif (!empty($mysoc->logo_mini) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_mini))
160
            {
161
            $urllogo=DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_mini);
162
            }*/
163
        } else {
164
            $urllogo = constant('DOL_URL_ROOT') . '/theme/dolibarr_512x512_white.png';
165
            $logoContainerAdditionalClass = '';
166
        }
167
168
        $title = $langs->trans("GoIntoSetupToChangeLogo");
169
170
        print "\n" . '<!-- Show logo on menu -->' . "\n";
171
        print_start_menu_entry_auguria('companylogo', 'class="tmenu tmenucompanylogo nohover"', 1);
172
173
        print '<div class="center ' . $logoContainerAdditionalClass . ' menulogocontainer"><img class="mycompany" title="' . dol_escape_htmltag($title) . '" alt="" src="' . $urllogo . '" style="max-width: 100px"></div>' . "\n";
174
175
        print_end_menu_entry_auguria(4);
176
    }
177
178
    if (empty($noout)) {
179
        foreach ($menu->liste as $menuval) {
180
            print_start_menu_entry_auguria($menuval['idsel'], $menuval['classname'], $menuval['enabled']);
181
            print_text_menu_entry_auguria($menuval['titre'], $menuval['enabled'], ($menuval['url'] != '#' ? DOL_URL_ROOT : '') . $menuval['url'], $menuval['id'], $menuval['idsel'], $menuval['classname'], ($menuval['target'] ? $menuval['target'] : $atarget), $menuval);
182
            print_end_menu_entry_auguria($menuval['enabled']);
183
        }
184
    }
185
186
    $showmode = 1;
187
    if (empty($noout) && !getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
188
        print_start_menu_entry_auguria('', 'class="tmenuend"', $showmode);
189
        print_end_menu_entry_auguria($showmode);
190
        print_end_menu_array_auguria();
191
    }
192
193
    return 0;
194
}
195
196
197
/**
198
 * Output start menu array
199
 *
200
 * @return  void
201
 */
202
function print_start_menu_array_auguria()
203
{
204
    print '<div class="tmenudiv">';
205
    print '<ul role="navigation" class="tmenu"' . (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? ' alt="Top menu"' : '') . '>';
206
}
207
208
/**
209
 * Output start menu entry
210
 *
211
 * @param string $idsel Text
212
 * @param string $classname String to add a css class
213
 * @param int $showmode 0 = hide, 1 = allowed or 2 = not allowed
214
 * @return  void
215
 */
216
function print_start_menu_entry_auguria($idsel, $classname, $showmode)
217
{
218
    if ($showmode) {
219
        print '<li ' . $classname . ' id="mainmenutd_' . $idsel . '">';
220
        //print '<div class="tmenuleft tmenusep"></div>';
221
        print '<div class="tmenucenter">';
222
    }
223
}
224
225
/**
226
 * Output menu entry
227
 *
228
 * @param string $text Text
229
 * @param int $showmode 0 = hide, 1 = allowed or 2 = not allowed
230
 * @param string $url Url
231
 * @param string $id Id
232
 * @param string $idsel Id sel
233
 * @param string $classname Class name
234
 * @param string $atarget Target
235
 * @param array $menuval All the $menuval array
236
 * @return  void
237
 */
238
function print_text_menu_entry_auguria($text, $showmode, $url, $id, $idsel, $classname, $atarget, $menuval = array())
239
{
240
    global $langs, $conf;
241
242
    $classnameimg = str_replace('class="', 'class="tmenuimage ', $classname);
243
    $classnametxt = str_replace('class="', 'class="tmenulabel ', $classname);
244
245
    if ($showmode == 1) {
246
        print '<a ' . $classnameimg . ' tabindex="-1" href="' . $url . '"' . ($atarget ? ' target="' . $atarget . '"' : '') . ' title="' . dol_escape_htmltag($text) . '">';
247
        print '<div class="' . $id . ' ' . $idsel . ' topmenuimage">';
248
249
        if (!empty($menuval['prefix']) && strpos($menuval['prefix'], '<span') === 0) {
250
            print $menuval['prefix'];
251
        } elseif (!empty($menuval['prefix']) && strpos($menuval['prefix'], 'fa-') === 0) {
252
            print '<span class="' . $id . ' ' . $menuval['prefix'] . '" id="mainmenuspan_' . $idsel . '"></span>';
253
        } else {
254
            print '<span class="' . $id . ' tmenuimageforpng" id="mainmenuspan_' . $idsel . '"></span>';
255
        }
256
        print '</div>';
257
        print '</a>';
258
        if (!getDolGlobalString('THEME_TOPMENU_DISABLE_TEXT')) {
259
            print '<a ' . $classnametxt . ' id="mainmenua_' . $idsel . '" href="' . $url . '"' . ($atarget ? ' target="' . $atarget . '"' : '') . ' title="' . dol_escape_htmltag($text) . '">';
260
            print '<span class="mainmenuaspan">';
261
            print $text;
262
            print '</span>';
263
            print '</a>';
264
        }
265
    } elseif ($showmode == 2) {
266
        print '<div ' . $classnameimg . ' title="' . dol_escape_htmltag($text . ' - ' . $langs->trans("NotAllowed")) . '">';
267
        print '<div class="' . $id . ' ' . $idsel . ' topmenuimage tmenudisabled">';
268
        if (!empty($menuval['prefix']) && strpos($menuval['prefix'], '<span') === 0) {
269
            print $menuval['prefix'];
270
        } else {
271
            print '<span class="' . $id . ' tmenuimageforpng tmenudisabled" id="mainmenuspan_' . $idsel . '"></span>';
272
        }
273
        print '</div>';
274
        print '</div>';
275
        if (!getDolGlobalString('THEME_TOPMENU_DISABLE_TEXT')) {
276
            print '<span ' . $classnametxt . ' id="mainmenua_' . $idsel . '" href="#" title="' . dol_escape_htmltag($text . ' - ' . $langs->trans("NotAllowed")) . '">';
277
            print '<span class="mainmenuaspan">';
278
            print $text;
279
            print '</span>';
280
            print '</span>';
281
        }
282
    }
283
}
284
285
/**
286
 * Output end menu entry
287
 *
288
 * @param int $showmode 0 = hide, 1 = allowed or 2 = not allowed
289
 * @return  void
290
 */
291
function print_end_menu_entry_auguria($showmode)
292
{
293
    if ($showmode) {
294
        print '</div></li>';
295
    }
296
    print "\n";
297
}
298
299
/**
300
 * Output menu array
301
 *
302
 * @return  void
303
 */
304
function print_end_menu_array_auguria()
305
{
306
    print '</ul>';
307
    print '</div>';
308
    print "\n";
309
}
310
311
312
/**
313
 * Core function to output left menu auguria
314
 * Fill &$menu (example with $forcemainmenu='home' $forceleftmenu='all', return left menu tree of Home)
315
 *
316
 * @param DoliDB $db Database handler
317
 * @param array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,level:int,prefix:string}> $menu_array_before Table of menu entries to show before entries of menu handler (menu->liste filled with menu->add)
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{rowid:string...vel:int,prefix:string}> at position 18 could not be parsed: Expected '}' at position 18, but found 'int'.
Loading history...
318
 * @param array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,level:int,prefix:string}> $menu_array_after Table of menu entries to show after entries of menu handler (menu->liste filled with menu->add)
319
 * @param array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,level:int,prefix:string}> $tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
320
 * @param Menu $menu Object Menu to return back list of menu entries
321
 * @param int $noout Disable output (Initialise &$menu only).
322
 * @param string $forcemainmenu 'x'=Force mainmenu to mainmenu='x'
323
 * @param string $forceleftmenu 'all'=Force leftmenu to '' (= all). If value come being '', we change it to value in session and 'none' if not defined in session.
324
 * @param array $moredata An array with more data to output
325
 * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
326
 * @return  int                             Nb of menu entries
327
 */
328
function print_left_auguria_menu($db, $menu_array_before, $menu_array_after, &$tabMenu, &$menu, $noout = 0, $forcemainmenu = '', $forceleftmenu = '', $moredata = null, $type_user = 0)
329
{
330
    global $user, $conf, $langs, $hookmanager;
331
    global $dolibarr_main_db_name, $mysoc;
332
333
    $newmenu = $menu;
334
335
    $mainmenu = ($forcemainmenu ? $forcemainmenu : $_SESSION["mainmenu"]);
336
    $leftmenu = ($forceleftmenu ? '' : (empty($_SESSION["leftmenu"]) ? 'none' : $_SESSION["leftmenu"]));
337
338
    global $usemenuhider;
339
    $usemenuhider = 0;
340
341
    if (is_array($moredata) && !empty($moredata['searchform'])) {   // searchform can contains select2 code or link to show old search form or link to switch on search page
342
        print "\n";
343
        print "<!-- Begin SearchForm -->\n";
344
        print '<div id="blockvmenusearch" class="blockvmenusearch">' . "\n";
345
        print $moredata['searchform'];
346
        print '</div>' . "\n";
347
        print "<!-- End SearchForm -->\n";
348
    }
349
350
    if (is_array($moredata) && !empty($moredata['bookmarks'])) {
351
        print "\n";
352
        print "<!-- Begin Bookmarks -->\n";
353
        print '<div id="blockvmenubookmarks" class="blockvmenubookmarks">' . "\n";
354
        print $moredata['bookmarks'];
355
        print '</div>' . "\n";
356
        print "<!-- End Bookmarks -->\n";
357
    }
358
359
    $substitarray = getCommonSubstitutionArray($langs, 0, null, null);
360
361
    // We update newmenu with entries found into database
362
    $menuArbo = new Menubase($db, 'auguria');
363
    $newmenu = $menuArbo->menuLeftCharger($newmenu, $mainmenu, $leftmenu, ($user->socid ? 1 : 0), 'auguria', $tabMenu);
364
365
    // We update newmenu for special dynamic menus
366
    if (isModEnabled('bank') && $user->hasRight('banque', 'lire') && $mainmenu == 'bank') { // Entry for each bank account
367
        include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php'; // Required for to get Account::TYPE_CASH for example
368
369
        $sql = "SELECT rowid, label, courant, rappro, courant";
370
        $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
371
        $sql .= " WHERE entity = " . $conf->entity;
372
        $sql .= " AND clos = 0";
373
        $sql .= " ORDER BY label";
374
375
        $resql = $db->query($sql);
376
        if ($resql) {
377
            $numr = $db->num_rows($resql);
378
            $i = 0;
379
380
            if ($numr > 0) {
381
                $newmenu->add('/compta/bank/list.php?search_status=opened', $langs->trans("BankAccounts"), 0, $user->hasRight('banque', 'lire'));
382
            }
383
384
            while ($i < $numr) {
385
                $objp = $db->fetch_object($resql);
386
                $newmenu->add('/compta/bank/card.php?id=' . $objp->rowid, $objp->label, 1, $user->hasRight('banque', 'lire'));
387
                if ($objp->rappro && $objp->courant != Account::TYPE_CASH && empty($objp->clos)) {  // If not cash account and not closed and can be reconciliate
388
                    $newmenu->add('/compta/bank/bankentries_list.php?id=' . $objp->rowid, $langs->trans("Conciliate"), 2, $user->hasRight('banque', 'consolidate'));
389
                }
390
                $i++;
391
            }
392
        } else {
393
            dol_print_error($db);
394
        }
395
        $db->free($resql);
396
    }
397
398
    if (isModEnabled('accounting') && $user->hasRight('accounting', 'comptarapport', 'lire') && $mainmenu == 'accountancy') {   // Entry in accountancy journal for each bank account
399
        $newmenu->add('', $langs->trans("RegistrationInAccounting"), 1, $user->hasRight('accounting', 'comptarapport', 'lire'), '', 'accountancy', 'accountancy_journal', 10);
400
401
        // Multi journal
402
        $sql = "SELECT rowid, code, label, nature";
403
        $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_journal";
404
        $sql .= " WHERE entity = " . $conf->entity;
405
        $sql .= " AND active = 1";
406
        $sql .= " ORDER BY label DESC";
407
408
        $resql = $db->query($sql);
409
        if ($resql) {
410
            $numr = $db->num_rows($resql);
411
            $i = 0;
412
413
            if ($numr > 0) {
414
                while ($i < $numr) {
415
                    $objp = $db->fetch_object($resql);
416
417
                    $nature = '';
418
419
                    // Must match array $sourceList defined into journals_list.php
420
                    if ($objp->nature == 2 && isModEnabled('invoice') && !getDolGlobalString('ACCOUNTING_DISABLE_BINDING_ON_SALES')) {
421
                        $nature = "sells";
422
                    }
423
                    if (
424
                        $objp->nature == 3
425
                        && isModEnabled('supplier_invoice')
426
                        && !getDolGlobalString('ACCOUNTING_DISABLE_BINDING_ON_PURCHASES')
427
                    ) {
428
                        $nature = "purchases";
429
                    }
430
                    if ($objp->nature == 4 && isModEnabled('bank')) {
431
                        $nature = "bank";
432
                    }
433
                    if ($objp->nature == 5 && isModEnabled('expensereport') && !getDolGlobalString('ACCOUNTING_DISABLE_BINDING_ON_EXPENSEREPORTS')) {
434
                        $nature = "expensereports";
435
                    }
436
                    if ($objp->nature == 1) {
437
                        $nature = "various";
438
                    }
439
                    if ($objp->nature == 8) {
440
                        $nature = "inventory";
441
                    }
442
                    if ($objp->nature == 9) {
443
                        $nature = "hasnew";
444
                    }
445
446
                    // To enable when page exists
447
                    if (!getDolGlobalString('ACCOUNTANCY_SHOW_DEVELOP_JOURNAL')) {
448
                        if ($nature == 'hasnew' || $nature == 'inventory') {
449
                            $nature = '';
450
                        }
451
                    }
452
453
                    if ($nature) {
454
                        $langs->load('accountancy');
455
                        $journallabel = $langs->transnoentities($objp->label); // Labels in this table are set by loading llx_accounting_abc.sql. Label can be 'ACCOUNTING_SELL_JOURNAL', 'InventoryJournal', ...
456
                        $newmenu->add('/accountancy/journal/' . $nature . 'journal.php?mainmenu=accountancy&leftmenu=accountancy_journal&id_journal=' . $objp->rowid, $journallabel, 2, $user->hasRight('accounting', 'comptarapport', 'lire'));
457
                    }
458
                    $i++;
459
                }
460
            } else {
461
                // Should not happen. Entries are added
462
                $newmenu->add('', $langs->trans("NoJournalDefined"), 2, $user->hasRight('accounting', 'comptarapport', 'lire'));
463
            }
464
        } else {
465
            dol_print_error($db);
466
        }
467
        $db->free($resql);
468
    }
469
470
    if (isModEnabled('ftp') && $mainmenu == 'ftp') {    // Entry for FTP
471
        $MAXFTP = 20;
472
        $i = 1;
473
        while ($i <= $MAXFTP) {
474
            $paramkey = 'FTP_NAME_' . $i;
475
            //print $paramkey;
476
            if (!empty($conf->global->$paramkey)) {
477
                $link = "/ftp/index.php?idmenu=" . $_SESSION["idmenu"] . "&numero_ftp=" . $i;
478
479
                $newmenu->add($link, dol_trunc($conf->global->$paramkey, 24));
480
            }
481
            $i++;
482
        }
483
    }
484
485
486
    // Build final $menu_array = $menu_array_before +$newmenu->liste + $menu_array_after
487
    //var_dump($menu_array_before);exit;
488
    //var_dump($menu_array_after);exit;
489
    $menu_array = $newmenu->liste;
490
    if (is_array($menu_array_before)) {
491
        $menu_array = array_merge($menu_array_before, $menu_array);
492
    }
493
    if (is_array($menu_array_after)) {
494
        $menu_array = array_merge($menu_array, $menu_array_after);
495
    }
496
    //var_dump($menu_array);exit;
497
    if (!is_array($menu_array)) {
498
        return 0;
499
    }
500
501
    // Allow the $menu_array of the menu to be manipulated by modules
502
    $parameters = array(
503
        'mainmenu' => $mainmenu,
504
    );
505
    $hook_items = $menu_array;
506
    $reshook = $hookmanager->executeHooks('menuLeftMenuItems', $parameters, $hook_items); // Note that $action and $object may have been modified by some hooks
507
508
    if (is_numeric($reshook)) {
509
        if ($reshook == 0 && !empty($hookmanager->results)) {
510
            $menu_array[] = $hookmanager->results; // add
511
        } elseif ($reshook == 1) {
512
            $menu_array = $hookmanager->results; // replace
513
        }
514
515
        // @todo Sort menu items by 'position' value
516
        //      $position = array();
517
        //      foreach ($menu_array as $key => $row) {
518
        //          $position[$key] = $row['position'];
519
        //      }
520
        //      $array1_sort_order = SORT_ASC;
521
        //      array_multisort($position, $array1_sort_order, $menu_array);
522
    }
523
524
    // Phan has a hard time tracking the type, for instance because it get hookmanager->results
525
    // Force the typing at this point to get useful analysis below:
526
    '@phan-var-force array<array{rowid:string,fk_menu:string,langs:string,enabled:int<0,2>,type:string,fk_mainmenu:string,fk_leftmenu:string,url:string,titre:string,perms:string,target:string,mainmenu:string,leftmenu:string,position:int,prefix:string,level:int}> $menu_array';
527
528
    // Show menu
529
    $invert = !getDolGlobalString('MAIN_MENU_INVERT') ? "" : "invert";
530
    if (empty($noout)) {
531
        $altok = 0;
532
        $blockvmenuopened = false;
533
        $lastlevel0 = '';
534
        $num = count($menu_array);
535
        foreach (array_keys($menu_array) as $i) {     // Loop on each menu entry (foreach better for static analysis)
536
            $showmenu = true;
537
            if (getDolGlobalString('MAIN_MENU_HIDE_UNAUTHORIZED') && empty($menu_array[$i]['enabled'])) {
538
                $showmenu = false;
539
            }
540
541
            // Begin of new left menu block
542
            if (empty($menu_array[$i]['level']) && $showmenu) {
543
                $altok++;
544
                $blockvmenuopened = true;
545
                $lastopened = true;
546
                for ($j = ($i + 1); $j < $num; $j++) {
547
                    if (empty($menu_array[$j]['level'])) {
548
                        $lastopened = false;
549
                    }
550
                }
551
                if ($altok % 2 == 0) {
552
                    print '<div class="blockvmenu blockvmenuimpair' . $invert . ($lastopened ? ' blockvmenulast' : '') . ($altok == 1 ? ' blockvmenufirst' : '') . '">' . "\n";
553
                } else {
554
                    print '<div class="blockvmenu blockvmenupair' . $invert . ($lastopened ? ' blockvmenulast' : '') . ($altok == 1 ? ' blockvmenufirst' : '') . '">' . "\n";
555
                }
556
            }
557
558
            // Add tabulation
559
            $tabstring = '';
560
            $tabul = ($menu_array[$i]['level'] - 1);
561
            if ($tabul > 0) {
562
                for ($j = 0; $j < $tabul; $j++) {
563
                    $tabstring .= '&nbsp;&nbsp;&nbsp;';
564
                }
565
            }
566
567
            // $menu_array[$i]['url'] can be a relative url, a full external url. We try substitution
568
569
            $menu_array[$i]['url'] = make_substitutions($menu_array[$i]['url'], $substitarray);
570
571
            $url = $shorturl = $shorturlwithoutparam = $menu_array[$i]['url'];
572
            if (!preg_match("/^(http:\/\/|https:\/\/)/i", $menu_array[$i]['url'])) {
573
                $tmp = explode('?', $menu_array[$i]['url'], 2);
574
                $url = $shorturl = $tmp[0];
575
                $param = (isset($tmp[1]) ? $tmp[1] : ''); // params in url of the menu link
576
577
                // Complete param to force leftmenu to '' to close open menu when we click on a link with no leftmenu defined.
578
                if ((!preg_match('/mainmenu/i', $param)) && (!preg_match('/leftmenu/i', $param)) && !empty($menu_array[$i]['mainmenu'])) {
579
                    $param .= ($param ? '&' : '') . 'mainmenu=' . $menu_array[$i]['mainmenu'] . '&leftmenu=';
580
                }
581
                if ((!preg_match('/mainmenu/i', $param)) && (!preg_match('/leftmenu/i', $param)) && empty($menu_array[$i]['mainmenu'])) {
582
                    $param .= ($param ? '&' : '') . 'leftmenu=';
583
                }
584
                //$url.="idmenu=".$menu_array[$i]['rowid'];    // Already done by menuLoad
585
                $url = dol_buildpath($url, 3) . ($param ? '?' . $param : '');
586
                $shorturlwithoutparam = $shorturl;
587
                $shorturl = $shorturl . ($param ? '?' . $param : '');
588
            }
589
590
            print '<!-- Process menu entry with mainmenu=' . $menu_array[$i]['mainmenu'] . ', leftmenu=' . $menu_array[$i]['leftmenu'] . ', level=' . $menu_array[$i]['level'] . ' enabled=' . $menu_array[$i]['enabled'] . ', position=' . $menu_array[$i]['position'] . ' prefix=' . $menu_array[$i]['prefix'] . ' -->' . "\n";
591
592
            // Menu level 0
593
            if ($menu_array[$i]['level'] == 0) {
594
                if ($menu_array[$i]['enabled']) {     // Enabled so visible
595
                    print '<div class="menu_titre">' . $tabstring;
596
                    if ($shorturlwithoutparam) {
597
                        print '<a class="vmenu" title="' . dol_escape_htmltag(dol_string_nohtmltag($menu_array[$i]['titre'])) . '" href="' . $url . '"' . ($menu_array[$i]['target'] ? ' target="' . $menu_array[$i]['target'] . '"' : '') . '>';
598
                    } else {
599
                        print '<span class="vmenu">';
600
                    }
601
                    if (!empty($menu_array[$i]['prefix'])) {
602
                        if (preg_match('/^fa\-[a-zA-Z0-9\-_]+$/', $menu_array[$i]['prefix'])) {
603
                            print '<span class="fas ' . $menu_array[$i]['prefix'] . ' paddingright pictofixedwidth"></span>';
604
                        } else {
605
                            print $menu_array[$i]['prefix'];
606
                        }
607
                    }
608
609
                    // print ($menu_array[$i]['prefix'] ? $menu_array[$i]['prefix'] : '');
610
                    print $menu_array[$i]['titre'];
611
                    if ($shorturlwithoutparam) {
612
                        print '</a>';
613
                    } else {
614
                        print '</span>';
615
                    }
616
                    print '</div>' . "\n";
617
                    $lastlevel0 = 'enabled';
618
                } elseif ($showmenu) {                 // Not enabled but visible (so greyed)
619
                    print '<div class="menu_titre">' . $tabstring;
620
                    print '<span class="vmenudisabled">';
621
                    if (!empty($menu_array[$i]['prefix'])) {
622
                        print $menu_array[$i]['prefix'];
623
                    }
624
                    print $menu_array[$i]['titre'];
625
                    print '</span>';
626
                    print '</div>' . "\n";
627
                    $lastlevel0 = 'greyed';
628
                } else {
629
                    $lastlevel0 = 'hidden';
630
                }
631
                if ($showmenu) {
632
                    print '<div class="menu_top"></div>' . "\n";
633
                }
634
            }
635
636
            // Menu level > 0
637
            if ($menu_array[$i]['level'] > 0) {
638
                $cssmenu = '';
639
                if ($menu_array[$i]['url']) {
640
                    $cssmenu = ' menu_contenu' . dol_string_nospecial(preg_replace('/\.php.*$/', '', $menu_array[$i]['url']));
641
                }
642
643
                if ($menu_array[$i]['enabled'] && $lastlevel0 == 'enabled') {
644
                    // Enabled so visible, except if parent was not enabled.
645
                    print '<div class="menu_contenu' . $cssmenu . '">';
646
                    print $tabstring;
647
                    if ($shorturlwithoutparam) {
648
                        print '<a class="vsmenu" title="' . dol_escape_htmltag(dol_string_nohtmltag($menu_array[$i]['titre'])) . '" href="' . $url . '"' . ($menu_array[$i]['target'] ? ' target="' . $menu_array[$i]['target'] . '"' : '') . '>';
649
                    } else {
650
                        print '<span class="vsmenu" title="' . dol_escape_htmltag($menu_array[$i]['titre']) . '">';
651
                    }
652
                    print $menu_array[$i]['titre'];
653
                    if ($shorturlwithoutparam) {
654
                        print '</a>';
655
                    } else {
656
                        print '</span>';
657
                    }
658
                    // If title is not pure text and contains a table, no carriage return added
659
                    if (!strstr($menu_array[$i]['titre'], '<table')) {
660
                        print '<br>';
661
                    }
662
                    print '</div>' . "\n";
663
                } elseif ($showmenu && $lastlevel0 == 'enabled') {
664
                    // Not enabled but visible (so greyed), except if parent was not enabled.
665
                    print '<div class="menu_contenu' . $cssmenu . '">';
666
                    print $tabstring;
667
                    print '<span class="spanlilevel0 vsmenudisabled vsmenudisabledmargin">' . $menu_array[$i]['titre'] . '</span><br>';
668
                    print '</div>' . "\n";
669
                }
670
            }
671
672
            // If next is a new block or if there is nothing after
673
            if (empty($menu_array[$i + 1]['level'])) {               // End menu block
674
                if ($showmenu) {
675
                    print '<div class="menu_end"></div>' . "\n";
676
                }
677
                if ($blockvmenuopened) {
678
                    print '</div>' . "\n";
679
                    $blockvmenuopened = false;
680
                }
681
            }
682
        }
683
684
        if ($altok) {
685
            print '<div class="blockvmenuend"></div>'; // End menu block
686
        }
687
    }
688
689
    return count($menu_array);
690
}
691
692
693
/**
694
 * Function to test if an entry is enabled or not
695
 *
696
 * @param string $type_user 0=We need backoffice menu, 1=We need frontoffice menu
697
 * @param array $menuentry Array for menu entry
698
 * @param array $listofmodulesforexternal Array with list of modules allowed to external users
699
 * @return  int                                     0=Hide, 1=Show, 2=Show gray
700
 */
701
function dol_auguria_showmenu($type_user, &$menuentry, &$listofmodulesforexternal)
702
{
703
    global $conf;
704
705
    //print 'type_user='.$type_user.' module='.$menuentry['module'].' enabled='.$menuentry['enabled'].' perms='.$menuentry['perms'];
706
    //print 'ok='.in_array($menuentry['module'], $listofmodulesforexternal);
707
    if (empty($menuentry['enabled'])) {
708
        return 0; // Entry disabled by condition
709
    }
710
    if ($type_user && $menuentry['module']) {
711
        $tmploops = explode('|', $menuentry['module']);
712
        $found = 0;
713
        foreach ($tmploops as $tmploop) {
714
            if (in_array($tmploop, $listofmodulesforexternal)) {
715
                $found++;
716
                break;
717
            }
718
        }
719
        if (!$found) {
720
            return 0; // Entry is for menus all excluded to external users
721
        }
722
    }
723
    if (!$menuentry['perms'] && $type_user) {
724
        return 0; // No permissions and user is external
725
    }
726
    if (!$menuentry['perms'] && getDolGlobalString('MAIN_MENU_HIDE_UNAUTHORIZED')) {
727
        return 0; // No permissions and option to hide when not allowed, even for internal user, is on
728
    }
729
    if (!$menuentry['perms']) {
730
        return 2; // No permissions and user is external
731
    }
732
    return 1;
733
}
734