dolibarr 22.0.5
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
31class Import
32{
36 public $db;
37
41 public $error = '';
42
46 public $errors = array();
47
51 public $errno;
52
56 public $array_import_module;
57
61 public $array_import_perms;
62
66 public $array_import_icon;
67
71 public $array_import_code;
72
76 public $array_import_label;
77
81 public $array_import_tables;
82
86 public $array_import_tables_creator;
87
91 public $array_import_fields;
92
96 public $array_import_fieldshidden;
97
101 public $array_import_entities;
102
106 public $array_import_regex;
107
111 public $array_import_updatekeys;
112
116 public $array_import_preselected_updatekeys;
117
121 public $array_import_examplevalues;
122
126 public $array_import_convertvalue;
127
131 public $array_import_run_sql_after;
132
133 // To store import templates
137 public $id;
141 public $hexa; // List of fields in the export profile
145 public $datatoimport;
146
150 public $model_name;
151
155 public $fk_user;
156
157
163 public function __construct($db)
164 {
165 $this->db = $db;
166 }
167
168
169 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
177 public function load_arrays($user, $filter = '')
178 {
179 // phpcs:enable
180 global $langs, $conf;
181
182 dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
183
184 $i = 0;
185
186 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
187 $modulesdir = dolGetModulesDirs();
188
189 // Load list of modules
190 foreach ($modulesdir as $dir) {
191 $handle = @opendir(dol_osencode($dir));
192 if (!is_resource($handle)) {
193 continue;
194 }
195
196 // Search module files
197 while (($file = readdir($handle)) !== false) {
198 // Ignore Module Builder backup files (*.php.back)
199 if (preg_match('/\.back$/i', $file)) {
200 continue;
201 }
202
203 if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
204 continue;
205 }
206
207 $modulename = $reg[1];
208
209 // Defined if module is enabled
210 $enabled = true;
211 $part = strtolower(preg_replace('/^mod/i', '', $modulename));
212 // Adds condition for propal module
213 if ($part === 'propale') {
214 $part = 'propal';
215 }
216 if (empty($conf->$part->enabled)) {
217 $enabled = false;
218 }
219
220 if (empty($enabled)) {
221 continue;
222 }
223
224 // Init load class
225 $file = $dir."/".$modulename.".class.php";
226 $classname = $modulename;
227 require_once $file;
228 $module = new $classname($this->db);
229 '@phan-var-force DolibarrModules $module';
230
231 if (isset($module->import_code) && is_array($module->import_code)) {
232 foreach ($module->import_code as $r => $value) { // @phan-suppress-current-line PhanTypeMismatchForeach
233 if ($filter && ($filter != $module->import_code[$r])) {
234 continue;
235 }
236
237 // Test if permissions are ok
238 /*$perm=$module->import_permission[$r][0];
239 //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
240 if ($perm[2])
241 {
242 $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
243 }
244 else
245 {
246 $bool=$user->rights->{$perm[0]}->{$perm[1]};
247 }
248 if ($perm[0]=='user' && $user->admin) $bool=true;
249 //print $bool." $perm[0]"."<br>";
250 */
251
252 // Load lang file
253 $langtoload = $module->getLangFilesArray();
254 if (is_array($langtoload)) {
255 foreach ($langtoload as $key) {
256 $langs->load($key);
257 }
258 }
259
260 // Permission
261 $this->array_import_perms[$i] = $user->hasRight('import', 'run');
262 // Icon
263 $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
264 // Code of dataset export
265 $this->array_import_code[$i] = $module->import_code[$r];
266 // Label of dataset export
267 $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
268 // Array of tables to import (key=alias, value=tablename)
269 $this->array_import_tables[$i] = $module->import_tables_array[$r];
270 // Array of tables creator field to import (key=alias, value=creator field name)
271 $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
272 // Array of fields to import (key=field, value=label)
273 $this->array_import_fields[$i] = $module->import_fields_array[$r];
274 // Array of hidden fields to import (key=field, value=label)
275 $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
276 // Array of entities to export (key=field, value=entity)
277 $this->array_import_entities[$i] = $module->import_entities_array[$r];
278 // Array of aliases to export (key=field, value=alias)
279 $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
280 // Array of columns allowed as UPDATE options
281 $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
282 // Array of columns preselected as UPDATE options
283 // import_preselected_updatekeys_array does not exist - backward compatibility ? @phan-suppress-next-line PhanUndeclaredProperty
284 $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
285 // Array of examples
286 $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
287 // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
288 $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
289 // Sql request to run after import
290 $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
291 // Module
292 $this->array_import_module[$i] = array('position_of_profile' => ($module->module_position.'-'.$module->import_code[$r]), 'module' => $module);
293
294 dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
295 $i++;
296 }
297 }
298 }
299 closedir($handle);
300 }
301 return 1;
302 }
303
304
305
306 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
317 public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
318 {
319 // phpcs:enable
320 global $conf, $langs;
321
322 $indice = 0;
323
324 dol_syslog(get_class($this)."::build_example_file ".$model);
325
326 // Create the import class for the model Import_XXX
327 $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
328 $file = "import_".$model.".modules.php";
329 $classname = "Import".$model;
330 require_once $dir.$file;
331 $objmodel = new $classname($this->db, $datatoimport);
332 '@phan-var-force ModeleImports $objmodel';
333
334 $outputlangs = $langs; // Lang for output
335 $s = '';
336
337 // Genere en-tete
338 $s .= $objmodel->write_header_example($outputlangs);
339
340 // Genere ligne de titre
341 $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
342
343 // Genere ligne de titre
344 $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
345
346 // Genere pied de page
347 $s .= $objmodel->write_footer_example($outputlangs);
348
349 return $s;
350 }
351
358 public function create($user)
359 {
360 dol_syslog("Import.class.php::create");
361
362 // Check parameters
363 if (empty($this->model_name)) {
364 $this->error = 'ErrorWrongParameters';
365 return -1;
366 }
367 if (empty($this->datatoimport)) {
368 $this->error = 'ErrorWrongParameters';
369 return -1;
370 }
371 if (empty($this->hexa)) {
372 $this->error = 'ErrorWrongParameters';
373 return -1;
374 }
375
376 $this->db->begin();
377
378 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
379 $sql .= 'fk_user,';
380 $sql .= ' label,';
381 $sql .= ' type,';
382 $sql .= ' field';
383 $sql .= ')';
384 $sql .= " VALUES (";
385 $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
386 $sql .= " '".$this->db->escape($this->model_name)."',";
387 $sql .= " '".$this->db->escape($this->datatoimport)."',";
388 $sql .= " '".$this->db->escape($this->hexa)."'";
389 $sql .= ")";
390
391 $resql = $this->db->query($sql);
392 if ($resql) {
393 $this->db->commit();
394 return 1;
395 } else {
396 $this->error = $this->db->lasterror();
397 $this->errno = $this->db->lasterrno();
398 $this->db->rollback();
399 return -1;
400 }
401 }
402
409 public function fetch($id)
410 {
411 $sql = 'SELECT em.rowid, em.field, em.label, em.type';
412 $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
413 $sql .= ' WHERE em.rowid = '.((int) $id);
414
415 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
416 $result = $this->db->query($sql);
417 if ($result) {
418 $obj = $this->db->fetch_object($result);
419 if ($obj) {
420 $this->id = $obj->rowid;
421 $this->hexa = $obj->field;
422 $this->model_name = $obj->label;
423 $this->datatoimport = $obj->type;
424 $this->fk_user = $obj->fk_user;
425 return 1;
426 } else {
427 $this->error = "Model not found";
428 return -2;
429 }
430 } else {
431 dol_print_error($this->db);
432 return -3;
433 }
434 }
435
443 public function delete($user, $notrigger = 0)
444 {
445 $error = 0;
446
447 $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
448 $sql .= " WHERE rowid=".((int) $this->id);
449
450 $this->db->begin();
451
452 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
453 $resql = $this->db->query($sql);
454 if (!$resql) {
455 $error++;
456 $this->errors[] = "Error ".$this->db->lasterror();
457 }
458
459 /* Not used. This is not a business object. To convert it we must herit from CommonObject
460 if (!$error) {
461 // Call trigger
462 $result=$this->call_trigger('IMPORT_DELETE',$user);
463 if ($result < 0) $error++;
464 // End call triggers
465 }
466 }
467 */
468
469 // Commit or rollback
470 if ($error) {
471 foreach ($this->errors as $errmsg) {
472 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
473 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
474 }
475 $this->db->rollback();
476 return -1 * $error;
477 } else {
478 $this->db->commit();
479 return 1;
480 }
481 }
482}
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79