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

Base/Menubase.php (2 issues)

1
<?php
2
/* Copyright (C) 2007-2009	Laurent Destailleur	<[email protected]>
3
 * Copyright (C) 2009-2012	Regis Houssin		<[email protected]>
4
 * Copyright (C) 2018       Frédéric France     <[email protected]>
5
 * Copyright (C) 2019       Alxarafe            <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
namespace Alixar\Base;
21
22
use Alxarafe\Helpers\Config;
23
use Alixar\Helpers\Globals;
24
use Alixar\Helpers\DolUtils;
25
26
/**
27
 *  \file       htdocs/core/class/menubase.class.php
28
 *  \ingroup    core
29
 *  \brief      File of class to manage dynamic menu entries
30
 */
31
32
/**
33
 *  Class to manage menu entries
34
 */
35
class Menubase
36
{
37
38
    /**
39
     * @var DoliDB Database handler.
40
     */
41
    //public Globals::Globals::Globals::Globals::$db;
42
43
    /**
44
     * @var string Error code (or message)
45
     */
46
    public $error;
47
48
    /**
49
     * @var string[] Error codes (or messages)
50
     */
51
    public $errors = array();
52
53
    /**
54
     * @var int ID
55
     */
56
    public $id;
57
    public $menu_handler;
58
    public $module;
59
    public $type;
60
    public $mainmenu;
61
62
    /**
63
     * @var int ID
64
     */
65
    public $fk_menu;
66
67
    /**
68
     * @var string fk_mainmenu
69
     */
70
    public $fk_mainmenu;
71
72
    /**
73
     * @var string fk_leftmenu
74
     */
75
    public $fk_leftmenu;
76
77
    /**
78
     * @var int position
79
     */
80
    public $position;
81
    public $url;
82
    public $target;
83
    public $titre;
84
    //public Globals::$langs;
85
    public $level;
86
    public $leftmenu;  //<! Not used
87
    public $perms;
88
    public $enabled;
89
    //public $user;
90
    public $tms;
91
92
    /**
93
     * 	Constructor
94
     *
95
     *  @param		DoliDB		Globals::$db 		    Database handler
96
     *  @param     	string		$menu_handler	Menu handler
97
     */
98
    function __construct($menu_handler = '')
99
    {
100
        //Globals::$db = Globals::$db;
101
        $this->menu_handler = $menu_handler;
102
        return 1;
103
    }
104
105
    /**
106
     *      Create menu entry into database
107
     *
108
     *      @param      User	$user       User that create
109
     *      @return     int      			<0 if KO, Id of record if OK
110
     */
111
    function create($user = null)
112
    {
113
        //global $conf, Globals::$langs;
114
        // Clean parameters
115
        $this->menu_handler = trim($this->menu_handler);
116
        $this->module = trim($this->module);
117
        $this->type = trim($this->type);
118
        $this->mainmenu = trim($this->mainmenu);
119
        $this->leftmenu = trim($this->leftmenu);
120
        $this->fk_menu = (int) $this->fk_menu;          // If -1, fk_mainmenu and fk_leftmenu must be defined
121
        $this->fk_mainmenu = trim($this->fk_mainmenu);
122
        $this->fk_leftmenu = trim($this->fk_leftmenu);
123
        $this->position = (int) $this->position;
124
        $this->url = trim($this->url);
125
        $this->target = trim($this->target);
126
        $this->titre = trim($this->titre);
127
        $this->langs = trim($this->langs);
128
        $this->perms = trim($this->perms);
129
        $this->enabled = trim($this->enabled);
130
        $this->user = trim($this->user);
131
        if (empty($this->position)) {
132
            $this->position = 0;
133
        }
134
        if (!$this->level) {
135
            $this->level = 0;
136
        }
137
138
        // Check parameters
139
        if (empty($this->menu_handler)) {
140
            return -1;
141
        }
142
143
        // For PGSQL, we must first found the max rowid and use it as rowid in insert because postgresql
144
        // may use an already used value because its internal cursor does not increase when we do
145
        // an insert with a forced id.
146
        if (in_array(Globals::$db->type, array('pgsql'))) {
147
            $sql = "SELECT MAX(rowid) as maxrowid FROM " . MAIN_DB_PREFIX . "menu";
148
            $resqlrowid = Globals::$db->query($sql);
149
            if ($resqlrowid) {
150
                $obj = Globals::$db->fetch_object($resqlrowid);
151
                $maxrowid = $obj->maxrowid;
152
153
                // Max rowid can be empty if there is no record yet
154
                if (empty($maxrowid)) {
155
                    $maxrowid = 1;
156
                }
157
158
                $sql = "SELECT setval('" . MAIN_DB_PREFIX . "menu_rowid_seq', " . ($maxrowid) . ")";
159
                //print $sql; exit;
160
                $resqlrowidset = Globals::$db->query($sql);
161
                if (!$resqlrowidset) {
162
                    dol_print_error(Globals::$db);
163
                }
164
            } else {
165
                dol_print_error(Globals::$db);
166
            }
167
        }
168
169
        // Check that entry does not exists yet on key menu_handler-fk_menu-position-url-entity, to avoid errors with postgresql
170
        $sql = "SELECT count(*)";
171
        $sql .= " FROM " . MAIN_DB_PREFIX . "menu";
172
        $sql .= " WHERE menu_handler = '" . Globals::$db->escape($this->menu_handler) . "'";
173
        $sql .= " AND fk_menu = " . ((int) $this->fk_menu);
174
        $sql .= " AND position = " . ((int) $this->position);
175
        $sql .= " AND url = '" . Globals::$db->escape($this->url) . "'";
176
        $sql .= " AND entity = " . Globals::$conf->entity;
177
178
        $result = Globals::$db->query($sql);
179
        if ($result) {
180
            $row = Globals::$db->fetch_row($result);
181
182
            if ($row[0] == 0) {   // If not found
183
                // Insert request
184
                $sql = "INSERT INTO " . MAIN_DB_PREFIX . "menu(";
185
                $sql .= "menu_handler,";
186
                $sql .= "entity,";
187
                $sql .= "module,";
188
                $sql .= "type,";
189
                $sql .= "mainmenu,";
190
                $sql .= "leftmenu,";
191
                $sql .= "fk_menu,";
192
                $sql .= "fk_mainmenu,";
193
                $sql .= "fk_leftmenu,";
194
                $sql .= "position,";
195
                $sql .= "url,";
196
                $sql .= "target,";
197
                $sql .= "titre,";
198
                $sql .= "langs,";
199
                $sql .= "perms,";
200
                $sql .= "enabled,";
201
                $sql .= "usertype";
202
                $sql .= ") VALUES (";
203
                $sql .= " '" . Globals::$db->escape($this->menu_handler) . "',";
204
                $sql .= " '" . Globals::$db->escape($conf->entity) . "',";
205
                $sql .= " '" . Globals::$db->escape($this->module) . "',";
206
                $sql .= " '" . Globals::$db->escape($this->type) . "',";
207
                $sql .= " " . ($this->mainmenu ? "'" . Globals::$db->escape($this->mainmenu) . "'" : "''") . ",";    // Can't be null
208
                $sql .= " " . ($this->leftmenu ? "'" . Globals::$db->escape($this->leftmenu) . "'" : "null") . ",";
209
                $sql .= " " . ((int) $this->fk_menu) . ",";
210
                $sql .= " " . ($this->fk_mainmenu ? "'" . Globals::$db->escape($this->fk_mainmenu) . "'" : "null") . ",";
211
                $sql .= " " . ($this->fk_leftmenu ? "'" . Globals::$db->escape($this->fk_leftmenu) . "'" : "null") . ",";
212
                $sql .= " " . ((int) $this->position) . ",";
213
                $sql .= " '" . Globals::$db->escape($this->url) . "',";
214
                $sql .= " '" . Globals::$db->escape($this->target) . "',";
215
                $sql .= " '" . Globals::$db->escape($this->titre) . "',";
216
                $sql .= " '" . Globals::$db->escape($this->langs) . "',";
217
                $sql .= " '" . Globals::$db->escape($this->perms) . "',";
218
                $sql .= " '" . Globals::$db->escape($this->enabled) . "',";
219
                $sql .= " '" . Globals::$db->escape($this->user) . "'";
220
                $sql .= ")";
221
222
                DolUtils::dol_syslog(get_class($this) . "::create", LOG_DEBUG);
223
                $resql = Globals::$db->query($sql);
224
                if ($resql) {
225
                    $this->id = Globals::$db->last_insert_id(MAIN_DB_PREFIX . "menu");
226
                    DolUtils::dol_syslog(get_class($this) . "::create record added has rowid=" . $this->id, LOG_DEBUG);
227
228
                    return $this->id;
229
                } else {
230
                    $this->error = "Error " . Globals::$db->lasterror();
231
                    return -1;
232
                }
233
            } else {
234
                DolUtils::dol_syslog(get_class($this) . "::create menu entry already exists", LOG_WARNING);
235
                $this->error = 'Error Menu entry already exists';
236
                return 0;
237
            }
238
        } else {
239
            return -1;
240
        }
241
    }
242
243
    /**
244
     *  Update menu entry into database.
245
     *
246
     *  @param	User	$user        	User that modify
247
     *  @param  int		$notrigger	    0=no, 1=yes (no update trigger)
248
     *  @return int 		        	<0 if KO, >0 if OK
249
     */
250
    function update($user = null, $notrigger = 0)
251
    {
252
        //global $conf, Globals::$langs;
253
        // Clean parameters
254
        $this->rowid = trim($this->rowid);
255
        $this->menu_handler = trim($this->menu_handler);
256
        $this->module = trim($this->module);
257
        $this->type = trim($this->type);
258
        $this->mainmenu = trim($this->mainmenu);
259
        $this->leftmenu = trim($this->leftmenu);
260
        $this->fk_menu = (int) $this->fk_menu;
261
        $this->fk_mainmenu = trim($this->fk_mainmenu);
262
        $this->fk_leftmenu = trim($this->fk_leftmenu);
263
        $this->position = (int) $this->position;
264
        $this->url = trim($this->url);
265
        $this->target = trim($this->target);
266
        $this->titre = trim($this->titre);
267
        $this->langs = trim($this->langs);
268
        $this->perms = trim($this->perms);
269
        $this->enabled = trim($this->enabled);
270
        $this->user = trim($this->user);
271
272
        // Check parameters
273
        // Put here code to add control on parameters values
274
        // Update request
275
        $sql = "UPDATE " . MAIN_DB_PREFIX . "menu SET";
276
        $sql .= " menu_handler='" . Globals::$db->escape($this->menu_handler) . "',";
277
        $sql .= " module='" . Globals::$db->escape($this->module) . "',";
278
        $sql .= " type='" . Globals::$db->escape($this->type) . "',";
279
        $sql .= " mainmenu='" . Globals::$db->escape($this->mainmenu) . "',";
280
        $sql .= " leftmenu='" . Globals::$db->escape($this->leftmenu) . "',";
281
        $sql .= " fk_menu=" . $this->fk_menu . ",";
282
        $sql .= " fk_mainmenu=" . ($this->fk_mainmenu ? "'" . Globals::$db->escape($this->fk_mainmenu) . "'" : "null") . ",";
283
        $sql .= " fk_leftmenu=" . ($this->fk_leftmenu ? "'" . Globals::$db->escape($this->fk_leftmenu) . "'" : "null") . ",";
284
        $sql .= " position=" . ($this->position > 0 ? $this->position : 0) . ",";
285
        $sql .= " url='" . Globals::$db->escape($this->url) . "',";
286
        $sql .= " target='" . Globals::$db->escape($this->target) . "',";
287
        $sql .= " titre='" . Globals::$db->escape($this->titre) . "',";
288
        $sql .= " langs='" . Globals::$db->escape($this->langs) . "',";
289
        $sql .= " perms='" . Globals::$db->escape($this->perms) . "',";
290
        $sql .= " enabled='" . Globals::$db->escape($this->enabled) . "',";
291
        $sql .= " usertype='" . Globals::$db->escape($this->user) . "'";
292
        $sql .= " WHERE rowid=" . $this->id;
293
294
        DolUtils::dol_syslog(get_class($this) . "::update", LOG_DEBUG);
295
        $resql = Globals::$db->query($sql);
296
        if (!$resql) {
297
            $this->error = "Error " . Globals::$db->lasterror();
298
            return -1;
299
        }
300
301
        return 1;
302
    }
303
304
    /**
305
     *   Load object in memory from database
306
     *
307
     *   @param		int		$id         Id object
308
     *   @param		User    $user       User that load
309
     *   @return	int         		<0 if KO, >0 if OK
310
     */
311
    function fetch($id, $user = null)
312
    {
313
        //global Globals::$langs;
314
315
        $sql = "SELECT";
316
        $sql .= " t.rowid,";
317
        $sql .= " t.menu_handler,";
318
        $sql .= " t.entity,";
319
        $sql .= " t.module,";
320
        $sql .= " t.type,";
321
        $sql .= " t.mainmenu,";
322
        $sql .= " t.leftmenu,";
323
        $sql .= " t.fk_menu,";
324
        $sql .= " t.fk_mainmenu,";
325
        $sql .= " t.fk_leftmenu,";
326
        $sql .= " t.position,";
327
        $sql .= " t.url,";
328
        $sql .= " t.target,";
329
        $sql .= " t.titre,";
330
        $sql .= " t.langs,";
331
        $sql .= " t.perms,";
332
        $sql .= " t.enabled,";
333
        $sql .= " t.usertype as user,";
334
        $sql .= " t.tms";
335
        $sql .= " FROM " . MAIN_DB_PREFIX . "menu as t";
336
        $sql .= " WHERE t.rowid = " . $id;
337
338
        DolUtils::dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
339
        $resql = Globals::$db->query($sql);
340
        if ($resql) {
341
            if (Globals::$db->num_rows($resql)) {
342
                $obj = Globals::$db->fetch_object($resql);
343
344
                $this->id = $obj->rowid;
345
346
                $this->menu_handler = $obj->menu_handler;
347
                $this->entity = $obj->entity;
348
                $this->module = $obj->module;
349
                $this->type = $obj->type;
350
                $this->mainmenu = $obj->mainmenu;
351
                $this->leftmenu = $obj->leftmenu;
352
                $this->fk_menu = $obj->fk_menu;
353
                $this->fk_mainmenu = $obj->fk_mainmenu;
354
                $this->fk_leftmenu = $obj->fk_leftmenu;
355
                $this->position = $obj->position;
356
                $this->url = $obj->url;
357
                $this->target = $obj->target;
358
                $this->titre = $obj->titre;
359
                $this->langs = $obj->langs;
360
                $this->perms = $obj->perms;
361
                $this->enabled = str_replace("\"", "'", $obj->enabled);
362
                $this->user = $obj->user;
363
                $this->tms = Globals::$db->jdate($obj->tms);
364
            }
365
            Globals::$db->free($resql);
366
367
            return 1;
368
        } else {
369
            $this->error = "Error " . Globals::$db->lasterror();
370
            return -1;
371
        }
372
    }
373
374
    /**
375
     *  Delete object in database
376
     *
377
     * 	@param	User	$user       User that delete
378
     * 	@return	int					<0 if KO, >0 if OK
379
     */
380
    function delete($user)
381
    {
382
        // global $conf, Globals::$langs;
383
384
        $sql = "DELETE FROM " . MAIN_DB_PREFIX . "menu";
385
        $sql .= " WHERE rowid=" . $this->id;
386
387
        DolUtils::dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
388
        $resql = Globals::$db->query($sql);
389
        if (!$resql) {
390
            $this->error = "Error " . Globals::$db->lasterror();
391
            return -1;
392
        }
393
394
        return 1;
395
    }
396
397
    /**
398
     *  Initialise an instance with random values.
399
     *  Used to build previews or test instances.
400
     * 	id must be 0 if object instance is a specimen.
401
     *
402
     *  @return	void
403
     */
404
    function initAsSpecimen()
405
    {
406
        $this->id = 0;
407
408
        $this->menu_handler = 'all';
409
        $this->module = 'specimen';
410
        $this->type = 'top';
411
        $this->mainmenu = '';
412
        $this->fk_menu = '0';
0 ignored issues
show
Documentation Bug introduced by
The property $fk_menu was declared of type integer, but '0' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
413
        $this->position = '';
0 ignored issues
show
Documentation Bug introduced by
The property $position was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
414
        $this->url = 'http://dummy';
415
        $this->target = '';
416
        $this->titre = 'Specimen menu';
417
        $this->langs = '';
418
        $this->level = '';
419
        $this->leftmenu = '';
420
        $this->perms = '';
421
        $this->enabled = '';
422
        $this->user = '';
423
        $this->tms = '';
424
    }
425
426
    /**
427
     * 	Load tabMenu array with top menu entries found into database.
428
     *
429
     * 	@param	string	$mymainmenu		Value for mainmenu to filter menu to load (always '')
430
     * 	@param	string	$myleftmenu		Value for leftmenu to filter menu to load (always '')
431
     * 	@param	int		$type_user		0=Menu for backoffice, 1=Menu for front office
432
     * 	@param	string	$menu_handler	Filter on name of menu_handler used (auguria, eldy...)
433
     * 	@param  array	$tabMenu       If array with menu entries already loaded, we put this array here (in most cases, it's empty)
434
     * 	@return	array					Return array with menu entries for top menu
435
     */
436
    function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
437
    {
438
        // global Globals::$langs, $user, $conf; // To export to DolUtils::dol_eval function
439
        // global $mainmenu, $leftmenu;  // To export to DolUtils::dol_eval function
440
441
        $mainmenu = $mymainmenu;  // To export to DolUtils::dol_eval function
442
        $leftmenu = $myleftmenu;  // To export to DolUtils::dol_eval function
443
444
        $newTabMenu = array();
445
        foreach ($tabMenu as $val) {
446
            if ($val['type'] == 'top') {
447
                $newTabMenu[] = $val;
448
            }
449
        }
450
451
        return $newTabMenu;
452
    }
453
454
    /**
455
     * 	Load entries found from database (and stored into $tabMenu) in $this->newmenu array.
456
     *  Warning: Entries in $tabMenu must have child after parent
457
     *
458
     * 	@param	Menu	$newmenu        Menu array to complete (in most cases, it's empty, may be already initialized with some menu manager like eldy)
459
     * 	@param	string	$mymainmenu		Value for mainmenu to filter menu to load (often $_SESSION["mainmenu"])
460
     * 	@param	string	$myleftmenu		Value for leftmenu to filter menu to load (always '')
461
     * 	@param	int		$type_user		0=Menu for backoffice, 1=Menu for front office
462
     * 	@param	string	$menu_handler	Filter on name of menu_handler used (auguria, eldy...)
463
     * 	@param  array	$tabMenu       Array with menu entries already loaded
464
     * 	@return Menu    		       	Menu array for particular mainmenu value or full tabArray
465
     */
466
    function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
467
    {
468
        //global Globals::$langs, $user, $conf;  // To export to DolUtils::dol_eval function
469
        // global $mainmenu, $leftmenu;  // To export to DolUtils::dol_eval function
470
471
        $mainmenu = $mymainmenu;  // To export to DolUtils::dol_eval function
472
        $leftmenu = $myleftmenu;  // To export to DolUtils::dol_eval function
473
        // Detect what is top mainmenu id
474
        $menutopid = '';
475
        foreach ($tabMenu as $key => $val) {
476
            // Define menutopid of mainmenu
477
            if (empty($menutopid) && $val['type'] == 'top' && $val['mainmenu'] == $mainmenu) {
478
                $menutopid = $val['rowid'];
479
                break;
480
            }
481
        }
482
483
        // We initialize newmenu with first already found menu entries
484
        $this->newmenu = $newmenu;
485
486
        // Now complete $this->newmenu->list to add entries found into $tabMenu that are childs of mainmenu=$menutopid, using the fk_menu link that is int (old method)
487
        $this->recur($tabMenu, $menutopid, 1);
488
489
        // Now complete $this->newmenu->list when fk_menu value is -1 (left menu added by modules with no top menu)
490
        foreach ($tabMenu as $key => $val) {
491
            //var_dump($tabMenu);
492
            if ($val['fk_menu'] == -1 && $val['fk_mainmenu'] == $mainmenu) {    // We found a menu entry not linked to parent with good mainmenu
493
                //print 'Try to add menu (current is mainmenu='.$mainmenu.' leftmenu='.$leftmenu.') for '.join(',',$val).' fk_mainmenu='.$val['fk_mainmenu'].' fk_leftmenu='.$val['fk_leftmenu'].'<br>';
494
                //var_dump($this->newmenu->liste);exit;
495
                if (empty($val['fk_leftmenu'])) {
496
                    $this->newmenu->add($val['url'], $val['titre'], 0, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
497
                    //var_dump($this->newmenu->liste);
498
                } else {
499
                    // Search first menu with this couple (mainmenu,leftmenu)=(fk_mainmenu,fk_leftmenu)
500
                    $searchlastsub = 0;
501
                    $lastid = 0;
502
                    $nextid = 0;
503
                    $found = 0;
504
                    foreach ($this->newmenu->liste as $keyparent => $valparent) {
505
                        //var_dump($valparent);
506
                        if ($searchlastsub) {    // If we started to search for last submenu
507
                            if ($valparent['level'] >= $searchlastsub) {
508
                                $lastid = $keyparent;
509
                            }
510
                            if ($valparent['level'] < $searchlastsub) {
511
                                $nextid = $keyparent;
512
                                break;
513
                            }
514
                        }
515
                        if ($valparent['mainmenu'] == $val['fk_mainmenu'] && $valparent['leftmenu'] == $val['fk_leftmenu']) {
516
                            //print "We found parent: keyparent='.$keyparent.' - level=".$valparent['level'].' - '.join(',',$valparent).'<br>';
517
                            // Now we look to find last subelement of this parent (we add at end)
518
                            $searchlastsub = ($valparent['level'] + 1);
519
                            $lastid = $keyparent;
520
                            $found = 1;
521
                        }
522
                    }
523
                    //print 'We must insert menu entry between entry '.$lastid.' and '.$nextid.'<br>';
524
                    if ($found) {
525
                        $this->newmenu->insert($lastid, $val['url'], $val['titre'], $searchlastsub, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
526
                    } else {
527
                        DolUtils::dol_syslog("Error. Modules " . $val['module'] . " has defined a menu entry with a parent='fk_mainmenu=" . $val['fk_leftmenu'] . ",fk_leftmenu=" . $val['fk_leftmenu'] . "' and position=" . $val['position'] . '. The parent was not found. May be you forget it into your definition of menu, or may be the parent has a "position" that is after the child (fix field "position" of parent or child in this case).', LOG_WARNING);
528
                        //print "Parent menu not found !!<br>";
529
                    }
530
                }
531
            }
532
        }
533
534
        return $this->newmenu;
535
    }
536
537
    /**
538
     *  Load entries found in database into variable $tabMenu. Note that only "database menu entries" are loaded here, hardcoded will not be present into output.
539
     *
540
     *  @param	string	$mymainmenu     Value for mainmenu that defined mainmenu
541
     *  @param	string	$myleftmenu     Value for left that defined leftmenu
542
     *  @param  int		$type_user      Looks for menu entry for 0=Internal users, 1=External users
543
     *  @param  string	$menu_handler   Name of menu_handler used ('auguria', 'eldy'...)
544
     *  @param  array	$tabMenu        Array to store new entries found (in most cases, it's empty, but may be alreay filled)
545
     *  @return int     		        >0 if OK, <0 if KO
546
     */
547
    function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
548
    {
549
        //global Globals::$langs, $user, $conf; // To export to DolUtils::dol_eval function
550
        //global $mainmenu, $leftmenu; // To export to DolUtils::dol_eval function
551
552
        $menutopid = 0;
553
        $mainmenu = $mymainmenu;  // To export to DolUtils::dol_eval function
554
        $leftmenu = $myleftmenu;  // To export to DolUtils::dol_eval function
555
556
        $sql = "SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";
557
        $sql .= " FROM " . MAIN_DB_PREFIX . "menu as m";
558
        $sql .= " WHERE m.entity IN (0," . Globals::$conf->entity . ")";
559
        $sql .= " AND m.menu_handler IN ('" . $menu_handler . "','all')";
560
        if ($type_user == 0) {
561
            $sql .= " AND m.usertype IN (0,2)";
562
        }
563
        if ($type_user == 1) {
564
            $sql .= " AND m.usertype IN (1,2)";
565
        }
566
        $sql .= " ORDER BY m.position, m.rowid";
567
        //print $sql;
568
        //$tmp1=microtime(true);
569
        //print '>>> 1 0<br>';
570
        DolUtils::dol_syslog(get_class($this) . "::menuLoad mymainmenu=" . $mymainmenu . " myleftmenu=" . $myleftmenu . " type_user=" . $type_user . " menu_handler=" . $menu_handler . " tabMenu size=" . count($tabMenu) . "", LOG_DEBUG);
571
        $resql = Config::$dbEngine->select($sql);
572
        if (is_array($resql)) {
573
            $a = 0;
574
            $b = 0;
575
            foreach ($resql as $array) {
576
                //$objm = Globals::$db->fetch_object($resql);
577
                //$menu = json_decode(json_encode($array));
578
                $menu = $array;
579
580
                // Define $right
581
                $perms = true;
582
                if ($menu['perms']) {
583
                    $tmpcond = $menu['perms'];
584
                    if ($leftmenu == 'all') {
585
                        $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force part of condition to true
586
                    }
587
                    $perms = DolUtils::verifCond($tmpcond);
588
                    //print "verifCond rowid=".$menu['rowid']." ".$tmpcond.":".$perms."<br>\n";
589
                }
590
591
                // Define $enabled
592
                $enabled = true;
593
                if ($menu['enabled']) {
594
                    $tmpcond = $menu['enabled'];
595
                    if ($leftmenu == 'all') {
596
                        $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force part of condition to true
597
                    }
598
                    $enabled = DolUtils::verifCond($tmpcond);
599
                }
600
601
                // Define $title
602
                if ($enabled) {
603
                    $title = Globals::$langs->trans($menu['titre']);  // If $menu['titre'] start with $, a DolUtils::dol_eval is done.
604
                    //var_dump($title.'-'.$menu['titre']);
605
                    if ($title == $menu['titre']) {   // Translation not found
606
                        if (!empty($menu['langs'])) {    // If there is a dedicated translation file
607
                            //print 'Load file '.$menu['langs'].'<br>';
608
                            Globals::$langs->load($menu['langs']);
609
                        }
610
611
                        $substitarray = array('__LOGIN__' => Globals::$user->login, '__USER_ID__' => Globals::$user->id, '__USER_SUPERVISOR_ID__' => Globals::$user->fk_user);
612
                        $menu['titre'] = DolUtils::make_substitutions($menu['titre'], $substitarray);
613
614
                        if (preg_match("/\//", $menu['titre'])) { // To manage translation when title is string1/string2
615
                            $tab_titre = explode("/", $menu['titre']);
616
                            $title = Globals::$langs->trans($tab_titre[0]) . "/" . Globals::$langs->trans($tab_titre[1]);
617
                        } else if (preg_match('/\|\|/', $menu['titre'])) { // To manage different translation (Title||AltTitle@ConditionForAltTitle)
618
                            $tab_title = explode("||", $menu['titre']);
619
                            $alt_title = explode("@", $tab_title[1]);
620
                            $title_enabled = verifCond($alt_title[1]);
621
                            $title = ($title_enabled ? Globals::$langs->trans($alt_title[0]) : Globals::$langs->trans($tab_title[0]));
622
                        } else {
623
                            $title = Globals::$langs->trans($menu['titre']);
624
                        }
625
                    }
626
                    //$tmp4=microtime(true);
627
                    //print '>>> 3 '.($tmp4 - $tmp3).'<br>';
628
                    // We complete tabMenu
629
                    $tabMenu[$b]['rowid'] = $menu['rowid'];
630
                    $tabMenu[$b]['module'] = $menu['module'];
631
                    $tabMenu[$b]['fk_menu'] = $menu['fk_menu'];
632
                    $tabMenu[$b]['url'] = $menu['url'];
633
                    if (!preg_match("/^(http:\/\/|https:\/\/)/i", $tabMenu[$b]['url'])) {
634
                        if (preg_match('/\?/', $tabMenu[$b]['url'])) {
635
                            $tabMenu[$b]['url'] .= '&amp;idmenu=' . $menu['rowid'];
636
                        } else {
637
                            $tabMenu[$b]['url'] .= '?idmenu=' . $menu['rowid'];
638
                        }
639
                    }
640
                    $tabMenu[$b]['titre'] = $title;
641
                    $tabMenu[$b]['target'] = $menu['target'];
642
                    $tabMenu[$b]['mainmenu'] = $menu['mainmenu'];
643
                    $tabMenu[$b]['leftmenu'] = $menu['leftmenu'];
644
                    $tabMenu[$b]['perms'] = $perms;
645
                    $tabMenu[$b]['enabled'] = $enabled;
646
                    $tabMenu[$b]['type'] = $menu['type'];
647
                    //$tabMenu[$b]['langs']       = $menu['langs'];
648
                    $tabMenu[$b]['fk_mainmenu'] = $menu['fk_mainmenu'];
649
                    $tabMenu[$b]['fk_leftmenu'] = $menu['fk_leftmenu'];
650
                    $tabMenu[$b]['position'] = (int) $menu['position'];
651
652
                    $b++;
653
                }
654
655
                $a++;
656
            }
657
            //Globals::$db->free($resql);
658
            // Currently $tabMenu is sorted on position.
659
            // If a child have a position lower that its parent, we can make a loop to fix this here, but we prefer to show a warning
660
            // into the leftMenuCharger later to avoid useless operations.
661
662
            return 1;
663
        } else {
664
            dol_print_error(Globals::$db);
665
            return -1;
666
        }
667
    }
668
669
    /**
670
     *  Complete this->newmenu with menu entry found in $tab
671
     *
672
     *  @param  array	$tab			Tab array with all menu entries
673
     *  @param  int		$pere			Id of parent
674
     *  @param  int		$level			Level
675
     *  @return	void
676
     */
677
    private function recur($tab, $pere, $level)
678
    {
679
        // Loop on tab array
680
        $num = count($tab);
681
        for ($x = 0; $x < $num; $x++) {
682
            //si un element a pour pere : $pere
683
            if ((($tab[$x]['fk_menu'] >= 0 && $tab[$x]['fk_menu'] == $pere)) && $tab[$x]['enabled']) {
684
                $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], ($level - 1), $tab[$x]['perms'], $tab[$x]['target'], $tab[$x]['mainmenu'], $tab[$x]['leftmenu']);
685
                $this->recur($tab, $tab[$x]['rowid'], ($level + 1));
686
            }
687
        }
688
    }
689
}
690