From 585b670df32a7bf49bfaa8f367bd93bc9a2a869c Mon Sep 17 00:00:00 2001 From: vChabot Date: Thu, 19 Jan 2012 14:03:33 +0000 Subject: [PATCH] [*] BO : categories multishop improved --- .../controllers/categories/list_header.tpl | 27 ++++----- classes/Category.php | 58 +++++++++++-------- classes/Tools.php | 5 +- .../admin/AdminCategoriesController.php | 49 +++++----------- controllers/admin/AdminGroupsController.php | 9 +-- controllers/admin/AdminProductsController.php | 7 +-- install-new/data/xml/category.xml | 3 +- install-new/langs/de/data/category.xml | 8 +++ install-new/langs/en/data/category.xml | 8 +++ install-new/langs/es/data/category.xml | 8 +++ install-new/langs/fr/data/category.xml | 8 +++ install-new/langs/it/data/category.xml | 8 +++ install-new/models/install.php | 2 +- .../generate_root_category_for_multishop.php | 44 ++++++++++++++ install-new/upgrade/sql/1.5.0.4.sql | 11 ++++ 15 files changed, 163 insertions(+), 92 deletions(-) create mode 100644 install-new/upgrade/php/generate_root_category_for_multishop.php diff --git a/admin-dev/themes/default/template/controllers/categories/list_header.tpl b/admin-dev/themes/default/template/controllers/categories/list_header.tpl index 1ceeefcd8..845c4ac0a 100644 --- a/admin-dev/themes/default/template/controllers/categories/list_header.tpl +++ b/admin-dev/themes/default/template/controllers/categories/list_header.tpl @@ -28,27 +28,20 @@ {block name=leadin}
- {if count($categories_tree) == 0} - {if $category_root->id_category == 0} -   {$category_root->name} + {assign var=i value=0} + {foreach $categories_tree key=key item=category} + {if $i++ == 0} +   + {assign var=params_url value=""} {else} -   {$categories_name} + {assign var=params_url value="&id_category={$category.id_category}&viewcategory"} {/if} - {else} - {if $category_root->id_category == 0} -   {$category_root->name} >  + {if $key == 0} + {$category.name} {else} -   {$category_root->name} >  + {$category.name} >  {/if} - - {foreach $categories_tree key=key item=category} - {if $key == 0} - {$category.name} - {else} - {$category.name} >  - {/if} - {/foreach} - {/if} + {/foreach}
{if isset($delete_category) && $delete_category}
diff --git a/classes/Category.php b/classes/Category.php index fd9aa872b..6d8a77ff2 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -390,19 +390,21 @@ class CategoryCore extends ObjectModel */ public static function regenerateEntireNtree() { - $categories = Db::getInstance()->executeS('SELECT id_category, id_parent FROM '._DB_PREFIX_.'category ORDER BY id_category ASC'); + $categories = Db::getInstance()->executeS('SELECT id_category, id_parent FROM '._DB_PREFIX_.'category ORDER BY id_parent, position ASC'); $categories_array = array(); foreach ($categories as $category) - $categories_array[(int)$category['id_parent']]['subcategories'][(int)$category['id_category']] = 1; - $n = 1; - Category::_subTree($categories_array, 1, $n); + $categories_array[$category['id_parent']]['subcategories'][] = $category['id_category']; + $n = 1; + + if (isset($categories_array[0]) && $categories_array[0]['subcategories']) + Category::_subTree($categories_array, $categories_array[0]['subcategories'][0], $n); } protected static function _subTree(&$categories, $id_category, &$n) { - $left = (int)$n++; + $left = $n++; if (isset($categories[(int)$id_category]['subcategories'])) - foreach (array_keys($categories[(int)$id_category]['subcategories']) as $id_subcategory) + foreach ($categories[(int)$id_category]['subcategories'] as $id_subcategory) Category::_subTree($categories, (int)$id_subcategory, $n); $right = (int)$n++; @@ -660,7 +662,7 @@ class CategoryCore extends ObjectModel */ public static function getHomeCategories($id_lang, $active = true) { - return Category::getChildren(1, $id_lang, $active); + return self::getChildren(1, $id_lang, $active); } public static function getRootCategory($id_lang = null, Shop $shop = null) @@ -671,17 +673,13 @@ class CategoryCore extends ObjectModel if (!$shop) $shop = $context->shop; - // context : no multishop - if (count(Shop::getShops()) == 1) - if (count(Category::getCategoriesWithoutParent()) > 1) - $category = new Category(); - else - $category = new Category($shop->getCategory(), $id_lang); - else // context : multishop - if (count(Category::getCategoriesWithoutParent()) > 1 && $context->shop() != Shop::CONTEXT_SHOP) - $category = new Category(); - else - $category = new Category($shop->getCategory(), $id_lang); + $is_more_than_one_root_category = count(Category::getCategoriesWithoutParent()) > 1; + if ((!Shop::isFeatureActive() && $is_more_than_one_root_category) || + Shop::isFeatureActive() && $is_more_than_one_root_category && $context->shop() != Shop::CONTEXT_SHOP) + $category = Category::getTopCategory(); + else + $category = new Category($shop->getCategory(), $id_lang); + return $category; } @@ -932,17 +930,20 @@ class CategoryCore extends ObjectModel LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)$id_lang.$context->shop->addSqlRestrictionOnLang('cl').')'; - if (count(Shop::getShops()) > 1 && $context->shop() == Shop::CONTEXT_SHOP) + if (Shop::isFeatureActive() && $context->shop() == Shop::CONTEXT_SHOP) $sql .= ' LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON c.`id_category` = cs.`id_category`'; $sql .= ' WHERE c.`id_category` = '.(int)$id_current; - if (count(Shop::getShops()) > 1 && $context->shop() == Shop::CONTEXT_SHOP) + if (Shop::isFeatureActive() && $context->shop() == Shop::CONTEXT_SHOP) $sql .= ' AND cs.`id_shop` = '.(int)$context->shop->getID(true); $root_category = Category::getRootCategory(); - if ($context->shop() == Shop::CONTEXT_SHOP && (!Tools::isSubmit('id_category') || (int)Tools::getValue('id_category') == (int)$root_category->id_category || (int)$root_category->id_category == (int)$context->shop->id_category)) + if (Shop::isFeatureActive() && $context->shop() == Shop::CONTEXT_SHOP && + (!Tools::isSubmit('id_category') || + (int)Tools::getValue('id_category') == (int)$root_category->id_category || + (int)$root_category->id_category == (int)$context->shop->id_category)) $sql .= ' AND c.`id_parent` != 0'; @@ -952,7 +953,7 @@ class CategoryCore extends ObjectModel $categories[] = $result[0]; else if (!$categories) $categories = array(); - if (!$result || $result[0]['id_category'] == $context->shop->id_category) + if (!$result || ($result[0]['id_category'] == $context->shop->id_category && Shop::isFeatureActive())) return $categories; $id_current = $result[0]['id_parent']; } @@ -1317,7 +1318,7 @@ class CategoryCore extends ObjectModel SELECT DISTINCT c.* FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)Context::getContext()->language->id.') - WHERE `id_parent` = 0 + WHERE `level_depth` = 1 '); } @@ -1328,5 +1329,14 @@ class CategoryCore extends ObjectModel FROM `'._DB_PREFIX_.'shop` WHERE `id_category` = '.(int)$this->id); } -} + public static function getTopCategory() + { + $id_category = Db::getInstance()->getValue(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = 0 + '); + return new Category($id_category); + } +} diff --git a/classes/Tools.php b/classes/Tools.php index 0293a0cba..dbc80d7a1 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -973,7 +973,9 @@ class ToolsCore if ($categoryType === 'products') { $interval = Category::getInterval($id_category); - $intervalRoot = Category::getInterval($context->shop->getCategory()); + $id_root_category = $context->shop->getCategory(); + $root_category = new Category($id_root_category); + $intervalRoot = Category::getInterval($id_root_category); if ($interval) { $sql = 'SELECT c.id_category, cl.name, cl.link_rewrite @@ -985,6 +987,7 @@ class ToolsCore AND c.nright <= '.$intervalRoot['nright'].' AND cl.id_lang = '.(int)$context->language->id.' AND c.active = 1 + AND c.level_depth > '.(int)$root_category->level_depth.' ORDER BY c.level_depth ASC'; $categories = Db::getInstance()->executeS($sql); diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index 7b071a189..d73d306d1 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -93,7 +93,10 @@ class AdminCategoriesControllerCore extends AdminController if (($id_category = Tools::getvalue('id_category')) && $this->action != 'select_delete') $this->_category = new Category($id_category); else - $this->_category = new Category($this->context->shop->id_category); + if (Shop::isFeatureActive() && $this->context->shop() == Shop::CONTEXT_SHOP) + $this->_category = new Category($this->context->shop->id_category); + else + $this->_category = Category::getTopCategory(); } public function initContent() @@ -123,22 +126,22 @@ class AdminCategoriesControllerCore extends AdminController $this->addRowAction('view'); $count_categories_without_parent = count(Category::getCategoriesWithoutParent()); - $nb_shop = count(Shop::getShops()); + $is_multishop = Shop::isFeatureActive(); if (Tools::isSubmit('id_category')) $id_parent = $this->_category->id; - else if ($nb_shop == 1 && $count_categories_without_parent > 1) - $id_parent = 0; - else if ($nb_shop > 1 && $count_categories_without_parent == 1) + else if (!$is_multishop && $count_categories_without_parent > 1) + $id_parent = 1; + else if ($is_multishop && $count_categories_without_parent == 1) + $id_parent = 2; + else if ($is_multishop && $count_categories_without_parent > 1 && $this->context->shop() != Shop::CONTEXT_SHOP) $id_parent = 1; - else if ($nb_shop > 1 && $count_categories_without_parent > 1 && $this->context->shop() != Shop::CONTEXT_SHOP) - $id_parent = 0; else $id_parent = $this->context->shop->id_category; $this->_filter .= ' AND `id_parent` = '.(int)$id_parent.' '; $this->_select = 'position '; // we add restriction for shop - if (Shop::CONTEXT_SHOP == Context::getContext()->shop() && $nb_shop > 1) + if (Shop::CONTEXT_SHOP == Context::getContext()->shop() && $is_multishop) { $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON a.`id_category` = cs.`id_category`'; $this->_where = ' AND cs.`id_shop` = '.(int)Context::getContext()->shop->getID(true); @@ -147,29 +150,11 @@ class AdminCategoriesControllerCore extends AdminController $categories_tree = $this->_category->getParentsCategories(); if (empty($categories_tree) && ($this->_category->id_category != 1 || Tools::isSubmit('id_category')) - && (Shop::CONTEXT_SHOP == Context::getContext()->shop() && $nb_shop == 1 && $count_categories_without_parent > 1)) + && (Shop::CONTEXT_SHOP == Context::getContext()->shop() && !$is_multishop && $count_categories_without_parent > 1)) $categories_tree = array(array('name' => $this->_category->name[$this->context->language->id])); asort($categories_tree); - if ($nb_shop == 1 && $count_categories_without_parent > 1) - $categories_name = $this->l('Root'); - else - if ($this->_category->getName() == '') - { - $categories_name = new Category($id_parent); - $categories_name = stripslashes($categories_name->getName()); - } - else - $categories_name = stripslashes($this->_category->getName()); - $root = Category::getRootCategory(); - if (!is_array($root->name) && empty($root->name)) - { - $root->name = $this->l('Root'); - $root->id = $root->id_category = 0; - } $this->tpl_list_vars['categories_tree'] = $categories_tree; - $this->tpl_list_vars['categories_name'] = $categories_name; - $this->tpl_list_vars['category_root'] = $root; if (Tools::isSubmit('submitBulkdelete'.$this->table) OR Tools::isSubmit('delete'.$this->table)) $this->tpl_list_vars['delete_category'] = true; @@ -271,10 +256,7 @@ class AdminCategoriesControllerCore extends AdminController $guest_group_information = sprintf($this->l('%s - Customer who placed an order with the Guest Checkout.'), "".$guest->name[$this->context->language->id].""); $default_group_information = sprintf($this->l('%s - All persons who created an account on this site.'), "".$default->name[$this->context->language->id].""); $root_category = Category::getRootCategory(); - if (!$root_category->id_category) - $root_category = array('id_category' => '0', 'name' => $this->l('Root')); - else - $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name); + $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name[$this->context->language->id]); $this->fields_form = array( 'tinymce' => true, 'legend' => array( @@ -451,10 +433,7 @@ class AdminCategoriesControllerCore extends AdminController $id_parent = (int)Tools::getValue('id_parent'); // if true, we are in a root category creation if (!$id_parent && !Tools::isSubmit('is_root_category')) - { - $_POST['id_parent'] = $id_parent = 0; - $_POST['is_root_category'] = 1; - } + $_POST['is_root_category'] = $_POST['level_depth'] = $_POST['id_parent'] = $id_parent = 1; if ($id_category) { if ($id_category != $id_parent) diff --git a/controllers/admin/AdminGroupsController.php b/controllers/admin/AdminGroupsController.php index 7cfe1c997..edf06cc31 100644 --- a/controllers/admin/AdminGroupsController.php +++ b/controllers/admin/AdminGroupsController.php @@ -247,13 +247,8 @@ class AdminGroupsControllerCore extends AdminController ) ) ); - if ($this->context->shop() == Shop::CONTEXT_SHOP) - { - $root_category = Category::getRootCategory(); - $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name); - } - else - $root_category = array('id_category' => '0', 'name' => $this->l('Root')); + $root_category = Category::getRootCategory(); + $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name[$this->context->language->id]); $this->fields_value['reduction'] = isset($group->reduction) ? $group->reduction : 0; $trads = array( diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 3d63d6f96..226310aae 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -2472,12 +2472,7 @@ class AdminProductsControllerCore extends AdminController $selected_cat = Product::getProductCategoriesFull($product->id, $this->default_form_language); } $root_category = Category::getRootCategory(); - if (!$root_category->id_category) - { - $root_category->id_category = 0; - $root_category->name = $this->l('Root'); - } - $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name); + $root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name[$this->context->language->id]); $translations = array( 'Root' => $root_category, 'selected' => $this->l('selected'), diff --git a/install-new/data/xml/category.xml b/install-new/data/xml/category.xml index ae59944b2..45845ae0f 100644 --- a/install-new/data/xml/category.xml +++ b/install-new/data/xml/category.xml @@ -5,6 +5,7 @@ - + + diff --git a/install-new/langs/de/data/category.xml b/install-new/langs/de/data/category.xml index 21fbec134..6e692ef85 100644 --- a/install-new/langs/de/data/category.xml +++ b/install-new/langs/de/data/category.xml @@ -1,5 +1,13 @@ + + Root + + root + + + + Start diff --git a/install-new/langs/en/data/category.xml b/install-new/langs/en/data/category.xml index 75b64387f..3c34ab702 100644 --- a/install-new/langs/en/data/category.xml +++ b/install-new/langs/en/data/category.xml @@ -1,5 +1,13 @@ + + Root + + root + + + + Home diff --git a/install-new/langs/es/data/category.xml b/install-new/langs/es/data/category.xml index 5441ac0c1..929aa84de 100644 --- a/install-new/langs/es/data/category.xml +++ b/install-new/langs/es/data/category.xml @@ -1,5 +1,13 @@ + + Root + + root + + + + Inicio diff --git a/install-new/langs/fr/data/category.xml b/install-new/langs/fr/data/category.xml index 969e6f73a..6389d16b9 100644 --- a/install-new/langs/fr/data/category.xml +++ b/install-new/langs/fr/data/category.xml @@ -1,5 +1,13 @@ + + Root + + root + + + + Accueil diff --git a/install-new/langs/it/data/category.xml b/install-new/langs/it/data/category.xml index 8fcc95190..3477765f4 100644 --- a/install-new/langs/it/data/category.xml +++ b/install-new/langs/it/data/category.xml @@ -1,5 +1,13 @@ + + Root + + root + + + + Home page diff --git a/install-new/models/install.php b/install-new/models/install.php index e2638bd21..3e71dce1e 100644 --- a/install-new/models/install.php +++ b/install-new/models/install.php @@ -219,7 +219,7 @@ class InstallModelInstall extends InstallAbstractModel $shop = new Shop(); $shop->active = true; $shop->id_group_shop = $group_shop->id; - $shop->id_category = 1; + $shop->id_category = 2; $shop->id_theme = 1; $shop->name = 'Default'; if (!$shop->add()) diff --git a/install-new/upgrade/php/generate_root_category_for_multishop.php b/install-new/upgrade/php/generate_root_category_for_multishop.php new file mode 100644 index 000000000..649c94d36 --- /dev/null +++ b/install-new/upgrade/php/generate_root_category_for_multishop.php @@ -0,0 +1,44 @@ +execute(' + UPDATE `'._DB_PREFIX_.'category` SET `level_depth`=`level_depth`+1 + '); + + Db::getInstance()->execute(' + INSERT INTO `'._DB_PREFIX_.'category` (`id_parent`, `level_depth`, `active`, `date_add`, `date_upd`, `is_root_category`) VALUES + (0, 0, 1, NOW(), NOW(), 0) + '); + $id = Db::getInstance()->Insert_ID(); + + $langs = Db::getInstance()->executeS(' + SELECT `id_lang` + FROM `'._DB_PREFIX_.'lang` + '); + + $shops = Db::getInstance()->executeS(' + SELECT `id_shop` + FROM `'._DB_PREFIX_.'shop` + '); + + $data = array(); + foreach ($langs as $lang) + foreach ($shops as $shop) + $data[] = array( + 'id_lang' => $lang['id_lang'], + 'id_shop' => $shop['id_shop'], + 'id_category' => $id, + 'name' => 'Root', + 'link_rewrite' => '', + ); + Db::getInstance()->autoExecute(_DB_PREFIX_.'category_lang', $data, 'INSERT'); + + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'category` + SET `id_parent` = '.(int)$id.' + WHERE `id_parent` = 0 AND `id_category` <> '.(int)$id.' + '); + + Category::regenerateEntireNtree(); +} \ No newline at end of file diff --git a/install-new/upgrade/sql/1.5.0.4.sql b/install-new/upgrade/sql/1.5.0.4.sql index e6f161ef7..cc4df1543 100644 --- a/install-new/upgrade/sql/1.5.0.4.sql +++ b/install-new/upgrade/sql/1.5.0.4.sql @@ -18,3 +18,14 @@ WHERE `name` = "order fenced" AND (`id_lang` = 1 OR `id_lang` = 3 OR `id_lang` = UPDATE `PREFIX_supply_order_state_lang` SET `name` = "Commande cloturée" WHERE `name` = "order fenced" AND id_lang = 2; + + +CREATE TABLE `PREFIX_category_shop` ( + `id_category` int(11) NOT NULL, + `id_shop` int(11) NOT NULL, + PRIMARY KEY (`id_category`, `id_shop`), + UNIQUE KEY `id_category_shop` (`id_category`,`id_shop`) +) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; + + +/* PHP:generate_root_category_for_multishop(); */; \ No newline at end of file