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