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