Passed
Push — master ( 043752...2a5ce0 )
by Alxarafe
27:58
created

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