From 801af96fcdb95ba0ad564116f7f2d459362cdd11 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 19 Oct 2022 16:09:02 +0200 Subject: [PATCH] FIX Import contact when duplicate thirdparties FIX Import of socialnetwork field --- .../modules/import/import_csv.modules.php | 21 +++++++++--- .../modules/import/import_xlsx.modules.php | 32 +++++++++++++------ htdocs/langs/en_US/errors.lang | 1 + 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 594ab96d955..09f93a436ea 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -458,17 +458,27 @@ class ImportCsv extends ModeleImports $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 (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); } $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id; + //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 $newval = $classinstance->id; - } else { + } elseif (! $error) { 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'])); } 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 (!in_array("socialnetworks", $listfields)) { $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) { - $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]; if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") { $json = new stdClass(); @@ -833,7 +847,6 @@ class ImportCsv extends ModeleImports if (empty($lastinsertid)) { // No insert done yet for a parent table $sqlSelect = "SELECT ".$fname." FROM ".$tablename; - $data = array_combine($listfields, $listvalues); $where = array(); // filters to forge SQL request $filters = array(); // filters to forge output error message diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 3637ce105c1..a6470aef520 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -502,17 +502,28 @@ class ImportXlsx extends ModeleImports }*/ $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 (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); } $this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval] = $classinstance->id; + //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 $newval = $classinstance->id; - } else { + } elseif (! $error) { 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'])); } 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 - if ($conf->socialnetworks->enabled && strpos($fieldname, "socialnetworks") !== false) { + if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) { if (!in_array("socialnetworks", $listfields)) { $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) { - $socialkey = array_search("socialnetworks", $listfields); + $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array. Example socialkey=19 $socialnetwork = explode("_", $fieldname)[1]; if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") { $json = new stdClass(); @@ -797,7 +810,7 @@ class ImportXlsx extends ModeleImports } elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) { $listvalues[] = "''"; } 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])) { // Loop on each hidden fields to add them into listfields/listvalues 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 } if ($val == 'user->id') { @@ -876,17 +889,16 @@ class ImportXlsx extends ModeleImports 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) - $where = array(); - if (empty($lastinsertid)) { // No insert done yet for a parent table $sqlSelect = "SELECT ".$fname." FROM " . $tablename; $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) { $col = $objimport->array_import_updatekeys[0][$key]; $key = preg_replace('/^.*\./i', '', $key); - if ($conf->socialnetworks->enabled && strpos($key, "socialnetworks") !== false) { + if (isModEnabled("socialnetworks") && strpos($key, "socialnetworks") !== false) { $tmp = explode("_", $key); $key = $tmp[0]; $socialnetwork = $tmp[1]; diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index 6ff5a63196a..8c64ccf7874 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -97,6 +97,7 @@ ErrorWrongValueForField=Field %s: '%s' does not match regex rule < ErrorHtmlInjectionForField=Field %s: The value '%s' contains a malicious data not allowed ErrorFieldValueNotIn=Field %s: '%s' is not a value found in field %s of %s ErrorFieldRefNotIn=Field %s: '%s' is not a %s existing ref +ErrorMultipleRecordFoundFromRef=Several record found when searching from ref %s. No way to know which ID to use. ErrorsOnXLines=%s errors found 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.