395 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
398 global $langs,
$conf, $user;
399 global $thirdparty_static;
400 global $tablewithentity_cache;
404 $this->errors = array();
405 $this->warnings = array();
412 $array_match_database_to_file = array_flip($array_match_file_to_database);
413 $sort_array_match_file_to_database = $array_match_file_to_database;
414 ksort($sort_array_match_file_to_database);
418 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1][
'val']))) {
420 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
421 $this->warnings[$warning][
'type'] =
'EMPTY';
424 $last_insert_id_array = array();
428 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
431 $listfields = array();
432 $listvalues = array();
434 $errorforthistable = 0;
437 if (!isset($tablewithentity_cache[$tablename])) {
438 dol_syslog(
"Check if table " . $tablename .
" has an entity field");
439 $resql = $this->db->DDLDescTable($tablename,
'entity');
441 $obj = $this->db->fetch_object($resql);
443 $tablewithentity_cache[$tablename] = 1;
445 $tablewithentity_cache[$tablename] = 0;
455 $arrayfield = array();
456 foreach ($sort_array_match_file_to_database as $key => $val) {
457 $arrayfield[$val] = ($key);
464 foreach ($sort_array_match_file_to_database as $key => $val) {
465 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
466 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
468 if ($alias != $fieldalias) {
472 if ($key <= $maxfields) {
475 if ($arrayrecord[($key)][
'type'] > 0) {
476 $newval = $arrayrecord[($key)][
'val'];
485 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
486 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue',
num2Alpha($key - 1));
487 $this->errors[$error][
'type'] =
'NOTNULL';
488 $errorforthistable++;
493 if (!empty($objimport->array_import_convertvalue[0][$val])) {
495 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
496 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
497 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
501 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
504 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
507 if ($isidorref ==
'ref') {
508 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
509 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
510 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
511 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] !=
'') {
512 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval];
515 if (empty($resultload)) {
516 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
519 $classinstance =
new $class($this->db);
520 if ($class ==
'CGenericDic') {
521 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
522 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
526 $param_array = array(
'', $newval);
527 if ($class ==
'AccountingAccount') {
540 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
543 $result = call_user_func_array(array($classinstance, $method), $param_array);
546 if (!($classinstance->id !=
'') && $result == -2) {
547 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
548 $this->errors[$error][
'type'] =
'FOREIGNKEY';
549 $errorforthistable++;
554 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
555 $param_array = array(
'',
'', $newval);
556 call_user_func_array(array($classinstance, $method), $param_array);
558 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] = $classinstance->id;
561 if ($classinstance->id !=
'') {
562 $newval = $classinstance->id;
563 } elseif (! $error) {
564 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
566 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
567 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
569 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
571 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
573 $this->errors[$error][
'type'] =
'FOREIGNKEY';
574 $errorforthistable++;
579 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
581 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
584 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
586 if ($isidorref ==
'ref') {
587 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
588 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
589 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
590 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
591 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
592 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] !=
'') {
593 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval];
596 if (empty($resultload)) {
597 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', code=' . $code);
600 $classinstance =
new $class($this->db);
602 $param_array = array(
'', $newval, $code);
603 call_user_func_array(array($classinstance, $method), $param_array);
604 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] = $classinstance->id;
605 if ($classinstance->id > 0) {
606 $newval = $classinstance->id;
608 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
610 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
612 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
614 $this->errors[$error][
'type'] =
'FOREIGNKEY';
615 $errorforthistable++;
620 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
621 if (empty($newval)) {
624 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
625 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
626 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
627 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
628 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
629 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] !=
'') {
630 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval];
633 if (empty($resultload)) {
634 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', units=' . $units);
637 $classinstance =
new $class($this->db);
639 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
640 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
641 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] = $scaleorid;
643 if ($classinstance->id > 0) {
644 $newval = $scaleorid ? $scaleorid : 0;
646 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
648 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
650 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
652 $this->errors[$error][
'type'] =
'FOREIGNKEY';
653 $errorforthistable++;
657 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
658 if (strtolower($newval) ==
'auto') {
659 $this->thirdpartyobject->get_codeclient(
null, 0);
660 $newval = $this->thirdpartyobject->code_client;
663 if (empty($newval)) {
664 $arrayrecord[($key)][
'type'] = -1;
666 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
667 if (strtolower($newval) ==
'auto') {
668 $this->thirdpartyobject->get_codefournisseur(
null, 1);
669 $newval = $this->thirdpartyobject->code_fournisseur;
672 if (empty($newval)) {
673 $arrayrecord[($key)][
'type'] = -1;
675 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
676 if (strtolower($newval) ==
'auto') {
677 $this->thirdpartyobject->get_codecompta(
'customer');
678 $newval = $this->thirdpartyobject->code_compta_client;
681 if (empty($newval)) {
682 $arrayrecord[($key)][
'type'] = -1;
684 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
685 if (strtolower($newval) ==
'auto') {
686 $this->thirdpartyobject->get_codecompta(
'supplier');
687 $newval = $this->thirdpartyobject->code_compta_fournisseur;
688 if (empty($newval)) {
689 $arrayrecord[($key)][
'type'] = -1;
693 if (empty($newval)) {
694 $arrayrecord[($key)][
'type'] = -1;
696 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
697 if (strtolower($newval) ==
'auto') {
700 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
701 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
703 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
704 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
705 $modForNumber =
new $classModForNumber();
706 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
710 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
711 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
712 require_once DOL_DOCUMENT_ROOT.$pathForObject;
713 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
714 $tmpobject =
new $tmpclassobject($this->db);
715 foreach ($arrayfield as $tmpkey => $tmpval) {
716 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
717 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
722 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
724 if (is_numeric($defaultref) && $defaultref <= 0) {
727 $newval = $defaultref;
729 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
730 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
731 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
732 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
734 if (empty($resultload)) {
735 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
738 $classinstance =
new $class($this->db);
739 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
740 if (empty($classinstance->error) && empty($classinstance->errors)) {
743 $this->errors[$error][
'type'] =
'CLASSERROR';
744 $this->errors[$error][
'lib'] = implode(
746 array_merge([$classinstance->error], $classinstance->errors)
748 $errorforthistable++;
751 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
753 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
755 $newval = rtrim(trim($newval),
"0");
757 $newval = trim($newval);
765 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
768 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
771 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
773 $cachekey = $field .
'@' . $table;
774 if (!empty($filter)) {
775 $cachekey .=
':' . $filter;
779 if (!is_array($this->cachefieldtable[$cachekey])) {
780 $sql =
"SELECT " . $field .
" as aliasfield FROM " . $table;
781 if (!empty($filter)) {
782 $sql .=
' WHERE ' . $filter;
786 $resql = $this->db->query($sql);
788 $num = $this->db->num_rows($resql);
791 $obj = $this->db->fetch_object($resql);
793 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
803 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
804 $tableforerror = $table;
805 if (!empty($filter)) {
806 $tableforerror .=
':' . $filter;
809 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
810 $this->errors[$error][
'type'] =
'FOREIGNKEY';
811 $errorforthistable++;
814 } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] .
'/i', $newval)) {
818 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
819 $this->errors[$error][
'type'] =
'REGEX';
820 $errorforthistable++;
829 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key,
dol_trunc($newval, 100));
830 $this->errors[$error][
'type'] =
'HTMLINJECTION';
831 $errorforthistable++;
840 if (isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
841 if (!in_array(
"socialnetworks", $listfields)) {
842 $listfields[] =
"socialnetworks";
843 $socialkey = array_search(
"socialnetworks", $listfields);
844 $listvalues[$socialkey] =
'';
846 if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
847 $socialkey = array_search(
"socialnetworks", $listfields);
848 $socialnetwork = explode(
"_", $fieldname)[1];
849 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
851 $json->$socialnetwork = $newval;
852 $listvalues[$socialkey] = json_encode($json);
854 $jsondata = $listvalues[$socialkey];
855 $json = json_decode($jsondata);
856 $json->$socialnetwork = $newval;
857 $listvalues[$socialkey] = json_encode($json);
861 $listfields[] = $fieldname;
864 if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
865 $listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
866 } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
867 $listvalues[] =
"''";
869 $listvalues[] =
"'".$this->db->escape($newval).
"'";
879 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
881 foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
882 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $key)) {
885 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
886 if (in_array($keyfieldcache, $listfields)) {
888 } elseif ($val ==
'user->id') {
889 $listfields[] = $keyfieldcache;
890 $listvalues[] = ((int) $user->id);
891 } elseif (preg_match(
'/^lastrowid-/', $val)) {
892 $tmp = explode(
'-', $val);
893 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
894 $listfields[] = $keyfieldcache;
895 $keyfield = $keyfieldcache;
896 $listvalues[] = (int) $lastinsertid;
898 } elseif (preg_match(
'/^const-/', $val)) {
899 $tmp = explode(
'-', $val, 2);
900 $listfields[] = $keyfieldcache;
901 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
902 } elseif (preg_match(
'/^rule-/', $val)) {
904 $classinstance =
null;
905 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
906 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
907 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
908 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
909 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
910 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
912 if (empty($resultload)) {
913 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
916 $classinstance =
new $class($this->db);
917 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
918 $fieldArr = explode(
'.', $fieldname);
919 if (count($fieldArr) > 0) {
920 $fieldname = $fieldArr[1];
924 $listfields[] = $fieldname;
925 if ($type ==
'int') {
926 $listvalues[] = (int) $res;
927 } elseif ($type ==
'double') {
928 $listvalues[] = (float) $res;
930 $listvalues[] =
"'".$this->db->escape($res).
"'";
933 $this->errors[$error][
'type'] =
'CLASSERROR';
934 if (is_object($classinstance)) {
935 $this->errors[$error][
'lib'] = implode(
937 array_merge([$classinstance->error], $classinstance->errors)
940 $this->errors[$error][
'lib']
941 =
"Unexpected rule ".$objimport->array_import_convertvalue[0][$fieldname][
'rule'];
944 $errorforthistable++;
949 $this->errors[$error][
'lib'] =
'Bad value of profile setup ' . $val .
' for array_import_fieldshidden';
950 $this->errors[$error][
'type'] =
'Import profile setup';
959 if (!$errorforthistable) {
961 if (!empty($listfields)) {
965 $is_table_category_link =
false;
967 if (strpos($tablename,
'_categorie_') !==
false) {
968 $is_table_category_link =
true;
972 if (!empty($updatekeys)) {
975 if (empty($lastinsertid)) {
976 $sqlSelect =
"SELECT ".$fname.
" FROM " . $tablename;
978 $data = array_combine($listfields, $listvalues);
982 '@phan-var string[] $where';
984 foreach ($updatekeys as $key) {
985 $col = $objimport->array_import_updatekeys[0][$key];
986 $key = preg_replace(
'/^.*\./i',
'', $key);
987 if (isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
988 $tmp = explode(
"_", $key);
990 $socialnetwork = $tmp[1];
991 $jsondata = $data[$key];
992 $json = json_decode($jsondata);
993 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
996 $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
997 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1000 $where[] = $key.
' = '.$data[$key];
1001 $filters[] = $col.
' = '.$data[$key];
1004 if (!empty($tablewithentity_cache[$tablename])) {
1008 $sqlSelect .=
" WHERE " . implode(
' AND ', $where);
1010 $resql = $this->db->query($sqlSelect);
1012 $num_rows = $this->db->num_rows($resql);
1013 if ($num_rows == 1) {
1014 $res = $this->db->fetch_object($resql);
1015 $lastinsertid = $res->rowid;
1016 if ($is_table_category_link) {
1017 $lastinsertid =
'linktable';
1019 $last_insert_id_array[$tablename] = $lastinsertid;
1020 } elseif ($num_rows > 1) {
1021 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1022 $this->errors[$error][
'type'] =
'SQL';
1029 $this->errors[$error][
'lib'] = $this->db->lasterror();
1030 $this->errors[$error][
'type'] =
'SQL';
1039 $sqlSelect =
"SELECT rowid FROM " . $tablename;
1042 if (empty($keyfield)) {
1043 $keyfield =
'rowid';
1045 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
1047 if (!empty($tablewithentity_cache[$tablename])) {
1051 $resql = $this->db->query($sqlSelect);
1053 $res = $this->db->fetch_object($resql);
1054 if ($this->db->num_rows($resql) == 1) {
1063 $this->errors[$error][
'lib'] = $this->db->lasterror();
1064 $this->errors[$error][
'type'] =
'SQL';
1069 if (!empty($lastinsertid)) {
1071 if (in_array(
"socialnetworks", $listfields)) {
1072 $socialkey = array_search(
"socialnetworks", $listfields);
1073 $tmpsql = $listvalues[$socialkey];
1074 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1078 $sqlstart =
"UPDATE " . $tablename;
1080 $data = array_combine($listfields, $listvalues);
1082 foreach ($data as $key => $val) {
1083 $set[] = $key.
" = ".$val;
1085 $sqlstart .=
" SET " . implode(
', ', $set) .
", import_key = '" . $this->db->escape($importid) .
"'";
1086 if (empty($keyfield)) {
1087 $keyfield =
'rowid';
1089 $sqlend =
" WHERE " . $keyfield .
" = ".((int) $lastinsertid);
1091 if ($is_table_category_link) {
1092 '@phan-var-force string[] $where';
1093 $sqlend =
" WHERE " . implode(
' AND ', $where);
1096 if (!empty($tablewithentity_cache[$tablename])) {
1100 $sql = $sqlstart . $sqlend;
1103 $resql = $this->db->query($sql);
1109 $this->errors[$error][
'lib'] = $this->db->lasterror();
1110 $this->errors[$error][
'type'] =
'SQL';
1117 if (!$error && !$updatedone) {
1119 if (in_array(
"socialnetworks", $listfields)) {
1120 $socialkey = array_search(
"socialnetworks", $listfields);
1121 $tmpsql = $listvalues[$socialkey];
1122 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1126 $sqlstart =
"INSERT INTO " . $tablename .
"(" . implode(
", ", $listfields) .
", import_key";
1127 $sqlend =
") VALUES(" . implode(
', ', $listvalues) .
", '" . $this->db->escape($importid) .
"'";
1128 if (!empty($tablewithentity_cache[$tablename])) {
1129 $sqlstart .=
", entity";
1130 $sqlend .=
", " .
$conf->entity;
1132 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1133 $sqlstart .=
", " . $objimport->array_import_tables_creator[0][$alias];
1134 $sqlend .=
", " . $user->id;
1136 $sql = $sqlstart . $sqlend .
")";
1141 $resql = $this->db->query($sql);
1143 if (!$is_table_category_link) {
1144 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1149 $this->errors[$error][
'lib'] = $this->db->lasterror();
1150 $this->errors[$error][
'type'] =
'SQL';