Passed
Push — GENERAL_BUG_REVIEW_240911 ( 8cbbee...d4fdcf )
by Rafael
64:20
created

ModeleImports::getDriverLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2005-2012  Laurent Destailleur <[email protected]>
4
 * Copyright (C) 2005-2012	Regis Houssin		<[email protected]>
5
 * Copyright (C) 2024		MDW							<[email protected]>
6
 * Copyright (C) 2024       Frédéric France             <[email protected]>
7
 * Copyright (C) 2024       Rafael San José             <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 * or see https://www.gnu.org/
22
 */
23
24
namespace Dolibarr\Code\Imports\Classes;
25
26
use Dolibarr\Code\Societe\Classes\Societe;
27
use DoliDB;
28
29
/**
30
 *  \file       htdocs/core/modules/import/modules_import.php
31
 *  \ingroup    export
32
 *  \brief      File of parent class for import file readers
33
 */
34
35
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/functions.lib.php';
36
37
/**
38
 *  Parent class for import file readers
39
 */
40
class ModeleImports
41
{
42
    /**
43
     * @var DoliDB Database handler.
44
     */
45
    public $db;
46
47
    public $datatoimport;
48
49
    /**
50
     * @var string Error code (or message)
51
     */
52
    public $error = '';
53
54
    /**
55
     * @var string[] Error codes (or messages)
56
     */
57
    public $errors = array();
58
59
    /**
60
     * @var string[] warnings codes (or messages)
61
     */
62
    public $warnings = array();
63
64
    /**
65
     * @var string Code of driver
66
     */
67
    public $id;
68
69
    /**
70
     * @var string label of driver
71
     */
72
    public $label;
73
74
    /**
75
     * @var string Extension of files imported by driver
76
     */
77
    public $extension;
78
79
    /**
80
     * Dolibarr version of driver
81
     * @var string
82
     */
83
    public $version = 'dolibarr';
84
85
    /**
86
     * PHP minimal version required by driver
87
     * @var array{0:int,1:int}
88
     */
89
    public $phpmin = array(7, 0);
90
91
    /**
92
     * Label of external lib used by driver
93
     * @var string
94
     */
95
    public $label_lib;
96
97
    /**
98
     * Version of external lib used by driver
99
     * @var string
100
     */
101
    public $version_lib;
102
103
    // Array of all drivers
104
    public $driverlabel = array();
105
106
    public $driverdesc = array();
107
108
    public $driverversion = array();
109
110
    public $drivererror = array();
111
112
    public $liblabel = array();
113
114
    public $libversion = array();
115
116
    /**
117
     * @var string charset
118
     */
119
    public $charset;
120
121
    /**
122
     * @var string picto
123
     */
124
    public $picto;
125
126
    /**
127
     * @var string description
128
     */
129
    public $desc;
130
131
    /**
132
     * @var string escape
133
     */
134
    public $escape;
135
136
    /**
137
     * @var string enclosure
138
     */
139
    public $enclosure;
140
141
    /**
142
     * @var Societe thirdparty
143
     */
144
    public $thirdpartyobject;
145
146
    /**
147
     * @var array   Element mapping from table name
148
     */
149
    public static $mapTableToElement = MODULE_MAPPING;
150
151
    /**
152
     *  Constructor
153
     */
154
    public function __construct()
155
    {
156
        global $hookmanager;
157
158
        if (is_object($hookmanager)) {
159
            $hookmanager->initHooks(array('import'));
160
            $parameters = array();
161
            $reshook = $hookmanager->executeHooks('constructModeleImports', $parameters, $this);
162
            if ($reshook >= 0 && !empty($hookmanager->resArray)) {
163
                foreach ($hookmanager->resArray as $mapList) {
164
                    self::$mapTableToElement[$mapList['table']] = $mapList['element'];
165
                }
166
            }
167
        }
168
    }
169
170
    /**
171
     * getDriverId
172
     *
173
     * @return string       Code of driver
174
     */
175
    public function getDriverId()
176
    {
177
        return $this->id;
178
    }
179
180
    /**
181
     *  getDriverLabel
182
     *
183
     * @return string  Label
184
     */
185
    public function getDriverLabel()
186
    {
187
        return $this->label;
188
    }
189
190
    /**
191
     *  getDriverDesc
192
     *
193
     * @return string  Description
194
     */
195
    public function getDriverDesc()
196
    {
197
        return $this->desc;
198
    }
199
200
    /**
201
     * getDriverExtension
202
     *
203
     * @return string   Driver suffix
204
     */
205
    public function getDriverExtension()
206
    {
207
        return $this->extension;
208
    }
209
210
    /**
211
     *  getDriverVersion
212
     *
213
     * @return string  Driver version
214
     */
215
    public function getDriverVersion()
216
    {
217
        return $this->version;
218
    }
219
220
    /**
221
     *  getDriverLabel
222
     *
223
     * @return string  Label of external lib
224
     */
225
    public function getLibLabel()
226
    {
227
        return $this->label_lib;
228
    }
229
230
    /**
231
     * getLibVersion
232
     *
233
     * @return string  Version of external lib
234
     */
235
    public function getLibVersion()
236
    {
237
        return $this->version_lib;
238
    }
239
240
241
    /**
242
     *  Load into memory list of available import format
243
     *
244
     * @param DoliDB $db Database handler
245
     * @param integer $maxfilenamelength Max length of value to show
246
     * @return array                       List of templates
247
     */
248
    public function listOfAvailableImportFormat($db, $maxfilenamelength = 0)
249
    {
250
        dol_syslog(get_class($this) . "::listOfAvailableImportFormat");
251
252
        $dir = DOL_DOCUMENT_ROOT . "/core/modules/import/";
253
        $handle = opendir($dir);
254
255
        // Search list ov drivers available and qualified
256
        if (is_resource($handle)) {
257
            while (($file = readdir($handle)) !== false) {
258
                $reg = array();
259
                if (preg_match("/^import_(.*)\.modules\.php/i", $file, $reg)) {
260
                    $moduleid = $reg[1];
261
262
                    // Loading Class
263
                    $file = $dir . "/import_" . $moduleid . ".modules.php";
264
                    $classname = "Import" . ucfirst($moduleid);
265
266
                    require_once $file;
267
                    $module = new $classname($db, '');
268
269
                    // Picto
270
                    $this->picto[$module->id] = $module->picto;
271
                    // Driver properties
272
                    $this->driverlabel[$module->id] = $module->getDriverLabel('');
273
                    $this->driverdesc[$module->id] = $module->getDriverDesc('');
274
                    $this->driverversion[$module->id] = $module->getDriverVersion('');
275
                    $this->drivererror[$module->id] = $module->error ? $module->error : '';
276
                    // If use an external lib
277
                    $this->liblabel[$module->id] = ($module->error ? '<span class="error">' . $module->error . '</span>' : $module->getLibLabel(''));
278
                    $this->libversion[$module->id] = $module->getLibVersion('');
279
                }
280
            }
281
        }
282
283
        return array_keys($this->driverlabel);
284
    }
285
286
287
    /**
288
     *  Return picto of import driver
289
     *
290
     * @param string $key Key
291
     * @return string
292
     */
293
    public function getPictoForKey($key)
294
    {
295
        return $this->picto[$key];
296
    }
297
298
    /**
299
     *  Return label of driver import
300
     *
301
     * @param string $key Key
302
     * @return string
303
     */
304
    public function getDriverLabelForKey($key)
305
    {
306
        return $this->driverlabel[$key];
307
    }
308
309
    /**
310
     *  Return description of import drivervoi la description d'un driver import
311
     *
312
     * @param string $key Key
313
     * @return string
314
     */
315
    public function getDriverDescForKey($key)
316
    {
317
        return $this->driverdesc[$key];
318
    }
319
320
    /**
321
     *  Renvoi version d'un driver import
322
     *
323
     * @param string $key Key
324
     * @return string
325
     */
326
    public function getDriverVersionForKey($key)
327
    {
328
        return $this->driverversion[$key];
329
    }
330
331
    /**
332
     *  Renvoi libelle de librairie externe du driver
333
     *
334
     * @param string $key Key
335
     * @return string
336
     */
337
    public function getLibLabelForKey($key)
338
    {
339
        return $this->liblabel[$key];
340
    }
341
342
    /**
343
     *  Renvoi version de librairie externe du driver
344
     *
345
     * @param string $key Key
346
     * @return string
347
     */
348
    public function getLibVersionForKey($key)
349
    {
350
        return $this->libversion[$key];
351
    }
352
353
    /**
354
     * Get element from table name with prefix
355
     *
356
     * @param string $tableNameWithPrefix Table name with prefix
357
     * @return  string  Element name or table element as default
358
     */
359
    public function getElementFromTableWithPrefix($tableNameWithPrefix)
360
    {
361
        $tableElement = preg_replace('/^' . preg_quote($this->db->prefix(), '/') . '/', '', $tableNameWithPrefix);
362
        $element = $tableElement;
363
364
        if (isset(self::$mapTableToElement[$tableElement])) {
365
            $element = self::$mapTableToElement[$tableElement];
366
        }
367
368
        return $element;
369
    }
370
}
371