From e6606f3b366dfd8e973f3e08748cc80bc7a9d57c Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Wed, 18 Jan 2023 11:22:52 +0100 Subject: [PATCH] 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; + } }