From de62e4cdb893bd071421f3cda1605a9186c07c42 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 3 Jul 2016 11:11:43 +0200 Subject: [PATCH 01/10] CSV import : refactor SQL insert construction --- .../modules/import/import_csv.modules.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 895ed6de89d..3fbbae17a9e 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -557,22 +557,22 @@ class ImportCsv extends ModeleImports if ($listfields) { //var_dump($objimport->array_import_convertvalue); exit; + + // Build SQL UPDATE request + $sqlstart = 'UPDATE '.$tablename; - // Build SQL request - if (empty($tablewithentity_cache[$tablename])) - { - $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; - $sql.=') VALUES('.$listvalues.", '".$importid."'"; + // Build SQL INSERT request + $sqlstart = 'INSERT INTO '.$tablename.'('.$listfields.', import_key'; + $sqlend = ') VALUES('.$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; } - else - { - $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key, entity'; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; - $sql.=') VALUES('.$listvalues.", '".$importid."', ".$conf->entity ; - } - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id; - $sql.=')'; + $sql = $sqlstart.$sqlend.')'; dol_syslog("import_csv.modules", LOG_DEBUG); //print '> '.join(',',$arrayrecord); From 142aedc0cd7551c712bc62b105b4b280e69571b3 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 3 Jul 2016 11:15:28 +0200 Subject: [PATCH 02/10] CSV import : listfields and listvalues are now arrays --- .../modules/import/import_csv.modules.php | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 3fbbae17a9e..0a78f59ab03 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -304,8 +304,8 @@ class ImportCsv extends ModeleImports { // Build sql request $sql=''; - $listfields=''; - $listvalues=''; + $listfields=array(); + $listvalues=array(); $i=0; $errorforthistable=0; @@ -514,36 +514,34 @@ class ImportCsv extends ModeleImports } // Define $listfields and $listvalues to build SQL request - if ($listfields) { $listfields.=', '; $listvalues.=', '; } - $listfields.=$fieldname; + $listfields[] = $fieldname; // Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert - if (empty($newval) && $arrayrecord[($key-1)]['type'] < 0) $listvalues.=($newval=='0'?$newval:"null"); - elseif (empty($newval) && $arrayrecord[($key-1)]['type'] == 0) $listvalues.="''"; - else $listvalues.="'".$this->db->escape($newval)."'"; + if (empty($newval) && $arrayrecord[($key-1)]['type'] < 0) $listvalues[] = ($newval=='0'?$newval:"null"); + elseif (empty($newval) && $arrayrecord[($key-1)]['type'] == 0) $listvalues[] = "''"; + else $listvalues[] = "'".$this->db->escape($newval)."'"; } $i++; } // We add hidden fields (but only if there is at least one field to add into table) - if ($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 foreach($objimport->array_import_fieldshidden[0] as $key => $val) { if (! preg_match('/^'.preg_quote($alias).'\./', $key)) continue; // Not a field of current table - if ($listfields) { $listfields.=', '; $listvalues.=', '; } if ($val == 'user->id') { - $listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key); - $listvalues.=$user->id; + $listfields[] = preg_replace('/^'.preg_quote($alias).'\./','',$key); + $listvalues[] = $user->id; } elseif (preg_match('/^lastrowid-/',$val)) { $tmp=explode('-',$val); $lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0; - $listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key); - $listvalues.=$lastinsertid; + $listfields[] = preg_replace('/^'.preg_quote($alias).'\./','',$key); + $listvalues[] = $lastinsertid; //print $key."-".$val."-".$listfields."-".$listvalues."
";exit; } } @@ -554,7 +552,7 @@ class ImportCsv extends ModeleImports if (! $errorforthistable) { //print "$alias/$tablename/$listfields/$listvalues
"; - if ($listfields) + if (!empty($listfields)) { //var_dump($objimport->array_import_convertvalue); exit; @@ -562,8 +560,8 @@ class ImportCsv extends ModeleImports $sqlstart = 'UPDATE '.$tablename; // Build SQL INSERT request - $sqlstart = 'INSERT INTO '.$tablename.'('.$listfields.', import_key'; - $sqlend = ') VALUES('.$listvalues.", '".$importid."'"; + $sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key'; + $sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'"; if (! empty($tablewithentity_cache[$tablename])) { $sqlstart.= ', entity'; $sqlend.= ', '.$conf->entity; From a950eb399c0490d6963eac79e3467edecaf1555e Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Thu, 7 Jul 2016 18:55:15 +0200 Subject: [PATCH 03/10] =?UTF-8?q?Commit=20nul=20pour=20des=20CRLF,=20?= =?UTF-8?q?=C3=A0=20reverter=20+=20tard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phpoffice/phpexcel/Classes/PHPExcel/locale/cs/config | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/cs/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/da/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/da/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/de/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/de/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/en/uk/config | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/es/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/es/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/fi/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/fi/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/fr/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/fr/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/hu/config | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/hu/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/it/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/it/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/nl/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/nl/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/no/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/no/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/pl/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/pl/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/pt/br/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/pt/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/ru/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/ru/functions | 2 +- .../phpoffice/phpexcel/Classes/PHPExcel/locale/sv/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/tr/config | 4 ++-- .../phpoffice/phpexcel/Classes/PHPExcel/locale/tr/functions | 2 +- 30 files changed, 44 insertions(+), 44 deletions(-) diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/config index 500460e8ab5..8992916acaf 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/functions index b648e20c2b0..f9d69784ae2 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/cs/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/config index b959379b133..cef47e9502d 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -37,7 +37,7 @@ currencySymbol = kr ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NUL! DIV0 = #DIVISION/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/functions index 1599ccd1815..1db4d30bb1c 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/da/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/config index 7e2ba9d2891..ff7e29899cc 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = € ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NULL! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/functions index 8214f384878..ce85641a3c9 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/de/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/en/uk/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/en/uk/config index 00acff8bc34..f008e61cc90 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/en/uk/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/en/uk/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/config index 8f7d9e084ec..fa16f5639df 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = $ ## I'm surprised that the Excel Documentation suggests $ rath ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #¡NULO! DIV0 = #¡DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/functions index aa065969f3d..51ce48b3153 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/es/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/config index 36bc3fc70b8..a481864a6fa 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = $ # Symbol not known, should it be a € (Euro)? ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #TYHJÄ! DIV0 = #JAKO/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/functions index c10b3b9f381..7bed722a641 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fi/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/config index 80f7d5411a6..2240d6b9f8b 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = € ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NUL! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/functions index cce977b15bc..8d25f6ac193 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/fr/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/config index c6c315814c4..dec7cbde15d 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/functions index 941c1b740d0..4abce13b91b 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/hu/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/config index 97af8b9a570..f862a02d72d 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = € ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NULLO! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/functions index 862cf8302c8..b9219a6a4ce 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/it/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/config index a14b476c945..7377a181df0 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = € ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #LEEG! DIV0 = #DEEL/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/functions index 79b7acd1ef9..b6b8296ef04 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/nl/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/config index e3e3cc4f786..15fcc128640 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = kr ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NULL! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/functions index 3cccce42a1d..57a80a7a406 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/no/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/config index ea111797270..fb1e7b13d9d 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = zł ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #ZERO! DIV0 = #DZIEL/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/functions index 5607f8f672c..2e5697973c1 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pl/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/br/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/br/config index d39c5c63438..e99aad6bea4 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/br/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/br/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = R$ ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NULO! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/config index 5e486bb29ff..36df63cc012 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/pt/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = € ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #NULO! DIV0 = #DIV/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/config index 098c8075d29..205c342ada4 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = р ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #ПУСТО! DIV0 = #ДЕЛ/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/functions index 86bcd4f63a7..324c3df2a81 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/ru/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/sv/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/sv/config index c2094c06249..454e52ef52f 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/sv/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/sv/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = kr ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #Skärning! DIV0 = #Division/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/config b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/config index cca084b2ba5..8a103d3c196 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/config +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/config @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or @@ -36,7 +36,7 @@ currencySymbol = YTL ## -## Excel Error Codes (For future use) +## Excel Error Codes (For future use) ## NULL = #BOŞ! DIV0 = #SAYI/0! diff --git a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/functions b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/functions index 3e7c225f402..79645214714 100644 --- a/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/functions +++ b/htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/locale/tr/functions @@ -1,6 +1,6 @@ ## ## PHPExcel -## +## ## Copyright (c) 2006 - 2013 PHPExcel ## ## This library is free software; you can redistribute it and/or From e6411dd246e1cadc6ca3dc397a706fdf800667fb Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Mon, 22 Aug 2016 12:25:03 +0200 Subject: [PATCH 04/10] Start work to allow updates in import module --- .../modules/import/import_csv.modules.php | 108 ++++++++++++------ htdocs/core/modules/modSociete.class.php | 1 + htdocs/imports/class/import.class.php | 3 + htdocs/imports/import.php | 28 ++++- 4 files changed, 98 insertions(+), 42 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 0a78f59ab03..bf923828c76 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -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
"; + //print "$alias/$tablename/$listfields/$listvalues
"; 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 '
'."\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 '
';
+								//print_r($this->db);
+								//print_r($this->db->db);
+								echo '
'; + echo ''.var_dump($this->db->db->affected_rows).''; + 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++; + } } } } diff --git a/htdocs/core/modules/modSociete.class.php b/htdocs/core/modules/modSociete.class.php index baf680f0352..a72ae8c8adf 100644 --- a/htdocs/core/modules/modSociete.class.php +++ b/htdocs/core/modules/modSociete.class.php @@ -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++; diff --git a/htdocs/imports/class/import.class.php b/htdocs/imports/class/import.class.php index 254628b38fa..828e0f53719 100644 --- a/htdocs/imports/class/import.class.php +++ b/htdocs/imports/class/import.class.php @@ -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) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 89d69974577..3eefa101aea 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -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 ''; + print ''; + print $langs->trans("KeysToUseForUpdates"); + print ''; + if($action=='launchsimu') { + print $form->multiselectarray('updatekeysbis', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '', 'disabled'); + foreach($updatekeys as $val) { + print ''; + } + print '   '.$langs->trans("Modify").''; + } else { + print $form->multiselectarray('updatekeys', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%'); + print $form->textwithpicto("", $langs->trans("SelectColumnsOfYourFileForUpdateAttempt")); + } + /*echo '
';
+	print_r($objimport->array_import_updatekeys);
+	echo '
';*/ + print ''; + print ''; print '
'; @@ -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; From 578cab9a70e8e86752e9492320ef184d65884c82 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 2 Oct 2016 19:14:28 +0200 Subject: [PATCH 05/10] Error management + show nb insert/update + langs --- .../core/modules/import/import_csv.modules.php | 17 ++++++++++------- htdocs/imports/import.php | 8 ++++++-- htdocs/langs/en_US/exports.lang | 4 ++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index bf923828c76..012ef84f5f1 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -54,6 +54,9 @@ class ImportCsv extends ModeleImports var $cacheconvert=array(); // Array to cache list of value found after a convertion var $cachefieldtable=array(); // Array to cache list of value found into fields@tables + + var $nbinsert = 0; // # of insert done during the import + var $nbupdate = 0; // # of update done during the import /** @@ -578,17 +581,18 @@ class ImportCsv extends ModeleImports // Run update request $resql=$this->db->query($sql); if($resql) { - echo $sql; - echo '
';
-								//print_r($this->db);
-								//print_r($this->db->db);
-								echo '
'; - echo ''.var_dump($this->db->db->affected_rows).''; if($this->db->db->affected_rows > 0) { $this->nbupdate++; $updatedone = true; } } + else + { + //print 'E'; + $this->errors[$error]['lib']=$this->db->lasterror(); + $this->errors[$error]['type']='SQL'; + $error++; + } } // Update not done, we do insert @@ -605,7 +609,6 @@ class ImportCsv extends ModeleImports $sqlend.=', '.$user->id; } $sql = $sqlstart.$sqlend.')'; - echo $sql; dol_syslog("import_csv.modules", LOG_DEBUG); // Run insert request diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 3eefa101aea..141c83c1ba0 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -1461,7 +1461,11 @@ if ($step == 5 && $datatoimport) $db->rollback(); // We force rollback because this was just a simulation. // Show OK - if (! count($arrayoferrors) && ! count($arrayofwarnings)) print img_picto($langs->trans("OK"),'tick').' '.$langs->trans("NoError").'

