diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 199baf1e1c7..8ad5ef94b2f 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)); @@ -870,6 +872,10 @@ class ImportCsv extends ModeleImports $filters[] = $col.' = '.$data[$key]; } } + if (!empty($tablewithentity_cache[$tablename])) { + $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } $sqlSelect .= " WHERE ".implode(' AND ', $where); $resql = $this->db->query($sqlSelect); @@ -906,6 +912,10 @@ class ImportCsv extends ModeleImports } $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); + if (!empty($tablewithentity_cache[$tablename])) { + $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } + $resql = $this->db->query($sqlSelect); if ($resql) { $res = $this->db->fetch_object($resql); @@ -951,6 +961,10 @@ class ImportCsv extends ModeleImports $sqlend = " WHERE " . implode(' AND ', $where); } + if (!empty($tablewithentity_cache[$tablename])) { + $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } + $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 949c6bd9258..4ced5a7dff4 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 !!! @@ -916,6 +918,10 @@ class ImportXlsx extends ModeleImports $filters[] = $col.' = '.$data[$key]; } } + if (!empty($tablewithentity_cache[$tablename])) { + $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } $sqlSelect .= " WHERE " . implode(' AND ', $where); $resql = $this->db->query($sqlSelect); @@ -953,6 +959,10 @@ class ImportXlsx extends ModeleImports } $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid); + if (!empty($tablewithentity_cache[$tablename])) { + $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } + $resql = $this->db->query($sqlSelect); if ($resql) { $res = $this->db->fetch_object($resql); @@ -998,6 +1008,10 @@ class ImportXlsx extends ModeleImports $sqlend = " WHERE " . implode(' AND ', $where); } + if (!empty($tablewithentity_cache[$tablename])) { + $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")"; + } + $sql = $sqlstart . $sqlend; // Run update request diff --git a/htdocs/core/modules/import/modules_import.php b/htdocs/core/modules/import/modules_import.php index fa4a5ad3692..5fe408f6dd5 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', + 'product_price' => '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 @@ -267,4 +306,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 table element as default + */ + public function getElementFromTableWithPrefix($tableNameWithPrefix) + { + $tableElement = preg_replace('/^'.preg_quote($this->db->prefix(), '/').'/', '', $tableNameWithPrefix); + $element = $tableElement; + + if (isset(self::$mapTableToElement[$tableElement])) { + $element = self::$mapTableToElement[$tableElement]; + } + + return $element; + } }