dolibarr 25.0.0-alpha
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-2025 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_types;
63
67 public $array_import_perms;
68
72 public $array_import_icon;
73
77 public $array_import_code;
78
82 public $array_import_label;
83
87 public $array_import_tables;
88
92 public $array_import_tables_creator;
93
97 public $array_import_fields;
98
102 public $array_import_fieldshidden;
103
107 public $array_import_entities;
108
112 public $array_import_regex;
113
117 public $array_import_updatekeys;
118
122 public $array_import_preselected_updatekeys;
123
127 public $array_import_examplevalues;
128
132 public $array_import_convertvalue;
133
137 public $array_import_run_sql_after;
138
139 // To store import templates
143 public $id;
147 public $hexa; // List of fields in the export profile
151 public $datatoimport;
152
156 public $model_name;
157
161 public $fk_user;
162
163
169 public function __construct($db)
170 {
171 $this->db = $db;
172 }
173
174
175 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
183 public function load_arrays($user, $filter = '')
184 {
185 // phpcs:enable
186 global $langs, $conf;
187
188 dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
189
190 $i = 0;
191
192 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
193 $modulesdir = dolGetModulesDirs();
194
195 // Load list of modules
196 foreach ($modulesdir as $dir) {
197 $handle = @opendir(dol_osencode($dir));
198 if (!is_resource($handle)) {
199 continue;
200 }
201
202 // Search module files
203 while (($file = readdir($handle)) !== false) {
204 // Ignore Module Builder backup files (*.php.back)
205 if (preg_match('/\.back$/i', $file)) {
206 continue;
207 }
208
209 if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
210 continue;
211 }
212
213 $modulename = $reg[1];
214
215 // Defined if module is enabled
216 $enabled = true;
217 $part = strtolower(preg_replace('/^mod/i', '', $modulename));
218 // Adds condition for propal module
219 if ($part === 'propale') {
220 $part = 'propal';
221 }
222 if (empty($conf->$part->enabled)) {
223 $enabled = false;
224 }
225
226 if (empty($enabled)) {
227 continue;
228 }
229
230 // Init load class
231 $file = $dir."/".$modulename.".class.php";
232 $classname = $modulename;
233 require_once $file;
234 $module = new $classname($this->db);
235 '@phan-var-force DolibarrModules $module';
236
237 if (isset($module->import_code) && is_array($module->import_code)) {
238 foreach ($module->import_code as $r => $value) { // @phan-suppress-current-line PhanTypeMismatchForeach
239 if ($filter && ($filter != $module->import_code[$r])) {
240 continue;
241 }
242
243 // Test if permissions are ok
244 /*$perm=$module->import_permission[$r][0];
245 //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
246 if ($perm[2])
247 {
248 $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
249 }
250 else
251 {
252 $bool=$user->rights->{$perm[0]}->{$perm[1]};
253 }
254 if ($perm[0]=='user' && $user->admin) $bool=true;
255 //print $bool." $perm[0]"."<br>";
256 */
257
258 // Load lang file
259 $langtoload = $module->getLangFilesArray();
260 if (is_array($langtoload)) {
261 foreach ($langtoload as $key) {
262 $langs->load($key);
263 }
264 }
265
266 // Permission
267 $this->array_import_perms[$i] = $user->hasRight('import', 'run');
268 // Icon
269 $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
270 // Code of dataset export
271 $this->array_import_code[$i] = $module->import_code[$r];
272 // Label of dataset export
273 $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
274 // Array of tables to import (key=alias, value=tablename)
275 $this->array_import_tables[$i] = $module->import_tables_array[$r];
276 // Array of tables creator field to import (key=alias, value=creator field name)
277 $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
278 // Array of fields to import (key=field, value=label)
279 $this->array_import_fields[$i] = (isset($module->import_fields_array[$r]) ? $module->import_fields_array[$r] : []);
280 // Array of hidden fields to import (key=field, value=label)
281 $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
282 // Array of entities to export (key=field, value=entity)
283 $this->array_import_entities[$i] = (isset($module->import_entities_array[$r]) ? $module->import_entities_array[$r] : '');
284 // Array of aliases to export (key=field, value=alias)
285 $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
286 // Array of columns allowed as UPDATE options
287 $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
288 // Array of columns preselected as UPDATE options
289 // import_preselected_updatekeys_array does not exist - backward compatibility ? @phan-suppress-next-line PhanUndeclaredProperty
290 $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
291 // Array of examples
292 $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
293 // Table of conversion rules for a value from another source (key=field, value=array of rules)
294 $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
295 // Sql request to run after import
296 $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
297 // Module
298 $this->array_import_module[$i] = array('position_of_profile' => ($module->module_position.'-'.$module->import_code[$r]), 'module' => $module);
299 // Type
300 $this->array_import_types[$i] = (isset($module->import_TypeFields_array[$r]) ? $module->import_TypeFields_array[$r] : array());
301
302 dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
303 $i++;
304 }
305 }
306 }
307 closedir($handle);
308 }
309 return 1;
310 }
311
312
313
314 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
325 public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
326 {
327 // phpcs:enable
328 global $conf, $langs;
329
330 $indice = 0;
331
332 dol_syslog(get_class($this)."::build_example_file ".$model);
333
334 // Create the import class for the model Import_XXX
335 $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
336 $file = "import_".$model.".modules.php";
337 $classname = "Import".$model;
338 require_once $dir.$file;
339 $objmodel = new $classname($this->db, $datatoimport);
340 '@phan-var-force ModeleImports $objmodel';
341
342 $outputlangs = $langs; // Lang for output
343 $s = '';
344
345 // Generate header
346 $s .= $objmodel->write_header_example($outputlangs);
347
348 // Generate title line
349 $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
350
351 // Generate record line
352 $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
353
354 // Generate footer
355 $s .= $objmodel->write_footer_example($outputlangs);
356
357 return $s;
358 }
359
366 public function create($user)
367 {
368 dol_syslog("Import.class.php::create");
369
370 // Check parameters
371 if (empty($this->model_name)) {
372 $this->error = 'ErrorWrongParameters';
373 return -1;
374 }
375 if (empty($this->datatoimport)) {
376 $this->error = 'ErrorWrongParameters';
377 return -1;
378 }
379 if (empty($this->hexa)) {
380 $this->error = 'ErrorWrongParameters';
381 return -1;
382 }
383
384 $this->db->begin();
385
386 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
387 $sql .= 'fk_user,';
388 $sql .= ' label,';
389 $sql .= ' type,';
390 $sql .= ' field';
391 $sql .= ')';
392 $sql .= " VALUES (";
393 $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
394 $sql .= " '".$this->db->escape($this->model_name)."',";
395 $sql .= " '".$this->db->escape($this->datatoimport)."',";
396 $sql .= " '".$this->db->escape($this->hexa)."'";
397 $sql .= ")";
398
399 $resql = $this->db->query($sql);
400 if ($resql) {
401 $this->db->commit();
402 return 1;
403 } else {
404 $this->error = $this->db->lasterror();
405 $this->errno = $this->db->lasterrno();
406 $this->db->rollback();
407 return -1;
408 }
409 }
410
417 public function fetch($id)
418 {
419 $sql = 'SELECT em.rowid, em.field, em.label, em.type';
420 $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
421 $sql .= ' WHERE em.rowid = '.((int) $id);
422
423 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
424 $result = $this->db->query($sql);
425 if ($result) {
426 $obj = $this->db->fetch_object($result);
427 if ($obj) {
428 $this->id = $obj->rowid;
429 $this->hexa = $obj->field;
430 $this->model_name = $obj->label;
431 $this->datatoimport = $obj->type;
432 $this->fk_user = $obj->fk_user;
433 return 1;
434 } else {
435 $this->error = "Model not found";
436 return -2;
437 }
438 } else {
439 dol_print_error($this->db);
440 return -3;
441 }
442 }
443
451 public function delete($user, $notrigger = 0)
452 {
453 $error = 0;
454
455 $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
456 $sql .= " WHERE rowid=".((int) $this->id);
457
458 $this->db->begin();
459
460 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
461 $resql = $this->db->query($sql);
462 if (!$resql) {
463 $error++;
464 $this->errors[] = "Error ".$this->db->lasterror();
465 }
466
467 /* Not used. This is not a business object. To convert it we must herit from CommonObject
468 if (!$error) {
469 // Call trigger
470 $result=$this->call_trigger('IMPORT_DELETE',$user);
471 if ($result < 0) $error++;
472 // End call triggers
473 }
474 }
475 */
476
477 // Commit or rollback
478 if ($error) {
479 foreach ($this->errors as $errmsg) {
480 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
481 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
482 }
483 $this->db->rollback();
484 return -1 * $error;
485 } else {
486 $this->db->commit();
487 return 1;
488 }
489 }
490}
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...
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.