Test Failed
Push — main ( 29a1e7...86a642 )
by Rafael
61:24
created

MenuManager::loadMenu()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
nc 192
nop 2
dl 0
loc 50
rs 6.9
c 1
b 0
f 0

How to fix   Complexity   

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:

1
<?php
2
3
/* Copyright (C) 2007      Patrick Raguin       <[email protected]>
4
 * Copyright (C) 2009      Regis Houssin        <[email protected]>
5
 * Copyright (C) 2008-2013 Laurent Destailleur  <[email protected]>
6
 * Copyright (C) 2024		MDW						<[email protected]>
7
 * Copyright (C) 2024       Rafael San José         <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 *  \file       htdocs/core/menus/standard/auguria_menu.php
25
 *  \brief      Menu auguria manager
26
 */
27
28
29
/**
30
 *  Class to manage menu Auguria
31
 */
32
class MenuManager
33
{
34
    /**
35
     * @var DoliDB Database handler.
36
     */
37
    public $db;
38
39
    public $type_user; // Put 0 for internal users, 1 for external users
40
    public $atarget = ""; // To store default target to use onto links
41
    public $name = "auguria";
42
43
    /**
44
     * @var Menu
45
     */
46
    public $menu;
47
48
    public $menu_array;
49
    public $menu_array_after;
50
51
    public $tabMenu;
52
53
54
    /**
55
     *  Constructor
56
     *
57
     * @param DoliDB $db        Database handler
58
     * @param int    $type_user Type of user
59
     */
60
    public function __construct($db, $type_user)
61
    {
62
        $this->type_user = $type_user;
63
        $this->db = $db;
64
    }
65
66
67
    /**
68
     * Load this->tabMenu
69
     *
70
     * @param string $forcemainmenu To force mainmenu to load
71
     * @param string $forceleftmenu To force leftmenu to load
72
     *
73
     * @return  void
74
     */
75
    public function loadMenu($forcemainmenu = '', $forceleftmenu = '')
76
    {
77
        global $conf, $user, $langs;
78
79
        // We save into session the main menu selected
80
        if (GETPOSTISSET("mainmenu")) {
81
            $_SESSION["mainmenu"] = GETPOST("mainmenu", 'aZ09');
82
        }
83
        if (GETPOSTISSET("idmenu")) {
84
            $_SESSION["idmenu"] = GETPOSTINT("idmenu");
85
        }
86
87
        // Read now mainmenu and leftmenu that define which menu to show
88
        if (GETPOSTISSET("mainmenu")) {
89
            // On sauve en session le menu principal choisi
90
            $mainmenu = GETPOST("mainmenu", 'aZ09');
91
            $_SESSION["mainmenu"] = $mainmenu;
92
            $_SESSION["leftmenuopened"] = "";
93
        } else {
94
            // On va le chercher en session si non defini par le lien
95
            $mainmenu = isset($_SESSION["mainmenu"]) ? $_SESSION["mainmenu"] : '';
96
        }
97
        if (!empty($forcemainmenu)) {
98
            $mainmenu = $forcemainmenu;
99
        }
100
101
        if (GETPOSTISSET("leftmenu")) {
102
            // On sauve en session le menu principal choisi
103
            $leftmenu = GETPOST("leftmenu", 'aZ09');
104
            $_SESSION["leftmenu"] = $leftmenu;
105
106
            if ($_SESSION["leftmenuopened"] == $leftmenu) { // To collapse
107
                //$leftmenu="";
108
                $_SESSION["leftmenuopened"] = "";
109
            } else {
110
                $_SESSION["leftmenuopened"] = $leftmenu;
111
            }
112
        } else {
113
            // On va le chercher en session si non defini par le lien
114
            $leftmenu = isset($_SESSION["leftmenu"]) ? $_SESSION["leftmenu"] : '';
115
        }
116
        if (!empty($forceleftmenu)) {
117
            $leftmenu = $forceleftmenu;
118
        }
119
120
        require_once DOL_DOCUMENT_ROOT . '/core/class/menubase.class.php';
121
        $tabMenu = [];
122
        $menuArbo = new Menubase($this->db, 'auguria');
123
        $menuArbo->menuLoad($mainmenu, $leftmenu, $this->type_user, 'auguria', $tabMenu);
124
        $this->tabMenu = $tabMenu;
125
        //var_dump($tabMenu);
126
127
        //if ($forcemainmenu == 'all') { var_dump($this->tabMenu); exit; }
128
    }
129
130
131
    /**
132
     *  Show menu.
133
     *  Menu defined in sql tables were stored into $this->tabMenu BEFORE this is called.
134
     *
135
     * @param string $mode     'top', 'topnb', 'left', 'jmobile' (used to get full xml ul/li menu)
136
     * @param array  $moredata An array with more data to output
137
     *
138
     * @return int                     0 or nb of top menu entries if $mode = 'topnb'
139
     */
140
    public function showmenu($mode, $moredata = null)
141
    {
142
        global $conf, $langs, $user;
143
144
        require_once DOL_DOCUMENT_ROOT . '/core/menus/standard/auguria.lib.php';
145
146
        if ($this->type_user == 1) {
147
            $conf->global->MAIN_SEARCHFORM_SOCIETE_DISABLED = 1;
148
            $conf->global->MAIN_SEARCHFORM_CONTACT_DISABLED = 1;
149
        }
150
151
        require_once DOL_DOCUMENT_ROOT . '/core/class/menu.class.php';
152
        $this->menu = new Menu();
153
154
        if (!getDolGlobalString('MAIN_MENU_INVERT')) {
155
            if ($mode == 'top') {
156
                print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
157
            }
158
            if ($mode == 'left') {
159
                print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata);
0 ignored issues
show
Bug introduced by
The property menu_array does not exist on MenuManager. Did you mean menu_array_after?
Loading history...
160
            }
161
        } else {
162
            $conf->global->MAIN_SHOW_LOGO = 0;
163
            if ($mode == 'top') {
164
                print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0);
165
            }
166
            if ($mode == 'left') {
167
                print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
168
            }
169
        }
