diff --git a/admin-dev/tabs/AdminProducts.php b/admin-dev/tabs/AdminProducts.php index 2a26699e2..af4395a95 100644 --- a/admin-dev/tabs/AdminProducts.php +++ b/admin-dev/tabs/AdminProducts.php @@ -1399,7 +1399,6 @@ class AdminProducts extends AdminTab public function display($token = NULL) { - if ($id_category = (int)Tools::getValue('id_category')) AdminTab::$currentIndex .= '&id_category='.$id_category; $this->getList($this->context->language->id, !$this->context->cookie->__get($this->table.'Orderby') ? 'position' : NULL, !$this->context->cookie->__get($this->table.'Orderway') ? 'ASC' : NULL, 0, NULL, $this->context->shop->getID(true)); diff --git a/classes/CMSCategory.php b/classes/CMSCategory.php index 1b49ca30b..27eaf66d5 100644 --- a/classes/CMSCategory.php +++ b/classes/CMSCategory.php @@ -211,7 +211,7 @@ class CMSCategoryCore extends ObjectModel $category['cms'] = Db::getInstance()->ExecuteS($sql); if ($links == 1) { - $category['link'] = $context->link->getCMSCategoryLink($current, $category['link_rewrite']); + $category['link'] = $link->getCMSCategoryLink($current, $category['link_rewrite']); foreach($category['cms'] as $key => $cms) $category['cms'][$key]['link'] = $link->getCMSLink($cms['id_cms'], $cms['link_rewrite']); } diff --git a/classes/Category.php b/classes/Category.php index 3c2805f6b..db50e67ae 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -596,7 +596,7 @@ class CategoryCore extends ObjectModel if (!$shop) $shop = Context::getContext()->shop; - return new Category($context->shop->getCategory(), $id_lang); + return new Category($shop->getCategory(), $id_lang); } /** @@ -986,12 +986,30 @@ class CategoryCore extends ObjectModel */ public static function getInterval($id) { - $sql = 'SELECT nleft, nright FROM '._DB_PREFIX_.'category + $sql = 'SELECT nleft, nright, level_depth + FROM '._DB_PREFIX_.'category WHERE id_category = '.(int)$id; if (!$result = Db::getInstance()->getRow($sql)) return false; return $result; } + + /** + * Check if current category is a child of shop root category + * + * @since 1.5.0 + * @param Shop $shop + * @return bool + */ + public function inShop(Shop $shop = null) + { + if (!$shop) + $shop = Context::getContext()->shop; + + if (!$interval = Category::getInterval($shop->getCategory())) + return false; + return ($this->nleft >= $interval['nleft'] && $this->nright <= $interval['nright']); + } public function getChildrenWs() { diff --git a/classes/Tools.php b/classes/Tools.php index 38f56eec6..712397426 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -819,7 +819,8 @@ class ToolsCore { if (!$context) $context = Context::getContext(); - + + $id_category = (int)$id_category; if ($id_category == 1) return ''.$path.''; @@ -828,23 +829,23 @@ class ToolsCore $pipe = '>'; $fullPath = ''; - if ($categoryType === 'products') { - $category = Db::getInstance()->getRow(' - SELECT id_category, level_depth, nleft, nright - FROM '._DB_PREFIX_.'category - WHERE id_category = '.(int)$id_category); - - if (isset($category['id_category'])) + $interval = Category::getInterval($id_category); + $intervalRoot = Category::getInterval($context->shop->getCategory()); + if ($interval) { - $categories = Db::getInstance()->ExecuteS(' - SELECT c.id_category, cl.name, cl.link_rewrite - FROM '._DB_PREFIX_.'category c - LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = c.id_category) - WHERE c.nleft <= '.(int)$category['nleft'].' AND c.nright >= '.(int)$category['nright'].' AND cl.id_lang = '.(int)$context->language->id.' AND c.id_category != 1 - ORDER BY c.level_depth ASC - LIMIT '.(int)$category['level_depth']); + $sql = 'SELECT c.id_category, cl.name, cl.link_rewrite + FROM '._DB_PREFIX_.'category c + LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = c.id_category'.$context->shop->sqlLang('cl').') + WHERE c.nleft <= '.$interval['nleft'].' + AND c.nright >= '.$interval['nright'].' + AND c.nleft >= '.$intervalRoot['nleft'].' + AND c.nright <= '.$intervalRoot['nright'].' + AND cl.id_lang = '.(int)$context->language->id.' + AND c.active = 1 + ORDER BY c.level_depth ASC'; + $categories = Db::getInstance()->ExecuteS($sql); $n = 1; $nCategories = (int)sizeof($categories); @@ -860,7 +861,7 @@ class ToolsCore return $fullPath.$path; } } - elseif ($categoryType === 'CMS') + else if ($categoryType === 'CMS') { $category = new CMSCategory($id_category, $context->language->id); if (!Validate::isLoadedObject($category)) @@ -872,7 +873,7 @@ class ToolsCore else $fullPath = ($linkOntheLastItem ? '' : '').htmlentities($path, ENT_NOQUOTES, 'UTF-8').($linkOntheLastItem ? '' : ''); - return self::getPath((int)($category->id_parent), $fullPath, $linkOntheLastItem, $categoryType); + return self::getPath($category->id_parent, $fullPath, $linkOntheLastItem, $categoryType); } } @@ -883,17 +884,22 @@ class ToolsCore { if (!$context) $context = Context::getContext(); - + + $id_category = (int)$id_category; $pipe = (Configuration::get('PS_NAVIGATION_PIPE') ? Configuration::get('PS_NAVIGATION_PIPE') : '>'); - if($type_cat === 'products') - $category = new Category($id_category, $context->language->id); + $defaultCategory = 1; + if ($type_cat === 'products') + { + $defaultCategory = $context->shop->getCategory(); + $category = new Category($id_category, $context->language->id); + } else if ($type_cat === 'CMS') $category = new CMSCategory($id_category, $context->language->id); if (!Validate::isLoadedObject($category)) - $id_category = 1; - if ($id_category == 1) + $id_category = $defaultCategory; + if ($id_category == $defaultCategory) return htmlentities($end, ENT_NOQUOTES, 'UTF-8'); return self::getPath($id_category, $category->name, true, $type_cat).''.$pipe.' '.htmlentities($end, ENT_NOQUOTES, 'UTF-8').''; diff --git a/controllers/CMSController.php b/controllers/CMSController.php index fd4dd90fd..06b5c29f4 100644 --- a/controllers/CMSController.php +++ b/controllers/CMSController.php @@ -99,7 +99,7 @@ class CmsControllerCore extends FrontController $this->context->smarty->assign(array( 'cms' => $this->cms, 'content_only' => (int)(Tools::getValue('content_only')), - 'path' => ((isset($this->cms->id_cms_category) AND $this->cms->id_cms_category) ? Tools::getFullPath((int)($this->cms->id_cms_category), $this->cms->meta_title, 'CMS') : Tools::getFullPath(1, $this->cms->meta_title, 'CMS')) + 'path' => ((isset($this->cms->id_cms_category) AND $this->cms->id_cms_category) ? Tools::getFullPath($this->cms->id_cms_category, $this->cms->meta_title, 'CMS') : Tools::getFullPath(1, $this->cms->meta_title, 'CMS')) )); } elseif ($this->assignCase == 2) @@ -108,7 +108,7 @@ class CmsControllerCore extends FrontController 'category' => $this->cms_category, 'sub_category' => $this->cms_category->getSubCategories($this->context->language->id), 'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id) ), - 'path' => ($this->cms_category->id !== 1) ? Tools::getPath((int)($this->cms_category->id), $this->cms_category->name, false, 'CMS') : '', + 'path' => ($this->cms_category->id !== 1) ? Tools::getPath($this->cms_category->id, $this->cms_category->name, false, 'CMS') : '', )); } } diff --git a/controllers/CategoryController.php b/controllers/CategoryController.php index 0f42eac80..342368f3f 100644 --- a/controllers/CategoryController.php +++ b/controllers/CategoryController.php @@ -92,7 +92,7 @@ class CategoryControllerCore extends FrontController $this->errors[] = Tools::displayError('Missing category ID'); else { - if (!Validate::isLoadedObject($this->category)) + if (!Validate::isLoadedObject($this->category) || !$this->category->inShop()) $this->errors[] = Tools::displayError('Category does not exist'); elseif (!$this->category->checkAccess($this->context->customer->id)) $this->errors[] = Tools::displayError('You do not have access to this category.'); @@ -103,7 +103,7 @@ class CategoryControllerCore extends FrontController $rewrited_url = $this->context->link->getCategoryLink((int)$this->category->id, $this->category->link_rewrite); /* Scenes (could be externalised to another controler if you need them */ - $this->context->smarty->assign('scenes', Scene::getScenes((int)($this->category->id), $this->context->language->id, true, false)); + $this->context->smarty->assign('scenes', Scene::getScenes($this->category->id, $this->context->language->id, true, false)); /* Scenes images formats */ if ($sceneImageTypes = ImageType::getImagesTypes('scenes')) @@ -142,7 +142,7 @@ class CategoryControllerCore extends FrontController 'id_category' => (int)($this->category->id), 'id_category_parent' => (int)($this->category->id_parent), 'return_category_name' => Tools::safeOutput($this->category->name), - 'path' => Tools::getPath((int)($this->category->id)), + 'path' => Tools::getPath($this->category->id), 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'categorySize' => Image::getSize('category'), 'mediumSize' => Image::getSize('medium'), diff --git a/controllers/ProductController.php b/controllers/ProductController.php index 2f437c695..8423629a7 100644 --- a/controllers/ProductController.php +++ b/controllers/ProductController.php @@ -167,7 +167,7 @@ class ProductControllerCore extends FrontController if (isset($category) AND Validate::isLoadedObject($category)) { $this->context->smarty->assign(array( - 'path' => Tools::getPath((int)$category->id, $this->product->name, true), + 'path' => Tools::getPath($category->id, $this->product->name, true), 'category' => $category, 'subCategories' => $category->getSubCategories($this->context->language->id, true), 'id_category_current' => (int)($category->id), diff --git a/modules/blockcategories/blockcategories.php b/modules/blockcategories/blockcategories.php index 50a7aeead..029444a76 100644 --- a/modules/blockcategories/blockcategories.php +++ b/modules/blockcategories/blockcategories.php @@ -122,8 +122,11 @@ class BlockCategories extends Module '; } - public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0) + public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0) { + if (is_null($id_category)) + $id_category = $this->context->shop->getCategory(); + $children = array(); if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth)) foreach ($resultParents[$id_category] as $subcat) @@ -156,7 +159,7 @@ class BlockCategories extends Module if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite FROM `'._DB_PREFIX_.'category` c - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.' AND cl.`id_shop`='.(int)$id_current_shop.') + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.$id_lang.$this->context->shop->sqlLang('cl').') LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) WHERE (c.`active` = 1 OR c.`id_category` = 1) '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').' @@ -177,13 +180,8 @@ class BlockCategories extends Module } $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH')); - unset($resultParents); - unset($resultIds); - //TODO clean that - $res = $blockCategTree; - $shopcurrentroot = $this->context->shop->getCategory(); - if ($blockCategTree['id'] != $shopcurrentroot) - $blockCategTree = $this->cleanTree($blockCategTree['children']); + unset($resultParents, $resultIds); + $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false); if (Tools::isSubmit('id_category')) { @@ -216,7 +214,6 @@ class BlockCategories extends Module public function hookFooter($params) { - $this->context = $this->context; $id_current_shop = $this->context->shop->getID(); $id_customer = (int)($params['cookie']->id_customer); @@ -225,7 +222,8 @@ class BlockCategories extends Module $id_product = (int)(Tools::getValue('id_product', 0)); $id_category = (int)(Tools::getValue('id_category', 0)); $id_lang = (int)($params['cookie']->id_lang); - $smartyCacheId = 'blockcategories|'.$groups.'_'.$id_lang.'_'.$id_product.'_'.$id_category; + $smartyCacheId = 'blockcategories|'.$id_current_shop.'_'.$groups.'_'.$id_lang.'_'.$id_product.'_'.$id_category; + Tools::enableCache(); if (!$this->isCached('blockcategories_footer.tpl', $smartyCacheId)) { @@ -234,7 +232,7 @@ class BlockCategories extends Module if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite FROM `'._DB_PREFIX_.'category` c - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.$id_lang.' AND cl.`id_shop`='.(int)$id_current_shop.') + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.$id_lang.$this->context->shop->sqlLang('cl').') LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) WHERE (c.`active` = 1 OR c.`id_category` = 1) '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').' @@ -260,13 +258,8 @@ class BlockCategories extends Module $this->context->smarty->assign('widthColumn', $widthColumn); $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH')); - unset($resultParents); - unset($resultIds); - //TODO clean that - $res = $blockCategTree; - if($blockCategTree['id'] != $this->context->shop->getCategory()) - $blockCategTree = $this->cleanTree($blockCategTree['children']); - $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false); + unset($resultParents, $resultIds); + $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false); if (Tools::isSubmit('id_category')) @@ -297,18 +290,6 @@ class BlockCategories extends Module Tools::restoreCacheSettings(); return $display; } - - public function cleanTree($categories) - { - $id_category_root = Context::getContext()->shop->getCategory(); - foreach ($categories AS $row) - { - if ($row['id'] == $id_category_root) - return $row; - elseif (sizeof($row['children'])) - $this->cleanTree($row['children']); - } - } public function hookRightColumn($params) {