dolibarr  16.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 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 {
32  public $array_import_module;
33  public $array_import_perms;
34  public $array_import_icon;
35  public $array_import_code;
36  public $array_import_label;
37  public $array_import_tables;
38  public $array_import_tables_creator;
39  public $array_import_fields;
40  public $array_import_fieldshidden;
41  public $array_import_entities;
42  public $array_import_regex;
43  public $array_import_updatekeys;
44  public $array_import_examplevalues;
45  public $array_import_convertvalue;
46  public $array_import_run_sql_after;
47 
51  public $error = '';
52 
56  public $errors = array();
57 
58  // To store import templates
59  public $hexa; // List of fields in the export profile
60  public $datatoimport;
61  public $model_name; // Name of export profile
62  public $fk_user;
63 
64 
70  public function __construct($db)
71  {
72  $this->db = $db;
73  }
74 
75 
76  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
84  public function load_arrays($user, $filter = '')
85  {
86  // phpcs:enable
87  global $langs, $conf;
88 
89  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
90 
91  $i = 0;
92 
93  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
94  $modulesdir = dolGetModulesDirs();
95 
96  // Load list of modules
97  foreach ($modulesdir as $dir) {
98  $handle = @opendir(dol_osencode($dir));
99  if (!is_resource($handle)) {
100  continue;
101  }
102 
103  // Search module files
104  while (($file = readdir($handle)) !== false) {
105  if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
106  continue;
107  }
108 
109  $modulename = $reg[1];
110 
111  // Defined if module is enabled
112  $enabled = true;
113  $part = strtolower(preg_replace('/^mod/i', '', $modulename));
114  // Adds condition for propal module
115  if ($part === 'propale') {
116  $part = 'propal';
117  }
118  if (empty($conf->$part->enabled)) {
119  $enabled = false;
120  }
121 
122  if (empty($enabled)) {
123  continue;
124  }
125 
126  // Init load class
127  $file = $dir."/".$modulename.".class.php";
128  $classname = $modulename;
129  require_once $file;
130  $module = new $classname($this->db);
131 
132  if (isset($module->import_code) && is_array($module->import_code)) {
133  foreach ($module->import_code as $r => $value) {
134  if ($filter && ($filter != $module->import_code[$r])) {
135  continue;
136  }
137 
138  // Test if permissions are ok
139  /*$perm=$module->import_permission[$r][0];
140  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
141  if ($perm[2])
142  {
143  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
144  }
145  else
146  {
147  $bool=$user->rights->{$perm[0]}->{$perm[1]};
148  }
149  if ($perm[0]=='user' && $user->admin) $bool=true;
150  //print $bool." $perm[0]"."<br>";
151  */
152 
153  // Load lang file
154  $langtoload = $module->getLangFilesArray();
155  if (is_array($langtoload)) {
156  foreach ($langtoload as $key) {
157  $langs->load($key);
158  }
159  }
160 
161  // Permission
162  $this->array_import_perms[$i] = $user->rights->import->run;
163  // Icon
164  $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
165  // Code du dataset export
166  $this->array_import_code[$i] = $module->import_code[$r];
167  // Libelle du dataset export
168  $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
169  // Array of tables to import (key=alias, value=tablename)
170  $this->array_import_tables[$i] = $module->import_tables_array[$r];
171  // Array of tables creator field to import (key=alias, value=creator field name)
172  $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
173  // Array of fields to import (key=field, value=label)
174  $this->array_import_fields[$i] = $module->import_fields_array[$r];
175  // Array of hidden fields to import (key=field, value=label)
176  $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
177  // Tableau des entites a exporter (cle=champ, valeur=entite)
178  $this->array_import_entities[$i] = $module->import_entities_array[$r];
179  // Tableau des alias a exporter (cle=champ, valeur=alias)
180  $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
181  // Array of columns allowed as UPDATE options
182  $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
183  // Array of examples
184  $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
185  // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
186  $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
187  // Sql request to run after import
188  $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
189  // Module
190  $this->array_import_module[$i] = array('position_of_profile'=>($module->module_position.'-'.$module->import_code[$r]), 'module'=>$module);
191 
192  dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
193  $i++;
194  }
195  }
196  }
197  closedir($handle);
198  }
199  return 1;
200  }
201 
202 
203 
204  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
215  public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
216  {
217  // phpcs:enable
218  global $conf, $langs;
219 
220  $indice = 0;
221 
222  dol_syslog(get_class($this)."::build_example_file ".$model);
223 
224  // Creation de la classe d'import du model Import_XXX
225  $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
226  $file = "import_".$model.".modules.php";
227  $classname = "Import".$model;
228  require_once $dir.$file;
229  $objmodel = new $classname($this->db, $datatoimport);
230 
231  $outputlangs = $langs; // Lang for output
232  $s = '';
233 
234  // Genere en-tete
235  $s .= $objmodel->write_header_example($outputlangs);
236 
237  // Genere ligne de titre
238  $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
239 
240  // Genere ligne de titre
241  $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
242 
243  // Genere pied de page
244  $s .= $objmodel->write_footer_example($outputlangs);
245 
246  return $s;
247  }
248 
255  public function create($user)
256  {
257  global $conf;
258 
259  dol_syslog("Import.class.php::create");
260 
261  // Check parameters
262  if (empty($this->model_name)) {
263  $this->error = 'ErrorWrongParameters'; return -1;
264  }
265  if (empty($this->datatoimport)) {
266  $this->error = 'ErrorWrongParameters'; return -1;
267  }
268  if (empty($this->hexa)) {
269  $this->error = 'ErrorWrongParameters'; return -1;
270  }
271 
272  $this->db->begin();
273 
274  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
275  $sql .= 'fk_user,';
276  $sql .= ' label,';
277  $sql .= ' type,';
278  $sql .= ' field';
279  $sql .= ')';
280  $sql .= " VALUES (";
281  $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
282  $sql .= " '".$this->db->escape($this->model_name)."',";
283  $sql .= " '".$this->db->escape($this->datatoimport)."',";
284  $sql .= " '".$this->db->escape($this->hexa)."'";
285  $sql .= ")";
286 
287  $resql = $this->db->query($sql);
288  if ($resql) {
289  $this->db->commit();
290  return 1;
291  } else {
292  $this->error = $this->db->lasterror();
293  $this->errno = $this->db->lasterrno();
294  $this->db->rollback();
295  return -1;
296  }
297  }
298 
305  public function fetch($id)
306  {
307  $sql = 'SELECT em.rowid, em.field, em.label, em.type';
308  $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
309  $sql .= ' WHERE em.rowid = '.((int) $id);
310 
311  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
312  $result = $this->db->query($sql);
313  if ($result) {
314  $obj = $this->db->fetch_object($result);
315  if ($obj) {
316  $this->id = $obj->rowid;
317  $this->hexa = $obj->field;
318  $this->model_name = $obj->label;
319  $this->datatoimport = $obj->type;
320  $this->fk_user = $obj->fk_user;
321  return 1;
322  } else {
323  $this->error = "Model not found";
324  return -2;
325  }
326  } else {
327  dol_print_error($this->db);
328  return -3;
329  }
330  }
331 
339  public function delete($user, $notrigger = 0)
340  {
341  global $conf, $langs;
342  $error = 0;
343 
344  $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
345  $sql .= " WHERE rowid=".((int) $this->id);
346 
347  $this->db->begin();
348 
349  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
350  $resql = $this->db->query($sql);
351  if (!$resql) {
352  $error++; $this->errors[] = "Error ".$this->db->lasterror();
353  }
354 
355  if (!$error) {
356  if (!$notrigger) {
357  /* Not used. This is not a business object. To convert it we must herit from CommonObject
358  // Call trigger
359  $result=$this->call_trigger('IMPORT_DELETE',$user);
360  if ($result < 0) $error++;
361  // End call triggers
362  */
363  }
364  }
365 
366  // Commit or rollback
367  if ($error) {
368  foreach ($this->errors as $errmsg) {
369  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
370  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
371  }
372  $this->db->rollback();
373  return -1 * $error;
374  } else {
375  $this->db->commit();
376  return 1;
377  }
378  }
379 }
Import\create
create($user)
Save an export model in database.
Definition: import.class.php:255
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
Import
Class to manage imports.
Definition: import.class.php:30
Import\__construct
__construct($db)
Constructor.
Definition: import.class.php:70
Import\fetch
fetch($id)
Load an import profil from database.
Definition: import.class.php:305
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dolGetModulesDirs
dolGetModulesDirs($subdir='')
Return list of modules directories.
Definition: functions2.lib.php:80
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Import\build_example_file
build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
Build an import example file.
Definition: import.class.php:215
Import\load_arrays
load_arrays($user, $filter='')
Load description int this->array_import_module, this->array_import_fields, ...
Definition: import.class.php:84