FIX: categories import: prevent mismatch between category type and object type

This commit is contained in:
Marc de Lima Lucio 2019-07-30 12:31:03 +02:00
parent fe9ddd7649
commit 449ef92007
3 changed files with 42 additions and 14 deletions

View File

@ -493,15 +493,24 @@ class ImportCsv extends ModeleImports
if (! empty($objimport->array_import_regex[0][$val]) && ($newval != '')) if (! empty($objimport->array_import_regex[0][$val]) && ($newval != ''))
{ {
// If test is "Must exist in a field@table" // If test is "Must exist in a field@table"
if (preg_match('/^(.*)@(.*)$/',$objimport->array_import_regex[0][$val],$reg)) if (preg_match('/^(.+)@([^:]+)(:.+)?$/',$objimport->array_import_regex[0][$val],$reg))
{ {
$field=$reg[1]; $field=$reg[1];
$table=$reg[2]; $table=$reg[2];
$filter=substr($reg[3], 1);
$cachekey = $field.'@'.$table;
if(! empty($filter)) $cachekey.= ':'.$filter;
// Load content of field@table into cache array // Load content of field@table into cache array
if (! is_array($this->cachefieldtable[$field.'@'.$table])) // If content of field@table not already loaded into cache if (! is_array($this->cachefieldtable[$cachekey])) // If content of field@table not already loaded into cache
{ {
$sql="SELECT ".$field." as aliasfield FROM ".$table; $sql="SELECT ".$field." as aliasfield FROM ".$table;
if(! empty($filter))
{
$sql.= ' WHERE ' . $filter;
}
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -510,7 +519,7 @@ class ImportCsv extends ModeleImports
while ($i < $num) while ($i < $num)
{ {
$obj=$this->db->fetch_object($resql); $obj=$this->db->fetch_object($resql);
if ($obj) $this->cachefieldtable[$field.'@'.$table][]=$obj->aliasfield; if ($obj) $this->cachefieldtable[$cachekey][]=$obj->aliasfield;
$i++; $i++;
} }
} }
@ -521,9 +530,11 @@ class ImportCsv extends ModeleImports
} }
// Now we check cache is not empty (should not) and key is into cache // Now we check cache is not empty (should not) and key is into cache
if (! is_array($this->cachefieldtable[$field.'@'.$table]) || ! in_array($newval,$this->cachefieldtable[$field.'@'.$table])) if (! is_array($this->cachefieldtable[$cachekey]) || ! in_array($newval,$this->cachefieldtable[$cachekey]))
{ {
$this->errors[$error]['lib']=$langs->transnoentitiesnoconv('ErrorFieldValueNotIn',$key,$newval,$field,$table); $tableforerror = $table;
if(! empty($filter)) $tableforerror.= ':'.$filter;
$this->errors[$error]['lib']=$langs->transnoentitiesnoconv('ErrorFieldValueNotIn',$key,$newval,$field,$tableforerror);
$this->errors[$error]['type']='FOREIGNKEY'; $this->errors[$error]['type']='FOREIGNKEY';
$errorforthistable++; $errorforthistable++;
$error++; $error++;

View File

@ -519,15 +519,24 @@ class ImportXlsx extends ModeleImports
if (! empty($objimport->array_import_regex[0][$val]) && ($newval != '')) if (! empty($objimport->array_import_regex[0][$val]) && ($newval != ''))
{ {
// If test is "Must exist in a field@table" // If test is "Must exist in a field@table"
if (preg_match('/^(.*)@(.*)$/',$objimport->array_import_regex[0][$val],$reg)) if (preg_match('/^(.+)@([^:]+)(:.+)?$/',$objimport->array_import_regex[0][$val],$reg))
{ {
$field=$reg[1]; $field=$reg[1];
$table=$reg[2]; $table=$reg[2];
$filter=substr($reg[3], 1);
$cachekey = $field.'@'.$table;
if(! empty($filter)) $cachekey.= ':'.$filter;
// Load content of field@table into cache array // Load content of field@table into cache array
if (! is_array($this->cachefieldtable[$field.'@'.$table])) // If content of field@table not already loaded into cache if (! is_array($this->cachefieldtable[$cachekey])) // If content of field@table not already loaded into cache
{ {
$sql="SELECT ".$field." as aliasfield FROM ".$table; $sql="SELECT ".$field." as aliasfield FROM ".$table;
if(! empty($filter))
{
$sql.= ' WHERE ' . $filter;
}
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -536,7 +545,7 @@ class ImportXlsx extends ModeleImports
while ($i < $num) while ($i < $num)
{ {
$obj=$this->db->fetch_object($resql); $obj=$this->db->fetch_object($resql);
if ($obj) $this->cachefieldtable[$field.'@'.$table][]=$obj->aliasfield; if ($obj) $this->cachefieldtable[$cachekey][]=$obj->aliasfield;
$i++; $i++;
} }
} }
@ -547,9 +556,11 @@ class ImportXlsx extends ModeleImports
} }
// Now we check cache is not empty (should not) and key is into cache // Now we check cache is not empty (should not) and key is into cache
if (! is_array($this->cachefieldtable[$field.'@'.$table]) || ! in_array($newval,$this->cachefieldtable[$field.'@'.$table])) if (! is_array($this->cachefieldtable[$cachekey]) || ! in_array($newval,$this->cachefieldtable[$cachekey]))
{ {
$this->errors[$error]['lib']=$langs->transnoentitiesnoconv('ErrorFieldValueNotIn',$key,$newval,$field,$table); $tableforerror = $table;
if(! empty($filter)) $tableforerror.= ':'.$filter;
$this->errors[$error]['lib']=$langs->transnoentitiesnoconv('ErrorFieldValueNotIn',$key,$newval,$field,$tableforerror);
$this->errors[$error]['type']='FOREIGNKEY'; $this->errors[$error]['type']='FOREIGNKEY';
$errorforthistable++; $errorforthistable++;
$error++; $error++;

View File

@ -422,8 +422,8 @@ class modCategorie extends DolibarrModules
$this->import_icon[$r]=$this->picto; $this->import_icon[$r]=$this->picto;
$this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
$this->import_tables_array[$r]=array('cp'=>MAIN_DB_PREFIX.'categorie_product'); $this->import_tables_array[$r]=array('cp'=>MAIN_DB_PREFIX.'categorie_product');
$this->import_fields_array[$r]=array('cp.fk_categorie'=>"Category*",'cp.fk_product'=>"Product*" $this->import_fields_array[$r]=array('cp.fk_categorie'=>"Category*",'cp.fk_product'=>"Product*");
); $this->import_regex_array[$r]=array('cp.fk_categorie'=>'rowid@'.MAIN_DB_PREFIX.'categorie:type=0');
$this->import_convertvalue_array[$r]=array( $this->import_convertvalue_array[$r]=array(
'cp.fk_categorie'=>array('rule'=>'fetchidfromref','classfile'=>'/categories/class/categorie.class.php','class'=>'Categorie','method'=>'fetch','element'=>'category'), 'cp.fk_categorie'=>array('rule'=>'fetchidfromref','classfile'=>'/categories/class/categorie.class.php','class'=>'Categorie','method'=>'fetch','element'=>'category'),
@ -441,7 +441,10 @@ class modCategorie extends DolibarrModules
$this->import_icon[$r]=$this->picto; $this->import_icon[$r]=$this->picto;
$this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
$this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_societe'); $this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_societe');
$this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_soc'=>"ThirdParty*" $this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_soc'=>"ThirdParty*");
$this->import_regex_array[$r]=array(
'cs.fk_categorie'=>'rowid@'.MAIN_DB_PREFIX.'categorie:type=2',
'cs.fk_soc'=>'rowid@'.MAIN_DB_PREFIX.'societe:client>0'
); );
$this->import_convertvalue_array[$r]=array( $this->import_convertvalue_array[$r]=array(
@ -460,7 +463,10 @@ class modCategorie extends DolibarrModules
$this->import_icon[$r]=$this->picto; $this->import_icon[$r]=$this->picto;
$this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
$this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_fournisseur'); $this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_fournisseur');
$this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_soc'=>"Supplier*" $this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_soc'=>"Supplier*");
$this->import_regex_array[$r]=array(
'cs.fk_categorie'=>'rowid@'.MAIN_DB_PREFIX.'categorie:type=1',
'cs.fk_soc'=>'rowid@'.MAIN_DB_PREFIX.'societe:fournisseur>0'
); );
$this->import_convertvalue_array[$r]=array( $this->import_convertvalue_array[$r]=array(