Start work to allow updates in import module
This commit is contained in:
parent
34b4d0225e
commit
e6411dd246
@ -268,7 +268,7 @@ class ImportCsv extends ModeleImports
|
||||
* @param string $importid Import key
|
||||
* @return int <0 if KO, >0 if OK
|
||||
*/
|
||||
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid)
|
||||
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid,$updatekeys)
|
||||
{
|
||||
global $langs,$conf,$user;
|
||||
global $thirdparty_static; // Specific to thirdparty import
|
||||
@ -551,47 +551,79 @@ class ImportCsv extends ModeleImports
|
||||
// If no error for this $alias/$tablename, we have a complete $listfields and $listvalues that are defined
|
||||
if (! $errorforthistable)
|
||||
{
|
||||
//print "$alias/$tablename/$listfields/$listvalues<br>";
|
||||
//print "$alias/$tablename/$listfields/$listvalues<br>";
|
||||
if (!empty($listfields))
|
||||
{
|
||||
//var_dump($objimport->array_import_convertvalue); exit;
|
||||
|
||||
// Build SQL UPDATE request
|
||||
$sqlstart = 'UPDATE '.$tablename;
|
||||
|
||||
// Build SQL INSERT request
|
||||
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
|
||||
$sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
|
||||
if (! empty($tablewithentity_cache[$tablename])) {
|
||||
$sqlstart.= ', entity';
|
||||
$sqlend.= ', '.$conf->entity;
|
||||
}
|
||||
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
||||
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
||||
$sqlend.=', '.$user->id;
|
||||
}
|
||||
$sql = $sqlstart.$sqlend.')';
|
||||
dol_syslog("import_csv.modules", LOG_DEBUG);
|
||||
|
||||
//print '> '.join(',',$arrayrecord);
|
||||
//print 'sql='.$sql;
|
||||
//print '<br>'."\n";
|
||||
|
||||
// Run insert request
|
||||
if ($sql)
|
||||
{
|
||||
$resql=$this->db->query($sql);
|
||||
$last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
|
||||
if ($resql)
|
||||
{
|
||||
//print '.';
|
||||
$updatedone = false;
|
||||
if(!empty($updatekeys)) {
|
||||
// Build SQL UPDATE request
|
||||
$sqlstart = 'UPDATE '.$tablename;
|
||||
|
||||
$data = array_combine($listfields, $listvalues);
|
||||
$set = array();
|
||||
foreach ($data as $key => $val) {
|
||||
$set[] = $key.' = '.$val;
|
||||
}
|
||||
else
|
||||
$sqlstart.= ' SET '.implode(', ', $set);
|
||||
|
||||
$where = array();
|
||||
foreach ($updatekeys as $key) {
|
||||
$key=preg_replace('/^.*\./i','',$key);
|
||||
$where[] = $key.' = '.$data[$key];
|
||||
}
|
||||
$sqlend = ' WHERE '.implode(' AND ', $where);
|
||||
|
||||
$sql = $sqlstart.$sqlend;
|
||||
|
||||
// Run update request
|
||||
$resql=$this->db->query($sql);
|
||||
if($resql) {
|
||||
echo $sql;
|
||||
echo '<pre>';
|
||||
//print_r($this->db);
|
||||
//print_r($this->db->db);
|
||||
echo '</pre>';
|
||||
echo '<b>'.var_dump($this->db->db->affected_rows).'</b>';
|
||||
if($this->db->db->affected_rows > 0) {
|
||||
$this->nbupdate++;
|
||||
$updatedone = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update not done, we do insert
|
||||
if(!$updatedone) {
|
||||
// Build SQL INSERT request
|
||||
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
|
||||
$sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
|
||||
if (! empty($tablewithentity_cache[$tablename])) {
|
||||
$sqlstart.= ', entity';
|
||||
$sqlend.= ', '.$conf->entity;
|
||||
}
|
||||
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
||||
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
||||
$sqlend.=', '.$user->id;
|
||||
}
|
||||
$sql = $sqlstart.$sqlend.')';
|
||||
echo $sql;
|
||||
dol_syslog("import_csv.modules", LOG_DEBUG);
|
||||
|
||||
// Run insert request
|
||||
if ($sql)
|
||||
{
|
||||
//print 'E';
|
||||
$this->errors[$error]['lib']=$this->db->lasterror();
|
||||
$this->errors[$error]['type']='SQL';
|
||||
$error++;
|
||||
$resql=$this->db->query($sql);
|
||||
$last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
|
||||
if ($resql)
|
||||
{
|
||||
$this->nbinsert++;
|
||||
}
|
||||
else
|
||||
{
|
||||
//print 'E';
|
||||
$this->errors[$error]['lib']=$this->db->lasterror();
|
||||
$this->errors[$error]['type']='SQL';
|
||||
$error++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -340,6 +340,7 @@ class modSociete extends DolibarrModules
|
||||
//$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
|
||||
$this->import_regex_array[$r]=array('s.status'=>'^[0|1]','s.client'=>'^[0|1|2|3]','s.fournisseur'=>'^[0|1]','s.fk_typent'=>'id@'.MAIN_DB_PREFIX.'c_typent','s.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$');
|
||||
$this->import_examplevalues_array[$r]=array('s.nom'=>"MyBigCompany",'s.status'=>"0 (closed) or 1 (active)",'s.client'=>'0 (no customer no prospect)/1 (customer)/2 (prospect)/3 (customer and prospect)','s.fournisseur'=>'0 or 1','s.datec'=>dol_print_date(dol_now(),'%Y-%m-%d'),'s.code_client'=>'CU01-0001 or empty or "auto"','s.code_fournisseur'=>'SU01-0001 or empty or "auto"','s.address'=>"61 jump street",'s.zip'=>"123456",'s.town'=>"Big town",'s.fk_pays'=>'US, FR, DE...','s.phone'=>"0101010101",'s.fax'=>"0101010102",'s.url'=>"http://mycompany.com",'s.email'=>"test@mycompany.com",'s.siret'=>"",'s.siren'=>"",'s.ape'=>"",'s.idprof4'=>"",'s.idprof5'=>"",'s.idprof6'=>"",'s.tva_intra'=>"FR0123456789",'s.capital'=>"10000",'s.note_private'=>"This is an example of private note for record",'s.note_public'=>"This is an example of public note for record",'s.fk_typent'=>"2",'s.fk_effectif'=>"3","s.fk_forme_juridique"=>"1",'s.fk_prospectlevel'=>'PL_MEDIUM','s.fk_stcomm'=>'0','s.default_lang'=>'en_US','s.barcode'=>'123456789','s.datec'=>"2015-01-01 or 2015-01-01 12:30:00");
|
||||
$this->import_updatekeys_array[$r]=array('s.nom'=>'Name','s.code_client'=>'CustomerCode','s.code_fournisseur'=>'SupplierCode','s.code_compta'=>'CustomerAccountancyCode','s.code_compta_fournisseur'=>'SupplierAccountancyCode');
|
||||
|
||||
// Import list of contact and attributes
|
||||
$r++;
|
||||
|
||||
@ -38,6 +38,7 @@ class Import
|
||||
var $array_import_fieldshidden;
|
||||
var $array_import_entities;
|
||||
var $array_import_regex;
|
||||
var $array_import_updatekeys;
|
||||
var $array_import_examplevalues;
|
||||
var $array_import_convertvalue;
|
||||
var $array_import_run_sql_after;
|
||||
@ -153,6 +154,8 @@ class Import
|
||||
$this->array_import_entities[$i]=$module->import_entities_array[$r];
|
||||
// Tableau des alias a exporter (cle=champ, valeur=alias)
|
||||
$this->array_import_regex[$i]=$module->import_regex_array[$r];
|
||||
// Array of columns allowed as UPDATE options
|
||||
$this->array_import_updatekeys[$i]=$module->import_updatekeys_array[$r];
|
||||
// Array of examples
|
||||
$this->array_import_examplevalues[$i]=$module->import_examplevalues_array[$r];
|
||||
// Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
|
||||
|
||||
@ -76,7 +76,8 @@ $import_name = GETPOST('import_name');
|
||||
$hexa = GETPOST('hexa');
|
||||
$importmodelid = GETPOST('importmodelid');
|
||||
$excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 1);
|
||||
$endatlinenb = (GETPOST('endatlinenb') ? GETPOST('endatlinenb') : '');
|
||||
$endatlinenb = (GETPOST('endatlinenb') ? GETPOST('endatlinenb') : '');
|
||||
$updatekeys = (GETPOST('updatekeys') ? GETPOST('updatekeys') : array());
|
||||
$separator = (GETPOST('separator') ? GETPOST('separator') : (! empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE)?$conf->global->IMPORT_CSV_SEPARATOR_TO_USE:','));
|
||||
$enclosure = (GETPOST('enclosure') ? GETPOST('enclosure') : '"');
|
||||
|
||||
@ -1165,8 +1166,9 @@ if ($step == 5 && $datatoimport)
|
||||
|
||||
$param='&leftmenu=import&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines.'&separator='.urlencode($separator).'&enclosure='.urlencode($enclosure);
|
||||
$param2 = $param; // $param2 = $param without excludefirstline and endatlinenb
|
||||
if ($excludefirstline) $param.='&excludefirstline='.$excludefirstline;
|
||||
if ($endatlinenb) $param.='&endatlinenb='.$endatlinenb;
|
||||
if ($excludefirstline) $param.='&excludefirstline='.$excludefirstline;
|
||||
if ($endatlinenb) $param.='&endatlinenb='.$endatlinenb;
|
||||
if (!empty($updatekeys)) $param.='&updatekeys[]='.implode('&updatekeys[]=', $updatekeys);
|
||||
|
||||
llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones');
|
||||
|
||||
@ -1272,6 +1274,24 @@ if ($step == 5 && $datatoimport)
|
||||
}
|
||||
print '</td></tr>';
|
||||
|
||||
print '<tr><td>';
|
||||
print $langs->trans("KeysToUseForUpdates");
|
||||
print '</td><td>';
|
||||
if($action=='launchsimu') {
|
||||
print $form->multiselectarray('updatekeysbis', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '', 'disabled');
|
||||
foreach($updatekeys as $val) {
|
||||
print '<input type="hidden" name="updatekeys[]" value="'.$val.'">';
|
||||
}
|
||||
print ' <a href="'.$_SERVER["PHP_SELF"].'?step=5'.$param.'">'.$langs->trans("Modify").'</a>';
|
||||
} else {
|
||||
print $form->multiselectarray('updatekeys', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%');
|
||||
print $form->textwithpicto("", $langs->trans("SelectColumnsOfYourFileForUpdateAttempt"));
|
||||
}
|
||||
/*echo '<pre>';
|
||||
print_r($objimport->array_import_updatekeys);
|
||||
echo '</pre>';*/
|
||||
print '</td></tr>';
|
||||
|
||||
print '</table>';
|
||||
|
||||
print '<br>';
|
||||
@ -1405,7 +1425,7 @@ if ($step == 5 && $datatoimport)
|
||||
if ($endatlinenb && ($sourcelinenb > $endatlinenb)) continue;
|
||||
|
||||
// Run import
|
||||
$result=$obj->import_insert($arrayrecord,$array_match_file_to_database,$objimport,count($fieldssource),$importid);
|
||||
$result=$obj->import_insert($arrayrecord,$array_match_file_to_database,$objimport,count($fieldssource),$importid,$updatekeys);
|
||||
|
||||
if (count($obj->errors)) $arrayoferrors[$sourcelinenb]=$obj->errors;
|
||||
if (count($obj->warnings)) $arrayofwarnings[$sourcelinenb]=$obj->warnings;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user