Passed
Push — main ( c1d479...a20d4a )
by Rafael
46:41
created

Load::loadLangs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace DoliCore\Tools;
20
21
use DoliCore\Base\Config;
22
use DoliCore\Base\Constants;
23
use DoliCore\Base\Translate;
24
use DoliCore\Lib\Conf;
25
use DoliCore\Lib\HookManager;
26
use DoliDB;
27
use DoliModules\Company\Model\Company;
28
use DoliModules\User\Model\User;
29
use MenuManager;
30
31
/**
32
 * Class Load
33
 *
34
 * This class loads Dolibarr's global variables.
35
 * Replaces main.inc.php
36
 *
37
 * @package DoliCore\Tools
38
 */
39
abstract class Load
40
{
41
    /**
42
     * Contains the information of the old $conf global var.
43
     *
44
     * Config::getConf() can be used at any point to retrieve the contents of the
45
     * $conf variable used globally by Dolibarr.
46
     *
47
     * The content of the variable is saved with the first call and this copy is
48
     * returned. If it is necessary to regenerate it, the parameter true can be
49
     * passed to it.
50
     *
51
     * @var null|Conf
52
     */
53
    private static $config = null;
54
55
    /**
56
     * Contains a DoliDB connection.
57
     *
58
     * @var DoliDB, null
59
     */
60
    private static $db;
61
62
    /**
63
     * Contains a HookManager class.
64
     *
65
     * @var $hookManager
0 ignored issues
show
Documentation Bug introduced by
The doc comment $hookManager at position 0 could not be parsed: Unknown type name '$hookManager' at position 0 in $hookManager.
Loading history...
66
     */
67
    private static $hook_manager;
68
69
    /**
70
     * Contains a Translate class
71
     *
72
     * @var Translate
73
     */
74
    private static $langs;
75
76
    /**
77
     * Contains a User class instance.
78
     *
79
     * @var User
80
     */
81
    private static $user;
82
83
    private static $menu_manager;
84
85
    private static $mysoc;
86
87
    /**
88
     * Returns a stdClass with the information contained in the conf.php file.
89
     *
90
     * @param $reload
91
     *
92
     * @return Conf|null
93
     */
94
    public static function getConfig($reload = false): ?Conf
95
    {
96
        if ($reload || !isset(self::$config)) {
97
            self::$config = self::loadConfig();
98
        }
99
100
        return self::$config;
101
    }
102
103
    private static function loadConfig()
104
    {
105
106
        self::$config = Config::loadDolibarrConfig();
107
        return self::$config;
108
    }
109
110
    /**
111
     * Returns a HookManager class instance.
112
     *
113
     * @return HookManager|null
114
     */
115
    public static function getHookManager(): ?HookManager
116
    {
117
        if (empty(self::$hook_manager)) {
118
            self::$hook_manager = self::loadHookManager();
119
        }
120
        return self::$hook_manager;
121
    }
122
123
    /**
124
     * Returns a HookManager class instance.
125
     *
126
     * @return mixed
127
     */
128
    private static function loadHookManager()
129
    {
130
        self::$hook_manager = new HookManager(self::$db);
131
        return self::$hook_manager;
132
    }
133
134
    /**
135
     * Returns a DoliDB connection instance.
136
     *
137
     * @return DoliDB|null
138
     */
139
    public static function getDb(): ?DoliDB
140
    {
141
        if (!isset(self::$config)) {
142
            return null;
143
        }
144
145
        if (!isset(self::$db)) {
146
            self::$db = self::loadDb();
147
        }
148
149
        return self::$db;
150
    }
151
152
    /**
153
     * Returns a Dolibarr DB connection (DoliDB) instance.
154
     *
155
     * @return DoliDb
156
     * @throws \Exception
157
     */
158
    private static function loadDb()
159
    {
160
        $conf = self::$config;
161
        self::$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int)$conf->db->port);
162
        self::$config->setValues(self::$db);
0 ignored issues
show
Bug introduced by
The method setValues() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
        self::$config->/** @scrutinizer ignore-call */ 
163
                       setValues(self::$db);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
164
        return self::$db;
165
    }
166
167
    /**
168
     * Returns a Translate class instance.
169
     *
170
     * @return Translate|null
171
     */
172
    public static function getLangs(): ?Translate
173
    {
174
        if (!isset(self::$langs)) {
175
            self::$langs = self::loadLangs();
176
        }
177
        return self::$langs;
178
    }
179
180
    /**
181
     * Returns a Translate class instance.
182
     *
183
     * @return Translate
184
     */
185
    private static function loadLangs()
186
    {
187
        self::$langs = new Translate('', self::$config);
188
        return self::$langs;
189
    }
190
191
    /**
192
     * Returns a User class instance.
193
     *
194
     * @return User|null
195
     */
196
    public static function getUser(): ?User
197
    {
198
        if (!isset(self::$user)) {
199
            self::$user = self::loadUser();
200
        }
201
        return self::$user;
202
    }
203
204
    /**
205
     * Returns a user class instance
206
     *
207
     * @return User
208
     */
209
    private static function loadUser()
210
    {
211
        self::$user = new User(self::$db);
212
        return self::$user;
213
    }
214
215
    /**
216
     * Returns a MenuManager class instance.
217
     *
218
     * @return mixed
219
     */
220
    public static function getMenuManager()
