dolibarr  x.y.z
import_csv.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
5  * Copyright (C) 2012-2016 Juanjo Menent <jmenent@2byte.es>
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  * or see https://www.gnu.org/
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
29 
30 
34 class ImportCsv extends ModeleImports
35 {
39  public $db;
40 
41  public $datatoimport;
42 
46  public $error = '';
47 
51  public $errors = array();
52 
56  public $id;
57 
61  public $label;
62 
63  public $extension; // Extension of files imported by driver
64 
69  public $version = 'dolibarr';
70 
71  public $label_lib; // Label of external lib used by driver
72 
73  public $version_lib; // Version of external lib used by driver
74 
75  public $separator;
76 
77  public $file; // Path of file
78 
79  public $handle; // Handle fichier
80 
81  public $cacheconvert = array(); // Array to cache list of value found after a convertion
82 
83  public $cachefieldtable = array(); // Array to cache list of value found into fields@tables
84 
85  public $nbinsert = 0; // # of insert done during the import
86 
87  public $nbupdate = 0; // # of update done during the import
88 
89 
96  public function __construct($db, $datatoimport)
97  {
98  global $conf, $langs;
99  $this->db = $db;
100 
101  $this->separator = (GETPOST('separator') ?GETPOST('separator') : (empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? ',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE));
102  $this->enclosure = '"';
103  $this->escape = '"';
104 
105  $this->id = 'csv'; // Same value then xxx in file name export_xxx.modules.php
106  $this->label = 'Csv'; // Label of driver
107  $this->desc = $langs->trans("CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
108  $this->extension = 'csv'; // Extension for generated file by this driver
109  $this->picto = 'mime/other'; // Picto
110  $this->version = '1.34'; // Driver version
111 
112  // If driver use an external library, put its name here
113  $this->label_lib = 'Dolibarr';
114  $this->version_lib = DOL_VERSION;
115 
116  $this->datatoimport = $datatoimport;
117  if (preg_match('/^societe_/', $datatoimport)) {
118  $this->thirdpartyobject = new Societe($this->db);
119  }
120  }
121 
122 
123  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
130  public function write_header_example($outputlangs)
131  {
132  // phpcs:enable
133  return '';
134  }
135 
136  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
144  public function write_title_example($outputlangs, $headerlinefields)
145  {
146  // phpcs:enable
147  $s = join($this->separator, array_map('cleansep', $headerlinefields));
148  return $s."\n";
149  }
150 
151  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
159  public function write_record_example($outputlangs, $contentlinevalues)
160  {
161  // phpcs:enable
162  $s = join($this->separator, array_map('cleansep', $contentlinevalues));
163  return $s."\n";
164  }
165 
166  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
173  public function write_footer_example($outputlangs)
174  {
175  // phpcs:enable
176  return '';
177  }
178 
179 
180  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
187  public function import_open_file($file)
188  {
189  // phpcs:enable
190  global $langs;
191  $ret = 1;
192 
193  dol_syslog(get_class($this)."::open_file file=".$file);
194 
195  ini_set('auto_detect_line_endings', 1); // For MAC compatibility
196 
197  $this->handle = fopen(dol_osencode($file), "r");
198  if (!$this->handle) {
199  $langs->load("errors");
200  $this->error = $langs->trans("ErrorFailToOpenFile", $file);
201  $ret = -1;
202  } else {
203  $this->file = $file;
204  }
205 
206  return $ret;
207  }
208 
209 
210  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
217  public function import_get_nb_of_lines($file)
218  {
219  // phpcs:enable
220  return dol_count_nb_of_line($file);
221  }
222 
223 
224  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
230  public function import_read_header()
231  {
232  // phpcs:enable
233  return 0;
234  }
235 
236 
237  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
243  public function import_read_record()
244  {
245  // phpcs:enable
246  global $conf;
247 
248  $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
249 
250  // End of file
251  if ($arrayres === false) {
252  return false;
253  }
254 
255  //var_dump($this->handle);
256  //var_dump($arrayres);exit;
257  $newarrayres = array();
258  if ($arrayres && is_array($arrayres)) {
259  foreach ($arrayres as $key => $val) {
260  if (!empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) { // Forced charset
261  if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8') {
262  $newarrayres[$key]['val'] = $val;
263  $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
264  } else {
265  $newarrayres[$key]['val'] = utf8_encode($val);
266  $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
267  }
268  } else // Autodetect format (UTF8 or ISO)
269  {
270  if (utf8_check($val)) {
271  $newarrayres[$key]['val'] = $val;
272  $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
273  } else {
274  $newarrayres[$key]['val'] = utf8_encode($val);
275  $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
276  }
277  }
278  }
279 
280  $this->col = count($newarrayres);
281  }
282 
283  return $newarrayres;
284  }
285 
286  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
292  public function import_close_file()
293  {
294  // phpcs:enable
295  fclose($this->handle);
296  return 0;
297  }
298 
299 
300  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
312  public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
313  {
314  // phpcs:enable
315  global $langs, $conf, $user;
316  global $thirdparty_static; // Specific to thirdparty import
317  global $tablewithentity_cache; // Cache to avoid to call desc at each rows on tables
318 
319  $error = 0;
320  $warning = 0;
321  $this->errors = array();
322  $this->warnings = array();
323 
324  //dol_syslog("import_csv.modules maxfields=".$maxfields." importid=".$importid);
325 
326  //var_dump($array_match_file_to_database);
327  //var_dump($arrayrecord); exit;
328 
329  $array_match_database_to_file = array_flip($array_match_file_to_database);
330  $sort_array_match_file_to_database = $array_match_file_to_database;
331  ksort($sort_array_match_file_to_database);
332 
333  //var_dump($sort_array_match_file_to_database);
334 
335  if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val']))) {
336  //print 'W';
337  $this->warnings[$warning]['lib'] = $langs->trans('EmptyLine');
338  $this->warnings[$warning]['type'] = 'EMPTY';
339  $warning++;
340  } else {
341  $last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
342  $updatedone = false;
343  $insertdone = false;
344  // For each table to insert, me make a separate insert
345  foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
346  // Build sql request
347  $sql = '';
348  $listfields = array();
349  $listvalues = array();
350  $i = 0;
351  $errorforthistable = 0;
352 
353  // Define $tablewithentity_cache[$tablename] if not already defined
354  if (!isset($tablewithentity_cache[$tablename])) { // keep this test with "isset"
355  dol_syslog("Check if table ".$tablename." has an entity field");
356  $resql = $this->db->DDLDescTable($tablename, 'entity');
357  if ($resql) {
358  $obj = $this->db->fetch_object($resql);
359  if ($obj) {
360  $tablewithentity_cache[$tablename] = 1; // table contains entity field
361  } else {
362  $tablewithentity_cache[$tablename] = 0; // table does not contains entity field
363  }
364  } else {
365  dol_print_error($this->db);
366  }
367  } else {
368  //dol_syslog("Table ".$tablename." check for entity into cache is ".$tablewithentity_cache[$tablename]);
369  }
370 
371  // Define array to convert fields ('c.ref', ...) into column index (1, ...)
372  $arrayfield = array();
373  foreach ($sort_array_match_file_to_database as $key => $val) {
374  $arrayfield[$val] = ($key - 1);
375  }
376 
377  // $arrayrecord start at key 0
378  // $sort_array_match_file_to_database start at key 1
379 
380  // Loop on each fields in the match array: $key = 1..n, $val=alias of field (s.nom)
381  foreach ($sort_array_match_file_to_database as $key => $val) {
382  $fieldalias = preg_replace('/\..*$/i', '', $val);
383  $fieldname = preg_replace('/^.*\./i', '', $val);
384 
385  if ($alias != $fieldalias) {
386  continue; // Not a field of current table
387  }
388 
389  if ($key <= $maxfields) {
390  // Set $newval with value to insert and set $listvalues with sql request part for insert
391  $newval = '';
392  if ($arrayrecord[($key - 1)]['type'] > 0) {
393  $newval = $arrayrecord[($key - 1)]['val']; // If type of field into input file is not empty string (so defined into input file), we get value
394  }
395 
396  //var_dump($newval);var_dump($val);
397  //var_dump($objimport->array_import_convertvalue[0][$val]);
398 
399  // Make some tests on $newval
400 
401  // Is it a required field ?
402  if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == '')) {
403  $this->errors[$error]['lib'] = $langs->trans('ErrorMissingMandatoryValue', $key);
404  $this->errors[$error]['type'] = 'NOTNULL';
405  $errorforthistable++;
406  $error++;
407  } else {
408  // Test format only if field is not a missing mandatory field (field may be a value or empty but not mandatory)
409  // We convert field if required
410  if (!empty($objimport->array_import_convertvalue[0][$val])) {
411  //print 'Must convert '.$newval.' with rule '.join(',',$objimport->array_import_convertvalue[0][$val]).'. ';
412  if ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeid'
413  || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromref'
414  || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel'
415  ) {
416  // New val can be an id or ref. If it start with id: it is forced to id, if it start with ref: it is forced to ref. It not, we try to guess.
417  $isidorref = 'id';
418  if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) {
419  $isidorref = 'ref';
420  }
421 
422  $newval = preg_replace('/^(id|ref):/i', '', $newval); // Remove id: or ref: that was used to force if field is id or ref
423  //print 'Newval is now "'.$newval.'" and is type '.$isidorref."<br>\n";
424 
425  if ($isidorref == 'ref') { // If value into input import file is a ref, we apply the function defined into descriptor
426  $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
427  $class = $objimport->array_import_convertvalue[0][$val]['class'];
428  $method = $objimport->array_import_convertvalue[0][$val]['method'];
429  if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '') {
430  $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval];
431  } else {
432  $resultload = dol_include_once($file);
433  if (empty($resultload)) {
434  dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
435  break;
436  }
437  $classinstance = new $class($this->db);
438  if ($class == 'CGenericDic') {
439  $classinstance->element = $objimport->array_import_convertvalue[0][$val]['element'];
440  $classinstance->table_element = $objimport->array_import_convertvalue[0][$val]['table_element'];
441  }
442 
443  // Try the fetch from code or ref
444  $param_array = array('', $newval);
445  if ($class == 'AccountingAccount') {
446  //var_dump($arrayrecord[0]['val']);
447  /*include_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
448  $tmpchartofaccount = new AccountancySystem($this->db);
449  $tmpchartofaccount->fetch($conf->global->CHARTOFACCOUNTS);
450  //var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
451  if ((! ($conf->global->CHARTOFACCOUNTS > 0)) || $tmpchartofaccount->ref != $arrayrecord[0]['val'])
452  {
453  $this->errors[$error]['lib']=$langs->trans('ErrorImportOfChartLimitedToCurrentChart', $tmpchartofaccount->ref);
454  $this->errors[$error]['type']='RESTRICTONCURRENCTCHART';
455  $errorforthistable++;
456  $error++;
457  }*/
458  $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
459  }
460 
461  $result = call_user_func_array(array($classinstance, $method), $param_array);
462 
463  // If duplicate record found
464  if (!($classinstance->id != '') && $result == -2) {
465  $this->errors[$error]['lib'] = $langs->trans('ErrorMultipleRecordFoundFromRef', $newval);
466  $this->errors[$error]['type'] = 'FOREIGNKEY';
467  $errorforthistable++;
468  $error++;
469  }
470 
471  // If not found, try the fetch from label
472  if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') {
473  $param_array = array('', '', $newval);
474  call_user_func_array(array($classinstance, $method), $param_array);
475  }
476  $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id;
477 
478  //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
479  if ($classinstance->id != '') { // id may be 0, it is a found value
480  $newval = $classinstance->id;
481  } elseif (! $error) {
482  if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
483  $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
484  } elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) {
485  $this->errors[$error]['lib'] = $langs->trans('ErrorFieldRefNotIn', num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
486  } else {
487  $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
488  }
489  $this->errors[$error]['type'] = 'FOREIGNKEY';
490  $errorforthistable++;
491  $error++;
492  }
493  }
494  }
495  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeandlabel') {
496  $isidorref = 'id';
497  if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) {
498  $isidorref = 'ref';
499  }
500  $newval = preg_replace('/^(id|ref):/i', '', $newval);
501 
502  if ($isidorref == 'ref') {
503  $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
504  $class = $objimport->array_import_convertvalue[0][$val]['class'];
505  $method = $objimport->array_import_convertvalue[0][$val]['method'];
506  $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield'];
507  $code = $arrayrecord[$arrayfield[$codefromfield]]['val'];
508  if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] != '') {
509  $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval];
510  } else {
511  $resultload = dol_include_once($file);
512  if (empty($resultload)) {
513  dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', code='.$code);
514  break;
515  }
516  $classinstance = new $class($this->db);
517  // Try the fetch from code and ref
518  $param_array = array('', $newval, $code);
519  call_user_func_array(array($classinstance, $method), $param_array);
520  $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] = $classinstance->id;
521  if ($classinstance->id > 0) { // we found record
522  $newval = $classinstance->id;
523  } else {
524  if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
525  $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
526  } else {
527  $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
528  }
529  $this->errors[$error]['type'] = 'FOREIGNKEY';
530  $errorforthistable++;
531  $error++;
532  }
533  }
534  }
535  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'zeroifnull') {
536  if (empty($newval)) {
537  $newval = '0';
538  }
539  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchscalefromcodeunits') {
540  $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
541  $class = $objimport->array_import_convertvalue[0][$val]['class'];
542  $method = $objimport->array_import_convertvalue[0][$val]['method'];
543  $units = $objimport->array_import_convertvalue[0][$val]['units'];
544  if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] != '') {
545  $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval];
546  } else {
547  $resultload = dol_include_once($file);
548  if (empty($resultload)) {
549  dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', units='.$units);
550  break;
551  }
552  $classinstance = new $class($this->db);
553  // Try the fetch from code or ref
554  call_user_func_array(array($classinstance, $method), array('', '', $newval, $units));
555  $scaleorid = (($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
556  $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] = $scaleorid;
557  //print 'We have made a '.$class.'->'.$method." to get a value from key '".$newval."' and we got '".$scaleorid."'.";exit;
558  if ($classinstance->id > 0) { // we found record
559  $newval = $scaleorid ? $scaleorid : 0;
560  } else {
561  if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
562  $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
563  } else {
564  $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
565  }
566  $this->errors[$error]['type'] = 'FOREIGNKEY';
567  $errorforthistable++;
568  $error++;
569  }
570  }
571  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomercodeifauto') {
572  if (strtolower($newval) == 'auto') {
573  $this->thirdpartyobject->get_codeclient(0, 0);
574  $newval = $this->thirdpartyobject->code_client;
575  //print 'code_client='.$newval;
576  }
577  if (empty($newval)) {
578  $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
579  }
580  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto') {
581  if (strtolower($newval) == 'auto') {
582  $this->thirdpartyobject->get_codefournisseur(0, 1);
583  $newval = $this->thirdpartyobject->code_fournisseur;
584  //print 'code_fournisseur='.$newval;
585  }
586  if (empty($newval)) {
587  $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
588  }
589  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto') {
590  if (strtolower($newval) == 'auto') {
591  $this->thirdpartyobject->get_codecompta('customer');
592  $newval = $this->thirdpartyobject->code_compta;
593  //print 'code_compta='.$newval;
594  }
595  if (empty($newval)) {
596  $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
597  }
598  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto') {
599  if (strtolower($newval) == 'auto') {
600  $this->thirdpartyobject->get_codecompta('supplier');
601  $newval = $this->thirdpartyobject->code_compta_fournisseur;
602  if (empty($newval)) {
603  $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
604  }
605  //print 'code_compta_fournisseur='.$newval;
606  }
607  if (empty($newval)) {
608  $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
609  }
610  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
611  if (strtolower($newval) == 'auto') {
612  $defaultref = '';
613 
614  $classModForNumber = $objimport->array_import_convertvalue[0][$val]['class'];
615  $pathModForNumber = $objimport->array_import_convertvalue[0][$val]['path'];
616 
617  if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
618  require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
619  $modForNumber = new $classModForNumber;
620 
621  $tmpobject = null;
622  // Set the object with the date property when we can
623  if (!empty($objimport->array_import_convertvalue[0][$val]['classobject'])) {
624  $pathForObject = $objimport->array_import_convertvalue[0][$val]['pathobject'];
625  require_once DOL_DOCUMENT_ROOT.$pathForObject;
626  $tmpclassobject = $objimport->array_import_convertvalue[0][$val]['classobject'];
627  $tmpobject = new $tmpclassobject($this->db);
628  foreach ($arrayfield as $tmpkey => $tmpval) { // $arrayfield is array('c.ref'=>0, ...)
629  if (in_array($tmpkey, array('t.date', 'c.date_commande'))) {
630  $tmpobject->date = dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]]['val'], 1);
631  }
632  }
633  }
634 
635  $defaultref = $modForNumber->getNextValue(null, $tmpobject);
636  }
637  if (is_numeric($defaultref) && $defaultref <= 0) { // If error
638  $defaultref = '';
639  }
640  $newval = $defaultref;
641  }
642  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute') {
643  $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
644  $class = $objimport->array_import_convertvalue[0][$val]['class'];
645  $method = $objimport->array_import_convertvalue[0][$val]['method'];
646  $resultload = dol_include_once($file);
647  if (empty($resultload)) {
648  dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
649  break;
650  }
651  $classinstance = new $class($this->db);
652  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, ($key - 1)));
653  $newval = $res; // We get new value computed.
654  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
655  $newval = price2num($newval);
656  } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'accountingaccount') {
657  if (empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
658  $newval = rtrim(trim($newval), "0");
659  } else {
660  $newval = trim($newval);
661  }
662  }
663 
664  //print 'Val to use as insert is '.$newval.'<br>';
665  }
666 
667  // Test regexp
668  if (!empty($objimport->array_import_regex[0][$val]) && ($newval != '')) {
669  // If test is "Must exist in a field@table or field@table:..."
670  $reg = array();
671  if (preg_match('/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
672  $field = $reg[1];
673  $table = $reg[2];
674  $filter = !empty($reg[3]) ?substr($reg[3], 1) : '';
675 
676  $cachekey = $field.'@'.$table;
677  if (!empty($filter)) {
678  $cachekey .= ':'.$filter;
679  }
680 
681  // Load content of field@table into cache array
682  if (!is_array($this->cachefieldtable[$cachekey])) { // If content of field@table not already loaded into cache
683  $sql = "SELECT ".$field." as aliasfield FROM ".$table;
684  if (!empty($filter)) {
685  $sql .= ' WHERE '.$filter;
686  }
687 
688  $resql = $this->db->query($sql);
689  if ($resql) {
690  $num = $this->db->num_rows($resql);
691  $i = 0;
692  while ($i < $num) {
693  $obj = $this->db->fetch_object($resql);
694  if ($obj) {
695  $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
696  }
697  $i++;
698  }
699  } else {
700  dol_print_error($this->db);
701  }
702  }
703 
704  // Now we check cache is not empty (should not) and key is into cache
705  if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
706  $tableforerror = $table;
707  if (!empty($filter)) {
708  $tableforerror .= ':'.$filter;
709  }
710  $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, $field, $tableforerror);
711  $this->errors[$error]['type'] = 'FOREIGNKEY';
712  $errorforthistable++;
713  $error++;
714  }
715  } elseif (!preg_match('/'.$objimport->array_import_regex[0][$val].'/i', $newval)) {
716  // If test is just a static regex
717  //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."<br>";
718  $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorWrongValueForField', num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
719  $this->errors[$error]['type'] = 'REGEX';
720  $errorforthistable++;
721  $error++;
722  }
723  }
724 
725  // Check HTML injection
726  $inj = testSqlAndScriptInject($newval, 0);
727  if ($inj) {
728  $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorHtmlInjectionForField', num2Alpha($key - 1), dol_trunc($newval, 100));
729  $this->errors[$error]['type'] = 'HTMLINJECTION';
730  $errorforthistable++;
731  $error++;
732  }
733 
734  // Other tests
735  // ...
736  }
737 
738  // Define $listfields and $listvalues to build SQL request
739  if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) {
740  if (!in_array("socialnetworks", $listfields)) {
741  $listfields[] = "socialnetworks";
742  $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
743  $listvalues[$socialkey] = '';
744  }
745  //var_dump($newval); var_dump($arrayrecord[($key - 1)]['type']);
746  if (!empty($newval) && $arrayrecord[($key - 1)]['type'] > 0) {
747  $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
748  //var_dump('sk='.$socialkey); // socialkey=19
749  $socialnetwork = explode("_", $fieldname)[1];
750  if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") {
751  $json = new stdClass();
752  $json->$socialnetwork = $newval;
753  $listvalues[$socialkey] = json_encode($json);
754  } else {
755  $jsondata = $listvalues[$socialkey];
756  $json = json_decode($jsondata);
757  $json->$socialnetwork = $newval;
758  $listvalues[$socialkey] = json_encode($json);
759  }
760  }
761  } else {
762  $listfields[] = $fieldname;
763  // Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert
764  if (empty($newval) && $arrayrecord[($key - 1)]['type'] < 0) {
765  $listvalues[] = ($newval == '0' ? $newval : "null");
766  } elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) {
767  $listvalues[] = "''";
768  } else {
769  $listvalues[] = "'".$this->db->escape($newval)."'";
770  }
771  }
772  }
773  $i++;
774  }
775 
776  // We add hidden fields (but only if there is at least one field to add into table)
777  // We process here all the fields that were declared into the array $this->import_fieldshidden_array of the descriptor file.
778  // Previously we processed the ->import_fields_array.
779  if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
780  // Loop on each hidden fields to add them into listfields/listvalues
781  foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
782  if (!preg_match('/^'.preg_quote($alias, '/').'\./', $key)) {
783  continue; // Not a field of current table
784  }
785  if ($val == 'user->id') {
786  $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
787  $listvalues[] = ((int) $user->id);
788  } elseif (preg_match('/^lastrowid-/', $val)) {
789  $tmp = explode('-', $val);
790  $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
791  $keyfield = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
792  $listfields[] = $keyfield;
793  $listvalues[] = $lastinsertid;
794  //print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit;
795  } elseif (preg_match('/^const-/', $val)) {
796  $tmp = explode('-', $val, 2);
797  $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
798  $listvalues[] = "'".$this->db->escape($tmp[1])."'";
799  } elseif (preg_match('/^rule-/', $val)) {
800  $fieldname = $key;
801  if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
802  if ($objimport->array_import_convertvalue[0][$fieldname]['rule'] == 'compute') {
803  $file = (empty($objimport->array_import_convertvalue[0][$fieldname]['classfile']) ? $objimport->array_import_convertvalue[0][$fieldname]['file'] : $objimport->array_import_convertvalue[0][$fieldname]['classfile']);
804  $class = $objimport->array_import_convertvalue[0][$fieldname]['class'];
805  $method = $objimport->array_import_convertvalue[0][$fieldname]['method'];
806  $resultload = dol_include_once($file);
807  if (empty($resultload)) {
808  dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method);
809  break;
810  }
811  $classinstance = new $class($this->db);
812  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, ($key - 1)));
813  $fieldArr = explode('.', $fieldname);
814  if (count($fieldArr) > 0) {
815  $fieldname = $fieldArr[1];
816  }
817  $listfields[] = $fieldname;
818  $listvalues[] = $res;
819  }
820  }
821  } else {
822  $this->errors[$error]['lib'] = 'Bad value of profile setup '.$val.' for array_import_fieldshidden';
823  $this->errors[$error]['type'] = 'Import profile setup';
824  $error++;
825  }
826  }
827  }
828  //print 'listfields='.$listfields.'<br>listvalues='.$listvalues.'<br>';
829 
830  // If no error for this $alias/$tablename, we have a complete $listfields and $listvalues that are defined
831  // so we can try to make the insert or update now.
832  if (!$errorforthistable) {
833  //print "$alias/$tablename/$listfields/$listvalues<br>";
834  if (!empty($listfields)) {
835  $updatedone = false;
836  $insertdone = false;
837 
838  $is_table_category_link = false;
839  $fname = 'rowid';
840  if (strpos($tablename, '_categorie_') !== false) {
841  $is_table_category_link = true;
842  $fname='*';
843  }
844 
845  if (!empty($updatekeys)) {
846  // We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
847 
848  if (empty($lastinsertid)) { // No insert done yet for a parent table
849  $sqlSelect = "SELECT ".$fname." FROM ".$tablename;
850  $data = array_combine($listfields, $listvalues);
851  $where = array(); // filters to forge SQL request
852  $filters = array(); // filters to forge output error message
853  foreach ($updatekeys as $key) {
854  $col = $objimport->array_import_updatekeys[0][$key];
855  $key = preg_replace('/^.*\./i', '', $key);
856  if (isModEnabled("socialnetworks") && strpos($key, "socialnetworks") !== false) {
857  $tmp = explode("_", $key);
858  $key = $tmp[0];
859  $socialnetwork = $tmp[1];
860  $jsondata = $data[$key];
861  $json = json_decode($jsondata);
862  $stringtosearch = json_encode($socialnetwork).':'.json_encode($json->$socialnetwork);
863  //var_dump($stringtosearch);
864  //var_dump($this->db->escape($stringtosearch)); // This provide a value for sql string (but not for a like)
865  $where[] = $key." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
866  $filters[] = $col." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
867  //var_dump($where[1]); // This provide a value for sql string inside a like
868  } else {
869  $where[] = $key.' = '.$data[$key];
870  $filters[] = $col.' = '.$data[$key];
871  }
872  }
873  $sqlSelect .= " WHERE ".implode(' AND ', $where);
874 
875  $resql = $this->db->query($sqlSelect);
876  if ($resql) {
877  $num_rows = $this->db->num_rows($resql);
878  if ($num_rows == 1) {
879  $res = $this->db->fetch_object($resql);
880  $lastinsertid = $res->rowid;
881  if ($is_table_category_link) $lastinsertid = 'linktable'; // used to apply update on tables like llx_categorie_product and avoid being blocked for all file content if at least one entry already exists
882  $last_insert_id_array[$tablename] = $lastinsertid;
883  } elseif ($num_rows > 1) {
884  $this->errors[$error]['lib'] = $langs->trans('MultipleRecordFoundWithTheseFilters', implode(', ', $filters));
885  $this->errors[$error]['type'] = 'SQL';
886  $error++;
887  } else {
888  // No record found with filters, insert will be tried below
889  }
890  } else {
891  //print 'E';
892  $this->errors[$error]['lib'] = $this->db->lasterror();
893  $this->errors[$error]['type'] = 'SQL';
894  $error++;
895  }
896  } else {
897  // We have a last INSERT ID (got by previous pass), so we check if we have a row referencing this foreign key.
898  // This is required when updating table with some extrafields. When inserting a record in parent table, we can make
899  // a direct insert into subtable extrafields, but when me wake an update, the insertid is defined and the child record
900  // may already exists. So we rescan the extrafield table to know if record exists or not for the rowid.
901  // Note: For extrafield tablename, we have in importfieldshidden_array an enty 'extra.fk_object'=>'lastrowid-tableparent' so $keyfield is 'fk_object'
902  $sqlSelect = "SELECT rowid FROM ".$tablename;
903 
904  if (empty($keyfield)) {
905  $keyfield = 'rowid';
906  }
907  $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid);
908 
909  $resql = $this->db->query($sqlSelect);
910  if ($resql) {
911  $res = $this->db->fetch_object($resql);
912  if ($this->db->num_rows($resql) == 1) {
913  // We have a row referencing this last foreign key, continue with UPDATE.
914  } else {
915  // No record found referencing this last foreign key,
916  // force $lastinsertid to 0 so we INSERT below.
917  $lastinsertid = 0;
918  }
919  } else {
920  //print 'E';
921  $this->errors[$error]['lib'] = $this->db->lasterror();
922  $this->errors[$error]['type'] = 'SQL';
923  $error++;
924  }
925  }
926 
927  if (!empty($lastinsertid)) {
928  // We db escape social network field because he isn't in field creation
929  if (in_array("socialnetworks", $listfields)) {
930  $socialkey = array_search("socialnetworks", $listfields);
931  $tmpsql = $listvalues[$socialkey];
932  $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
933  }
934 
935  // Build SQL UPDATE request
936  $sqlstart = "UPDATE ".$tablename;
937 
938  $data = array_combine($listfields, $listvalues);
939  $set = array();
940  foreach ($data as $key => $val) {
941  $set[] = $key." = ".$val;
942  }
943  $sqlstart .= " SET ".implode(', ', $set);
944 
945  if (empty($keyfield)) {
946  $keyfield = 'rowid';
947  }
948  $sqlend = " WHERE ".$keyfield." = ".((int) $lastinsertid);
949 
950  if ($is_table_category_link) {
951  $sqlend = " WHERE " . implode(' AND ', $where);
952  }
953 
954  $sql = $sqlstart.$sqlend;
955 
956  // Run update request
957  $resql = $this->db->query($sql);
958  if ($resql) {
959  // No error, update has been done. $this->db->db->affected_rows can be 0 if data hasn't changed
960  $updatedone = true;
961  } else {
962  //print 'E';
963  $this->errors[$error]['lib'] = $this->db->lasterror();
964  $this->errors[$error]['type'] = 'SQL';
965  $error++;
966  }
967  }
968  }
969 
970  // Update not done, we do insert
971  if (!$error && !$updatedone) {
972  // We db escape social network field because he isn't in field creation
973  if (in_array("socialnetworks", $listfields)) {
974  $socialkey = array_search("socialnetworks", $listfields);
975  $tmpsql = $listvalues[$socialkey];
976  $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
977  }
978 
979  // Build SQL INSERT request
980  $sqlstart = "INSERT INTO ".$tablename."(".implode(", ", $listfields).", import_key";
981  $sqlend = ") VALUES(".implode(', ', $listvalues).", '".$this->db->escape($importid)."'";
982  if (!empty($tablewithentity_cache[$tablename])) {
983  $sqlstart .= ", entity";
984  $sqlend .= ", ".$conf->entity;
985  }
986  if (!empty($objimport->array_import_tables_creator[0][$alias])) {
987  $sqlstart .= ", ".$objimport->array_import_tables_creator[0][$alias];
988  $sqlend .= ", ".$user->id;
989  }
990  $sql = $sqlstart.$sqlend.")";
991  //dol_syslog("import_csv.modules", LOG_DEBUG);
992 
993  // Run insert request
994  if ($sql) {
995  $resql = $this->db->query($sql);
996  if ($resql) {
997  if (!$is_table_category_link) {
998  $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
999  }
1000  $insertdone = true;
1001  } else {
1002  //print 'E';
1003  $this->errors[$error]['lib'] = $this->db->lasterror();
1004  $this->errors[$error]['type'] = 'SQL';
1005  $error++;
1006  }
1007  }
1008  }
1009  }
1010  /*else
1011  {
1012  dol_print_error('','ErrorFieldListEmptyFor '.$alias."/".$tablename);
1013  }*/
1014  }
1015 
1016  if ($error) {
1017  break;
1018  }
1019  }
1020 
1021  if ($updatedone) {
1022  $this->nbupdate++;
1023  }
1024  if ($insertdone) {
1025  $this->nbinsert++;
1026  }
1027  }
1028 
1029  return 1;
1030  }
1031 }
1032 
1039 function cleansep($value)
1040 {
1041  return str_replace(array(',', ';'), '/', $value);
1042 }
Class to import CSV files.
write_header_example($outputlangs)
Output header of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
__construct($db, $datatoimport)
Constructor.
import_read_record()
Return array of next record in input file.
write_record_example($outputlangs, $contentlinevalues)
Output record of an example file for this format.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_close_file()
Close file handle.
import_read_header()
Input header line from file.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
import_open_file($file)
Open input file.
Parent class for import file readers.
Class to manage third parties objects (customers, suppliers, prospects...)
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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:407
dol_count_nb_of_line($file)
Count number of lines in a file.
Definition: files.lib.php:553
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
num2Alpha($n)
Return a numeric value into an Excel like column number.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
cleansep($value)
Clean a string from separator.
testSqlAndScriptInject($val, $type)
Security: WAF layer for SQL Injection and XSS Injection (scripts) protection (Filters on GET,...
Definition: main.inc.php:87
$conf db
API class for accounts.
Definition: inc.php:41