28 require_once DOL_DOCUMENT_ROOT.
'/core/modules/import/modules_import.php';
57 public $version =
'dolibarr';
69 public $cacheconvert = array();
71 public $cachefieldtable = array();
92 parent::__construct();
95 $this->separator = (
GETPOST(
'separator') ?
GETPOST(
'separator') : (!
getDolGlobalString(
'IMPORT_CSV_SEPARATOR_TO_USE') ?
',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE));
96 $this->enclosure =
'"';
100 $this->label =
'Csv';
101 $this->desc = $langs->trans(
"CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
102 $this->extension =
'csv';
103 $this->picto =
'mime/other';
104 $this->version =
'1.34';
107 $this->label_lib =
'Dolibarr';
108 $this->version_lib = DOL_VERSION;
110 $this->datatoimport = $datatoimport;
111 if (preg_match(
'/^societe_/', $datatoimport)) {
112 $this->thirdpartyobject =
new Societe($this->db);
141 $s = implode($this->separator, array_map(
'cleansep', $headerlinefields));
156 $s = implode($this->separator, array_map(
'cleansep', $contentlinevalues));
187 dol_syslog(get_class($this).
"::open_file file=".$file);
189 ini_set(
'auto_detect_line_endings', 1);
192 if (!$this->handle) {
193 $langs->load(
"errors");
194 $this->error = $langs->trans(
"ErrorFailToOpenFile", $file);
242 $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
245 if ($arrayres ===
false) {
251 $newarrayres = array();
252 if ($arrayres && is_array($arrayres)) {
253 foreach ($arrayres as $key => $val) {
255 if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) ==
'utf8') {
256 $newarrayres[$key][
'val'] = $val;
257 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
259 $newarrayres[$key][
'val'] = mb_convert_encoding($val,
'UTF-8',
'ISO-8859-1');
260 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
264 $newarrayres[$key][
'val'] = $val;
265 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
267 $newarrayres[$key][
'val'] = mb_convert_encoding($val,
'UTF-8',
'ISO-8859-1');
268 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
273 $this->col = count($newarrayres);
288 fclose($this->handle);
305 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
308 global $langs, $conf, $user;
309 global $thirdparty_static;
310 global $tablewithentity_cache;
314 $this->errors = array();
315 $this->warnings = array();
322 $array_match_database_to_file = array_flip($array_match_file_to_database);
323 $sort_array_match_file_to_database = $array_match_file_to_database;
324 ksort($sort_array_match_file_to_database);
328 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
330 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
331 $this->warnings[$warning][
'type'] =
'EMPTY';
334 $last_insert_id_array = array();
338 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
341 $listfields = array();
342 $listvalues = array();
344 $errorforthistable = 0;
347 if (!isset($tablewithentity_cache[$tablename])) {
348 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
349 $resql = $this->db->DDLDescTable($tablename,
'entity');
351 $obj = $this->db->fetch_object($resql);
353 $tablewithentity_cache[$tablename] = 1;
355 $tablewithentity_cache[$tablename] = 0;
365 $arrayfield = array();
366 foreach ($sort_array_match_file_to_database as $key => $val) {
367 $arrayfield[$val] = ($key - 1);
374 foreach ($sort_array_match_file_to_database as $key => $val) {
375 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
376 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
378 if ($alias != $fieldalias) {
382 if ($key <= $maxfields) {
385 if ($arrayrecord[($key - 1)][
'type'] > 0) {
386 $newval = $arrayrecord[($key - 1)][
'val'];
395 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
397 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
398 $this->errors[$error][
'type'] =
'NOTNULL';
399 $errorforthistable++;
404 if (!empty($objimport->array_import_convertvalue[0][$val])) {
406 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
407 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
408 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
412 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
416 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
419 if ($isidorref ==
'ref') {
420 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
421 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
422 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
423 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] !=
'') {
424 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval];
427 if (empty($resultload)) {
428 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
431 $classinstance =
new $class($this->db);
432 if ($class ==
'CGenericDic') {
433 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
434 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
438 $param_array = array(
'', $newval);
439 if ($class ==
'AccountingAccount') {
452 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
454 if ($class ==
'CActionComm') {
455 $param_array = array($newval);
457 $result = call_user_func_array(array($classinstance, $method), $param_array);
460 if (!($classinstance->id !=
'') && $result == -2) {
461 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
462 $this->errors[$error][
'type'] =
'FOREIGNKEY';
463 $errorforthistable++;
468 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
469 $param_array = array(
'',
'', $newval);
470 call_user_func_array(array($classinstance, $method), $param_array);
472 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] = $classinstance->id;
475 if ($classinstance->id !=
'') {
476 $newval = $classinstance->id;
477 } elseif (! $error) {
478 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
479 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
480 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
481 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
483 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
485 $this->errors[$error][
'type'] =
'FOREIGNKEY';
486 $errorforthistable++;
491 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
493 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
496 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
498 if ($isidorref ==
'ref') {
499 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
500 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
501 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
502 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
503 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
504 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] !=
'') {
505 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval];
508 if (empty($resultload)) {
509 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
512 $classinstance =
new $class($this->db);
514 $param_array = array(
'', $newval, $code);
515 call_user_func_array(array($classinstance, $method), $param_array);
516 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] = $classinstance->id;
517 if ($classinstance->id > 0) {
518 $newval = $classinstance->id;
520 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
521 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
523 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
525 $this->errors[$error][
'type'] =
'FOREIGNKEY';
526 $errorforthistable++;
531 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
532 if (empty($newval)) {
535 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
536 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
537 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
538 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
539 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
540 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] !=
'') {
541 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval];
544 if (empty($resultload)) {
545 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
548 $classinstance =
new $class($this->db);
550 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
551 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
552 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] = $scaleorid;
554 if ($classinstance->id > 0) {
555 $newval = $scaleorid ? $scaleorid : 0;
557 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
558 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
560 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
562 $this->errors[$error][
'type'] =
'FOREIGNKEY';
563 $errorforthistable++;
567 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
568 if (strtolower($newval) ==
'auto') {
569 $this->thirdpartyobject->get_codeclient(0, 0);
570 $newval = $this->thirdpartyobject->code_client;
573 if (empty($newval)) {
574 $arrayrecord[($key - 1)][
'type'] = -1;
576 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
577 if (strtolower($newval) ==
'auto') {
578 $this->thirdpartyobject->get_codefournisseur(0, 1);
579 $newval = $this->thirdpartyobject->code_fournisseur;
582 if (empty($newval)) {
583 $arrayrecord[($key - 1)][
'type'] = -1;
585 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
586 if (strtolower($newval) ==
'auto') {
587 $this->thirdpartyobject->get_codecompta(
'customer');
588 $newval = $this->thirdpartyobject->code_compta;
591 if (empty($newval)) {
592 $arrayrecord[($key - 1)][
'type'] = -1;
594 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
595 if (strtolower($newval) ==
'auto') {
596 $this->thirdpartyobject->get_codecompta(
'supplier');
597 $newval = $this->thirdpartyobject->code_compta_fournisseur;
598 if (empty($newval)) {
599 $arrayrecord[($key - 1)][
'type'] = -1;
603 if (empty($newval)) {
604 $arrayrecord[($key - 1)][
'type'] = -1;
606 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
607 if (strtolower($newval) ==
'auto') {
610 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
611 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
613 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
614 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
615 $modForNumber =
new $classModForNumber();
619 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
620 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
621 require_once DOL_DOCUMENT_ROOT.$pathForObject;
622 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
623 $tmpobject =
new $tmpclassobject($this->db);
624 foreach ($arrayfield as $tmpkey => $tmpval) {
625 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
626 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
631 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
633 if (is_numeric($defaultref) && $defaultref <= 0) {
636 $newval = $defaultref;
638 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
639 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
640 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
641 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
643 if (empty($resultload)) {
644 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
647 $classinstance =
new $class($this->db);
648 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, ($key - 1)));
649 if (empty($classinstance->error) && empty($classinstance->errors)) {
652 $this->errors[$error][
'type'] =
'CLASSERROR';
653 $this->errors[$error][
'lib'] = implode(
655 array_merge([$classinstance->error], $classinstance->errors)
657 $errorforthistable++;
660 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
662 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
664 $newval = rtrim(trim($newval),
"0");
666 $newval = trim($newval);
674 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
677 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
680 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
682 $cachekey = $field.
'@'.$table;
683 if (!empty($filter)) {
684 $cachekey .=
':'.$filter;
688 if (!is_array($this->cachefieldtable[$cachekey])) {
689 $sql =
"SELECT ".$field.
" as aliasfield FROM ".$table;
690 if (!empty($filter)) {
691 $sql .=
' WHERE '.$filter;
694 $resql = $this->db->query(
$sql);
696 $num = $this->db->num_rows($resql);
699 $obj = $this->db->fetch_object($resql);
701 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
711 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
712 $tableforerror = $table;
713 if (!empty($filter)) {
714 $tableforerror .=
':'.$filter;
716 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
717 $this->errors[$error][
'type'] =
'FOREIGNKEY';
718 $errorforthistable++;
721 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
724 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
725 $this->errors[$error][
'type'] =
'REGEX';
726 $errorforthistable++;
734 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
735 $this->errors[$error][
'type'] =
'HTMLINJECTION';
736 $errorforthistable++;
745 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
746 if (!in_array(
"socialnetworks", $listfields)) {
747 $listfields[] =
"socialnetworks";
748 $socialkey = array_search(
"socialnetworks", $listfields);
749 $listvalues[$socialkey] =
'';
752 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
753 $socialkey = array_search(
"socialnetworks", $listfields);
755 $socialnetwork = explode(
"_", $fieldname)[1];
756 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
758 $json->$socialnetwork = $newval;
759 $listvalues[$socialkey] = json_encode($json);
761 $jsondata = $listvalues[$socialkey];
762 $json = json_decode($jsondata);
763 $json->$socialnetwork = $newval;
764 $listvalues[$socialkey] = json_encode($json);
768 $listfields[] = $fieldname;
770 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
771 $listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
772 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
773 $listvalues[] =
"''";
775 $listvalues[] =
"'".$this->db->escape($newval).
"'";
785 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
787 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
788 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
791 $keyfield = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
793 if (in_array($keyfield, $listfields)) {
795 } elseif ($tmpval ==
'user->id') {
796 $listfields[] = $keyfield;
797 $listvalues[] = ((int) $user->id);
798 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
799 $tmp = explode(
'-', $tmpval);
800 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
801 $listfields[] = $keyfield;
802 $listvalues[] = (int) $lastinsertid;
804 } elseif (preg_match(
'/^const-/', $tmpval)) {
805 $tmp = explode(
'-', $tmpval, 2);
806 $listfields[] = $keyfield;
807 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
808 } elseif (preg_match(
'/^rule-/', $tmpval)) {
809 $fieldname = $tmpkey;
810 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
811 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
812 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
813 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
814 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
815 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
817 if (empty($resultload)) {
818 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
821 $classinstance =
new $class($this->db);
822 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, ($key - 1)));
823 if (empty($classinstance->error) && empty($classinstance->errors)) {
824 $fieldArr = explode(
'.', $fieldname);
825 if (count($fieldArr) > 0) {
826 $fieldname = $fieldArr[1];
830 $listfields[] = $fieldname;
831 if ($type ==
'int') {
832 $listvalues[] = (int) $res;
833 } elseif ($type ==
'double') {
834 $listvalues[] = (
float) $res;
836 $listvalues[] =
"'".$this->db->escape($res).
"'";
839 $this->errors[$error][
'type'] =
'CLASSERROR';
840 $this->errors[$error][
'lib'] = implode(
842 array_merge([$classinstance->error], $classinstance->errors)
844 $errorforthistable++;
850 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
851 $this->errors[$error][
'type'] =
'Import profile setup';
860 if (!$errorforthistable) {
862 if (!empty($listfields)) {
866 $is_table_category_link =
false;
868 if (strpos($tablename,
'_categorie_') !==
false) {
869 $is_table_category_link =
true;
873 if (!empty($updatekeys)) {
876 if (empty($lastinsertid)) {
877 $sqlSelect =
"SELECT ".$fname.
" FROM ".$tablename;
878 $data = array_combine($listfields, $listvalues);
881 foreach ($updatekeys as $key) {
882 $col = $objimport->array_import_updatekeys[0][$key];
883 $key = preg_replace(
'/^.*\./i',
'', $key);
884 if (
isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
885 $tmp = explode(
"_", $key);
887 $socialnetwork = $tmp[1];
888 $jsondata = $data[$key];
889 $json = json_decode($jsondata);
890 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
893 $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
894 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
897 $where[] = $key.
' = '.$data[$key];
898 $filters[] = $col.
' = '.$data[$key];
901 if (!empty($tablewithentity_cache[$tablename])) {
905 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
907 $resql = $this->db->query($sqlSelect);
909 $num_rows = $this->db->num_rows($resql);
910 if ($num_rows == 1) {
911 $res = $this->db->fetch_object($resql);
912 $lastinsertid = $res->rowid;
913 if ($is_table_category_link) {
914 $lastinsertid =
'linktable';
916 $last_insert_id_array[$tablename] = $lastinsertid;
917 } elseif ($num_rows > 1) {
918 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
919 $this->errors[$error][
'type'] =
'SQL';
926 $this->errors[$error][
'lib'] = $this->db->lasterror();
927 $this->errors[$error][
'type'] =
'SQL';
936 $sqlSelect =
"SELECT rowid FROM ".$tablename;
938 if (empty($keyfield)) {
941 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
943 if (!empty($tablewithentity_cache[$tablename])) {
947 $resql = $this->db->query($sqlSelect);
949 $res = $this->db->fetch_object($resql);
950 if ($this->db->num_rows($resql) == 1) {
959 $this->errors[$error][
'lib'] = $this->db->lasterror();
960 $this->errors[$error][
'type'] =
'SQL';
965 if (!empty($lastinsertid)) {
967 if (in_array(
"socialnetworks", $listfields)) {
968 $socialkey = array_search(
"socialnetworks", $listfields);
969 $tmpsql = $listvalues[$socialkey];
970 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
974 $sqlstart =
"UPDATE ".$tablename;
976 $data = array_combine($listfields, $listvalues);
978 foreach ($data as $key => $val) {
979 $set[] = $key.
" = ".$val;
981 $sqlstart .=
" SET ".implode(
', ', $set).
", import_key = '".$this->db->escape($importid).
"'";
983 if (empty($keyfield)) {
986 $sqlend =
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
988 if ($is_table_category_link) {
989 '@phan-var-force string[] $where';
990 $sqlend =
" WHERE " . implode(
' AND ', $where);
993 if (!empty($tablewithentity_cache[$tablename])) {
997 $sql = $sqlstart.$sqlend;
1000 $resql = $this->db->query(
$sql);
1006 $this->errors[$error][
'lib'] = $this->db->lasterror();
1007 $this->errors[$error][
'type'] =
'SQL';
1014 if (!$error && !$updatedone) {
1016 if (in_array(
"socialnetworks", $listfields)) {
1017 $socialkey = array_search(
"socialnetworks", $listfields);
1018 $tmpsql = $listvalues[$socialkey];
1019 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1023 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $listfields).
", import_key";
1024 $sqlend =
") VALUES(".implode(
', ', $listvalues).
", '".$this->db->escape($importid).
"'";
1025 if (!empty($tablewithentity_cache[$tablename])) {
1026 $sqlstart .=
", entity";
1027 $sqlend .=
", ".$conf->entity;
1029 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1030 $sqlstart .=
", ".$objimport->array_import_tables_creator[0][$alias];
1031 $sqlend .=
", ".$user->id;
1033 $sql = $sqlstart.$sqlend.
")";
1038 $resql = $this->db->query(
$sql);
1040 if (!$is_table_category_link) {
1041 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1046 $this->errors[$error][
'lib'] = $this->db->lasterror();
1047 $this->errors[$error][
'type'] =
'SQL';
1084 return str_replace(array(
',',
';'),
'/', $value);
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.
getElementFromTableWithPrefix($tableNameWithPrefix)
Get element from table name with prefix.
Class to manage third parties objects (customers, suppliers, prospects...)
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
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...
dol_count_nb_of_line($file)
Count number of lines in a file.
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_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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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,...
div float
Buy price without taxes.