'; + if (! count($arrayoferrors) && ! count($arrayofwarnings)) { + print img_picto($langs->trans("OK"),'tick').' '.$langs->trans("NoError").'
'; + print $langs->trans("NbInsert", $obj->nbinsert).'
'; + print $langs->trans("NbUpdate", $obj->nbupdate).'

'; + } else print $langs->trans("NbOfLinesOK",$nbok).'

'; // Show Errors @@ -1768,7 +1772,7 @@ if ($step == 6 && $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; diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index 55e592dc91d..2011a8a0d8e 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -122,3 +122,7 @@ SelectFilterFields=If you want to filter on some values, just input values here. FilteredFields=Filtered fields FilteredFieldsValues=Value for filter FormatControlRule=Format control rule +## imports updates +KeysToUseForUpdates=Key to use for updating data +NbInsert=Number of inserted lines: %s +NbUpdate=Number of updated lines: %s \ No newline at end of file From f995768f24cee4f5db3c1cb048b720ff3dcb6df6 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 2 Oct 2016 19:17:22 +0200 Subject: [PATCH 06/10] Show import results --- htdocs/imports/import.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 141c83c1ba0..ce8e0c1f316 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -1818,7 +1818,9 @@ if ($step == 6 && $datatoimport) // Show result print '
'; print '
'; - print $langs->trans("NbOfLinesImported",$nbok).'