170
171
        if ($mode == 'topnb') {
172
            print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);
173
            return $this->menu->getNbOfVisibleMenuEntries();
174
        }
175
176
        if ($mode == 'jmobile') {     // Used to get menu in xml ul/li
177
            print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);
178
179
            // $this->menu->liste is top menu
180
            //var_dump($this->menu->liste);exit;
181
            $lastlevel = [];
182
            $showmenu = true;  // Is current menu shown - define here to keep static code checker happy
183
            print '<!-- Generate menu list from menu handler ' . $this->name . ' -->' . "\n";
184
            foreach ($this->menu->liste as $key => $val) {      // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
185
                print '<ul class="ulmenu" data-inset="true">';
186
                print '<li class="lilevel0">';
187
                if ($val['enabled'] == 1) {
188
                    $substitarray = ['__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user];
189
                    $substitarray['__USERID__'] = $user->id; // For backward compatibility
190
                    $val['url'] = make_substitutions($val['url'], $substitarray);
191
192
                    $relurl = dol_buildpath($val['url'], 1);
193
                    $canonurl = preg_replace('/\?.*$/', '', $val['url']);
194
195
                    print '<a class="alilevel0" href="#">';
196
197
                    // Add font-awesome
198
                    if ($val['level'] == 0 && !empty($val['prefix'])) {
199
                        print str_replace('<span class="', '<span class="paddingright pictofixedwidth ', $val['prefix']);
200
                    }
201
202
                    print $val['titre'];
203
                    print '</a>' . "\n";
204
205
                    // Search submenu for this mainmenu entry
206
                    $tmpmainmenu = $val['mainmenu'];
207
                    $tmpleftmenu = 'all';
208
                    $submenu = new Menu();
209
                    print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $submenu, 1, $tmpmainmenu, $tmpleftmenu);
210
                    if (!empty($submenu->liste[0]['url'])) {
211
                        $nexturl = dol_buildpath($submenu->liste[0]['url'], 1);
212
                    } else {
213
                        $nexturl = '';
214
                    }
215
216
                    $canonrelurl = preg_replace('/\?.*$/', '', $relurl);
217
                    $canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
218
                    //var_dump($canonrelurl);
219
                    //var_dump($canonnexturl);
220
                    print '<ul>' . "\n";
221
                    if (
222
                        ($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], ['tools']))
223
                        || (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false)
224
                    ) {
225
                        // We add sub entry
226
                        print str_pad('', 1) . '<li class="lilevel1 ui-btn-icon-right ui-btn">'; // ui-btn to highlight on clic
227
                        print '<a href="' . $relurl . '">';
228
229
                        if ($val['level'] == 0) {
230
                            print '<span class="fas fa-home fa-fw paddingright pictofixedwidth" aria-hidden="true"></span>';
231
                        }
232
233
                        if ($langs->trans(ucfirst($val['mainmenu']) . "Dashboard") == ucfirst($val['mainmenu']) . "Dashboard") {  // No translation
234
                            if (in_array($val['mainmenu'], ['cashdesk', 'externalsite', 'website', 'collab', 'takepos'])) {
235
                                print $langs->trans("Access");
236
                            } else {
237
                                print $langs->trans("Dashboard");
238
                            }
239
                        } else {
240
                            print $langs->trans(ucfirst($val['mainmenu']) . "Dashboard");
241
                        }
242
                        print '</a>';
243
                        print '</li>' . "\n";
244
                    }
