dolibarr  19.0.0-dev
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 Frédéric France <frederic.france@netlogic.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 
52  public $array_import_module;
53  public $array_import_perms;
54  public $array_import_icon;
55  public $array_import_code;
56  public $array_import_label;
57  public $array_import_tables;
58  public $array_import_tables_creator;
59  public $array_import_fields;
60  public $array_import_fieldshidden;
61  public $array_import_entities;
62  public $array_import_regex;
63  public $array_import_updatekeys;
64  public $array_import_examplevalues;
65  public $array_import_convertvalue;
66  public $array_import_run_sql_after;
67 
68  // To store import templates
69  public $id;
70  public $hexa; // List of fields in the export profile
71  public $datatoimport;
72  public $model_name; // Name of export profile
73  public $fk_user;
74 
75 
81  public function __construct($db)
82  {
83  $this->db = $db;
84  }
85 
86 
87  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
95  public function load_arrays($user, $filter = '')
96  {
97  // phpcs:enable
98  global $langs, $conf;
99 
100  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
101 
102  $i = 0;
103 
104  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
105  $modulesdir = dolGetModulesDirs();
106 
107  // Load list of modules
108  foreach ($modulesdir as $dir) {
109  $handle = @opendir(dol_osencode($dir));
110  if (!is_resource($handle)) {
111  continue;
112  }
113 
114  // Search module files
115  while (($file = readdir($handle)) !== false) {
116  if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
117  continue;
118  }
119 
120  $modulename = $reg[1];
121 
122  // Defined if module is enabled
123  $enabled = true;
124  $part = strtolower(preg_replace('/^mod/i', '', $modulename));
125  // Adds condition for propal module
126  if ($part === 'propale') {
127  $part = 'propal';
128  }
129  if (empty($conf->$part->enabled)) {
130  $enabled = false;
131  }
132 
133  if (empty($enabled)) {
134  continue;
135  }
136 
137  // Init load class
138  $file = $dir."/".$modulename.".class.php";
139  $classname = $modulename;
140  require_once $file;
141  $module = new $classname($this->db);
142 
143  if (isset($module->import_code) && is_array($module->import_code)) {
144  foreach ($module->import_code as $r => $value) {
145  if ($filter && ($filter != $module->import_code[$r])) {
146  continue;
147  }
148 
149  // Test if permissions are ok
150  /*$perm=$module->import_permission[$r][0];
151  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
152  if ($perm[2])
153  {
154  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
155  }
156  else
157  {
158  $bool=$user->rights->{$perm[0]}->{$perm[1]};
159  }
160  if ($perm[0]=='user' && $user->admin) $bool=true;
161  //print $bool." $perm[0]"."<br>";
162  */
163 
164  // Load lang file
165  $langtoload = $module->getLangFilesArray();
166  if (is_array($langtoload)) {
167  foreach ($langtoload as $key) {
168  $langs->load($key);
169  }
170  }
171 
172  // Permission
173  $this->array_import_perms[$i] = $user->rights->import->run;
174  // Icon
175  $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
176  // Code of dataset export
177  $this->array_import_code[$i] = $module->import_code[$r];
178  // Label of dataset export
179  $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
180  // Array of tables to import (key=alias, value=tablename)
181  $this->array_import_tables[$i] = $module->import_tables_array[$r];
182  // Array of tables creator field to import (key=alias, value=creator field name)
183  $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
184  // Array of fields to import (key=field, value=label)
185  $this->array_import_fields[$i] = $module->import_fields_array[$r];
186  // Array of hidden fields to import (key=field, value=label)
187  $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
188  // Tableau des entites a exporter (cle=champ, valeur=entite)
189  $this->array_import_entities[$i] = $module->import_entities_array[$r];
190  // Tableau des alias a exporter (cle=champ, valeur=alias)
191  $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
192  // Array of columns allowed as UPDATE options
193  $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
194  // Array of examples
195  $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
196  // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
197  $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
198  // Sql request to run after import
199  $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
200  // Module
201  $this->array_import_module[$i] = array('position_of_profile'=>($module->module_position.'-'.$module->import_code[$r]), 'module'=>$module);
202 
203  dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
204  $i++;
205  }
206  }
207  }
208  closedir($handle);
209  }
210  return 1;
211  }
212 
213 
214 
215  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
226  public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
227  {
228  // phpcs:enable
229  global $conf, $langs;
230 
231  $indice = 0;
232 
233  dol_syslog(get_class($this)."::build_example_file ".$model);
234 
235  // Creation de la classe d'import du model Import_XXX
236  $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
237  $file = "import_".$model.".modules.php";
238  $classname = "Import".$model;
239  require_once $dir.$file;
240  $objmodel = new $classname($this->db, $datatoimport);
241 
242  $outputlangs = $langs; // Lang for output
243  $s = '';
244 
245  // Genere en-tete
246  $s .= $objmodel->write_header_example($outputlangs);
247 
248  // Genere ligne de titre
249  $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
250 
251  // Genere ligne de titre
252  $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
253 
254  // Genere pied de page
255  $s .= $objmodel->write_footer_example($outputlangs);
256 
257  return $s;
258  }
259 
266  public function create($user)
267  {
268  global $conf;
269 
270  dol_syslog("Import.class.php::create");
271 
272  // Check parameters
273  if (empty($this->model_name)) {
274  $this->error = 'ErrorWrongParameters'; return -1;
275  }
276  if (empty($this->datatoimport)) {
277  $this->error = 'ErrorWrongParameters'; return -1;
278  }
279  if (empty($this->hexa)) {
280  $this->error = 'ErrorWrongParameters'; return -1;
281  }
282 
283  $this->db->begin();
284 
285  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
286  $sql .= 'fk_user,';
287  $sql .= ' label,';
288  $sql .= ' type,';
289  $sql .= ' field';
290  $sql .= ')';
291  $sql .= " VALUES (";
292  $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
293  $sql .= " '".$this->db->escape($this->model_name)."',";
294  $sql .= " '".$this->db->escape($this->datatoimport)."',";
295  $sql .= " '".$this->db->escape($this->hexa)."'";
296  $sql .= ")";
297 
298  $resql = $this->db->query($sql);
299  if ($resql) {
300  $this->db->commit();
301  return 1;
302  } else {
303  $this->error = $this->db->lasterror();
304  $this->errno = $this->db->lasterrno();
305  $this->db->rollback();
306  return -1;
307  }
308  }
309 
316  public function fetch($id)
317  {
318  $sql = 'SELECT em.rowid, em.field, em.label, em.type';
319  $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
320  $sql .= ' WHERE em.rowid = '.((int) $id);
321 
322  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
323  $result = $this->db->query($sql);
324  if ($result) {
325  $obj = $this->db->fetch_object($result);
326  if ($obj) {
327  $this->id = $obj->rowid;
328  $this->hexa = $obj->field;
329  $this->model_name = $obj->label;
330  $this->datatoimport = $obj->type;
331  $this->fk_user = $obj->fk_user;
332  return 1;
333  } else {
334  $this->error = "Model not found";
335  return -2;
336  }
337  } else {
338  dol_print_error($this->db);
339  return -3;
340  }
341  }
342 
350  public function delete($user, $notrigger = 0)
351  {
352  $error = 0;
353 
354  $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
355  $sql .= " WHERE rowid=".((int) $this->id);
356 
357  $this->db->begin();
358 
359  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
360  $resql = $this->db->query($sql);
361  if (!$resql) {
362  $error++; $this->errors[] = "Error ".$this->db->lasterror();
363  }
364 
365  if (!$error) {
366  if (!$notrigger) {
367  /* Not used. This is not a business object. To convert it we must herit from CommonObject
368  // Call trigger
369  $result=$this->call_trigger('IMPORT_DELETE',$user);
370  if ($result < 0) $error++;
371  // End call triggers
372  */
373  }
374  }
375 
376  // Commit or rollback
377  if ($error) {
378  foreach ($this->errors as $errmsg) {
379  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
380  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
381  }
382  $this->db->rollback();
383  return -1 * $error;
384  } else {
385  $this->db->commit();
386  return 1;
387  }
388  }
389 }
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('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dolGetModulesDirs($subdir='')
Return list of modules directories.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db='', $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.