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();
1023 $array_match_database_to_file = array_flip($array_match_file_to_database);
1024 $sort_array_match_file_to_database = $array_match_file_to_database;
1025 ksort($sort_array_match_file_to_database);
1029 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
1031 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
1032 $this->warnings[$warning][
'type'] =
'EMPTY';
1035 $last_insert_id_array = array();
1036 $updatedone =
false;
1037 $insertdone =
false;
1039 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
1042 $sql_listfields = array();
1043 $sql_listvalues = array();
1045 $errorforthistable = 0;
1048 if (!isset($tablewithentity_cache[$tablename])) {
1049 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
1050 $resql = $this->db->DDLDescTable($tablename,
'entity');
1052 $obj = $this->db->fetch_object($resql);
1054 $tablewithentity_cache[$tablename] = 1;
1056 $tablewithentity_cache[$tablename] = 0;
1066 $arrayfield = array();
1067 foreach ($sort_array_match_file_to_database as $key => $val) {
1068 $arrayfield[$val] = ($key - 1);
1075 foreach ($sort_array_match_file_to_database as $key => $val) {
1076 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
1077 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
1079 if ($alias != $fieldalias) {
1083 if ($key <= $maxfields) {
1086 if ($arrayrecord[($key - 1)][
'type'] > 0) {
1087 $newval = $arrayrecord[($key - 1)][
'val'];
1096 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((
string) $newval ==
'')) {
1098 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
1099 $this->errors[$error][
'type'] =
'NOTNULL';
1100 $errorforthistable++;
1105 if (!empty($objimport->array_import_convertvalue[0][$val])) {
1107 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
1108 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
1109 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
1113 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1117 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1120 if ($isidorref ==
'ref') {
1121 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1122 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1123 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1124 $cachekey = $file.
'_'.$class.
'_'.$method.
'_';
1125 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1126 $newval = $this->cacheconvert[$cachekey][$newval];
1129 if (empty($resultload)) {
1130 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1133 $classinstance =
new $class($this->db);
1134 if ($class ==
'CGenericDic') {
1135 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
1136 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
1140 $param_array = array(
'', $newval);
1141 if ($class ==
'AccountingAccount') {
1154 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
1156 if ($class ==
'CActionComm') {
1157 $param_array = array($newval);
1159 $result = call_user_func_array(array($classinstance, $method), $param_array);
1162 if (!($classinstance->id !=
'') && $result == -2) {
1163 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
1164 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1165 $errorforthistable++;
1170 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
1171 $param_array = array(
'',
'', $newval);
1172 call_user_func_array(array($classinstance, $method), $param_array);
1174 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1177 if ($classinstance->id !=
'') {
1178 $newval = $classinstance->id;
1179 } elseif (! $error) {
1180 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1181 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1182 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
1183 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
1185 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
1187 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1188 $errorforthistable++;
1193 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
1195 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1198 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1200 if ($isidorref ==
'ref') {
1201 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1202 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1203 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1204 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
1205 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
1206 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$code;
1207 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1208 $newval = $this->cacheconvert[$cachekey][$newval];
1211 if (empty($resultload)) {
1212 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
1215 $classinstance =
new $class($this->db);
1217 $param_array = array(
'', $newval, $code);
1218 call_user_func_array(array($classinstance, $method), $param_array);
1219 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1220 if ($classinstance->id > 0) {
1221 $newval = $classinstance->id;
1223 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1224 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1226 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1228 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1229 $errorforthistable++;
1234 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
1235 if (empty($newval)) {
1238 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
1239 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1240 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1241 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1242 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
1243 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$units;
1244 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1245 $newval = $this->cacheconvert[$cachekey][$newval];
1248 if (empty($resultload)) {
1249 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
1252 $classinstance =
new $class($this->db);
1254 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
1255 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
1256 $this->cacheconvert[$cachekey][$newval] = $scaleorid;
1258 if ($classinstance->id > 0) {
1259 $newval = $scaleorid ? $scaleorid : 0;
1261 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1262 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1264 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1266 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1267 $errorforthistable++;
1271 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
1272 if (strtolower($newval) ==
'auto') {
1273 $this->thirdpartyobject->get_codeclient(
null, 0);
1274 $newval = $this->thirdpartyobject->code_client;
1277 if (empty($newval)) {
1278 $arrayrecord[($key - 1)][
'type'] = -1;
1280 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
1281 if (strtolower($newval) ==
'auto') {
1282 $this->thirdpartyobject->get_codefournisseur(
null, 1);
1283 $newval = $this->thirdpartyobject->code_fournisseur;
1286 if (empty($newval)) {
1287 $arrayrecord[($key - 1)][
'type'] = -1;
1289 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
1290 if (strtolower($newval) ==
'auto') {
1291 $this->thirdpartyobject->get_codecompta(
'customer');
1292 $newval = $this->thirdpartyobject->code_compta_client;
1295 if (empty($newval)) {
1296 $arrayrecord[($key - 1)][
'type'] = -1;
1298 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
1299 if (strtolower($newval) ==
'auto') {
1300 $this->thirdpartyobject->get_codecompta(
'supplier');
1301 $newval = $this->thirdpartyobject->code_compta_fournisseur;
1302 if (empty($newval)) {
1303 $arrayrecord[($key - 1)][
'type'] = -1;
1307 if (empty($newval)) {
1308 $arrayrecord[($key - 1)][
'type'] = -1;
1310 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
1311 if (strtolower($newval) ==
'auto') {
1314 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
1315 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
1317 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
1318 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
1319 $modForNumber =
new $classModForNumber();
1320 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
1324 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
1325 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
1326 require_once DOL_DOCUMENT_ROOT.$pathForObject;
1327 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
1328 $tmpobject =
new $tmpclassobject($this->db);
1329 foreach ($arrayfield as $tmpkey => $tmpval) {
1330 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
1331 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
1336 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
1338 if (is_numeric($defaultref) && $defaultref <= 0) {
1341 $newval = $defaultref;
1343 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
1344 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1345 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1346 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1348 if (empty($resultload)) {
1349 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1352 $classinstance =
new $class($this->db);
1353 $computedFieldPos = isset($arrayfield[$val]) ? ((int) $arrayfield[$val]) : 0;
1354 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1355 if (empty($classinstance->error) && empty($classinstance->errors)) {
1358 $this->errors[$error][
'type'] =
'CLASSERROR';
1359 $this->errors[$error][
'lib'] = implode(
1361 array_merge([$classinstance->error], $classinstance->errors)
1363 $errorforthistable++;
1366 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
1368 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
1370 $newval = rtrim(trim($newval),
"0");
1372 $newval = trim($newval);
1380 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
1383 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
1386 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
1388 $cachekey = $field.
'@'.$table;
1389 if (!empty($filter)) {
1390 $cachekey .=
':'.$filter;
1394 if (!isset($this->cachefieldtable[$cachekey]) || !is_array($this->cachefieldtable[$cachekey])) {
1395 $sql =
"SELECT ".$this->db->sanitize($field).
" as aliasfield FROM ".$this->db->sanitize($table);
1396 if (!empty($filter)) {
1398 $sql .=
" WHERE ".forgeSQLFromUniversalSearchCriteria($filter, $errormessage, 1);
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 (!isset($this->cachefieldtable[$cachekey]) || !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", $sql_listfields)) {
1454 $sql_listfields[] =
"socialnetworks";
1455 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1456 $sql_listvalues[$socialkey] =
'';
1459 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
1460 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1462 $socialnetwork = explode(
"_", $fieldname)[1];
1463 if (empty($sql_listvalues[$socialkey]) || $sql_listvalues[$socialkey] ==
"null") {
1465 $json->$socialnetwork = $newval;
1466 $sql_listvalues[$socialkey] = json_encode($json);
1468 $jsondata = $sql_listvalues[$socialkey];
1469 $json = json_decode($jsondata);
1470 $json->$socialnetwork = $newval;
1471 $sql_listvalues[$socialkey] = json_encode($json);
1475 $sql_listfields[] = $this->db->sanitize($fieldname);
1477 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
1478 $sql_listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
1479 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
1480 $sql_listvalues[] =
"''";
1482 $sql_listvalues[] =
"'".$this->db->escape($newval).
"'";
1492 if (!empty($sql_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, $sql_listfields)) {
1502 } elseif ($tmpval ==
'user->id') {
1503 $sql_listfields[] = $keyfieldcache;
1504 $sql_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 $sql_listfields[] = $keyfieldcache;
1509 $sql_listvalues[] = (int) $lastinsertid;
1510 $keyfield = $keyfieldcache;
1512 } elseif (preg_match(
'/^const-/', $tmpval)) {
1513 $tmp = explode(
'-', $tmpval, 2);
1514 $sql_listfields[] = $keyfieldcache;
1515 $sql_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 $sql_listfields[] = $this->db->sanitize($fieldname);
1540 if ($type ==
'int') {
1541 $sql_listvalues[] = (int) $res;
1542 } elseif ($type ==
'double') {
1543 $sql_listvalues[] = (float) $res;
1545 $sql_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($sql_listfields)) {
1572 $updatedone =
false;
1573 $insertdone =
false;
1576 $is_table_category_link =
false;
1577 $sanitizedfname =
'rowid';
1578 if (strpos($tablename,
'_categorie_') !==
false) {
1579 $is_table_category_link =
true;
1580 $sanitizedfname =
'*';
1583 if (!empty($updatekeys)) {
1586 if (empty($lastinsertid)) {
1587 $sqlSelect =
"SELECT ".$sanitizedfname.
" FROM ".$this->db->sanitize($tablename);
1588 $data = array_combine($sql_listfields, $sql_listvalues);
1591 foreach ($updatekeys as $key) {
1592 if (! array_key_exists($key, $objimport->array_import_updatekeys[0])) {
1593 $this->errors[$error][
'lib'] =
'You try to search duplicates on field '.dol_string_nohtmltag($key).
' that is not an allowed field.';
1594 $this->errors[$error][
'type'] =
'UPDATEKEYBADCOLUMN';
1598 $col = $objimport->array_import_updatekeys[0][$key];
1599 $keyfordata = preg_replace(
'/^.*\./i',
'', $key);
1600 $keyfordata = preg_replace(
'/[^a-zA-Z0-9\._]/',
'', $keyfordata);
1602 if (
isModEnabled(
"socialnetworks") && strpos($keyfordata,
"socialnetworks") !==
false) {
1603 $tmp = explode(
"_", $keyfordata);
1604 $keyfordata = $tmp[0];
1605 $socialnetwork = $tmp[1];
1606 $jsondata = $data[$keyfordata];
1607 $json = json_decode($jsondata);
1608 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
1611 $where[] = $this->db->sanitize($keyfordata).
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1612 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1615 $sanitizedvalue = $data[$keyfordata];
1626 if ((
string) $sanitizedvalue ===
'') {
1627 $this->errors[$error][
'lib'] =
'You request to search duplicates on field '.$keyfordata.
' but no value was provided for this field on this line.';
1628 $this->errors[$error][
'type'] =
'UPDATEKEYBADVALUE';
1631 $where[] = $this->db->sanitize($keyfordata).
" = ".$sanitizedvalue;
1632 $filters[] = $col.
" = ".$sanitizedvalue;
1636 if (!empty($tablewithentity_cache[$tablename])) {
1640 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
1643 $resql = $this->db->query($sqlSelect);
1645 $num_rows = $this->db->num_rows($resql);
1646 if ($num_rows == 1) {
1647 $res = $this->db->fetch_object($resql);
1648 $lastinsertid = $res->rowid;
1649 $keyfield =
'rowid';
1650 if ($is_table_category_link) {
1651 $lastinsertid =
'linktable';
1653 $last_insert_id_array[$tablename] = $lastinsertid;
1654 } elseif ($num_rows > 1) {
1655 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1656 $this->errors[$error][
'type'] =
'SQL';
1663 $this->errors[$error][
'lib'] = $this->db->lasterror();
1664 $this->errors[$error][
'type'] =
'SQL';
1674 $sqlSelect =
"SELECT rowid FROM ".$tablename;
1675 if (empty($keyfield)) {
1676 $keyfield =
'rowid';
1679 $sqlSelect .=
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1681 if (!empty($tablewithentity_cache[$tablename])) {
1685 $resql = $this->db->query($sqlSelect);
1687 $res = $this->db->fetch_object($resql);
1688 if ($this->db->num_rows($resql) == 1) {
1697 $this->errors[$error][
'lib'] = $this->db->lasterror();
1698 $this->errors[$error][
'type'] =
'SQL';
1703 if (!empty($lastinsertid)) {
1705 if (in_array(
"socialnetworks", $sql_listfields)) {
1706 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1707 $tmpsql = $sql_listvalues[$socialkey];
1708 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1712 $sqlstart =
"UPDATE ".$tablename;
1714 $data = array_combine($sql_listfields, $sql_listvalues);
1716 foreach ($data as $key => $val) {
1717 $sql_set[] = $key.
" = ".$val;
1719 $sqlstart .=
" SET ".implode(
', ', $sql_set).
", import_key = '".$this->db->escape($importid).
"'";
1720 if (empty($keyfield)) {
1721 $keyfield =
'rowid';
1724 $sqlend =
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1726 if ($is_table_category_link && !empty($where)) {
1727 '@phan-var-force string[] $where';
1728 $sqlend =
" WHERE " . implode(
' AND ', $where);
1731 if (!empty($tablewithentity_cache[$tablename])) {
1735 $sql = $sqlstart.$sqlend;
1738 $resql = $this->db->query($sql);
1742 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1743 $restrigger = $this->
triggerImportSqlOperation($tablename,
'update', is_numeric($lastinsertid) ? (
int) $lastinsertid : 0, $importid, $user, $langs,
$conf);
1744 if ($restrigger < 0) {
1745 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1746 $this->errors[$error][
'type'] =
'TRIGGER';
1749 } elseif (!$importissimulation) {
1754 $this->errors[$error][
'lib'] = $this->db->lasterror();
1755 $this->errors[$error][
'type'] =
'SQL';
1762 if (!$error && !$updatedone) {
1764 if (in_array(
"socialnetworks", $sql_listfields)) {
1765 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1766 $tmpsql = $sql_listvalues[$socialkey];
1767 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1771 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $sql_listfields).
", import_key";
1772 $sqlend =
") VALUES(".implode(
', ', $sql_listvalues).
", '".$this->db->escape($importid).
"'";
1773 if (!empty($tablewithentity_cache[$tablename])) {
1774 $sqlstart .=
", entity";
1775 $sqlend .=
", ".((int)
$conf->entity);
1777 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1778 $sqlstart .=
", ".$this->db->sanitize($objimport->array_import_tables_creator[0][$alias]);
1779 $sqlend .=
", ".((int) $user->id);
1781 $sql = $sqlstart.$sqlend.
")";
1785 $resql = $this->db->query($sql);
1787 if (!$is_table_category_link) {
1788 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1791 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1792 $triggerrowid = (!$is_table_category_link && !empty($last_insert_id_array[$tablename])) ? (
int) $last_insert_id_array[$tablename] : 0;
1794 if ($restrigger < 0) {
1795 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1796 $this->errors[$error][
'type'] =
'TRIGGER';
1799 } elseif (!$importissimulation) {
1804 $this->errors[$error][
'lib'] = $this->db->lasterror();
1805 $this->errors[$error][
'type'] =
'SQL';