diff --git a/classes/Category.php b/classes/Category.php index c87b33543..356cef134 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -1367,19 +1367,25 @@ class CategoryCore extends ObjectModel */ public function addShop($id_shop) { - $sql = ''; + $sql = ' + INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES + '; + $datas = ''; if (!$id_shop) { foreach (Shop::getShops(true) as $shop) - $sql .= '('.(int)$this->id.', '.(int)$shop['id_shop'].'),'; + if (!$this->existsInShop($shop['id_shop'])) + $datas .= '('.(int)$this->id.', '.(int)$shop['id_shop'].'),'; // removing last comma to avoid SQL error - $sql = substr($sql, 0, strlen($sql) - 1); + $datas = substr($datas, 0, strlen($datas) - 1); } else - $sql .= '('.(int)$this->id.', '.(int)$id_shop.')'; + if (!$this->existsInShop($id_shop)) + $datas .= '('.(int)$this->id.', '.(int)$id_shop.')'; - return Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES - '.$sql); + if (empty($datas)) + return false; + else + return Db::getInstance()->execute($sql.$datas); } public static function getRootCategories($id_lang = null, $active = true) @@ -1524,4 +1530,13 @@ class CategoryCore extends ObjectModel return $return; } + + public function existsInShop($id_shop) + { + return (bool)Db::getInstance()->getValue(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'category_shop` + WHERE `id_category` = '.(int)$this->id.' + AND `id_shop` = '.(int)$id_shop); + } } diff --git a/controllers/admin/AdminImportController.php b/controllers/admin/AdminImportController.php index 94f030868..35b729da9 100644 --- a/controllers/admin/AdminImportController.php +++ b/controllers/admin/AdminImportController.php @@ -163,6 +163,10 @@ class AdminImportControllerCore extends AdminController 'meta_description' => array('label' => $this->l('Meta-description')), 'link_rewrite' => array('label' => $this->l('URL rewritten')), 'image' => array('label' => $this->l('Image URL')), + 'shop' => array( + 'label' => $this->l('ID / Name of shop'), + 'help' => $this->l('Ignore this field if you don\'t use the multishop tool. If you leave this field empty, default shop will be used'), + ), ); self::$default_values = array( @@ -899,6 +903,18 @@ class AdminImportControllerCore extends AdminController $this->errors[] = ($field_error !== true ? $field_error : '').($lang_field_error !== true ? $lang_field_error : ''). Db::getInstance()->getMsgError(); } + else + { + // Associate category to shop + if (Shop::isFeatureActive() && $info['shop']) + { + if (!is_numeric($info['shop'])) + $info['id_shop'] = Shop::getIdByName($info['shop']); + } + else + $info['shop'] = Configuration::get('PS_SHOP_DEFAULT'); + $category->addShop($info['shop']); + } } /* Import has finished, we can regenerate the categories nested tree */