From 483e9814f9ae4d646d1361c3bf4706aa5659c9e9 Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Thu, 1 Dec 2022 14:33:35 +0100 Subject: [PATCH 1/4] NEW filter on entity in import --- htdocs/core/modules/import/import_csv.modules.php | 12 ++++++++++++ htdocs/core/modules/import/import_xlsx.modules.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 22c12024c2b..0fc2a6609cb 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -870,6 +870,10 @@ class ImportCsv extends ModeleImports $filters[] = $col.' = '.$data[$key]; } } + if (!empty($tablewithentity_cache[$tablename])) { + $where[] = "entity = ".((int) $conf->entity); + $filters[] = "entity = ".((int) $conf->entity); + } $sqlSelect .= " WHERE ".implode(' AND ', $where); $resql = $this->db->query($sqlSelect); @@ -906,6 +910,10 @@ class ImportCsv extends ModeleImports } $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); + if (!empty($tablewithentity_cache[$tablename])) { + $sqlSelect .= " AND entity = ".((int) $conf->entity); + } + $resql = $this->db->query($sqlSelect); if ($resql) { $res = $this->db->fetch_object($resql); @@ -951,6 +959,10 @@ class ImportCsv extends ModeleImports $sqlend = " WHERE " . implode(' AND ', $where); } + if (!empty($tablewithentity_cache[$tablename])) { + $sqlend .= " AND entity = ".((int) $conf->entity); + } + $sql = $sqlstart.$sqlend; // Run update request diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 35adc4476a0..ebfce90c26e 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -916,6 +916,10 @@ class ImportXlsx extends ModeleImports $filters[] = $col.' = '.$data[$key]; } } + if (!empty($tablewithentity_cache[$tablename])) { + $where[] = "entity = ".((int) $conf->entity); + $filters[] = "entity = ".((int) $conf->entity); + } $sqlSelect .= " WHERE " . implode(' AND ', $where); $resql = $this->db->query($sqlSelect); @@ -953,6 +957,10 @@ class ImportXlsx extends ModeleImports } $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); + if (!empty($tablewithentity_cache[$tablename])) { + $sqlSelect .= " AND entity = ".((int) $conf->entity); + } + $resql = $this->db->query($sqlSelect); if ($resql) { $res = $this->db->fetch_object($resql); @@ -998,6 +1006,10 @@ class ImportXlsx extends ModeleImports $sqlend = " WHERE " . implode(' AND ', $where); } + if (!empty($tablewithentity_cache[$tablename])) { + $sqlend .= " AND entity = ".((int) $conf->entity); + } + $sql = $sqlstart . $sqlend; // Run update request From e6606f3b366dfd8e973f3e08748cc80bc7a9d57c Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Wed, 18 Jan 2023 11:22:52 +0100 Subject: [PATCH 2/4] NEW map table to element for get entity in import --- .../modules/import/import_csv.modules.php | 10 ++-- .../modules/import/import_xlsx.modules.php | 10 ++-- htdocs/core/modules/import/modules_import.php | 59 ++++++++++++++++++- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 0fc2a6609cb..393e3c54cee 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -96,6 +96,8 @@ class ImportCsv extends ModeleImports public function __construct($db, $datatoimport) { global $conf, $langs; + + parent::__construct(); $this->db = $db; $this->separator = (GETPOST('separator') ?GETPOST('separator') : (empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? ',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE)); @@ -871,8 +873,8 @@ class ImportCsv extends ModeleImports } } if (!empty($tablewithentity_cache[$tablename])) { - $where[] = "entity = ".((int) $conf->entity); - $filters[] = "entity = ".((int) $conf->entity); + $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $sqlSelect .= " WHERE ".implode(' AND ', $where); @@ -911,7 +913,7 @@ class ImportCsv extends ModeleImports $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); if (!empty($tablewithentity_cache[$tablename])) { - $sqlSelect .= " AND entity = ".((int) $conf->entity); + $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $resql = $this->db->query($sqlSelect); @@ -960,7 +962,7 @@ class ImportCsv extends ModeleImports } if (!empty($tablewithentity_cache[$tablename])) { - $sqlend .= " AND entity = ".((int) $conf->entity); + $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $sql = $sqlstart.$sqlend; diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index ebfce90c26e..acd2a10b040 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -106,6 +106,8 @@ class ImportXlsx extends ModeleImports public function __construct($db, $datatoimport) { global $conf, $langs; + + parent::__construct(); $this->db = $db; // this is used as an extension from the example file code, so we have to put xlsx here !!! @@ -917,8 +919,8 @@ class ImportXlsx extends ModeleImports } } if (!empty($tablewithentity_cache[$tablename])) { - $where[] = "entity = ".((int) $conf->entity); - $filters[] = "entity = ".((int) $conf->entity); + $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $sqlSelect .= " WHERE " . implode(' AND ', $where); @@ -958,7 +960,7 @@ class ImportXlsx extends ModeleImports $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); if (!empty($tablewithentity_cache[$tablename])) { - $sqlSelect .= " AND entity = ".((int) $conf->entity); + $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $resql = $this->db->query($sqlSelect); @@ -1007,7 +1009,7 @@ class ImportXlsx extends ModeleImports } if (!empty($tablewithentity_cache[$tablename])) { - $sqlend .= " AND entity = ".((int) $conf->entity); + $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; } $sql = $sqlstart . $sqlend; diff --git a/htdocs/core/modules/import/modules_import.php b/htdocs/core/modules/import/modules_import.php index d875afc5c4f..73e36bf246c 100644 --- a/htdocs/core/modules/import/modules_import.php +++ b/htdocs/core/modules/import/modules_import.php @@ -75,14 +75,53 @@ class ModeleImports public $libversion = array(); + /** + * @var array Element mapping from table name + */ + public static $mapTableToElement = array( + 'actioncomm' => 'agenda', + 'adherent' => 'member', + 'adherent_type' => 'member_type', + 'bank_account' => 'bank_account', + 'categorie' => 'category', + 'commande' => 'commande', + 'commande_fournisseur' => 'commande_fournisseur', + 'contrat' => 'contract', + 'entrepot' => 'stock', + 'expensereport' => 'expensereport', + 'facture' => 'invoice', + 'facture_fourn' => 'facture_fourn', + 'fichinter' => 'intervention', + 'holiday' => 'holiday', + 'product' => 'product', + 'productprice' => 'productprice', + 'product_fournisseur_price' => 'productsupplierprice', + 'projet' => 'project', + 'propal' => 'propal', + 'societe' => 'societe', + 'socpeople' => 'contact', + 'supplier_proposal' => 'supplier_proposal', + 'ticket' => 'ticket', + ); /** * Constructor */ public function __construct() { - } + global $hookmanager; + if (is_object($hookmanager)) { + $hookmanager->initHooks(array('import')); + $parameters = array(); + $reshook = $hookmanager->executeHooks('constructModeleImports', $parameters, $this); + if ($reshook >= 0 && !empty($hookmanager->resArray)) { + foreach ($hookmanager->resArray as $mapList) { + self::$mapTableToElement[$mapList['table']] = $mapList['element']; + } + } + } + } /** * getDriverId @@ -269,4 +308,22 @@ class ModeleImports { return $this->libversion[$key]; } + + /** + * Get element from table name with prefix + * + * @param string $tableNameWithPrefix Table name with prefix + * @return string Element name or '' if not found + */ + public function getElementFromTableWithPrefix($tableNameWithPrefix) + { + $element = ''; + + $tableElement = preg_replace('/^'.preg_quote($this->db->prefix(), '/').'/', '', $tableNameWithPrefix); + if (isset(self::$mapTableToElement[$tableElement])) { + $element = self::$mapTableToElement[$tableElement]; + } + + return $element; + } } From 1e8dc90b41dcbd0d1cbc46f614dd28d3cddff5f4 Mon Sep 17 00:00:00 2001 From: lvessiller-opendsi Date: Fri, 20 Jan 2023 10:39:02 +0100 Subject: [PATCH 3/4] Update modules_import.php --- htdocs/core/modules/import/modules_import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/modules/import/modules_import.php b/htdocs/core/modules/import/modules_import.php index 73e36bf246c..5904af215d0 100644 --- a/htdocs/core/modules/import/modules_import.php +++ b/htdocs/core/modules/import/modules_import.php @@ -94,7 +94,7 @@ class ModeleImports 'fichinter' => 'intervention', 'holiday' => 'holiday', 'product' => 'product', - 'productprice' => 'productprice', + 'product_price' => 'productprice', 'product_fournisseur_price' => 'productsupplierprice', 'projet' => 'project', 'propal' => 'propal', From f3f840f7e81e2466421cfda22eb39c2a7cd3d20e Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Thu, 26 Jan 2023 15:05:59 +0100 Subject: [PATCH 4/4] NEW remove keys whose table element is the same as element in map list --- htdocs/core/modules/import/modules_import.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/htdocs/core/modules/import/modules_import.php b/htdocs/core/modules/import/modules_import.php index 5904af215d0..0c31f95ce66 100644 --- a/htdocs/core/modules/import/modules_import.php +++ b/htdocs/core/modules/import/modules_import.php @@ -82,26 +82,26 @@ class ModeleImports 'actioncomm' => 'agenda', 'adherent' => 'member', 'adherent_type' => 'member_type', - 'bank_account' => 'bank_account', + //'bank_account' => 'bank_account', 'categorie' => 'category', - 'commande' => 'commande', - 'commande_fournisseur' => 'commande_fournisseur', + //'commande' => 'commande', + //'commande_fournisseur' => 'commande_fournisseur', 'contrat' => 'contract', 'entrepot' => 'stock', - 'expensereport' => 'expensereport', + //'expensereport' => 'expensereport', 'facture' => 'invoice', - 'facture_fourn' => 'facture_fourn', + //'facture_fourn' => 'facture_fourn', 'fichinter' => 'intervention', - 'holiday' => 'holiday', - 'product' => 'product', + //'holiday' => 'holiday', + //'product' => 'product', 'product_price' => 'productprice', 'product_fournisseur_price' => 'productsupplierprice', 'projet' => 'project', - 'propal' => 'propal', - 'societe' => 'societe', + //'propal' => 'propal', + //'societe' => 'societe', 'socpeople' => 'contact', - 'supplier_proposal' => 'supplier_proposal', - 'ticket' => 'ticket', + //'supplier_proposal' => 'supplier_proposal', + //'ticket' => 'ticket', ); /** @@ -313,13 +313,13 @@ class ModeleImports * Get element from table name with prefix * * @param string $tableNameWithPrefix Table name with prefix - * @return string Element name or '' if not found + * @return string Element name or table element as default */ public function getElementFromTableWithPrefix($tableNameWithPrefix) { - $element = ''; - $tableElement = preg_replace('/^'.preg_quote($this->db->prefix(), '/').'/', '', $tableNameWithPrefix); + $element = $tableElement; + if (isset(self::$mapTableToElement[$tableElement])) { $element = self::$mapTableToElement[$tableElement]; }