245
246
                    if ($val['level'] == 0) {
247
                        if ($val['enabled']) {
248
                            $lastlevel[0] = 'enabled';
249
                        } elseif ($showmenu) {                 // Not enabled but visible (so greyed)
250
                            $lastlevel[0] = 'greyed';
251
                        } else {
252
                            $lastlevel[0] = 'hidden';
253
                        }
254
                    }
255
256
                    $lastlevel2 = [];
257
                    foreach ($submenu->liste as $key2 => $val2) {       // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
258
                        $showmenu = true;
259
                        if (getDolGlobalString('MAIN_MENU_HIDE_UNAUTHORIZED') && empty($val2['enabled'])) {
260
                            $showmenu = false;
261
                        }
262
263
                        // If at least one parent is not enabled, we do not show any menu of all children
264
                        if ($val2['level'] > 0) {
265
                            $levelcursor = $val2['level'] - 1;
266
                            while ($levelcursor >= 0) {
267
                                if ($lastlevel2[$levelcursor] != 'enabled') {
268
                                    $showmenu = false;
269
                                }
270
                                $levelcursor--;
271
                            }
272
                        }
273
274
                        if ($showmenu) {        // Visible (option to hide when not allowed is off or allowed)
275
                            $substitarray = ['__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user];
276
                            $substitarray['__USERID__'] = $user->id; // For backward compatibility
277
                            $val2['url'] = make_substitutions($val2['url'], $substitarray); // Make also substitution of __(XXX)__ and __[XXX]__
278
279
                            if (!preg_match("/^(http:\/\/|https:\/\/)/i", $val2['url'])) {
280
                                $relurl2 = dol_buildpath($val2['url'], 1);
281
                            } else {
282
                                $relurl2 = $val2['url'];
283
                            }
284
                            $canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
285
                            //var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
286
                            if (in_array($canonurl2, ['/admin/index.php', '/admin/tools/index.php', '/core/tools.php'])) {
287
                                $relurl2 = '';
288
                            }
289
290
                            $disabled = '';
291
                            if (!$val2['enabled']) {
292
                                $disabled = " vsmenudisabled";
293
                            }
294
295
                            print str_pad('', $val2['level'] + 1);
296
                            print '<li class="lilevel' . ($val2['level'] + 1);
297
                            if ($val2['level'] == 0) {
298
                                print ' ui-btn-icon-right ui-btn'; // ui-btn to highlight on clic
299
                            }
300
                            print $disabled . '">'; // ui-btn to highlight on clic
301
                            if ($relurl2) {
302
                                if ($val2['enabled']) { // Allowed
303
                                    print '<a href="' . $relurl2 . '"';
304
                                    //print ' data-ajax="false"';
305
                                    print '>';
306
                                    $lastlevel2[$val2['level']] = 'enabled';
307
                                } else { // Not allowed but visible (greyed)
308
                                    print '<a href="#" class="vsmenudisabled">';
309
                                    $lastlevel2[$val2['level']] = 'greyed';
310
                                }
311
                            } else {
312
                                if ($val2['enabled']) { // Allowed
313
                                    $lastlevel2[$val2['level']] = 'enabled';
314
                                } else {
315
                                    $lastlevel2[$val2['level']] = 'greyed';
316
                                }
317
                            }
318
319
                            if ($val2['level'] == 0 && !empty($val2['prefix'])) {
320
                                print $val2['prefix'];
321
                            } else {
322
                                print '<i class="fa fa-does-not-exists fa-fw paddingright pictofixedwidth"></i>';
323
                            }
324
325
                            print $val2['titre'];
326
                            if ($relurl2) {
327
                                print '</a>';
328
                            }
329
                            print '</li>' . "\n";
330
                        }
331
                    }
332
                    //var_dump($submenu);
333
                    print '</ul>';
334
                }
335
                if ($val['enabled'] == 2) {
336
                    print '<span class="spanlilevel0 vsmenudisabled">';
337
338
                    // Add font-awesome
339
                    if ($val['level'] == 0 && !empty($val['prefix'])) {
340
                        print $val['prefix'];
341
                    }
342
343
                    print $val['titre'];
344
                    print '</span>';
345
                }
346
                print '</li>';
347
                print '</ul>' . "\n";
348
            }
349
        }
350
351
        unset($this->menu);
352
353
        //print 'xx'.$mode;
354
        return 0;
355
    }
356
}
357