dolibarr  20.0.0-beta
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
30 class Import
31 {
35  public $db;
36 
40  public $error = '';
41 
45  public $errors = array();
46 
50  public $errno;
51 
55  public $array_import_module;
56 
60  public $array_import_perms;
61 
65  public $array_import_icon;
66 
70  public $array_import_code;
71 
75  public $array_import_label;
76 
80  public $array_import_tables;
81 
85  public $array_import_tables_creator;
86 
90  public $array_import_fields;
91 
95  public $array_import_fieldshidden;
96 
100  public $array_import_entities;
101 
105  public $array_import_regex;
106 
110  public $array_import_updatekeys;
111 
115  public $array_import_preselected_updatekeys;
116 
120  public $array_import_examplevalues;
121 
125  public $array_import_convertvalue;
126 
130  public $array_import_run_sql_after;
131 
132  // To store import templates
133  public $id;
134  public $hexa; // List of fields in the export profile
135  public $datatoimport;
136 
140  public $model_name;
141 
145  public $fk_user;
146 
147 
153  public function __construct($db)
154  {
155  $this->db = $db;
156  }
157 
158 
159  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
167  public function load_arrays($user, $filter = '')
168  {
169  // phpcs:enable
170  global $langs, $conf;
171 
172  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
173 
174  $i = 0;
175 
176  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
177  $modulesdir = dolGetModulesDirs();
178 
179  // Load list of modules
180  foreach ($modulesdir as $dir) {
181  $handle = @opendir(dol_osencode($dir));
182  if (!is_resource($handle)) {
183  continue;
184  }
185 
186  // Search module files
187  while (($file = readdir($handle)) !== false) {
188  if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
189  continue;
190  }
191 
192  $modulename = $reg[1];
193 
194  // Defined if module is enabled
195  $enabled = true;
196  $part = strtolower(preg_replace('/^mod/i', '', $modulename));
197  // Adds condition for propal module
198  if ($part === 'propale') {
199  $part = 'propal';
200  }
201  if (empty($conf->$part->enabled)) {
202  $enabled = false;
203  }
204 
205  if (empty($enabled)) {
206  continue;
207  }
208 
209  // Init load class
210  $file = $dir."/".$modulename.".class.php";
211  $classname = $modulename;
212  require_once $file;
213  $module = new $classname($this->db);
214 
215  if (isset($module->import_code) && is_array($module->import_code)) {
216  foreach ($module->import_code as $r => $value) {
217  if ($filter && ($filter != $module->import_code[$r])) {
218  continue;
219  }
220 
221  // Test if permissions are ok
222  /*$perm=$module->import_permission[$r][0];
223  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
224  if ($perm[2])
225  {
226  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
227  }
228  else
229  {
230  $bool=$user->rights->{$perm[0]}->{$perm[1]};
231  }
232  if ($perm[0]=='user' && $user->admin) $bool=true;
233  //print $bool." $perm[0]"."<br>";
234  */
235 
236  // Load lang file
237  $langtoload = $module->getLangFilesArray();
238  if (is_array($langtoload)) {
239  foreach ($langtoload as $key) {
240  $langs->load($key);
241  }
242  }
243 
244  // Permission
245  $this->array_import_perms[$i] = $user->hasRight('import', 'run');
246  // Icon
247  $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
248  // Code of dataset export
249  $this->array_import_code[$i] = $module->import_code[$r];
250  // Label of dataset export
251  $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
252  // Array of tables to import (key=alias, value=tablename)
253  $this->array_import_tables[$i] = $module->import_tables_array[$r];
254  // Array of tables creator field to import (key=alias, value=creator field name)
255  $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
256  // Array of fields to import (key=field, value=label)
257  $this->array_import_fields[$i] = $module->import_fields_array[$r];
258  // Array of hidden fields to import (key=field, value=label)
259  $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
260  // Array of entiteis to export (key=field, value=entity)
261  $this->array_import_entities[$i] = $module->import_entities_array[$r];
262  // Array of aliases to export (key=field, value=alias)
263  $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
264  // Array of columns allowed as UPDATE options
265  $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
266  // Array of columns preselected as UPDATE options
267  $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
268  // Array of examples
269  $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
270  // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
271  $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
272  // Sql request to run after import
273  $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
274  // Module
275  $this->array_import_module[$i] = array('position_of_profile'=>($module->module_position.'-'.$module->import_code[$r]), 'module'=>$module);
276 
277  dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
278  $i++;
279  }
280  }
281  }
282  closedir($handle);
283  }
284  return 1;
285  }
286 
287 
288 
289  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
300  public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
301  {
302  // phpcs:enable
303  global $conf, $langs;
304 
305  $indice = 0;
306 
307  dol_syslog(get_class($this)."::build_example_file ".$model);
308 
309  // Create the import class for the model Import_XXX
310  $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
311  $file = "import_".$model.".modules.php";
312  $classname = "Import".$model;
313  require_once $dir.$file;
314  $objmodel = new $classname($this->db, $datatoimport);
315 
316  $outputlangs = $langs; // Lang for output
317  $s = '';
318 
319  // Genere en-tete
320  $s .= $objmodel->write_header_example($outputlangs);
321 
322  // Genere ligne de titre
323  $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
324 
325  // Genere ligne de titre
326  $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
327 
328  // Genere pied de page
329  $s .= $objmodel->write_footer_example($outputlangs);
330 
331  return $s;
332  }
333 
340  public function create($user)
341  {
342  global $conf;
343 
344  dol_syslog("Import.class.php::create");
345 
346  // Check parameters
347  if (empty($this->model_name)) {
348  $this->error = 'ErrorWrongParameters';
349  return -1;
350  }
351  if (empty($this->datatoimport)) {
352  $this->error = 'ErrorWrongParameters';
353  return -1;
354  }
355  if (empty($this->hexa)) {
356  $this->error = 'ErrorWrongParameters';
357  return -1;
358  }
359 
360  $this->db->begin();
361 
362  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
363  $sql .= 'fk_user,';
364  $sql .= ' label,';
365  $sql .= ' type,';
366  $sql .= ' field';
367  $sql .= ')';
368  $sql .= " VALUES (";
369  $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
370  $sql .= " '".$this->db->escape($this->model_name)."',";
371  $sql .= " '".$this->db->escape($this->datatoimport)."',";
372  $sql .= " '".$this->db->escape($this->hexa)."'";
373  $sql .= ")";
374 
375  $resql = $this->db->query($sql);
376  if ($resql) {
377  $this->db->commit();
378  return 1;
379  } else {
380  $this->error = $this->db->lasterror();
381  $this->errno = $this->db->lasterrno();
382  $this->db->rollback();
383  return -1;
384  }
385  }
386 
393  public function fetch($id)
394  {
395  $sql = 'SELECT em.rowid, em.field, em.label, em.type';
396  $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
397  $sql .= ' WHERE em.rowid = '.((int) $id);
398 
399  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
400  $result = $this->db->query($sql);
401  if ($result) {
402  $obj = $this->db->fetch_object($result);
403  if ($obj) {
404  $this->id = $obj->rowid;
405  $this->hexa = $obj->field;
406  $this->model_name = $obj->label;
407  $this->datatoimport = $obj->type;
408  $this->fk_user = $obj->fk_user;
409  return 1;
410  } else {
411  $this->error = "Model not found";
412  return -2;
413  }
414  } else {
415  dol_print_error($this->db);
416  return -3;
417  }
418  }
419 
427  public function delete($user, $notrigger = 0)
428  {
429  $error = 0;
430 
431  $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
432  $sql .= " WHERE rowid=".((int) $this->id);
433 
434  $this->db->begin();
435 
436  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
437  $resql = $this->db->query($sql);
438  if (!$resql) {
439  $error++;
440  $this->errors[] = "Error ".$this->db->lasterror();
441  }
442 
443  if (!$error) {
444  if (!$notrigger) {
445  /* Not used. This is not a business object. To convert it we must herit from CommonObject
446  // Call trigger
447  $result=$this->call_trigger('IMPORT_DELETE',$user);
448  if ($result < 0) $error++;
449  // End call triggers
450  */
451  }
452  }
453 
454  // Commit or rollback
455  if ($error) {
456  foreach ($this->errors as $errmsg) {
457  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
458  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
459  }
460  $this->db->rollback();
461  return -1 * $error;
462  } else {
463  $this->db->commit();
464  return 1;
465  }
466  }
467 }
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 export model in database.
load_arrays($user, $filter='')
Load description int this->array_import_module, this->array_import_fields, ...
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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.