Passed
Push — master ( 65bdac...4e88da )
by Alxarafe
32:38
created

Base/MenuManager.php (1 issue)

1
<?php
2
/* Copyright (C) 2005-2013 Laurent Destailleur  <[email protected]>
3
 * Copyright (C) 2007-2009 Regis Houssin        <[email protected]>
4
 * Copyright (C) 2019       Alxarafe            <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace Alixar\Base;
20
21
use Alixar\Base\Menubase;
22
23
/**
24
 * 	\file       htdocs/core/menus/standard/eldy_menu.php
25
 * 	\brief      Menu eldy manager
26
 */
27
28
/**
29
 * 	Class to manage menu Eldy
30
 */
31
class MenuManager
32
{
33
34
    /**
35
     * @var DoliDB Database handler.
36
     */
37
    // public $db;
38
    var $type_user;         // Put 0 for internal users, 1 for external users
39
    var $atarget = "";                                // To store default target to use onto links
40
    var $name = "eldy";
41
    var $menu_array;
42
    var $menu_array_after;
43
    var $tabMenu;
44
45
    /**
46
     *  Constructor
47
     *
48
     *  @param	DoliDB		$db     	Database handler
49
     *  @param	int			$type_user	Type of user
50
     */
51
    function __construct($type_user)
52
    {
53
        $this->type_user = $type_user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type_user of type integer is incompatible with the declared type Alixar\Base\DoliDB of property $type_user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        // $this->db = $db;
55
    }
56
57
    /**
58
     * Load this->tabMenu
59
     *
60
     * @param	string	$forcemainmenu		To force mainmenu to load
61
     * @param	string	$forceleftmenu		To force leftmenu to load
62
     * @return	void
63
     */
64
    function loadMenu($forcemainmenu = '', $forceleftmenu = '')
65
    {
66
        // On sauve en session le menu principal choisi
67
        if (isset($_GET["mainmenu"])) {
68
            $_SESSION["mainmenu"] = $_GET["mainmenu"];
69
        }
70
        if (isset($_GET["idmenu"])) {
71
            $_SESSION["idmenu"] = $_GET["idmenu"];
72
        }
73
74
        // Read mainmenu and leftmenu that define which menu to show
75
        if (isset($_GET["mainmenu"])) {
76
            // On sauve en session le menu principal choisi
77
            $mainmenu = $_GET["mainmenu"];
78
            $_SESSION["mainmenu"] = $mainmenu;
79
            $_SESSION["leftmenuopened"] = "";
80
        } else {
81
            // On va le chercher en session si non defini par le lien
82
            $mainmenu = isset($_SESSION["mainmenu"]) ? $_SESSION["mainmenu"] : '';
83
        }
84
        if (!empty($forcemainmenu)) {
85
            $mainmenu = $forcemainmenu;
86
        }
87
88
        if (isset($_GET["leftmenu"])) {
89
            // On sauve en session le menu principal choisi
90
            $leftmenu = $_GET["leftmenu"];
91
            $_SESSION["leftmenu"] = $leftmenu;
92
93
            if ($_SESSION["leftmenuopened"] == $leftmenu) { // To collapse
94
                //$leftmenu="";
95
                $_SESSION["leftmenuopened"] = "";
96
            } else {
97
                $_SESSION["leftmenuopened"] = $leftmenu;
98
            }
99
        } else {
100
            // On va le chercher en session si non defini par le lien
101
            $leftmenu = isset($_SESSION["leftmenu"]) ? $_SESSION["leftmenu"] : '';
102
        }
103
        if (!empty($forceleftmenu)) {
104
            $leftmenu = $forceleftmenu;
105
        }
106
107
        require_once DOL_DOCUMENT_ROOT . '/core/class/menubase.class.php';
108
        $tabMenu = array();
109
        $menuArbo = new Menubase('eldy');
110
        $menuArbo->menuLoad($mainmenu, $leftmenu, $this->type_user, 'eldy', $tabMenu);
111
        $this->tabMenu = $tabMenu;
112
        //var_dump($tabMenu);
113
        //if ($forcemainmenu == 'all') { var_dump($this->tabMenu); exit; }
114
    }
115
116
    /**
117
     *  Show menu.
118
     *  Module defined in sql tables were stored into $this->tabMenu BEFORE this is called.
119
     *
120
     * 	@param	string	$mode			'top', 'topnb', 'left', 'jmobile' (used to get full xml ul/li menu)
121
     *  @param	array	$moredata		An array with more data to output
122
     *  @return int                     0 or nb of top menu entries if $mode = 'topnb'
123
     */
124
    function showmenu($mode, $moredata = null)
125
    {
126
        global $conf, $langs, $user;
127
128
        //var_dump($this->tabMenu);
129
130
        require_once DOL_DOCUMENT_ROOT . '/core/menus/standard/eldy.lib.php';
131
132
        if ($this->type_user == 1) {
133
            $conf->global->MAIN_SEARCHFORM_SOCIETE_DISABLED = 1;
134
            $conf->global->MAIN_SEARCHFORM_CONTACT_DISABLED = 1;
135
        }
136
137
        require_once DOL_DOCUMENT_ROOT . '/core/class/menu.class.php';
138
        $this->menu = new Menu();
139
140
        if (empty($conf->global->MAIN_MENU_INVERT)) {
141
            if ($mode == 'top') {
142
                print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
143
            }
144
            if ($mode == 'left') {
145
                print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata);
146
            }
147
        } else {
148
            $conf->global->MAIN_SHOW_LOGO = 0;
149
            if ($mode == 'top') {
150
                print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0);
151
            }
152
            if ($mode == 'left') {
153
                print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
154
            }
155
        }
