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 (!is_array($this->cachefieldtable[$cachekey])) {
1395 $sql =
"SELECT ".$this->db->sanitize($field).
" as aliasfield FROM ".$this->db->sanitize($table);
1396 if (!empty($filter)) {
1400 $resql = $this->db->query($sql);
1402 $num = $this->db->num_rows($resql);
1405 $obj = $this->db->fetch_object($resql);
1407 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
1417 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
1418 $tableforerror = $table;
1419 if (!empty($filter)) {
1420 $tableforerror .=
':'.$filter;
1422 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
1423 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1424 $errorforthistable++;
1427 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
1430 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
1431 $this->errors[$error][
'type'] =
'REGEX';
1432 $errorforthistable++;
1440 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
1441 $this->errors[$error][
'type'] =
'HTMLINJECTION';
1442 $errorforthistable++;
1451 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
1452 if (!in_array(
"socialnetworks", $sql_listfields)) {
1453 $sql_listfields[] =
"socialnetworks";
1454 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1455 $sql_listvalues[$socialkey] =
'';
1458 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
1459 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1461 $socialnetwork = explode(
"_", $fieldname)[1];
1462 if (empty($sql_listvalues[$socialkey]) || $sql_listvalues[$socialkey] ==
"null") {
1464 $json->$socialnetwork = $newval;
1465 $sql_listvalues[$socialkey] = json_encode($json);
1467 $jsondata = $sql_listvalues[$socialkey];
1468 $json = json_decode($jsondata);
1469 $json->$socialnetwork = $newval;
1470 $sql_listvalues[$socialkey] = json_encode($json);
1474 $sql_listfields[] = $this->db->sanitize($fieldname);
1476 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
1477 $sql_listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
1478 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
1479 $sql_listvalues[] =
"''";
1481 $sql_listvalues[] =
"'".$this->db->escape($newval).
"'";
1491 if (!empty($sql_listfields) && is_array($objimport->array_import_fieldshidden[0])) {
1493 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
1494 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
1497 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
1499 if (in_array($keyfieldcache, $sql_listfields)) {
1501 } elseif ($tmpval ==
'user->id') {
1502 $sql_listfields[] = $keyfieldcache;
1503 $sql_listvalues[] = ((int) $user->id);
1504 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
1505 $tmp = explode(
'-', $tmpval);
1506 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
1507 $sql_listfields[] = $keyfieldcache;
1508 $sql_listvalues[] = (int) $lastinsertid;
1509 $keyfield = $keyfieldcache;
1511 } elseif (preg_match(
'/^const-/', $tmpval)) {
1512 $tmp = explode(
'-', $tmpval, 2);
1513 $sql_listfields[] = $keyfieldcache;
1514 $sql_listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
1515 } elseif (preg_match(
'/^rule-/', $tmpval)) {
1516 $fieldname = $tmpkey;
1517 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
1518 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
1519 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
1520 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
1521 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
1522 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
1524 if (empty($resultload)) {
1525 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
1528 $classinstance =
new $class($this->db);
1529 $computedFieldPos = isset($arrayfield[$fieldname]) ? ((int) $arrayfield[$fieldname]) : 0;
1530 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1531 if (empty($classinstance->error) && empty($classinstance->errors)) {
1532 $fieldArr = explode(
'.', $fieldname);
1533 if (count($fieldArr) > 0) {
1534 $fieldname = $fieldArr[1];
1538 $sql_listfields[] = $this->db->sanitize($fieldname);
1539 if ($type ==
'int') {
1540 $sql_listvalues[] = (int) $res;
1541 } elseif ($type ==
'double') {
1542 $sql_listvalues[] = (float) $res;
1544 $sql_listvalues[] =
"'".$this->db->escape($res).
"'";
1547 $this->errors[$error][
'type'] =
'CLASSERROR';
1548 $this->errors[$error][
'lib'] = implode(
1550 array_merge([$classinstance->error], $classinstance->errors)
1552 $errorforthistable++;
1558 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
1559 $this->errors[$error][
'type'] =
'Import profile setup';
1568 if (!$errorforthistable) {
1570 if (!empty($sql_listfields)) {
1571 $updatedone =
false;
1572 $insertdone =
false;
1575 $is_table_category_link =
false;
1576 $sanitizedfname =
'rowid';
1577 if (strpos($tablename,
'_categorie_') !==
false) {
1578 $is_table_category_link =
true;
1579 $sanitizedfname =
'*';
1582 if (!empty($updatekeys)) {
1585 if (empty($lastinsertid)) {
1586 $sqlSelect =
"SELECT ".$sanitizedfname.
" FROM ".$this->db->sanitize($tablename);
1587 $data = array_combine($sql_listfields, $sql_listvalues);
1590 foreach ($updatekeys as $key) {
1591 if (! array_key_exists($key, $objimport->array_import_updatekeys[0])) {
1592 $this->errors[$error][
'lib'] =
'You try to search duplicates on field '.dol_string_nohtmltag($key).
' that is not an allowed field.';
1593 $this->errors[$error][
'type'] =
'UPDATEKEYBADCOLUMN';
1597 $col = $objimport->array_import_updatekeys[0][$key];
1598 $keyfordata = preg_replace(
'/^.*\./i',
'', $key);
1599 $keyfordata = preg_replace(
'/[^a-zA-Z0-9\._]/',
'', $keyfordata);
1601 if (
isModEnabled(
"socialnetworks") && strpos($keyfordata,
"socialnetworks") !==
false) {
1602 $tmp = explode(
"_", $keyfordata);
1603 $keyfordata = $tmp[0];
1604 $socialnetwork = $tmp[1];
1605 $jsondata = $data[$keyfordata];
1606 $json = json_decode($jsondata);
1607 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
1610 $where[] = $this->db->sanitize($keyfordata).
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1611 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1614 $sanitizedvalue = $data[$keyfordata];
1625 if ((
string) $sanitizedvalue ===
'') {
1626 $this->errors[$error][
'lib'] =
'You request to search duplicates on field '.$keyfordata.
' but no value was provided for this field on this line.';
1627 $this->errors[$error][
'type'] =
'UPDATEKEYBADVALUE';
1630 $where[] = $this->db->sanitize($keyfordata).
" = ".$sanitizedvalue;
1631 $filters[] = $col.
" = ".$sanitizedvalue;
1635 if (!empty($tablewithentity_cache[$tablename])) {
1639 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
1642 $resql = $this->db->query($sqlSelect);
1644 $num_rows = $this->db->num_rows($resql);
1645 if ($num_rows == 1) {
1646 $res = $this->db->fetch_object($resql);
1647 $lastinsertid = $res->rowid;
1648 $keyfield =
'rowid';
1649 if ($is_table_category_link) {
1650 $lastinsertid =
'linktable';
1652 $last_insert_id_array[$tablename] = $lastinsertid;
1653 } elseif ($num_rows > 1) {
1654 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1655 $this->errors[$error][
'type'] =
'SQL';
1662 $this->errors[$error][
'lib'] = $this->db->lasterror();
1663 $this->errors[$error][
'type'] =
'SQL';
1673 $sqlSelect =
"SELECT rowid FROM ".$tablename;
1674 if (empty($keyfield)) {
1675 $keyfield =
'rowid';
1678 $sqlSelect .=
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1680 if (!empty($tablewithentity_cache[$tablename])) {
1684 $resql = $this->db->query($sqlSelect);
1686 $res = $this->db->fetch_object($resql);
1687 if ($this->db->num_rows($resql) == 1) {
1696 $this->errors[$error][
'lib'] = $this->db->lasterror();
1697 $this->errors[$error][
'type'] =
'SQL';
1702 if (!empty($lastinsertid)) {
1704 if (in_array(
"socialnetworks", $sql_listfields)) {
1705 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1706 $tmpsql = $sql_listvalues[$socialkey];
1707 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1711 $sqlstart =
"UPDATE ".$tablename;
1713 $data = array_combine($sql_listfields, $sql_listvalues);
1715 foreach ($data as $key => $val) {
1716 $sql_set[] = $key.
" = ".$val;
1718 $sqlstart .=
" SET ".implode(
', ', $sql_set).
", import_key = '".$this->db->escape($importid).
"'";
1719 if (empty($keyfield)) {
1720 $keyfield =
'rowid';
1723 $sqlend =
" WHERE ".$this->db->escape($keyfield).
" = ".((int) $lastinsertid);
1725 if ($is_table_category_link && !empty($where)) {
1726 '@phan-var-force string[] $where';
1727 $sqlend =
" WHERE " . implode(
' AND ', $where);
1730 if (!empty($tablewithentity_cache[$tablename])) {
1734 $sql = $sqlstart.$sqlend;
1737 $resql = $this->db->query($sql);
1741 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1742 $restrigger = $this->
triggerImportSqlOperation($tablename,
'update', is_numeric($lastinsertid) ? (
int) $lastinsertid : 0, $importid, $user, $langs,
$conf);
1743 if ($restrigger < 0) {
1744 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1745 $this->errors[$error][
'type'] =
'TRIGGER';
1748 } elseif (!$importissimulation) {
1753 $this->errors[$error][
'lib'] = $this->db->lasterror();
1754 $this->errors[$error][
'type'] =
'SQL';
1761 if (!$error && !$updatedone) {
1763 if (in_array(
"socialnetworks", $sql_listfields)) {
1764 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1765 $tmpsql = $sql_listvalues[$socialkey];
1766 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1770 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $sql_listfields).
", import_key";
1771 $sqlend =
") VALUES(".implode(
', ', $sql_listvalues).
", '".$this->db->escape($importid).
"'";
1772 if (!empty($tablewithentity_cache[$tablename])) {
1773 $sqlstart .=
", entity";
1774 $sqlend .=
", ".((int)
$conf->entity);
1776 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1777 $sqlstart .=
", ".$this->db->sanitize($objimport->array_import_tables_creator[0][$alias]);
1778 $sqlend .=
", ".((int) $user->id);
1780 $sql = $sqlstart.$sqlend.
")";
1784 $resql = $this->db->query($sql);
1786 if (!$is_table_category_link) {
1787 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1790 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1791 $triggerrowid = (!$is_table_category_link && !empty($last_insert_id_array[$tablename])) ? (
int) $last_insert_id_array[$tablename] : 0;
1793 if ($restrigger < 0) {
1794 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1795 $this->errors[$error][
'type'] =
'TRIGGER';
1798 } elseif (!$importissimulation) {
1803 $this->errors[$error][
'lib'] = $this->db->lasterror();
1804 $this->errors[$error][
'type'] =
'SQL';