FIX Import contact when duplicate thirdparties

FIX Import of socialnetwork field
This commit is contained in:
Laurent Destailleur 2022-10-19 16:09:02 +02:00
parent fee00a3655
commit 801af96fcd
3 changed files with 40 additions and 14 deletions

View File

@ -458,17 +458,27 @@ class ImportCsv extends ModeleImports
$param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
} }
call_user_func_array(array($classinstance, $method), $param_array); $result = call_user_func_array(array($classinstance, $method), $param_array);
// If duplicate record found
if (!($classinstance->id != '') && $result == -2) {
$this->errors[$error]['lib'] = $langs->trans('ErrorMultipleRecordFoundFromRef', $newval);
$this->errors[$error]['type'] = 'FOREIGNKEY';
$errorforthistable++;
$error++;
}
// If not found, try the fetch from label // If not found, try the fetch from label
if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') { if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') {
$param_array = array('', '', $newval); $param_array = array('', '', $newval);
call_user_func_array(array($classinstance, $method), $param_array); call_user_func_array(array($classinstance, $method), $param_array);
} }
$this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id; $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id;
//print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. '; //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
if ($classinstance->id != '') { // id may be 0, it is a found value if ($classinstance->id != '') { // id may be 0, it is a found value
$newval = $classinstance->id; $newval = $classinstance->id;
} else { } elseif (! $error) {
if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) { if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
$this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict'])); $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
} elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) { } elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) {
@ -729,9 +739,13 @@ class ImportCsv extends ModeleImports
if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) { if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) {
if (!in_array("socialnetworks", $listfields)) { if (!in_array("socialnetworks", $listfields)) {
$listfields[] = "socialnetworks"; $listfields[] = "socialnetworks";
$socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
$listvalues[$socialkey] = '';
} }
//var_dump($newval); var_dump($arrayrecord[($key - 1)]['type']);
if (!empty($newval) && $arrayrecord[($key - 1)]['type'] > 0) { if (!empty($newval) && $arrayrecord[($key - 1)]['type'] > 0) {
$socialkey = array_search("socialnetworks", $listfields); $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
//var_dump('sk='.$socialkey); // socialkey=19
$socialnetwork = explode("_", $fieldname)[1]; $socialnetwork = explode("_", $fieldname)[1];
if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") { if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") {
$json = new stdClass(); $json = new stdClass();
@ -833,7 +847,6 @@ class ImportCsv extends ModeleImports
if (empty($lastinsertid)) { // No insert done yet for a parent table if (empty($lastinsertid)) { // No insert done yet for a parent table
$sqlSelect = "SELECT ".$fname." FROM ".$tablename; $sqlSelect = "SELECT ".$fname." FROM ".$tablename;
$data = array_combine($listfields, $listvalues); $data = array_combine($listfields, $listvalues);
$where = array(); // filters to forge SQL request $where = array(); // filters to forge SQL request
$filters = array(); // filters to forge output error message $filters = array(); // filters to forge output error message

View File

@ -502,17 +502,28 @@ class ImportXlsx extends ModeleImports
}*/ }*/
$param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
} }
call_user_func_array(array($classinstance, $method), $param_array);
$result = call_user_func_array(array($classinstance, $method), $param_array);
// If duplicate record found
if (!($classinstance->id != '') && $result == -2) {
$this->errors[$error]['lib'] = $langs->trans('ErrorMultipleRecordFoundFromRef', $newval);
$this->errors[$error]['type'] = 'FOREIGNKEY';
$errorforthistable++;
$error++;
}
// If not found, try the fetch from label // If not found, try the fetch from label
if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') { if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') {
$param_array = array('', '', $newval); $param_array = array('', '', $newval);
call_user_func_array(array($classinstance, $method), $param_array); call_user_func_array(array($classinstance, $method), $param_array);
} }
$this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval] = $classinstance->id; $this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval] = $classinstance->id;
//print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. '; //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
if ($classinstance->id != '') { // id may be 0, it is a found value if ($classinstance->id != '') { // id may be 0, it is a found value
$newval = $classinstance->id; $newval = $classinstance->id;
} else { } elseif (! $error) {
if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) { if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
$this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict'])); $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
} elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) { } elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) {
@ -770,12 +781,14 @@ class ImportXlsx extends ModeleImports
} }
// Define $listfields and $listvalues to build SQL request // Define $listfields and $listvalues to build SQL request
if ($conf->socialnetworks->enabled && strpos($fieldname, "socialnetworks") !== false) { if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) {
if (!in_array("socialnetworks", $listfields)) { if (!in_array("socialnetworks", $listfields)) {
$listfields[] = "socialnetworks"; $listfields[] = "socialnetworks";
$socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array. Example socialkey=19
$listvalues[$socialkey] = '';
} }
if (!empty($newval) && $arrayrecord[($key)]['type'] > 0) { if (!empty($newval) && $arrayrecord[($key)]['type'] > 0) {
$socialkey = array_search("socialnetworks", $listfields); $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array. Example socialkey=19
$socialnetwork = explode("_", $fieldname)[1]; $socialnetwork = explode("_", $fieldname)[1];
if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") { if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") {
$json = new stdClass(); $json = new stdClass();
@ -797,7 +810,7 @@ class ImportXlsx extends ModeleImports
} elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) { } elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) {
$listvalues[] = "''"; $listvalues[] = "''";
} else { } else {
$listvalues[] = "'" . $this->db->escape($newval) . "'"; $listvalues[] = "'".$this->db->escape($newval)."'";
} }
} }
} }
@ -810,7 +823,7 @@ class ImportXlsx extends ModeleImports
if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) { if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
// Loop on each hidden fields to add them into listfields/listvalues // Loop on each hidden fields to add them into listfields/listvalues
foreach ($objimport->array_import_fieldshidden[0] as $key => $val) { foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
if (!preg_match('/^' . preg_quote($alias, '/') . '\./', $key)) { if (!preg_match('/^'.preg_quote($alias, '/').'\./', $key)) {
continue; // Not a field of current table continue; // Not a field of current table
} }
if ($val == 'user->id') { if ($val == 'user->id') {
@ -876,17 +889,16 @@ class ImportXlsx extends ModeleImports
if (!empty($updatekeys)) { if (!empty($updatekeys)) {
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields) // We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
$where = array();
if (empty($lastinsertid)) { // No insert done yet for a parent table if (empty($lastinsertid)) { // No insert done yet for a parent table
$sqlSelect = "SELECT ".$fname." FROM " . $tablename; $sqlSelect = "SELECT ".$fname." FROM " . $tablename;
$data = array_combine($listfields, $listvalues); $data = array_combine($listfields, $listvalues);
$filters = array(); $where = array(); // filters to forge SQL request
$filters = array(); // filters to forge output error message
foreach ($updatekeys as $key) { foreach ($updatekeys as $key) {
$col = $objimport->array_import_updatekeys[0][$key]; $col = $objimport->array_import_updatekeys[0][$key];
$key = preg_replace('/^.*\./i', '', $key); $key = preg_replace('/^.*\./i', '', $key);
if ($conf->socialnetworks->enabled && strpos($key, "socialnetworks") !== false) { if (isModEnabled("socialnetworks") && strpos($key, "socialnetworks") !== false) {
$tmp = explode("_", $key); $tmp = explode("_", $key);
$key = $tmp[0]; $key = $tmp[0];
$socialnetwork = $tmp[1]; $socialnetwork = $tmp[1];

View File

@ -97,6 +97,7 @@ ErrorWrongValueForField=Field <b>%s</b>: '<b>%s</b>' does not match regex rule <
ErrorHtmlInjectionForField=Field <b>%s</b>: The value '<b>%s</b>' contains a malicious data not allowed ErrorHtmlInjectionForField=Field <b>%s</b>: The value '<b>%s</b>' contains a malicious data not allowed
ErrorFieldValueNotIn=Field <b>%s</b>: '<b>%s</b>' is not a value found in field <b>%s</b> of <b>%s</b> ErrorFieldValueNotIn=Field <b>%s</b>: '<b>%s</b>' is not a value found in field <b>%s</b> of <b>%s</b>
ErrorFieldRefNotIn=Field <b>%s</b>: '<b>%s</b>' is not a <b>%s</b> existing ref ErrorFieldRefNotIn=Field <b>%s</b>: '<b>%s</b>' is not a <b>%s</b> existing ref
ErrorMultipleRecordFoundFromRef=Several record found when searching from ref <b>%s</b>. No way to know which ID to use.
ErrorsOnXLines=%s errors found ErrorsOnXLines=%s errors found
ErrorFileIsInfectedWithAVirus=The antivirus program was not able to validate the file (file might be infected by a virus) ErrorFileIsInfectedWithAVirus=The antivirus program was not able to validate the file (file might be infected by a virus)
ErrorNumRefModel=A reference exists into database (%s) and is not compatible with this numbering rule. Remove record or renamed reference to activate this module. ErrorNumRefModel=A reference exists into database (%s) and is not compatible with this numbering rule. Remove record or renamed reference to activate this module.