dolibarr  13.0.2
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
29 class Import
30 {
31  public $array_import_module;
32  public $array_import_perms;
33  public $array_import_icon;
34  public $array_import_code;
35  public $array_import_label;
36  public $array_import_tables;
37  public $array_import_tables_creator;
38  public $array_import_fields;
39  public $array_import_fieldshidden;
40  public $array_import_entities;
41  public $array_import_regex;
42  public $array_import_updatekeys;
43  public $array_import_examplevalues;
44  public $array_import_convertvalue;
45  public $array_import_run_sql_after;
46 
50  public $error = '';
51 
55  public $errors = array();
56 
57 
63  public function __construct($db)
64  {
65  $this->db = $db;
66  }
67 
68 
69  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
77  public function load_arrays($user, $filter = '')
78  {
79  // phpcs:enable
80  global $langs, $conf;
81 
82  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
83 
84  $i = 0;
85 
86  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
87  $modulesdir = dolGetModulesDirs();
88 
89  // Load list of modules
90  foreach ($modulesdir as $dir)
91  {
92  $handle = @opendir(dol_osencode($dir));
93  if (!is_resource($handle)) continue;
94 
95  // Search module files
96  while (($file = readdir($handle)) !== false)
97  {
98  if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) continue;
99 
100  $modulename = $reg[1];
101 
102  // Defined if module is enabled
103  $enabled = true;
104  $part = strtolower(preg_replace('/^mod/i', '', $modulename));
105  // Adds condition for propal module
106  if ($part === 'propale') $part = 'propal';
107  if (empty($conf->$part->enabled)) $enabled = false;
108 
109  if (empty($enabled)) continue;
110 
111  // Init load class
112  $file = $dir."/".$modulename.".class.php";
113  $classname = $modulename;
114  require_once $file;
115  $module = new $classname($this->db);
116 
117  if (isset($module->import_code) && is_array($module->import_code))
118  {
119  foreach ($module->import_code as $r => $value)
120  {
121  if ($filter && ($filter != $module->import_code[$r])) continue;
122 
123  // Test if permissions are ok
124  /*$perm=$module->import_permission[$r][0];
125  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
126  if ($perm[2])
127  {
128  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
129  }
130  else
131  {
132  $bool=$user->rights->{$perm[0]}->{$perm[1]};
133  }
134  if ($perm[0]=='user' && $user->admin) $bool=true;
135  //print $bool." $perm[0]"."<br>";
136  */
137 
138  // Load lang file
139  $langtoload = $module->getLangFilesArray();
140  if (is_array($langtoload))
141  {
142  foreach ($langtoload as $key)
143  {
144  $langs->load($key);
145  }
146  }
147 
148  // Permission
149  $this->array_import_perms[$i] = $user->rights->import->run;
150  // Icon
151  $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
152  // Code du dataset export
153  $this->array_import_code[$i] = $module->import_code[$r];
154  // Libelle du dataset export
155  $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
156  // Array of tables to import (key=alias, value=tablename)
157  $this->array_import_tables[$i] = $module->import_tables_array[$r];
158  // Array of tables creator field to import (key=alias, value=creator field name)
159  $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
160  // Array of fields to import (key=field, value=label)
161  $this->array_import_fields[$i] = $module->import_fields_array[$r];
162  // Array of hidden fields to import (key=field, value=label)
163  $this->array_import_fieldshidden[$i] = $module->import_fieldshidden_array[$r];
164  // Tableau des entites a exporter (cle=champ, valeur=entite)
165  $this->array_import_entities[$i] = $module->import_entities_array[$r];
166  // Tableau des alias a exporter (cle=champ, valeur=alias)
167  $this->array_import_regex[$i] = $module->import_regex_array[$r];
168  // Array of columns allowed as UPDATE options
169  $this->array_import_updatekeys[$i] = $module->import_updatekeys_array[$r];
170  // Array of examples
171  $this->array_import_examplevalues[$i] = $module->import_examplevalues_array[$r];
172  // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
173  $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
174  // Sql request to run after import
175  $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
176  // Module
177  $this->array_import_module[$i] = array('position_of_profile'=>($module->module_position.'-'.$module->import_code[$r]), 'module'=>$module);
178 
179  dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
180  $i++;
181  }
182  }
183  }
184  closedir($handle);
185  }
186  return 1;
187  }
188 
189 
190 
191  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
202  public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
203  {
204  // phpcs:enable
205  global $conf, $langs;
206 
207  $indice = 0;
208 
209  dol_syslog(get_class($this)."::build_example_file ".$model);
210 
211  // Creation de la classe d'import du model Import_XXX
212  $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
213  $file = "import_".$model.".modules.php";
214  $classname = "Import".$model;
215  require_once $dir.$file;
216  $objmodel = new $classname($this->db, $datatoimport);
217 
218  $outputlangs = $langs; // Lang for output
219  $s = '';
220 
221  // Genere en-tete
222  $s .= $objmodel->write_header_example($outputlangs);
223 
224  // Genere ligne de titre
225  $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
226 
227  // Genere ligne de titre
228  $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
229 
230  // Genere pied de page
231  $s .= $objmodel->write_footer_example($outputlangs);
232 
233  return $s;
234  }
235 
242  public function create($user)
243  {
244  global $conf;
245 
246  dol_syslog("Import.class.php::create");
247 
248  // Check parameters
249  if (empty($this->model_name)) { $this->error = 'ErrorWrongParameters'; return -1; }
250  if (empty($this->datatoimport)) { $this->error = 'ErrorWrongParameters'; return -1; }
251  if (empty($this->hexa)) { $this->error = 'ErrorWrongParameters'; return -1; }
252 
253  $this->db->begin();
254 
255  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
256  $sql .= 'fk_user, label, type, field';
257  $sql .= ')';
258  $sql .= " VALUES (".($user->id > 0 ? $user->id : 0).", '".$this->db->escape($this->model_name)."', '".$this->db->escape($this->datatoimport)."', '".$this->db->escape($this->hexa)."')";
259 
260  dol_syslog(get_class($this)."::create", LOG_DEBUG);
261  $resql = $this->db->query($sql);
262  if ($resql)
263  {
264  $this->db->commit();
265  return 1;
266  } else {
267  $this->error = $this->db->lasterror();
268  $this->errno = $this->db->lasterrno();
269  $this->db->rollback();
270  return -1;
271  }
272  }
273 
280  public function fetch($id)
281  {
282  $sql = 'SELECT em.rowid, em.field, em.label, em.type';
283  $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
284  $sql .= ' WHERE em.rowid = '.((int) $id);
285 
286  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
287  $result = $this->db->query($sql);
288  if ($result)
289  {
290  $obj = $this->db->fetch_object($result);
291  if ($obj)
292  {
293  $this->id = $obj->rowid;
294  $this->hexa = $obj->field;
295  $this->model_name = $obj->label;
296  $this->datatoimport = $obj->type;
297  $this->fk_user = $obj->fk_user;
298  return 1;
299  } else {
300  $this->error = "Model not found";
301  return -2;
302  }
303  } else {
304  dol_print_error($this->db);
305  return -3;
306  }
307  }
308 
316  public function delete($user, $notrigger = 0)
317  {
318  global $conf, $langs;
319  $error = 0;
320 
321  $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
322  $sql .= " WHERE rowid=".$this->id;
323 
324  $this->db->begin();
325 
326  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
327  $resql = $this->db->query($sql);
328  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
329 
330  if (!$error)
331  {
332  if (!$notrigger)
333  {
334  /* Not used. This is not a business object. To convert it we must herit from CommonObject
335  // Call trigger
336  $result=$this->call_trigger('IMPORT_DELETE',$user);
337  if ($result < 0) $error++;
338  // End call triggers
339  */
340  }
341  }
342 
343  // Commit or rollback
344  if ($error)
345  {
346  foreach ($this->errors as $errmsg)
347  {
348  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
349  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
350  }
351  $this->db->rollback();
352  return -1 * $error;
353  } else {
354  $this->db->commit();
355  return 1;
356  }
357  }
358 }
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
create($user)
Save an export model in database.
Class to manage imports.
dolGetModulesDirs($subdir= '')
Return list of modules directories.
load_arrays($user, $filter= '')
Load description int this-&gt;array_import_module, this-&gt;array_import_fields, ...
$conf db
API class for accounts.
Definition: inc.php:54
fetch($id)
Load an import profil from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__construct($db)
Constructor.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
Build an import example file.