341 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
344 global $langs,
$conf, $user;
345 global $thirdparty_static;
346 global $tablewithentity_cache;
350 $this->errors = array();
351 $this->warnings = array();
358 $array_match_database_to_file = array_flip($array_match_file_to_database);
359 $sort_array_match_file_to_database = $array_match_file_to_database;
360 ksort($sort_array_match_file_to_database);
364 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
366 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
367 $this->warnings[$warning][
'type'] =
'EMPTY';
370 $last_insert_id_array = array();
374 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
377 $listfields = array();
378 $listvalues = array();
380 $errorforthistable = 0;
383 if (!isset($tablewithentity_cache[$tablename])) {
384 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
385 $resql = $this->db->DDLDescTable($tablename,
'entity');
387 $obj = $this->db->fetch_object($resql);
389 $tablewithentity_cache[$tablename] = 1;
391 $tablewithentity_cache[$tablename] = 0;
401 $arrayfield = array();
402 foreach ($sort_array_match_file_to_database as $key => $val) {
403 $arrayfield[$val] = ($key - 1);
410 foreach ($sort_array_match_file_to_database as $key => $val) {
411 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
412 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
414 if ($alias != $fieldalias) {
418 if ($key <= $maxfields) {
421 if ($arrayrecord[($key - 1)][
'type'] > 0) {
422 $newval = $arrayrecord[($key - 1)][
'val'];
431 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
433 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
434 $this->errors[$error][
'type'] =
'NOTNULL';
435 $errorforthistable++;
440 if (!empty($objimport->array_import_convertvalue[0][$val])) {
442 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
443 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
444 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
448 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
452 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
455 if ($isidorref ==
'ref') {
456 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
457 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
458 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
459 if (isset($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval]) && $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] != $newval) {
460 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval];
463 if (empty($resultload)) {
464 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
467 $classinstance =
new $class($this->db);
468 if ($class ==
'CGenericDic') {
469 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
470 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
474 $param_array = array(
'', $newval);
475 if ($class ==
'AccountingAccount') {
488 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
490 if ($class ==
'CActionComm') {
491 $param_array = array($newval);
493 $result = call_user_func_array(array($classinstance, $method), $param_array);
496 if (!($classinstance->id !=
'') && $result == -2) {
497 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
498 $this->errors[$error][
'type'] =
'FOREIGNKEY';
499 $errorforthistable++;
504 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
505 $param_array = array(
'',
'', $newval);
506 call_user_func_array(array($classinstance, $method), $param_array);
508 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] = $classinstance->id;
511 if ($classinstance->id !=
'') {
512 $newval = $classinstance->id;
513 } elseif (! $error) {
514 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
515 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
516 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
517 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
519 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
521 $this->errors[$error][
'type'] =
'FOREIGNKEY';
522 $errorforthistable++;
527 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
529 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
532 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
534 if ($isidorref ==
'ref') {
535 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
536 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
537 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
538 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
539 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
540 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] !=
'') {
541 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval];
544 if (empty($resultload)) {
545 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
548 $classinstance =
new $class($this->db);
550 $param_array = array(
'', $newval, $code);
551 call_user_func_array(array($classinstance, $method), $param_array);
552 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] = $classinstance->id;
553 if ($classinstance->id > 0) {
554 $newval = $classinstance->id;
556 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
557 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
559 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
561 $this->errors[$error][
'type'] =
'FOREIGNKEY';
562 $errorforthistable++;
567 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
568 if (empty($newval)) {
571 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
572 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
573 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
574 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
575 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
576 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] !=
'') {
577 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval];
580 if (empty($resultload)) {
581 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
584 $classinstance =
new $class($this->db);
586 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
587 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
588 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] = $scaleorid;
590 if ($classinstance->id > 0) {
591 $newval = $scaleorid ? $scaleorid : 0;
593 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
594 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
596 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
598 $this->errors[$error][
'type'] =
'FOREIGNKEY';
599 $errorforthistable++;
603 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
604 if (strtolower($newval) ==
'auto') {
605 $this->thirdpartyobject->get_codeclient(
null, 0);
606 $newval = $this->thirdpartyobject->code_client;
609 if (empty($newval)) {
610 $arrayrecord[($key - 1)][
'type'] = -1;
612 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
613 if (strtolower($newval) ==
'auto') {
614 $this->thirdpartyobject->get_codefournisseur(
null, 1);
615 $newval = $this->thirdpartyobject->code_fournisseur;
618 if (empty($newval)) {
619 $arrayrecord[($key - 1)][
'type'] = -1;
621 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
622 if (strtolower($newval) ==
'auto') {
623 $this->thirdpartyobject->get_codecompta(
'customer');
624 $newval = $this->thirdpartyobject->code_compta_client;
627 if (empty($newval)) {
628 $arrayrecord[($key - 1)][
'type'] = -1;
630 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
631 if (strtolower($newval) ==
'auto') {
632 $this->thirdpartyobject->get_codecompta(
'supplier');
633 $newval = $this->thirdpartyobject->code_compta_fournisseur;
634 if (empty($newval)) {
635 $arrayrecord[($key - 1)][
'type'] = -1;
639 if (empty($newval)) {
640 $arrayrecord[($key - 1)][
'type'] = -1;
642 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
643 if (strtolower($newval) ==
'auto') {
646 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
647 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
649 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
650 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
651 $modForNumber =
new $classModForNumber();
652 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
656 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
657 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
658 require_once DOL_DOCUMENT_ROOT.$pathForObject;
659 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
660 $tmpobject =
new $tmpclassobject($this->db);
661 foreach ($arrayfield as $tmpkey => $tmpval) {
662 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
663 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
668 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
670 if (is_numeric($defaultref) && $defaultref <= 0) {
673 $newval = $defaultref;
675 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
676 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
677 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
678 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
680 if (empty($resultload)) {
681 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
684 $classinstance =
new $class($this->db);
685 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, ($key - 1)));
686 if (empty($classinstance->error) && empty($classinstance->errors)) {
689 $this->errors[$error][
'type'] =
'CLASSERROR';
690 $this->errors[$error][
'lib'] = implode(
692 array_merge([$classinstance->error], $classinstance->errors)
694 $errorforthistable++;
697 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
699 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
701 $newval = rtrim(trim($newval),
"0");
703 $newval = trim($newval);
711 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
714 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
717 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
719 $cachekey = $field.
'@'.$table;
720 if (!empty($filter)) {
721 $cachekey .=
':'.$filter;
725 if (!is_array($this->cachefieldtable[$cachekey])) {
726 $sql =
"SELECT ".$field.
" as aliasfield FROM ".$table;
727 if (!empty($filter)) {
728 $sql .=
' WHERE '.$filter;
731 $resql = $this->db->query($sql);
733 $num = $this->db->num_rows($resql);
736 $obj = $this->db->fetch_object($resql);
738 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
748 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
749 $tableforerror = $table;
750 if (!empty($filter)) {
751 $tableforerror .=
':'.$filter;
753 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
754 $this->errors[$error][
'type'] =
'FOREIGNKEY';
755 $errorforthistable++;
758 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
761 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
762 $this->errors[$error][
'type'] =
'REGEX';
763 $errorforthistable++;
771 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
772 $this->errors[$error][
'type'] =
'HTMLINJECTION';
773 $errorforthistable++;
782 if (isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
783 if (!in_array(
"socialnetworks", $listfields)) {
784 $listfields[] =
"socialnetworks";
785 $socialkey = array_search(
"socialnetworks", $listfields);
786 $listvalues[$socialkey] =
'';
789 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
790 $socialkey = array_search(
"socialnetworks", $listfields);
792 $socialnetwork = explode(
"_", $fieldname)[1];
793 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
795 $json->$socialnetwork = $newval;
796 $listvalues[$socialkey] = json_encode($json);
798 $jsondata = $listvalues[$socialkey];
799 $json = json_decode($jsondata);
800 $json->$socialnetwork = $newval;
801 $listvalues[$socialkey] = json_encode($json);
805 $listfields[] = $fieldname;
807 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
808 $listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
809 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
810 $listvalues[] =
"''";
812 $listvalues[] =
"'".$this->db->escape($newval).
"'";
822 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
824 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
825 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
828 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
830 if (in_array($keyfieldcache, $listfields)) {
832 } elseif ($tmpval ==
'user->id') {
833 $listfields[] = $keyfieldcache;
834 $listvalues[] = ((int) $user->id);
835 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
836 $tmp = explode(
'-', $tmpval);
837 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
838 $listfields[] = $keyfieldcache;
839 $listvalues[] = (int) $lastinsertid;
840 $keyfield = $keyfieldcache;
842 } elseif (preg_match(
'/^const-/', $tmpval)) {
843 $tmp = explode(
'-', $tmpval, 2);
844 $listfields[] = $keyfieldcache;
845 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
846 } elseif (preg_match(
'/^rule-/', $tmpval)) {
847 $fieldname = $tmpkey;
848 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
849 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
850 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
851 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
852 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
853 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
855 if (empty($resultload)) {
856 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
859 $classinstance =
new $class($this->db);
860 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, ($key - 1)));
861 if (empty($classinstance->error) && empty($classinstance->errors)) {
862 $fieldArr = explode(
'.', $fieldname);
863 if (count($fieldArr) > 0) {
864 $fieldname = $fieldArr[1];
868 $listfields[] = $fieldname;
869 if ($type ==
'int') {
870 $listvalues[] = (int) $res;
871 } elseif ($type ==
'double') {
872 $listvalues[] = (float) $res;
874 $listvalues[] =
"'".$this->db->escape($res).
"'";
877 $this->errors[$error][
'type'] =
'CLASSERROR';
878 $this->errors[$error][
'lib'] = implode(
880 array_merge([$classinstance->error], $classinstance->errors)
882 $errorforthistable++;
888 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
889 $this->errors[$error][
'type'] =
'Import profile setup';
898 if (!$errorforthistable) {
900 if (!empty($listfields)) {
904 $is_table_category_link =
false;
906 if (strpos($tablename,
'_categorie_') !==
false) {
907 $is_table_category_link =
true;
911 if (!empty($updatekeys)) {
914 if (empty($lastinsertid)) {
915 $sqlSelect =
"SELECT ".$fname.
" FROM ".$tablename;
916 $data = array_combine($listfields, $listvalues);
919 foreach ($updatekeys as $key) {
920 $col = $objimport->array_import_updatekeys[0][$key];
921 $key = preg_replace(
'/^.*\./i',
'', $key);
922 if (isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
923 $tmp = explode(
"_", $key);
925 $socialnetwork = $tmp[1];
926 $jsondata = $data[$key];
927 $json = json_decode($jsondata);
928 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
931 $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
932 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
935 $where[] = $key.
' = '.$data[$key];
936 $filters[] = $col.
' = '.$data[$key];
939 if (!empty($tablewithentity_cache[$tablename])) {
943 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
945 $resql = $this->db->query($sqlSelect);
947 $num_rows = $this->db->num_rows($resql);
948 if ($num_rows == 1) {
949 $res = $this->db->fetch_object($resql);
950 $lastinsertid = $res->rowid;
952 if ($is_table_category_link) {
953 $lastinsertid =
'linktable';
955 $last_insert_id_array[$tablename] = $lastinsertid;
956 } elseif ($num_rows > 1) {
957 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
958 $this->errors[$error][
'type'] =
'SQL';
965 $this->errors[$error][
'lib'] = $this->db->lasterror();
966 $this->errors[$error][
'type'] =
'SQL';
975 $sqlSelect =
"SELECT rowid FROM ".$tablename;
976 if (empty($keyfield)) {
980 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
982 if (!empty($tablewithentity_cache[$tablename])) {
986 $resql = $this->db->query($sqlSelect);
988 $res = $this->db->fetch_object($resql);
989 if ($this->db->num_rows($resql) == 1) {
998 $this->errors[$error][
'lib'] = $this->db->lasterror();
999 $this->errors[$error][
'type'] =
'SQL';
1004 if (!empty($lastinsertid)) {
1006 if (in_array(
"socialnetworks", $listfields)) {
1007 $socialkey = array_search(
"socialnetworks", $listfields);
1008 $tmpsql = $listvalues[$socialkey];
1009 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1013 $sqlstart =
"UPDATE ".$tablename;
1015 $data = array_combine($listfields, $listvalues);
1017 foreach ($data as $key => $val) {
1018 $set[] = $key.
" = ".$val;
1020 $sqlstart .=
" SET ".implode(
', ', $set).
", import_key = '".$this->db->escape($importid).
"'";
1021 if (empty($keyfield)) {
1022 $keyfield =
'rowid';
1025 $sqlend =
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
1027 if ($is_table_category_link) {
1028 '@phan-var-force string[] $where';
1029 $sqlend =
" WHERE " . implode(
' AND ', $where);
1032 if (!empty($tablewithentity_cache[$tablename])) {
1036 $sql = $sqlstart.$sqlend;
1039 $resql = $this->db->query($sql);
1045 $this->errors[$error][
'lib'] = $this->db->lasterror();
1046 $this->errors[$error][
'type'] =
'SQL';
1053 if (!$error && !$updatedone) {
1055 if (in_array(
"socialnetworks", $listfields)) {
1056 $socialkey = array_search(
"socialnetworks", $listfields);
1057 $tmpsql = $listvalues[$socialkey];
1058 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1062 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $listfields).
", import_key";
1063 $sqlend =
") VALUES(".implode(
', ', $listvalues).
", '".$this->db->escape($importid).
"'";
1064 if (!empty($tablewithentity_cache[$tablename])) {
1065 $sqlstart .=
", entity";
1066 $sqlend .=
", ".$conf->entity;
1068 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1069 $sqlstart .=
", ".$objimport->array_import_tables_creator[0][$alias];
1070 $sqlend .=
", ".$user->id;
1072 $sql = $sqlstart.$sqlend.
")";
1077 $resql = $this->db->query($sql);
1079 if (!$is_table_category_link) {
1080 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1085 $this->errors[$error][
'lib'] = $this->db->lasterror();
1086 $this->errors[$error][
'type'] =
'SQL';