From c2cfe456475376c99c3d7108d64261eaef42ded1 Mon Sep 17 00:00:00 2001 From: vChabot Date: Wed, 21 Dec 2011 09:46:38 +0000 Subject: [PATCH] // adding categories with shop association --- classes/AdminController.php | 1 + classes/Category.php | 28 ++++++++++++++++++- .../admin/AdminCategoriesController.php | 6 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/classes/AdminController.php b/classes/AdminController.php index 8d80ec14c..3f217effe 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -606,6 +606,7 @@ class AdminControllerCore extends Controller $parent_id = (int)Tools::getValue('id_parent', 1); $this->afterAdd($object); $this->updateAssoShop($object->id); +// d($object); // Save and stay on same form if (Tools::isSubmit('submitAdd'.$this->table.'AndStay')) $this->redirect_after = self::$currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=3&update'.$this->table.'&token='.$token; diff --git a/classes/Category.php b/classes/Category.php index 0ef2585bd..9d55b5e1c 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -1205,9 +1205,13 @@ class CategoryCore extends ObjectModel return $categories; } + /** + * @param $id_shop + * @return bool + */ public function isParentCategoryAvailable($id_shop) { - return Db::getInstance()->getValue(' + return (bool)Db::getInstance()->getValue(' SELECT c.`id_category` FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_shop` cs @@ -1215,5 +1219,27 @@ class CategoryCore extends ObjectModel WHERE cs.`id_shop` = '.(int)$id_shop.' AND c.`id_parent` = '.(int)$this->id_parent); } + + /** + * Add association between shop and cateogries + * @param int $id_shop + * @return bool + */ + public function addShop($id_shop) + { + $sql = ''; + if (!$id_shop) + { + foreach (Shop::getShops(true) as $shop) + $sql .= '('.(int)$this->id.', '.(int)$shop['id_shop'].'),'; + // removing last comma to avoid SQL error + $sql = substr($sql, 0, strlen($sql) - 1); + } else + $sql .= '('.(int)$this->id.', '.(int)$id_shop.')'; + + return Db::getInstance()->execute(' + INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES + '.$sql); + } } diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index c1208c5b3..78d14a5a4 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -433,6 +433,12 @@ class AdminCategoriesControllerCore extends AdminController { return strip_tags(stripslashes($description)); } + + public function afterAdd($object) + { + // associate the category to the context shop + $object->addShop($this->context->shop->getID()); + } }