dolibarr 23.0.3
import.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
4 * Copyright (C) 2020 Ahmad Jamaly Rabib <rabib@metroworks.co.jp>
5 * Copyright (C) 2021-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
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 */
22
32class Import
33{
37 public $db;
38
42 public $error = '';
43
47 public $errors = array();
48
52 public $errno;
53
57 public $array_import_module;
58
62 public $array_import_perms;
63
67 public $array_import_icon;
68
72 public $array_import_code;
73
77 public $array_import_label;
78
82 public $array_import_tables;
83
87 public $array_import_tables_creator;
88
92 public $array_import_fields;
93
97 public $array_import_fieldshidden;
98
102 public $array_import_entities;
103
107 public $array_import_regex;
108
112 public $array_import_updatekeys;
113
117 public $array_import_preselected_updatekeys;
118
122 public $array_import_examplevalues;
123
127 public $array_import_convertvalue;
128
132 public $array_import_run_sql_after;
133
134 // To store import templates
138 public $id;
142 public $hexa; // List of fields in the export profile
146 public $datatoimport;
147
151 public $model_name;
152
156 public $fk_user;
157
158
164 public function __construct($db)
165 {
166 $this->db = $db;
167 }
168
169
170 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
178 public function load_arrays($user, $filter = '')
179 {
180 // phpcs:enable
181 global $langs, $conf;
182
183 dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
184
185 $i = 0;
186
187 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
188 $modulesdir = dolGetModulesDirs();
189
190 // Load list of modules
191 foreach ($modulesdir as $dir) {
192 $handle = @opendir(dol_osencode($dir));
193 if (!is_resource($handle)) {
194 continue;
195 }
196
197 // Search module files
198 while (($file = readdir($handle)) !== false) {
199 // Ignore Module Builder backup files (*.php.back)
200 if (preg_match('/\.back$/i', $file)) {
201 continue;
202 }
203
204 if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
205 continue;
206 }
207
208 $modulename = $reg[1];
209
210 // Defined if module is enabled
211 $enabled = true;
212 $part = strtolower(preg_replace('/^mod/i', '', $modulename));
213 // Adds condition for propal module
214 if ($part === 'propale') {
215 $part = 'propal';
216 }
217 if (empty($conf->$part->enabled)) {
218 $enabled = false;
219 }
220
221 if (empty($enabled)) {
222 continue;
223 }
224
225 // Init load class
226 $file = $dir."/".$modulename.".class.php";
227 $classname = $modulename;
228 require_once $file;
229 $module = new $classname($this->db);
230 '@phan-var-force DolibarrModules $module';
231
232 if (isset($module->import_code) && is_array($module->import_code)) {
233 foreach ($module->import_code as $r => $value) { // @phan-suppress-current-line PhanTypeMismatchForeach
234 if ($filter && ($filter != $module->import_code[$r])) {
235 continue;
236 }
237
238 // Test if permissions are ok
239 /*$perm=$module->import_permission[$r][0];
240 //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
241 if ($perm[2])
242 {
243 $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
244 }
245 else
246 {
247 $bool=$user->rights->{$perm[0]}->{$perm[1]};
248 }
249 if ($perm[0]=='user' && $user->admin) $bool=true;
250 //print $bool." $perm[0]"."<br>";
251 */
252
253 // Load lang file
254 $langtoload = $module->getLangFilesArray();
255 if (is_array($langtoload)) {
256 foreach ($langtoload as $key) {
257 $langs->load($key);
258 }
259 }
260
261 // Permission
262 $this->array_import_perms[$i] = $user->hasRight('import', 'run');
263 // Icon
264 $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
265 // Code of dataset export
266 $this->array_import_code[$i] = $module->import_code[$r];
267 // Label of dataset export
268 $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
269 // Array of tables to import (key=alias, value=tablename)
270 $this->array_import_tables[$i] = $module->import_tables_array[$r];
271 // Array of tables creator field to import (key=alias, value=creator field name)
272 $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
273 // Array of fields to import (key=field, value=label)
274 $this->array_import_fields[$i] = (isset($module->import_fields_array[$r]) ? $module->import_fields_array[$r] : []);
275 // Array of hidden fields to import (key=field, value=label)
276 $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
277 // Array of entities to export (key=field, value=entity)
278 $this->array_import_entities[$i] = (isset($module->import_entities_array[$r]) ? $module->import_entities_array[$r] : '');
279 // Array of aliases to export (key=field, value=alias)
280 $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
281 // Array of columns allowed as UPDATE options
282 $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
283 // Array of columns preselected as UPDATE options
284 // import_preselected_updatekeys_array does not exist - backward compatibility ? @phan-suppress-next-line PhanUndeclaredProperty
285 $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
286 // Array of examples
287 $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
288 // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
289 $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
290 // Sql request to run after import
291 $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
292 // Module
293 $this->array_import_module[$i] = array('position_of_profile' => ($module->module_position.'-'.$module->import_code[$r]), 'module' => $module);
294
295 dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
296 $i++;
297 }
298 }
299 }
300 closedir($handle);
301 }
302 return 1;
303 }
304
305
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
318 public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
319 {
320 // phpcs:enable
321 global $conf, $langs;
322
323 $indice = 0;
324
325 dol_syslog(get_class($this)."::build_example_file ".$model);
326
327 // Create the import class for the model Import_XXX
328 $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
329 $file = "import_".$model.".modules.php";
330 $classname = "Import".$model;
331 require_once $dir.$file;
332 $objmodel = new $classname($this->db, $datatoimport);
333 '@phan-var-force ModeleImports $objmodel';
334
335 $outputlangs = $langs; // Lang for output
336 $s = '';
337
338 // Genere en-tete
339 $s .= $objmodel->write_header_example($outputlangs);
340
341 // Genere ligne de titre
342 $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
343
344 // Genere ligne de titre
345 $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
346
347 // Genere pied de page
348 $s .= $objmodel->write_footer_example($outputlangs);
349
350 return $s;
351 }
352
359 public function create($user)
360 {
361 dol_syslog("Import.class.php::create");
362
363 // Check parameters
364 if (empty($this->model_name)) {
365 $this->error = 'ErrorWrongParameters';
366 return -1;
367 }
368 if (empty($this->datatoimport)) {
369 $this->error = 'ErrorWrongParameters';
370 return -1;
371 }
372 if (empty($this->hexa)) {
373 $this->error = 'ErrorWrongParameters';
374 return -1;
375 }
376
377 $this->db->begin();
378
379 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
380 $sql .= 'fk_user,';
381 $sql .= ' label,';
382 $sql .= ' type,';
383 $sql .= ' field';
384 $sql .= ')';
385 $sql .= " VALUES (";
386 $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
387 $sql .= " '".$this->db->escape($this->model_name)."',";
388 $sql .= " '".$this->db->escape($this->datatoimport)."',";
389 $sql .= " '".$this->db->escape($this->hexa)."'";
390 $sql .= ")";
391
392 $resql = $this->db->query($sql);
393 if ($resql) {
394 $this->db->commit();
395 return 1;
396 } else {
397 $this->error = $this->db->lasterror();
398 $this->errno = $this->db->lasterrno();
399 $this->db->rollback();
400 return -1;
401 }
402 }
403
410 public function fetch($id)
411 {
412 $sql = 'SELECT em.rowid, em.field, em.label, em.type';
413 $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
414 $sql .= ' WHERE em.rowid = '.((int) $id);
415
416 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
417 $result = $this->db->query($sql);
418 if ($result) {
419 $obj = $this->db->fetch_object($result);
420 if ($obj) {
421 $this->id = $obj->rowid;
422 $this->hexa = $obj->field;
423 $this->model_name = $obj->label;
424 $this->datatoimport = $obj->type;
425 $this->fk_user = $obj->fk_user;
426 return 1;
427 } else {
428 $this->error = "Model not found";
429 return -2;
430 }
431 } else {
432 dol_print_error($this->db);
433 return -3;
434 }
435 }
436
444 public function delete($user, $notrigger = 0)
445 {
446 $error = 0;
447
448 $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
449 $sql .= " WHERE rowid=".((int) $this->id);
450
451 $this->db->begin();
452
453 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
454 $resql = $this->db->query($sql);
455 if (!$resql) {
456 $error++;
457 $this->errors[] = "Error ".$this->db->lasterror();
458 }
459
460 /* Not used. This is not a business object. To convert it we must herit from CommonObject
461 if (!$error) {
462 // Call trigger
463 $result=$this->call_trigger('IMPORT_DELETE',$user);
464 if ($result < 0) $error++;
465 // End call triggers
466 }
467 }
468 */
469
470 // Commit or rollback
471 if ($error) {
472 foreach ($this->errors as $errmsg) {
473 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
474 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
475 }
476 $this->db->rollback();
477 return -1 * $error;
478 } else {
479 $this->db->commit();
480 return 1;
481 }
482 }
483}
Class to manage imports.
__construct($db)
Constructor.
fetch($id)
Load an import profil from database.
build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
Build an import example file.
create($user)
Save an import model in database.
load_arrays($user, $filter='')
Load description int this->array_import_module, this->array_import_fields, ... of an importable datas...
dolGetModulesDirs($subdir='')
Return list of directories that contain modules.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.