156
157
        if ($mode == 'topnb') {
158
            print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);  // no output
159
            return $this->menu->getNbOfVisibleMenuEntries();
160
        }
161
162
        if ($mode == 'jmobile') {     // Used to get menu in xml ul/li
163
            print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);      // Fill this->menu that is empty with top menu
164
            // $this->menu->liste is top menu
165
            //var_dump($this->menu->liste);exit;
166
            $lastlevel = array();
167
            print '<!-- Generate menu list from menu handler ' . $this->name . ' -->' . "\n";
168
            foreach ($this->menu->liste as $key => $val) {  // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
169
                print '<ul class="ulmenu" data-inset="true">';
170
                print '<li class="lilevel0">';
171
172
                if ($val['enabled'] == 1) {
173
                    $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
174
                    $substitarray['__USERID__'] = $user->id; // For backward compatibility
175
                    $val['url'] = make_substitutions($val['url'], $substitarray);
176
177
                    $relurl = dol_buildpath($val['url'], 1);
178
                    $canonurl = preg_replace('/\?.*$/', '', $val['url']);
179
180
                    print '<a class="alilevel0" href="#">';
181
182
                    // Add font-awesome
183
                    if ($val['level'] == 0 && $val['mainmenu'] == 'home') {
184
                        print '<span class="fa fa-home fa-fw paddingright" aria-hidden="true"></span>';
185
                    }
186
187
                    print $val['titre'];
188
                    print '</a>' . "\n";
189
190
                    // Search submenu fot this mainmenu entry
191
                    $tmpmainmenu = $val['mainmenu'];
192
                    $tmpleftmenu = 'all';
193
                    $submenu = new Menu();
194
                    print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $submenu, 1, $tmpmainmenu, $tmpleftmenu);       // Fill $submenu (example with tmpmainmenu='home' tmpleftmenu='all', return left menu tree of Home)
195
                    // Note: $submenu contains menu entry with substitution not yet done
196
                    //if ($tmpmainmenu.'-'.$tmpleftmenu == 'home-all') { var_dump($submenu); exit; }
197
                    //if ($tmpmainmenu=='accountancy') { var_dump($submenu->liste); exit; }
198
                    $nexturl = dol_buildpath($submenu->liste[0]['url'], 1);
199
200
                    $canonrelurl = preg_replace('/\?.*$/', '', $relurl);
201
                    $canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
202
                    //var_dump($canonrelurl);
203
                    //var_dump($canonnexturl);
204
                    print '<ul>' . "\n";
205
                    if (($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], array('tools'))) || (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false)) {
206
                        // We add sub entry
207
                        print str_pad('', 1) . '<li class="lilevel1 ui-btn-icon-right ui-btn">';  // ui-btn to highlight on clic
208
                        print '<a href="' . $relurl . '">';
209
                        if ($langs->trans(ucfirst($val['mainmenu']) . "Dashboard") == ucfirst($val['mainmenu']) . "Dashboard") {  // No translation
210
                            if (in_array($val['mainmenu'], array('cashdesk', 'externalsite', 'website', 'collab'))) {
211
                                print $langs->trans("Access");
212
                            } else {
213
                                print $langs->trans("Dashboard");
214
                            }
215
                        } else
216
                            print $langs->trans(ucfirst($val['mainmenu']) . "Dashboard");
217
                        print '</a>';
218
                        print '</li>' . "\n";
219
                    }
