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;
1058 dol_print_error($this->db);
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 $langs->load(
'errors');
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 $langs->load(
'errors');
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 $langs->load(
'errors');
1226 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1228 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1230 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1231 $errorforthistable++;
1236 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
1237 if (empty($newval)) {
1240 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
1241 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1242 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1243 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1244 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
1245 $cachekey = $file.
'_'.$class.
'_'.$method.
'_'.$units;
1246 if (isset($this->cacheconvert[$cachekey][$newval]) && $this->cacheconvert[$cachekey][$newval] !=
'') {
1247 $newval = $this->cacheconvert[$cachekey][$newval];
1250 if (empty($resultload)) {
1251 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
1254 $classinstance =
new $class($this->db);
1256 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
1257 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
1258 $this->cacheconvert[$cachekey][$newval] = $scaleorid;
1260 if ($classinstance->id > 0) {
1261 $newval = $scaleorid ? $scaleorid : 0;
1263 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
1264 $langs->load(
'errors');
1265 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
1267 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
1269 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1270 $errorforthistable++;
1274 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
1275 if (strtolower($newval) ==
'auto') {
1276 $this->thirdpartyobject->get_codeclient(
null, 0);
1277 $newval = $this->thirdpartyobject->code_client;
1280 if (empty($newval)) {
1281 $arrayrecord[($key - 1)][
'type'] = -1;
1283 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
1284 if (strtolower($newval) ==
'auto') {
1285 $this->thirdpartyobject->get_codefournisseur(
null, 1);
1286 $newval = $this->thirdpartyobject->code_fournisseur;
1289 if (empty($newval)) {
1290 $arrayrecord[($key - 1)][
'type'] = -1;
1292 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
1293 if (strtolower($newval) ==
'auto') {
1294 $this->thirdpartyobject->get_codecompta(
'customer');
1295 $newval = $this->thirdpartyobject->code_compta_client;
1298 if (empty($newval)) {
1299 $arrayrecord[($key - 1)][
'type'] = -1;
1301 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
1302 if (strtolower($newval) ==
'auto') {
1303 $this->thirdpartyobject->get_codecompta(
'supplier');
1304 $newval = $this->thirdpartyobject->code_compta_fournisseur;
1305 if (empty($newval)) {
1306 $arrayrecord[($key - 1)][
'type'] = -1;
1310 if (empty($newval)) {
1311 $arrayrecord[($key - 1)][
'type'] = -1;
1313 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
1314 if (strtolower($newval) ==
'auto') {
1317 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
1318 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
1320 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
1321 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
1322 $modForNumber =
new $classModForNumber();
1323 '@phan-var-force ModeleNumRefMembers|ModeleNumRefCommandes|ModeleNumRefSuppliersInvoices|ModeleNumRefSuppliersOrders|ModeleNumRefProjects|ModeleNumRefTask|ModeleNumRefPropales $modForNumber';
1327 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
1328 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
1329 require_once DOL_DOCUMENT_ROOT.$pathForObject;
1330 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
1331 $tmpobject =
new $tmpclassobject($this->db);
1332 foreach ($arrayfield as $tmpkey => $tmpval) {
1333 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
1334 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
1339 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
1341 if (is_numeric($defaultref) && $defaultref <= 0) {
1344 $newval = $defaultref;
1346 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
1347 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
1348 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
1349 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
1351 if (empty($resultload)) {
1352 dol_print_error(
null,
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
1355 $classinstance =
new $class($this->db);
1356 $computedFieldPos = isset($arrayfield[$val]) ? ((int) $arrayfield[$val]) : 0;
1357 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1358 if (empty($classinstance->error) && empty($classinstance->errors)) {
1361 $this->errors[$error][
'type'] =
'CLASSERROR';
1362 $this->errors[$error][
'lib'] = implode(
1364 array_merge([$classinstance->error], $classinstance->errors)
1366 $errorforthistable++;
1369 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
1371 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
1373 $newval = rtrim(trim($newval),
"0");
1375 $newval = trim($newval);
1383 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
1386 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
1389 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
1391 $cachekey = $field.
'@'.$table;
1392 if (!empty($filter)) {
1393 $cachekey .=
':'.$filter;
1397 if (!isset($this->cachefieldtable[$cachekey]) || !is_array($this->cachefieldtable[$cachekey])) {
1398 $sql =
"SELECT ".$this->db->sanitize($field).
" as aliasfield FROM ".$this->db->sanitize($table);
1399 if (!empty($filter)) {
1403 $resql = $this->db->query($sql);
1405 $num = $this->db->num_rows($resql);
1408 $obj = $this->db->fetch_object($resql);
1410 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
1415 dol_print_error($this->db);
1420 if (!isset($this->cachefieldtable[$cachekey]) || !is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
1421 $tableforerror = $table;
1422 if (!empty($filter)) {
1423 $tableforerror .=
':'.$filter;
1425 $langs->load(
'errors');
1426 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
1427 $this->errors[$error][
'type'] =
'FOREIGNKEY';
1428 $errorforthistable++;
1431 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
1434 $langs->load(
'errors');
1435 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
1436 $this->errors[$error][
'type'] =
'REGEX';
1437 $errorforthistable++;
1445 $langs->load(
'errors');
1446 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
1447 $this->errors[$error][
'type'] =
'HTMLINJECTION';
1448 $errorforthistable++;
1457 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
1458 if (!in_array(
"socialnetworks", $sql_listfields)) {
1459 $sql_listfields[] =
"socialnetworks";
1460 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1461 $sql_listvalues[$socialkey] =
'';
1464 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
1465 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1467 $socialnetwork = explode(
"_", $fieldname)[1];
1468 if (empty($sql_listvalues[$socialkey]) || $sql_listvalues[$socialkey] ==
"null") {
1470 $json->$socialnetwork = $newval;
1471 $sql_listvalues[$socialkey] = json_encode($json);
1473 $jsondata = $sql_listvalues[$socialkey];
1474 $json = json_decode($jsondata);
1475 $json->$socialnetwork = $newval;
1476 $sql_listvalues[$socialkey] = json_encode($json);
1480 $sql_listfields[] = $this->db->sanitize($fieldname);
1482 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
1483 $sql_listvalues[] = ($newval ==
'0' ? (int) $newval :
"null");
1484 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
1485 $sql_listvalues[] =
"''";
1487 $sql_listvalues[] =
"'".$this->db->escape($newval).
"'";
1497 if (!empty($sql_listfields) && is_array($objimport->array_import_fieldshidden[0])) {
1499 foreach ($objimport->array_import_fieldshidden[0] as $tmpkey => $tmpval) {
1500 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $tmpkey)) {
1503 $keyfieldcache = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $tmpkey);
1505 if (in_array($keyfieldcache, $sql_listfields)) {
1507 } elseif ($tmpval ==
'user->id') {
1508 $sql_listfields[] = $keyfieldcache;
1509 $sql_listvalues[] = ((int) $user->id);
1510 } elseif (preg_match(
'/^lastrowid-/', $tmpval)) {
1511 $tmp = explode(
'-', $tmpval);
1512 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
1513 $sql_listfields[] = $keyfieldcache;
1514 $sql_listvalues[] = (int) $lastinsertid;
1515 $keyfield = $keyfieldcache;
1517 } elseif (preg_match(
'/^const-/', $tmpval)) {
1518 $tmp = explode(
'-', $tmpval, 2);
1519 $sql_listfields[] = $keyfieldcache;
1520 $sql_listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
1521 } elseif (preg_match(
'/^rule-/', $tmpval)) {
1522 $fieldname = $tmpkey;
1523 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
1524 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
1525 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
1526 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
1527 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
1528 $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
1530 if (empty($resultload)) {
1531 dol_print_error(
null,
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
1534 $classinstance =
new $class($this->db);
1535 $computedFieldPos = isset($arrayfield[$fieldname]) ? ((int) $arrayfield[$fieldname]) : 0;
1536 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $computedFieldPos));
1537 if (empty($classinstance->error) && empty($classinstance->errors)) {
1538 $fieldArr = explode(
'.', $fieldname);
1539 if (count($fieldArr) > 0) {
1540 $fieldname = $fieldArr[1];
1544 $sql_listfields[] = $this->db->sanitize($fieldname);
1545 if ($type ==
'int') {
1546 $sql_listvalues[] = (int) $res;
1547 } elseif ($type ==
'double') {
1548 $sql_listvalues[] = (float) $res;
1550 $sql_listvalues[] =
"'".$this->db->escape($res).
"'";
1553 $this->errors[$error][
'type'] =
'CLASSERROR';
1554 $this->errors[$error][
'lib'] = implode(
1556 array_merge([$classinstance->error], $classinstance->errors)
1558 $errorforthistable++;
1564 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$tmpval.
' for array_import_fieldshidden';
1565 $this->errors[$error][
'type'] =
'Import profile setup';
1574 if (!$errorforthistable) {
1576 if (!empty($sql_listfields)) {
1577 $updatedone =
false;
1578 $insertdone =
false;
1581 $is_table_category_link =
false;
1582 $sanitizedfname =
'rowid';
1583 if (strpos($tablename,
'_categorie_') !==
false) {
1584 $is_table_category_link =
true;
1585 $sanitizedfname =
'*';
1588 if (!empty($updatekeys)) {
1591 if (empty($lastinsertid)) {
1592 $sqlSelect =
"SELECT ".$sanitizedfname.
" FROM ".$this->db->sanitize($tablename);
1593 $data = array_combine($sql_listfields, $sql_listvalues);
1596 foreach ($updatekeys as $key) {
1597 if (! array_key_exists($key, $objimport->array_import_updatekeys[0])) {
1598 $this->errors[$error][
'lib'] =
'You try to search duplicates on field '.dol_string_nohtmltag($key).
' that is not an allowed field.';
1599 $this->errors[$error][
'type'] =
'UPDATEKEYBADCOLUMN';
1603 $col = $objimport->array_import_updatekeys[0][$key];
1604 $keyfordata = preg_replace(
'/^.*\./i',
'', $key);
1605 $keyfordata = preg_replace(
'/[^a-zA-Z0-9\._]/',
'', $keyfordata);
1607 if (
isModEnabled(
"socialnetworks") && strpos($keyfordata,
"socialnetworks") !==
false) {
1608 $tmp = explode(
"_", $keyfordata);
1609 $keyfordata = $tmp[0];
1610 $socialnetwork = $tmp[1];
1611 $jsondata = $data[$keyfordata];
1612 $json = json_decode($jsondata);
1613 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
1616 $where[] = $this->db->sanitize($keyfordata).
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1617 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
1620 $sanitizedvalue = $data[$keyfordata];
1631 if ((
string) $sanitizedvalue ===
'') {
1632 $this->errors[$error][
'lib'] =
'You request to search duplicates on field '.$keyfordata.
' but no value was provided for this field on this line.';
1633 $this->errors[$error][
'type'] =
'UPDATEKEYBADVALUE';
1636 $where[] = $this->db->sanitize($keyfordata).
" = ".$sanitizedvalue;
1637 $filters[] = $col.
" = ".$sanitizedvalue;
1641 if (!empty($tablewithentity_cache[$tablename])) {
1645 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
1648 $resql = $this->db->query($sqlSelect);
1650 $num_rows = $this->db->num_rows($resql);
1651 if ($num_rows == 1) {
1652 $res = $this->db->fetch_object($resql);
1653 $lastinsertid = $res->rowid;
1654 $keyfield =
'rowid';
1655 if ($is_table_category_link) {
1656 $lastinsertid =
'linktable';
1658 $last_insert_id_array[$tablename] = $lastinsertid;
1659 } elseif ($num_rows > 1) {
1660 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
1661 $this->errors[$error][
'type'] =
'SQL';
1668 $this->errors[$error][
'lib'] = $this->db->lasterror();
1669 $this->errors[$error][
'type'] =
'SQL';
1679 $sqlSelect =
"SELECT rowid FROM ".$tablename;
1680 if (empty($keyfield)) {
1681 $keyfield =
'rowid';
1684 $sqlSelect .=
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1686 if (!empty($tablewithentity_cache[$tablename])) {
1690 $resql = $this->db->query($sqlSelect);
1692 $res = $this->db->fetch_object($resql);
1693 if ($this->db->num_rows($resql) == 1) {
1702 $this->errors[$error][
'lib'] = $this->db->lasterror();
1703 $this->errors[$error][
'type'] =
'SQL';
1708 if (!empty($lastinsertid)) {
1710 if (in_array(
"socialnetworks", $sql_listfields)) {
1711 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1712 $tmpsql = $sql_listvalues[$socialkey];
1713 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1717 $sqlstart =
"UPDATE ".$tablename;
1719 $data = array_combine($sql_listfields, $sql_listvalues);
1721 foreach ($data as $key => $val) {
1722 $sql_set[] = $key.
" = ".$val;
1724 $sqlstart .=
" SET ".implode(
', ', $sql_set).
", import_key = '".$this->db->escape($importid).
"'";
1725 if (empty($keyfield)) {
1726 $keyfield =
'rowid';
1729 $sqlend =
" WHERE ".$this->db->sanitize($keyfield).
" = ".((int) $lastinsertid);
1731 if ($is_table_category_link && !empty($where)) {
1732 '@phan-var-force string[] $where';
1733 $sqlend =
" WHERE " . implode(
' AND ', $where);
1736 if (!empty($tablewithentity_cache[$tablename])) {
1740 $sql = $sqlstart.$sqlend;
1743 $resql = $this->db->query($sql);
1747 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1748 $restrigger = $this->
triggerImportSqlOperation($tablename,
'update', is_numeric($lastinsertid) ? (
int) $lastinsertid : 0, $importid, $user, $langs,
$conf);
1749 if ($restrigger < 0) {
1752 $this->errors[] = array(
'lib' => $langs->trans(
'ErrorFailedTriggerCall'),
'type' =>
'TRIGGER');
1755 } elseif (!$importissimulation) {
1760 $this->errors[$error][
'lib'] = $this->db->lasterror();
1761 $this->errors[$error][
'type'] =
'SQL';
1768 if (!$error && !$updatedone) {
1770 if (in_array(
"socialnetworks", $sql_listfields)) {
1771 $socialkey = array_search(
"socialnetworks", $sql_listfields);
1772 $tmpsql = $sql_listvalues[$socialkey];
1773 $sql_listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1777 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $sql_listfields).
", import_key";
1778 $sqlend =
") VALUES(".implode(
', ', $sql_listvalues).
", '".$this->db->escape($importid).
"'";
1779 if (!empty($tablewithentity_cache[$tablename])) {
1780 $sqlstart .=
", entity";
1781 $sqlend .=
", ".((int)
$conf->entity);
1783 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1784 $sqlstart .=
", ".$this->db->sanitize($objimport->array_import_tables_creator[0][$alias]);
1785 $sqlend .=
", ".((int) $user->id);
1787 $sql = $sqlstart.$sqlend.
")";
1791 $resql = $this->db->query($sql);
1793 if (!$is_table_category_link) {
1794 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1797 if (!$importissimulation && $importtriggermode ===
'strict_line') {
1798 $triggerrowid = (!$is_table_category_link && !empty($last_insert_id_array[$tablename])) ? (
int) $last_insert_id_array[$tablename] : 0;
1800 if ($restrigger < 0) {
1803 $this->errors[] = array(
'lib' => $langs->trans(
'ErrorFailedTriggerCall'),
'type' =>
'TRIGGER');
1806 } elseif (!$importissimulation) {
1811 $this->errors[$error][
'lib'] = $this->db->lasterror();
1812 $this->errors[$error][
'type'] =
'SQL';