dolibarr 21.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-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 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 if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
199 continue;
200 }
201
202 $modulename = $reg[1];
203
204 // Defined if module is enabled
205 $enabled = true;
206 $part = strtolower(preg_replace('/^mod/i', '', $modulename));
207 // Adds condition for propal module
208 if ($part === 'propale') {
209 $part = 'propal';
210 }
211 if (empty($conf->$part->enabled)) {
212 $enabled = false;
213 }
214
215 if (empty($enabled)) {
216 continue;
217 }
218
219 // Init load class
220 $file = $dir."/".$modulename.".class.php";
221 $classname = $modulename;
222 require_once $file;
223 $module = new $classname($this->db);
224 '@phan-var-force DolibarrModules $module';
225
226 if (isset($module->import_code) && is_array($module->import_code)) {
227 foreach ($module->import_code as $r => $value) { // @phan-suppress-current-line PhanTypeMismatchForeach
228 if ($filter && ($filter != $module->import_code[$r])) {
229 continue;
230 }
231
232 // Test if permissions are ok
233 /*$perm=$module->import_permission[$r][0];
234 //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
235 if ($perm[2])
236 {
237 $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
238 }
239 else
240 {
241 $bool=$user->rights->{$perm[0]}->{$perm[1]};
242 }
243 if ($perm[0]=='user' && $user->admin) $bool=true;
244 //print $bool." $perm[0]"."<br>";
245 */
246
247 // Load lang file
248 $langtoload = $module->getLangFilesArray();
249 if (is_array($langtoload)) {
250 foreach ($langtoload as $key) {
251 $langs->load($key);
252 }
253 }
254
255 // Permission
256 $this->array_import_perms[$i] = $user->hasRight('import', 'run');
257 // Icon
258 $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
259 // Code of dataset export
260 $this->array_import_code[$i] = $module->import_code[$r];
261 // Label of dataset export
262 $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
263 // Array of tables to import (key=alias, value=tablename)
264 $this->array_import_tables[$i] = $module->import_tables_array[$r];
265 // Array of tables creator field to import (key=alias, value=creator field name)
266 $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
267 // Array of fields to import (key=field, value=label)
268 $this->array_import_fields[$i] = $module->import_fields_array[$r];
269 // Array of hidden fields to import (key=field, value=label)
270 $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
271 // Array of entities to export (key=field, value=entity)
272 $this->array_import_entities[$i] = $module->import_entities_array[$r];
273 // Array of aliases to export (key=field, value=alias)
274 $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
275 // Array of columns allowed as UPDATE options
276 $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
277 // Array of columns preselected as UPDATE options
278 // import_preselected_updatekeys_array does not exist - backward compatibility ? @phan-suppress-next-line PhanUndeclaredProperty
279 $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
280 // Array of examples
281 $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
282 // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
283 $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
284 // Sql request to run after import
285 $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
286 // Module
287 $this->array_import_module[$i] = array('position_of_profile' => ($module->module_position.'-'.$module->import_code[$r]), 'module' => $module);
288
289 dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
290 $i++;
291 }
292 }
293 }
294 closedir($handle);
295 }
296 return 1;
297 }
298
299
300
301 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
312 public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
313 {
314 // phpcs:enable
315 global $conf, $langs;
316
317 $indice = 0;
318
319 dol_syslog(get_class($this)."::build_example_file ".$model);
320
321 // Create the import class for the model Import_XXX
322 $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
323 $file = "import_".$model.".modules.php";
324 $classname = "Import".$model;
325 require_once $dir.$file;
326 $objmodel = new $classname($this->db, $datatoimport);
327 '@phan-var-force CommonObject $objmodel';
328
329 $outputlangs = $langs; // Lang for output
330 $s = '';
331
332 // Genere en-tete
333 $s .= $objmodel->write_header_example($outputlangs);
334
335 // Genere ligne de titre
336 $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
337
338 // Genere ligne de titre
339 $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
340
341 // Genere pied de page
342 $s .= $objmodel->write_footer_example($outputlangs);
343
344 return $s;
345 }
346
353 public function create($user)
354 {
355 global $conf;
356
357 dol_syslog("Import.class.php::create");
358
359 // Check parameters
360 if (empty($this->model_name)) {
361 $this->error = 'ErrorWrongParameters';
362 return -1;
363 }
364 if (empty($this->datatoimport)) {
365 $this->error = 'ErrorWrongParameters';
366 return -1;
367 }
368 if (empty($this->hexa)) {
369 $this->error = 'ErrorWrongParameters';
370 return -1;
371 }
372
373 $this->db->begin();
374
375 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
376 $sql .= 'fk_user,';
377 $sql .= ' label,';
378 $sql .= ' type,';
379 $sql .= ' field';
380 $sql .= ')';
381 $sql .= " VALUES (";
382 $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
383 $sql .= " '".$this->db->escape($this->model_name)."',";
384 $sql .= " '".$this->db->escape($this->datatoimport)."',";
385 $sql .= " '".$this->db->escape($this->hexa)."'";
386 $sql .= ")";
387
388 $resql = $this->db->query($sql);
389 if ($resql) {
390 $this->db->commit();
391 return 1;
392 } else {
393 $this->error = $this->db->lasterror();
394 $this->errno = $this->db->lasterrno();
395 $this->db->rollback();
396 return -1;
397 }
398 }
399
406 public function fetch($id)
407 {
408 $sql = 'SELECT em.rowid, em.field, em.label, em.type';
409 $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
410 $sql .= ' WHERE em.rowid = '.((int) $id);
411
412 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
413 $result = $this->db->query($sql);
414 if ($result) {
415 $obj = $this->db->fetch_object($result);
416 if ($obj) {
417 $this->id = $obj->rowid;
418 $this->hexa = $obj->field;
419 $this->model_name = $obj->label;
420 $this->datatoimport = $obj->type;
421 $this->fk_user = $obj->fk_user;
422 return 1;
423 } else {
424 $this->error = "Model not found";
425 return -2;
426 }
427 } else {
428 dol_print_error($this->db);
429 return -3;
430 }
431 }
432
440 public function delete($user, $notrigger = 0)
441 {
442 $error = 0;
443
444 $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
445 $sql .= " WHERE rowid=".((int) $this->id);
446
447 $this->db->begin();
448
449 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
450 $resql = $this->db->query($sql);
451 if (!$resql) {
452 $error++;
453 $this->errors[] = "Error ".$this->db->lasterror();
454 }
455
456 if (!$error) {
457 if (!$notrigger) {
458 /* Not used. This is not a business object. To convert it we must herit from CommonObject
459 // Call trigger
460 $result=$this->call_trigger('IMPORT_DELETE',$user);
461 if ($result < 0) $error++;
462 // End call triggers
463 */
464 }
465 }
466
467 // Commit or rollback
468 if ($error) {
469 foreach ($this->errors as $errmsg) {
470 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
471 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
472 }
473 $this->db->rollback();
474 return -1 * $error;
475 } else {
476 $this->db->commit();
477 return 1;
478 }
479 }
480}
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.