220
221
                    if ($val['level'] == 0) {
222
                        if ($val['enabled']) {
223
                            $lastlevel[0] = 'enabled';
224
                        } else if ($showmenu) {                 // Not enabled but visible (so greyed)
225
                            $lastlevel[0] = 'greyed';
226
                        } else {
227
                            $lastlevel[0] = 'hidden';
228
                        }
229
                    }
230
231
                    $lastlevel2 = array();
232
                    foreach ($submenu->liste as $key2 => $val2) {  // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
233
                        $showmenu = true;
234
                        if (!empty($conf->global->MAIN_MENU_HIDE_UNAUTHORIZED) && empty($val2['enabled'])) {
235
                            $showmenu = false;
236
                        }
237
238
                        // If at least one parent is not enabled, we do not show any menu of all children
239
                        if ($val2['level'] > 0) {
240
                            $levelcursor = $val2['level'] - 1;
241
                            while ($levelcursor >= 0) {
242
                                if ($lastlevel2[$levelcursor] != 'enabled') {
243
                                    $showmenu = false;
244
                                }
245
                                $levelcursor--;
246
                            }
247
                        }
248
249
                        if ($showmenu) {  // Visible (option to hide when not allowed is off or allowed)
250
                            $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
251
                            $substitarray['__USERID__'] = $user->id; // For backward compatibility
252
                            $val2['url'] = make_substitutions($val2['url'], $substitarray);  // Make also substitution of __(XXX)__ and __[XXX]__
253
254
                            if (!preg_match("/^(http:\/\/|https:\/\/)/i", $val2['url'])) {
255
                                $relurl2 = dol_buildpath($val2['url'], 1);
256
                            } else {
257
                                $relurl2 = $val2['url'];
258
                            }
259
                            $canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
260
                            //var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
261
                            if (in_array($canonurl2, array('/admin/index.php', '/admin/tools/index.php', '/core/tools.php'))) {
262
                                $relurl2 = '';
263
                            }
264
265
                            $disabled = '';
266
                            if (!$val2['enabled']) {
267
                                $disabled = " vsmenudisabled";
268
                            }
269
270
                            print str_pad('', $val2['level'] + 1);
271
                            print '<li class="lilevel' . ($val2['level'] + 1);
272
                            if ($val2['level'] == 0) {
273
                                print ' ui-btn-icon-right ui-btn';  // ui-btn to highlight on clic
274
                            }
275
                            print $disabled . '">';  // ui-btn to highlight on clic
276
                            if ($relurl2) {
277
                                if ($val2['enabled']) { // Allowed
278
                                    print '<a href="' . $relurl2 . '"';
279
                                    //print ' data-ajax="false"';
280
                                    print '>';
281
                                    $lastlevel2[$val2['level']] = 'enabled';
282
                                } else {     // Not allowed but visible (greyed)
283
                                    print '<a href="#" class="vsmenudisabled">';
284
                                    $lastlevel2[$val2['level']] = 'greyed';
285
                                }
286
                            } else {
287
                                if ($val2['enabled']) { // Allowed
288
                                    $lastlevel2[$val2['level']] = 'enabled';
289
                                } else {
290
                                    $lastlevel2[$val2['level']] = 'greyed';
291
                                }
292
                            }
293
                            //var_dump($val2['level']);
294
                            //var_dump($lastlevel2);
295
                            print $val2['titre'];
296
                            if ($relurl2) {
297
                                if ($val2['enabled']) // Allowed
298
                                    print '</a>';
299
                                else
300
                                    print '</a>';
301
                            }
302
                            print '</li>' . "\n";
303
                        }
304
                    }
305
                    //var_dump($submenu);
306
                    print '</ul>';
307
                }
308
                if ($val['enabled'] == 2) {
309
                    print '<font class="vsmenudisabled">' . $val['titre'] . '</font>';
310
                }
311
                print '</li>';
312
                print '</ul>' . "\n";
313
            }
314
        }
315
316
        unset($this->menu);
317
318
        //print 'xx'.$mode;
319
        return 0;
320
    }
321
}
322