1001 protected function commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, $recordpositionbase = 0)
1003 global $langs,
$conf, $user;
1004 global $thirdparty_static;
1005 global $tablewithentity_cache;
1007 if (is_array($arrayrecord) && !empty($recordpositionbase)) {
1008 $arrayrecord = array_values($arrayrecord);
1014 $importissimulation = !empty($this->importissimulation);
1015 $this->errors = array();
1016 $this->warnings = array();
1022 $array_match_database_to_file = array_flip($array_match_file_to_database);
1023 $sort_array_match_file_to_database = $array_match_file_to_database;
1024 ksort($sort_array_match_file_to_database);
1028 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
1030 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
1031 $this->warnings[$warning][
'type'] =
'EMPTY';
1034 $last_insert_id_array = array();
1035 $updatedone =
false;
1036 $insertdone =
false;
1038 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
1041 $sql_listfields = array();
1042 $sql_listvalues = array();
1044 $errorforthistable = 0;
1047 if (!isset($tablewithentity_cache[$tablename])) {
1048 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
1049 $resql = $this->db->DDLDescTable($tablename,
'entity');
1051 $obj = $this->db->fetch_object($resql);
1053 $tablewithentity_cache[$tablename] = 1;
1055 $tablewithentity_cache[$tablename] = 0;
1065 $arrayfield = array();
1066 foreach ($sort_array_match_file_to_database as $key => $val) {
1067 $arrayfield[$val] = ($key - 1);
1074 foreach ($sort_array_match_file_to_database as $key => $val) {
1075 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
1076 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
1078 if ($alias != $fieldalias) {
1082 if ($key <= $maxfields) {
1085 if ($arrayrecord[($key - 1)][
'type'] > 0) {
1086 $newval = $arrayrecord[($key - 1)][
'val'];
1095 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((
string) $newval ==
'')) {
1097 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
1098 $this->errors[$error][
'type'] =
'NOTNULL';
1099 $errorforthistable++;
1104 if (!empty($objimport->array_import_convertvalue[0][$val])) {
1106 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
1107 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
1108 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
1112 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1116 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1119 if ($isidorref ==
'ref') {
1120 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1121 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1122 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1123 $cachekey = $file.
'_'.$class.
'_'.$method.
'_';
1124 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1125 $newval = $this->cacheconvert[$cachekey][$newval];
1128 if (empty($resultload)) {
1129 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1132 $classinstance =
new $class($this->db);
1133 if ($class ==
'CGenericDic') {
1134 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
1135 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
1139 $param_array = array(
'', $newval);
1140 if ($class ==
'AccountingAccount') {
1153 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
1155 if ($class ==
'CActionComm') {
1156 $param_array = array($newval);
1158 $result = call_user_func_array(array($classinstance, $method), $param_array);
1161 if (!($classinstance->id !=
'') && $result == -2) {
1162 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
1163 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1164 $errorforthistable++;
1169 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
1170 $param_array = array(
'',
'', $newval);
1171 call_user_func_array(array($classinstance, $method), $param_array);
1173 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1176 if ($classinstance->id !=
'') {
1177 $newval = $classinstance->id;
1178 } elseif (! $error) {
1179 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1180 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1181 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
1182 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
1184 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
1186 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1187 $errorforthistable++;
1192 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
1194 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
1197 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
1199 if ($isidorref ==
'ref') {
1200 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1201 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1202 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1203 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
1204 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
1205 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$code;
1206 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1207 $newval = $this->cacheconvert[$cachekey][$newval];
1210 if (empty($resultload)) {
1211 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
1214 $classinstance =
new $class($this->db);
1216 $param_array = array(
'', $newval, $code);
1217 call_user_func_array(array($classinstance, $method), $param_array);
1218 $this->cacheconvert[$cachekey][$newval] = $classinstance->id;
1219 if ($classinstance->id > 0) {
1220 $newval = $classinstance->id;
1222 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1223 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1225 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1227 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1228 $errorforthistable++;
1233 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
1234 if (empty($newval)) {
1237 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
1238 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1239 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1240 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1241 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
1242 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$units;
1243 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1244 $newval = $this->cacheconvert[$cachekey][$newval];
1247 if (empty($resultload)) {
1248 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
1251 $classinstance =
new $class($this->db);
1253 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
1254 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
1255 $this->cacheconvert[$cachekey][$newval] = $scaleorid;
1257 if ($classinstance->id > 0) {
1258 $newval = $scaleorid ? $scaleorid : 0;
1260 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1261 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1263 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1265 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1266 $errorforthistable++;
1270 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
1271 if (strtolower($newval) ==
'auto') {
1272 $this->thirdpartyobject->get_codeclient(
null, 0);
1273 $newval = $this->thirdpartyobject->code_client;
1276 if (empty($newval)) {
1277 $arrayrecord[($key - 1)][
'type'] = -1;
1279 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
1280 if (strtolower($newval) ==
'auto') {
1281 $this->thirdpartyobject->get_codefournisseur(
null, 1);
1282 $newval = $this->thirdpartyobject->code_fournisseur;
1285 if (empty($newval)) {
1286 $arrayrecord[($key - 1)][
'type'] = -1;
1288 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
1289 if (strtolower($newval) ==
'auto') {
1290 $this->thirdpartyobject->get_codecompta(
'customer');
1291 $newval = $this->thirdpartyobject->code_compta_client;
1294 if (empty($newval)) {
1295 $arrayrecord[($key - 1)][
'type'] = -1;
1297 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
1298 if (strtolower($newval) ==
'auto') {
1299 $this->thirdpartyobject->get_codecompta(
'supplier');
1300 $newval = $this->thirdpartyobject->code_compta_fournisseur;
1301 if (empty($newval)) {
1302 $arrayrecord[($key - 1)][
'type'] = -1;
1306 if (empty($newval)) {
1307 $arrayrecord[($key - 1)][
'type'] = -1;
1309 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
1310 if (strtolower($newval) ==
'auto') {
1313 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
1314 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
1316 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
1317 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
1318 $modForNumber =
new $classModForNumber();
1319 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
1323 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
1324 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
1325 require_once DOL_DOCUMENT_ROOT.$pathForObject;
1326 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
1327 $tmpobject =
new $tmpclassobject($this->db);
1328 foreach ($arrayfield as $tmpkey => $tmpval) {
1329 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
1330 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
1335 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
1337 if (is_numeric($defaultref) && $defaultref <= 0) {
1340 $newval = $defaultref;
1342 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
1343 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1344 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1345 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1347 if (empty($resultload)) {
1348 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1351 $classinstance =
new $class($this->db);
1352 $computedFieldPos = isset($arrayfield[$val]) ? ((int) $arrayfield[$val]) : 0;
1353 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1354 if (empty($classinstance->error) && empty($classinstance->errors)) {
1357 $this->errors[$error][
'type'] =
'CLASSERROR';
1358 $this->errors[$error][
'lib'] = implode(
1360 array_merge([$classinstance->error], $classinstance->errors)
1362 $errorforthistable++;
1365 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
1367 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
1369 $newval = rtrim(trim($newval),
"0");
1371 $newval = trim($newval);
1379 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
1382 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
1385 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
1387 $cachekey = $field.
'@'.$table;
1388 if (!empty($filter)) {
1389 $cachekey .=
':'.$filter;
1393 if (!is_array($this->cachefieldtable[$cachekey])) {
1394 $sql =
"SELECT ".$this->db->sanitize($field).
" as aliasfield FROM ".$this->db->sanitize($table);
1395 if (!empty($filter)) {
1399 $resql = $this->db->query($sql);
1401 $num = $this->db->num_rows($resql);
1404 $obj = $this->db->fetch_object($resql);
1406 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
1416 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
1417 $tableforerror = $table;
1418 if (!empty($filter)) {
1419 $tableforerror .=
':'.$filter;
1421 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
1422 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1423 $errorforthistable++;
1426 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
1429 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
1430 $this->errors[$error][
'type'] =
'REGEX';
1431 $errorforthistable++;
1439 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
1440 $this->errors[$error][
'type'] =
'HTMLINJECTION';
1441 $errorforthistable++;
1450 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
1451 if (!in_array(
"socialnetworks", $sql_listfields)) {
1452 $sql_listfields[] =
"socialnetworks";
1453 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1454 $sql_listvalues[$socialkey] =
'';
1457 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
1458 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1460 $socialnetwork = explode(
"_", $fieldname)[1];
1461 if (empty($sql_listvalues[$socialkey]) || $sql_listvalues[$socialkey] ==
"null") {
1463 $json->$socialnetwork = $newval;
1464 $sql_listvalues[$socialkey] = json_encode($json);
1466 $jsondata = $sql_listvalues[$socialkey];
1467 $json = json_decode($jsondata);
1468 $json->$socialnetwork = $newval;
1469 $sql_listvalues[$socialkey] = json_encode($json);
1473 $sql_listfields[] = $this->db->sanitize($fieldname);
1475 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
1476 $sql_listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
1477 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
1478 $sql_listvalues[] =
"''";
1480 $sql_listvalues[] =
"'".$this->db->escape($newval).
"'";
1490 if (!empty($sql_listfields) && is_array($objimport->array_import_fieldshidden[0])) {
1492 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
1493 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
1496 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
1498 if (in_array($keyfieldcache, $sql_listfields)) {
1500 } elseif ($tmpval ==
'user->id') {
1501 $sql_listfields[] = $keyfieldcache;
1502 $sql_listvalues[] = ((int) $user->id);
1503 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
1504 $tmp = explode(
'-', $tmpval);
1505 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
1506 $sql_listfields[] = $keyfieldcache;
1507 $sql_listvalues[] = (int) $lastinsertid;
1508 $keyfield = $keyfieldcache;
1510 } elseif (preg_match(
'/^const-/', $tmpval)) {
1511 $tmp = explode(
'-', $tmpval, 2);
1512 $sql_listfields[] = $keyfieldcache;
1513 $sql_listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
1514 } elseif (preg_match(
'/^rule-/', $tmpval)) {
1515 $fieldname = $tmpkey;
1516 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
1517 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
1518 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
1519 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
1520 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
1521 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
1523 if (empty($resultload)) {
1524 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
1527 $classinstance =
new $class($this->db);
1528 $computedFieldPos = isset($arrayfield[$fieldname]) ? ((int) $arrayfield[$fieldname]) : 0;
1529 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1530 if (empty($classinstance->error) && empty($classinstance->errors)) {
1531 $fieldArr = explode(
'.', $fieldname);
1532 if (count($fieldArr) > 0) {
1533 $fieldname = $fieldArr[1];
1537 $sql_listfields[] = $this->db->sanitize($fieldname);
1538 if ($type ==
'int') {
1539 $sql_listvalues[] = (int) $res;
1540 } elseif ($type ==
'double') {
1541 $sql_listvalues[] = (float) $res;
1543 $sql_listvalues[] =
"'".$this->db->escape($res).
"'";
1546 $this->errors[$error][
'type'] =
'CLASSERROR';
1547 $this->errors[$error][
'lib'] = implode(
1549 array_merge([$classinstance->error], $classinstance->errors)
1551 $errorforthistable++;
1557 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
1558 $this->errors[$error][
'type'] =
'Import profile setup';
1567 if (!$errorforthistable) {
1569 if (!empty($sql_listfields)) {
1570 $updatedone =
false;
1571 $insertdone =
false;
1574 $is_table_category_link =
false;
1575 $sanitizedfname =
'rowid';
1576 if (strpos($tablename,
'_categorie_') !==
false) {
1577 $is_table_category_link =
true;
1578 $sanitizedfname =
'*';
1581 if (!empty($updatekeys)) {
1584 if (empty($lastinsertid)) {
1585 $sqlSelect =
"SELECT ".$sanitizedfname.
" FROM ".$this->db->sanitize($tablename);
1586 $data = array_combine($sql_listfields, $sql_listvalues);
1589 foreach ($updatekeys as $key) {
1590 if (! array_key_exists($key, $objimport->array_import_updatekeys[0])) {
1591 $this->errors[$error][
'lib'] =
'You try to search duplicates on field '.dol_string_nohtmltag($key).
' that is not an allowed field.';
1592 $this->errors[$error][
'type'] =
'UPDATEKEYBADCOLUMN';
1596 $col = $objimport->array_import_updatekeys[0][$key];
1597 $keyfordata = preg_replace(
'/^.*\./i',
'', $key);
1598 $keyfordata = preg_replace(
'/[^a-zA-Z0-9\._]/',
'', $keyfordata);
1600 if (
isModEnabled(
"socialnetworks") && strpos($keyfordata,
"socialnetworks") !==
false) {
1601 $tmp = explode(
"_", $keyfordata);
1602 $keyfordata = $tmp[0];
1603 $socialnetwork = $tmp[1];
1604 $jsondata = $data[$keyfordata];
1605 $json = json_decode($jsondata);
1606 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
1609 $where[] = $this->db->sanitize($keyfordata).
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1610 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1613 $sanitizedvalue = $data[$keyfordata];
1624 if ((
string) $sanitizedvalue ===
'') {
1625 $this->errors[$error][
'lib'] =
'You request to search duplicates on field '.$keyfordata.
' but no value was provided for this field on this line.';
1626 $this->errors[$error][
'type'] =
'UPDATEKEYBADVALUE';
1629 $where[] = $this->db->sanitize($keyfordata).
" = ".$sanitizedvalue;
1630 $filters[] = $col.
" = ".$sanitizedvalue;
1634 if (!empty($tablewithentity_cache[$tablename])) {
1638 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
1641 $resql = $this->db->query($sqlSelect);
1643 $num_rows = $this->db->num_rows($resql);
1644 if ($num_rows == 1) {
1645 $res = $this->db->fetch_object($resql);
1646 $lastinsertid = $res->rowid;
1647 $keyfield =
'rowid';
1648 if ($is_table_category_link) {
1649 $lastinsertid =
'linktable';
1651 $last_insert_id_array[$tablename] = $lastinsertid;
1652 } elseif ($num_rows > 1) {
1653 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1654 $this->errors[$error][
'type'] =
'SQL';
1661 $this->errors[$error][
'lib'] = $this->db->lasterror();
1662 $this->errors[$error][
'type'] =
'SQL';
1672 $sqlSelect =
"SELECT rowid FROM ".$tablename;
1673 if (empty($keyfield)) {
1674 $keyfield =
'rowid';
1677 $sqlSelect .=
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1679 if (!empty($tablewithentity_cache[$tablename])) {
1683 $resql = $this->db->query($sqlSelect);
1685 $res = $this->db->fetch_object($resql);
1686 if ($this->db->num_rows($resql) == 1) {
1695 $this->errors[$error][
'lib'] = $this->db->lasterror();
1696 $this->errors[$error][
'type'] =
'SQL';
1701 if (!empty($lastinsertid)) {
1703 if (in_array(
"socialnetworks", $sql_listfields)) {
1704 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1705 $tmpsql = $sql_listvalues[$socialkey];
1706 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1710 $sqlstart =
"UPDATE ".$tablename;
1712 $data = array_combine($sql_listfields, $sql_listvalues);
1714 foreach ($data as $key => $val) {
1715 $sql_set[] = $key.
" = ".$val;
1717 $sqlstart .=
" SET ".implode(
', ', $sql_set).
", import_key = '".$this->db->escape($importid).
"'";
1718 if (empty($keyfield)) {
1719 $keyfield =
'rowid';
1722 $sqlend =
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1724 if ($is_table_category_link && !empty($where)) {
1725 '@phan-var-force string[] $where';
1726 $sqlend =
" WHERE " . implode(
' AND ', $where);
1729 if (!empty($tablewithentity_cache[$tablename])) {
1733 $sql = $sqlstart.$sqlend;
1736 $resql = $this->db->query($sql);
1740 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1741 $restrigger = $this->
triggerImportSqlOperation($tablename,
'update', is_numeric($lastinsertid) ? (
int) $lastinsertid : 0, $importid, $user, $langs,
$conf);
1742 if ($restrigger < 0) {
1743 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1744 $this->errors[$error][
'type'] =
'TRIGGER';
1747 } elseif (!$importissimulation) {
1752 $this->errors[$error][
'lib'] = $this->db->lasterror();
1753 $this->errors[$error][
'type'] =
'SQL';
1760 if (!$error && !$updatedone) {
1762 if (in_array(
"socialnetworks", $sql_listfields)) {
1763 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1764 $tmpsql = $sql_listvalues[$socialkey];
1765 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1769 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $sql_listfields).
", import_key";
1770 $sqlend =
") VALUES(".implode(
', ', $sql_listvalues).
", '".$this->db->escape($importid).
"'";
1771 if (!empty($tablewithentity_cache[$tablename])) {
1772 $sqlstart .=
", entity";
1773 $sqlend .=
", ".((int)
$conf->entity);
1775 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1776 $sqlstart .=
", ".$this->db->sanitize($objimport->array_import_tables_creator[0][$alias]);
1777 $sqlend .=
", ".((int) $user->id);
1779 $sql = $sqlstart.$sqlend.
")";
1783 $resql = $this->db->query($sql);
1785 if (!$is_table_category_link) {
1786 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1789 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1790 $triggerrowid = (!$is_table_category_link && !empty($last_insert_id_array[$tablename])) ? (
int) $last_insert_id_array[$tablename] : 0;
1792 if ($restrigger < 0) {
1793 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFailedTriggerCall');
1794 $this->errors[$error][
'type'] =
'TRIGGER';
1797 } elseif (!$importissimulation) {
1802 $this->errors[$error][
'lib'] = $this->db->lasterror();
1803 $this->errors[$error][
'type'] =
'SQL';