'; + print $langs->trans("NbOfLinesImported",$nbok).'
'; + print $langs->trans("NbInsert", $obj->nbinsert).'
'; + print $langs->trans("NbUpdate", $obj->nbupdate).'

'; print $langs->trans("FileWasImported",$importid).'
'; print $langs->trans("YouCanUseImportIdToFindRecord",$importid).'
'; print '
'; From c4b96925758a7ec0e3a5c230203182067fcf0267 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Mon, 3 Oct 2016 09:46:31 +0200 Subject: [PATCH 07/10] Missing comment --- htdocs/core/modules/import/import_csv.modules.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 012ef84f5f1..79f10cc5551 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -269,6 +269,7 @@ class ImportCsv extends ModeleImports * @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...) * @param int $maxfields Max number of fields to use * @param string $importid Import key + * @param array $updatekeys Array of keys to use to try to do update * @return int <0 if KO, >0 if OK */ function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid,$updatekeys) From 5f704fe5e5cbde3c8c009362a7ad915e0de0fa60 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 13 Nov 2016 09:03:34 +0100 Subject: [PATCH 08/10] Import update, new way of working with a select --- .../modules/import/import_csv.modules.php | 110 ++++++++++++------ htdocs/langs/en_US/exports.lang | 3 +- 2 files changed, 77 insertions(+), 36 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index a07d67d9a0e..d0cc94ede9e 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -303,6 +303,8 @@ class ImportCsv extends ModeleImports else { $last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id) + $updatedone = false; + $insertdone = false; // For each table to insert, me make a separate insert foreach($objimport->array_import_tables[0] as $alias => $tablename) { @@ -548,7 +550,8 @@ class ImportCsv extends ModeleImports { $tmp=explode('-',$val); $lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0; - $listfields[] = preg_replace('/^'.preg_quote($alias).'\./','',$key); + $keyfield = preg_replace('/^'.preg_quote($alias).'\./','',$key); + $listfields[] = $keyfield; $listvalues[] = $lastinsertid; //print $key."-".$val."-".$listfields."-".$listvalues."
";exit; } @@ -563,45 +566,79 @@ class ImportCsv extends ModeleImports if (!empty($listfields)) { $updatedone = false; + $insertdone = 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; - } - $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) { - if($this->db->db->affected_rows > 0) { - $this->nbupdate++; - $updatedone = true; + // We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields) + if(empty($lastinsertid)) { + $sqlSelect = 'SELECT rowid FROM '.$tablename; + + $data = array_combine($listfields, $listvalues); + $where = array(); + $filters = array(); + foreach ($updatekeys as $key) { + $col = $objimport->array_import_updatekeys[0][$key]; + $key=preg_replace('/^.*\./i','',$key); + $where[] = $key.' = '.$data[$key]; + $filters[] = $col.' = '.$data[$key]; + } + $sqlSelect.= ' WHERE '.implode(' AND ', $where); + + $resql=$this->db->query($sqlSelect); + if($resql) { + $res = $this->db->fetch_object($resql); + if($resql->num_rows == 1) { + $lastinsertid = $res->rowid; + $last_insert_id_array[$tablename] = $lastinsertid; + } else { + $this->errors[$error]['lib']=$langs->trans('MultipleRecordFoundWithTheseFilters', implode($filter, ', ')); + $this->errors[$error]['type']='SQL'; + $error++; + } + } + else + { + //print 'E'; + $this->errors[$error]['lib']=$this->db->lasterror(); + $this->errors[$error]['type']='SQL'; + $error++; } } - else - { - //print 'E'; - $this->errors[$error]['lib']=$this->db->lasterror(); - $this->errors[$error]['type']='SQL'; - $error++; + + if(!empty($lastinsertid)) { + // Build SQL UPDATE request + $sqlstart = 'UPDATE '.$tablename; + + $data = array_combine($listfields, $listvalues); + $set = array(); + foreach ($data as $key => $val) { + $set[] = $key.' = '.$val; + } + $sqlstart.= ' SET '.implode(', ', $set); + + if(empty($keyfield)) $keyfield = 'rowid'; + $sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid; + + $sql = $sqlstart.$sqlend; + + // Run update request + $resql=$this->db->query($sql); + if($resql) { + if($this->db->db->affected_rows > 0) { + $updatedone = true; + } + } + else + { + //print 'E'; + $this->errors[$error]['lib']=$this->db->lasterror(); + $this->errors[$error]['type']='SQL'; + $error++; + } } } // Update not done, we do insert - if(!$updatedone) { + if(!$error && !$updatedone) { // Build SQL INSERT request $sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key'; $sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'"; @@ -615,7 +652,7 @@ class ImportCsv extends ModeleImports } $sql = $sqlstart.$sqlend.')'; dol_syslog("import_csv.modules", LOG_DEBUG); - + // Run insert request if ($sql) { @@ -623,7 +660,7 @@ class ImportCsv extends ModeleImports $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++; + $insertdone = true; } else { @@ -643,6 +680,9 @@ class ImportCsv extends ModeleImports if ($error) break; } + + if($updatedone) $this->nbupdate++; + if($insertdone) $this->nbinsert++; } return 1; diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index 7e08acbecfc..7c95f12a30d 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -123,4 +123,5 @@ FormatControlRule=Format control rule ## imports updates KeysToUseForUpdates=Key to use for updating data NbInsert=Number of inserted lines: %s -NbUpdate=Number of updated lines: %s \ No newline at end of file +NbUpdate=Number of updated lines: %s +MultipleRecordFoundWithTheseFilters=Multiple records have been found with these filters: %s \ No newline at end of file From 1dcecf487306546e22f8ea1921d6b923fcff70f4 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 13 Nov 2016 09:32:43 +0100 Subject: [PATCH 09/10] Fix select and update done check --- htdocs/core/modules/import/import_csv.modules.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index d0cc94ede9e..494dd84ab91 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -589,10 +589,12 @@ class ImportCsv extends ModeleImports if($resql->num_rows == 1) { $lastinsertid = $res->rowid; $last_insert_id_array[$tablename] = $lastinsertid; - } else { - $this->errors[$error]['lib']=$langs->trans('MultipleRecordFoundWithTheseFilters', implode($filter, ', ')); + } else if($resql->num_rows > 1) { + $this->errors[$error]['lib']=$langs->trans('MultipleRecordFoundWithTheseFilters', implode($filters, ', ')); $this->errors[$error]['type']='SQL'; $error++; + } else { + // No record found with filters, insert will be tried below } } else @@ -623,9 +625,8 @@ class ImportCsv extends ModeleImports // Run update request $resql=$this->db->query($sql); if($resql) { - if($this->db->db->affected_rows > 0) { - $updatedone = true; - } + // No error, update has been done. $this->db->db->affected_rows can be 0 if data hasn't changed + $updatedone = true; } else { From edef8f883b8a14ed7a311f16edba698333b24fea Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sun, 13 Nov 2016 09:40:09 +0100 Subject: [PATCH 10/10] Add update option on product module --- htdocs/core/modules/modProduct.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/core/modules/modProduct.class.php b/htdocs/core/modules/modProduct.class.php index 4ff68ae8d6b..330349b0842 100644 --- a/htdocs/core/modules/modProduct.class.php +++ b/htdocs/core/modules/modProduct.class.php @@ -248,6 +248,7 @@ class modProduct extends DolibarrModules $this->import_regex_array[$r]=array('p.ref'=>'[^ ]','p.tosell'=>'^[0|1]$','p.tobuy'=>'^[0|1]$','p.fk_product_type'=>'^[0|1]$','p.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$','p.recuperableonly'=>'^[0|1]$'); $import_sample=array('p.ref'=>"PREF123456",'p.label'=>"My product",'p.description'=>"This is a description example for record",'p.note'=>"Some note",'p.price'=>"100",'p.price_ttc'=>"110",'p.tva_tx'=>'10','p.tosell'=>"0 or 1",'p.tobuy'=>"0 or 1",'p.fk_product_type'=>"0 for product/1 for service",'p.finished'=>'','p.duration'=>"1y",'p.datec'=>'2008-12-31','p.recuperableonly'=>'0 or 1'); $this->import_examplevalues_array[$r]=array_merge($import_sample,$import_extrafield_sample); + $this->import_updatekeys_array[$r]=array('p.ref'=>'Ref','p.barcode'=>'BarCode'); if (! empty($conf->fournisseur->enabled)) { @@ -276,6 +277,7 @@ class modProduct extends DolibarrModules 'sp.unitprice'=>'50', 'sp.remise_percent'=>'0' ); + $this->import_updatekeys_array[$r]=array('sp.fk_product'=>'ProductOrService','sp.ref_fourn'=>'SupplierRef'); } if (! empty($conf->global->PRODUIT_MULTIPRICES))