1002 protected function commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, $recordpositionbase = 0)
1004 global $langs,
$conf, $user;
1005 global $thirdparty_static;
1006 global $tablewithentity_cache;
1008 if (is_array($arrayrecord) && !empty($recordpositionbase)) {
1009 $arrayrecord = array_values($arrayrecord);
1015 $importissimulation = !empty($this->importissimulation);
1016 $this->errors = array();
1017 $this->warnings = array();
1024 $array_match_database_to_file = array_flip($array_match_file_to_database);
1025 $sort_array_match_file_to_database = $array_match_file_to_database;
1026 ksort($sort_array_match_file_to_database);
1030 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
1032 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
1033 $this->warnings[$warning][
'type'] =
'EMPTY';
1036 $last_insert_id_array = array();
1037 $updatedone =
false;
1038 $insertdone =
false;
1040 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
1043 $listfields = array();
1044 $listvalues = array();
1046 $errorforthistable = 0;
1049 if (!isset($tablewithentity_cache[$tablename])) {
1050 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
1051 $resql = $this->db->DDLDescTable($tablename,
'entity');
1053 $obj = $this->db->fetch_object($resql);
1055 $tablewithentity_cache[$tablename] = 1;
1057 $tablewithentity_cache[$tablename] = 0;
1067 $arrayfield = array();
1068 foreach ($sort_array_match_file_to_database as $key => $val) {
1069 $arrayfield[$val] = ($key - 1);
1076 foreach ($sort_array_match_file_to_database as $key => $val) {
1077 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
1078 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
1080 if ($alias != $fieldalias) {
1084 if ($key <= $maxfields) {
1087 if ($arrayrecord[($key - 1)][
'type'] > 0) {
1088 $newval = $arrayrecord[($key - 1)][
'val'];
1097 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((
string) $newval ==
'')) {
1099 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
1100 $this->errors[$error][
'type'] =
'NOTNULL';
1101 $errorforthistable++;
1106 if (!empty($objimport->array_import_convertvalue[0][$val])) {
1108 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
1109 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
1110 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
1114 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1118 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1121 if ($isidorref ==
'ref') {
1122 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1123 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1124 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1125 $cachekey = $file.
'_'.$class.
'_'.$method.
'_';
1126 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1127 $newval = $this->cacheconvert[$cachekey][$newval];
1130 if (empty($resultload)) {
1131 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1134 $classinstance =
new $class($this->db);
1135 if ($class ==
'CGenericDic') {
1136 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
1137 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
1141 $param_array = array(
'', $newval);
1142 if ($class ==
'AccountingAccount') {
1155 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
1157 if ($class ==
'CActionComm') {
1158 $param_array = array($newval);
1160 $result = call_user_func_array(array($classinstance, $method), $param_array);
1163 if (!($classinstance->id !=
'') && $result == -2) {
1164 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
1165 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1166 $errorforthistable++;
1171 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
1172 $param_array = array(
'',
'', $newval);
1173 call_user_func_array(array($classinstance, $method), $param_array);
1175 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1178 if ($classinstance->id !=
'') {
1179 $newval = $classinstance->id;
1180 } elseif (! $error) {
1181 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1182 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1183 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
1184 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
1186 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
1188 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1189 $errorforthistable++;
1194 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
1196 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1199 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1201 if ($isidorref ==
'ref') {
1202 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1203 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1204 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1205 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
1206 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
1207 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$code;
1208 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1209 $newval = $this->cacheconvert[$cachekey][$newval];
1212 if (empty($resultload)) {
1213 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
1216 $classinstance =
new $class($this->db);
1218 $param_array = array(
'', $newval, $code);
1219 call_user_func_array(array($classinstance, $method), $param_array);
1220 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1221 if ($classinstance->id > 0) {
1222 $newval = $classinstance->id;
1224 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1225 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1227 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1229 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1230 $errorforthistable++;
1235 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
1236 if (empty($newval)) {
1239 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
1240 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1241 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1242 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1243 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
1244 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$units;
1245 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1246 $newval = $this->cacheconvert[$cachekey][$newval];
1249 if (empty($resultload)) {
1250 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
1253 $classinstance =
new $class($this->db);
1255 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
1256 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
1257 $this->cacheconvert[$cachekey][$newval] = $scaleorid;
1259 if ($classinstance->id > 0) {
1260 $newval = $scaleorid ? $scaleorid : 0;
1262 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1263 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1265 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1267 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1268 $errorforthistable++;
1272 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
1273 if (strtolower($newval) ==
'auto') {
1274 $this->thirdpartyobject->get_codeclient(
null, 0);
1275 $newval = $this->thirdpartyobject->code_client;
1278 if (empty($newval)) {
1279 $arrayrecord[($key - 1)][
'type'] = -1;
1281 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
1282 if (strtolower($newval) ==
'auto') {
1283 $this->thirdpartyobject->get_codefournisseur(
null, 1);
1284 $newval = $this->thirdpartyobject->code_fournisseur;
1287 if (empty($newval)) {
1288 $arrayrecord[($key - 1)][
'type'] = -1;
1290 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
1291 if (strtolower($newval) ==
'auto') {
1292 $this->thirdpartyobject->get_codecompta(
'customer');
1293 $newval = $this->thirdpartyobject->code_compta_client;
1296 if (empty($newval)) {
1297 $arrayrecord[($key - 1)][
'type'] = -1;
1299 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
1300 if (strtolower($newval) ==
'auto') {
1301 $this->thirdpartyobject->get_codecompta(
'supplier');
1302 $newval = $this->thirdpartyobject->code_compta_fournisseur;
1303 if (empty($newval)) {
1304 $arrayrecord[($key - 1)][
'type'] = -1;
1308 if (empty($newval)) {
1309 $arrayrecord[($key - 1)][
'type'] = -1;
1311 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
1312 if (strtolower($newval) ==
'auto') {
1315 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
1316 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
1318 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
1319 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
1320 $modForNumber =
new $classModForNumber();
1321 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
1325 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
1326 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
1327 require_once DOL_DOCUMENT_ROOT.$pathForObject;
1328 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
1329 $tmpobject =
new $tmpclassobject($this->db);
1330 foreach ($arrayfield as $tmpkey => $tmpval) {
1331 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
1332 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
1337 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
1339 if (is_numeric($defaultref) && $defaultref <= 0) {
1342 $newval = $defaultref;
1344 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
1345 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1346 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1347 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1349 if (empty($resultload)) {
1350 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1353 $classinstance =
new $class($this->db);
1354 $computedFieldPos = isset($arrayfield[$val]) ? ((int) $arrayfield[$val]) : 0;
1355 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1356 if (empty($classinstance->error) && empty($classinstance->errors)) {
1359 $this->errors[$error][
'type'] =
'CLASSERROR';
1360 $this->errors[$error][
'lib'] = implode(
1362 array_merge([$classinstance->error], $classinstance->errors)
1364 $errorforthistable++;
1367 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
1369 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
1371 $newval = rtrim(trim($newval),
"0");
1373 $newval = trim($newval);
1381 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
1384 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
1387 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
1389 $cachekey = $field.
'@'.$table;
1390 if (!empty($filter)) {
1391 $cachekey .=
':'.$filter;
1395 if (!is_array($this->cachefieldtable[$cachekey])) {
1396 $sql =
"SELECT ".$this->db->sanitize($field).
" as aliasfield FROM ".$this->db->sanitize($table);
1397 if (!empty($filter)) {
1398 $sql .= forgeSQLFromUniversalSearchCriteria($filter);
1401 $resql = $this->db->query($sql);
1403 $num = $this->db->num_rows($resql);
1406 $obj = $this->db->fetch_object($resql);
1408 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
1418 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
1419 $tableforerror = $table;
1420 if (!empty($filter)) {
1421 $tableforerror .=
':'.$filter;
1423 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
1424 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1425 $errorforthistable++;
1428 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
1431 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
1432 $this->errors[$error][
'type'] =
'REGEX';
1433 $errorforthistable++;
1441 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
1442 $this->errors[$error][
'type'] =
'HTMLINJECTION';
1443 $errorforthistable++;
1452 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
1453 if (!in_array(
"socialnetworks", $listfields)) {
1454 $listfields[] =
"socialnetworks";
1455 $socialkey = array_search(
"socialnetworks", $listfields);
1456 $listvalues[$socialkey] =
'';
1459 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
1460 $socialkey = array_search(
"socialnetworks", $listfields);
1462 $socialnetwork = explode(
"_", $fieldname)[1];
1463 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
1465 $json->$socialnetwork = $newval;
1466 $listvalues[$socialkey] = json_encode($json);
1468 $jsondata = $listvalues[$socialkey];
1469 $json = json_decode($jsondata);
1470 $json->$socialnetwork = $newval;
1471 $listvalues[$socialkey] = json_encode($json);
1475 $listfields[] = $fieldname;
1477 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
1478 $listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
1479 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
1480 $listvalues[] =
"''";
1482 $listvalues[] =
"'".$this->db->escape($newval).
"'";
1492 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
1494 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
1495 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
1498 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
1500 if (in_array($keyfieldcache, $listfields)) {
1502 } elseif ($tmpval ==
'user->id') {
1503 $listfields[] = $keyfieldcache;
1504 $listvalues[] = ((int) $user->id);
1505 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
1506 $tmp = explode(
'-', $tmpval);
1507 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
1508 $listfields[] = $keyfieldcache;
1509 $listvalues[] = (int) $lastinsertid;
1510 $keyfield = $keyfieldcache;
1512 } elseif (preg_match(
'/^const-/', $tmpval)) {
1513 $tmp = explode(
'-', $tmpval, 2);
1514 $listfields[] = $keyfieldcache;
1515 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
1516 } elseif (preg_match(
'/^rule-/', $tmpval)) {
1517 $fieldname = $tmpkey;
1518 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
1519 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
1520 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
1521 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
1522 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
1523 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
1525 if (empty($resultload)) {
1526 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
1529 $classinstance =
new $class($this->db);
1530 $computedFieldPos = isset($arrayfield[$fieldname]) ? ((int) $arrayfield[$fieldname]) : 0;
1531 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1532 if (empty($classinstance->error) && empty($classinstance->errors)) {
1533 $fieldArr = explode(
'.', $fieldname);
1534 if (count($fieldArr) > 0) {
1535 $fieldname = $fieldArr[1];
1539 $listfields[] = $fieldname;
1540 if ($type ==
'int') {
1541 $listvalues[] = (int) $res;
1542 } elseif ($type ==
'double') {
1543 $listvalues[] = (float) $res;
1545 $listvalues[] =
"'".$this->db->escape($res).
"'";
1548 $this->errors[$error][
'type'] =
'CLASSERROR';
1549 $this->errors[$error][
'lib'] = implode(
1551 array_merge([$classinstance->error], $classinstance->errors)
1553 $errorforthistable++;
1559 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
1560 $this->errors[$error][
'type'] =
'Import profile setup';
1569 if (!$errorforthistable) {
1571 if (!empty($listfields)) {
1572 $updatedone =
false;
1573 $insertdone =
false;
1576 $is_table_category_link =
false;
1578 if (strpos($tablename,
'_categorie_') !==
false) {
1579 $is_table_category_link =
true;
1583 if (!empty($updatekeys)) {
1586 if (empty($lastinsertid)) {
1587 $sqlSelect =
"SELECT ".$fname.
" FROM ".$tablename;
1588 $data = array_combine($listfields, $listvalues);
1591 foreach ($updatekeys as $key) {
1592 $col = $objimport->array_import_updatekeys[0][$key];
1593 $key = preg_replace(
'/^.*\./i',
'', $key);
1594 if (
isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
1595 $tmp = explode(
"_", $key);
1597 $socialnetwork = $tmp[1];
1598 $jsondata = $data[$key];
1599 $json = json_decode($jsondata);
1600 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
1603 $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1604 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1607 $where[] = $key.
' = '.$data[$key];
1608 $filters[] = $col.
' = '.$data[$key];
1611 if (!empty($tablewithentity_cache[$tablename])) {
1615 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
1617 $resql = $this->db->query($sqlSelect);
1619 $num_rows = $this->db->num_rows($resql);
1620 if ($num_rows == 1) {
1621 $res = $this->db->fetch_object($resql);
1622 $lastinsertid = $res->rowid;
1623 $keyfield =
'rowid';
1624 if ($is_table_category_link) {
1625 $lastinsertid =
'linktable';
1627 $last_insert_id_array[$tablename] = $lastinsertid;
1628 } elseif ($num_rows > 1) {
1629 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1630 $this->errors[$error][
'type'] =
'SQL';
1637 $this->errors[$error][
'lib'] = $this->db->lasterror();
1638 $this->errors[$error][
'type'] =
'SQL';
1647 $sqlSelect =
"SELECT rowid FROM ".$tablename;
1648 if (empty($keyfield)) {
1649 $keyfield =
'rowid';
1652 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
1654 if (!empty($tablewithentity_cache[$tablename])) {
1658 $resql = $this->db->query($sqlSelect);
1660 $res = $this->db->fetch_object($resql);
1661 if ($this->db->num_rows($resql) == 1) {
1670 $this->errors[$error][
'lib'] = $this->db->lasterror();
1671 $this->errors[$error][
'type'] =
'SQL';
1676 if (!empty($lastinsertid)) {
1678 if (in_array(
"socialnetworks", $listfields)) {
1679 $socialkey = array_search(
"socialnetworks", $listfields);
1680 $tmpsql = $listvalues[$socialkey];
1681 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1685 $sqlstart =
"UPDATE ".$tablename;
1687 $data = array_combine($listfields, $listvalues);
1689 foreach ($data as $key => $val) {
1690 $set[] = $key.
" = ".$val;
1692 $sqlstart .=
" SET ".implode(
', ', $set).
", import_key = '".$this->db->escape($importid).
"'";
1693 if (empty($keyfield)) {
1694 $keyfield =
'rowid';
1697 $sqlend =
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
1699 if ($is_table_category_link && !empty($where)) {
1700 '@phan-var-force string[] $where';
1701 $sqlend =
" WHERE " . implode(
' AND ', $where);
1704 if (!empty($tablewithentity_cache[$tablename])) {
1708 $sql = $sqlstart.$sqlend;
1711 $resql = $this->db->query($sql);
1715 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1716 $restrigger = $this->
triggerImportSqlOperation($tablename,
'update', is_numeric($lastinsertid) ? (
int) $lastinsertid : 0, $importid, $user, $langs,
$conf);
1717 if ($restrigger < 0) {
1718 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1719 $this->errors[$error][
'type'] =
'TRIGGER';
1722 } elseif (!$importissimulation) {
1727 $this->errors[$error][
'lib'] = $this->db->lasterror();
1728 $this->errors[$error][
'type'] =
'SQL';
1735 if (!$error && !$updatedone) {
1737 if (in_array(
"socialnetworks", $listfields)) {
1738 $socialkey = array_search(
"socialnetworks", $listfields);
1739 $tmpsql = $listvalues[$socialkey];
1740 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1744 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $listfields).
", import_key";
1745 $sqlend =
") VALUES(".implode(
', ', $listvalues).
", '".$this->db->escape($importid).
"'";
1746 if (!empty($tablewithentity_cache[$tablename])) {
1747 $sqlstart .=
", entity";
1748 $sqlend .=
", ".$conf->entity;
1750 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1751 $sqlstart .=
", ".$objimport->array_import_tables_creator[0][$alias];
1752 $sqlend .=
", ".$user->id;
1754 $sql = $sqlstart.$sqlend.
")";
1758 $resql = $this->db->query($sql);
1760 if (!$is_table_category_link) {
1761 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1764 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1765 $triggerrowid = (!$is_table_category_link && !empty($last_insert_id_array[$tablename])) ? (
int) $last_insert_id_array[$tablename] : 0;
1767 if ($restrigger < 0) {
1768 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1769 $this->errors[$error][
'type'] =
'TRIGGER';
1772 } elseif (!$importissimulation) {
1777 $this->errors[$error][
'lib'] = $this->db->lasterror();
1778 $this->errors[$error][
'type'] =
'SQL';