221
    {
222
        if (!isset(self::$menu_manager)) {
223
            self::$menu_manager = self::loadMenuManager();
224
        }
225
        return self::$menu_manager;
226
    }
227
228
    private static function loadMenuManager()
229
    {
230
        $conf = self::$config;
231
        $db = self::$db;
232
        $user = self::$user;
233
234
        $menumanager = null;
235
        if (!defined('NOREQUIREMENU')) {
236
            if (empty($user->socid)) {    // If internal user or not defined
237
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENU_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENU_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENU_STANDARD) : $conf->global->MAIN_MENU_STANDARD_FORCED);
238
            } else {
239
                // If external user
240
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENUFRONT_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENUFRONT_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENUFRONT_STANDARD) : $conf->global->MAIN_MENUFRONT_STANDARD_FORCED);
241
            }
242
243
            // Load the menu manager (only if not already done)
244
            $file_menu = $conf->standard_menu;
245
            if (GETPOST('menu', 'alpha')) {
246
                $file_menu = GETPOST('menu', 'alpha'); // example: menu=eldy_menu.php
247
            }
248
            if (!class_exists('MenuManager')) {
249
                $menufound = 0;
250
                $dirmenus = array_merge(["/../Dolibarr/Core/Menu/"], (array)$conf->modules_parts['menus']);
251
                foreach ($dirmenus as $dirmenu) {
252
                    $menufound = dol_include_once($dirmenu . "standard/" . $file_menu);
253
                    if (class_exists('MenuManager')) {
254
                        break;
255
                    }
256
                }
257
                if (!class_exists('MenuManager')) { // If failed to include, we try with standard eldy_menu.php
258
                    dol_syslog("You define a menu manager '" . $file_menu . "' that can not be loaded.", LOG_WARNING);
259
                    $file_menu = 'eldy_menu.php';
260
                    include_once DOL_DOCUMENT_ROOT . "/../Dolibarr/Core/Menu/standard/" . $file_menu;
261
                }
262
            }
263
            $menumanager = new \MenuManager($db, empty($user->socid) ? 0 : 1);
264
            $menumanager->loadMenu();
265
        }
266
267
        return self::$menu_manager = $menumanager;
268
    }
269
270
    public static function getMySoc()
271
    {
272
        if (!isset(self::$db)) {
273
            return null;
274
        }
275
276
        if (!isset(self::$mysoc)) {
277
            self::$mysoc = self::loadMySoc();
278
        }
279
        return self::$mysoc;
280
    }
281
282
    private static function loadMySoc()
283
    {
284
        $mysoc = new Company(self::$db);
285
        $mysoc->setMysoc(self::$config);
286
287
        // We set some specific default values according to country
288
289
        if ($mysoc->country_code == 'DE' && !isset(self::$config->global->MAIN_INVERT_SENDER_RECIPIENT)) {
290
            // For DE, we need to invert our address with customer address
291
            self::$config->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
292
        }
293
        if ($mysoc->country_code == 'FR' && !isset(self::$config->global->INVOICE_CATEGORY_OF_OPERATION)) {
294
            // For FR, default value of option to show category of operations is on by default. Decret n°2099-1299 2022-10-07
295
            self::$config->global->INVOICE_CATEGORY_OF_OPERATION = 1;
296
        }
297
        if ($mysoc->country_code == 'FR' && !isset(self::$config->global->INVOICE_DISABLE_REPLACEMENT)) {
298
            // For FR, the replacement invoice type is not allowed.
299
            // From an accounting point of view, this creates holes in the numbering of the invoice.
300
            // This is very problematic during a fiscal control.
301
            self::$config->global->INVOICE_DISABLE_REPLACEMENT = 1;
302
        }
303
        if ($mysoc->country_code == 'GR' && !isset(self::$config->global->INVOICE_DISABLE_REPLACEMENT)) {
304
            // The replacement invoice type is not allowed in Greece.
305
            self::$config->global->INVOICE_DISABLE_REPLACEMENT = 1;
306
        }
307
        if ($mysoc->country_code == 'GR' && !isset(self::$config->global->INVOICE_DISABLE_DEPOSIT)) {
308
            // The deposit invoice type is not allowed in Greece.
309
            self::$config->global->INVOICE_DISABLE_DEPOSIT = 1;
310
        }
311
        if ($mysoc->country_code == 'GR' && !isset(self::$config->global->INVOICE_CREDIT_NOTE_STANDALONE)) {
312
            // Standalone credit note is compulsory in Greece.
313
            self::$config->global->INVOICE_CREDIT_NOTE_STANDALONE = 1;
314
        }
315
        if ($mysoc->country_code == 'GR' && !isset(self::$config->global->INVOICE_SUBTYPE_ENABLED)) {
316
            // Invoice subtype is a requirement for Greece.
317
            self::$config->global->INVOICE_SUBTYPE_ENABLED = 1;
318
        }
319
320
        if (($mysoc->localtax1_assuj || $mysoc->localtax2_assuj) && !isset(self::$config->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
321
            // For countries using the 2nd or 3rd tax, we disable input/edit of lines using the price including tax (because 2nb and 3rd tax not yet taken into account).
322
            // Work In Progress to support all taxes into unit price entry when MAIN_UNIT_PRICE_WITH_TAX_IS_FOR_ALL_TAXES is set.
323
            self::$config->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
324
        }
325
326
        return $mysoc;
327
    }
328
329
}
330