diff --git a/modules/blockcategories/blockcategories.php b/modules/blockcategories/blockcategories.php index 865c856c9..54f8dcac7 100644 --- a/modules/blockcategories/blockcategories.php +++ b/modules/blockcategories/blockcategories.php @@ -33,7 +33,7 @@ class BlockCategories extends Module { $this->name = 'blockcategories'; $this->tab = 'front_office_features'; - $this->version = '2.1'; + $this->version = '2.2'; $this->author = 'PrestaShop'; parent::__construct(); @@ -107,11 +107,16 @@ class BlockCategories extends Module if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth)) foreach ($resultParents[$id_category] as $subcat) $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1); + if (!isset($resultIds[$id_category])) return false; - $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), - 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], - 'children' => $children); + $return = array( + 'id' => $id_category, + 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), + 'name' => $resultIds[$id_category]['name'], + 'desc'=> $resultIds[$id_category]['description'], + 'children' => $children + ); return $return; } @@ -181,86 +186,82 @@ class BlockCategories extends Module } public function hookLeftColumn($params) - { - if (!$this->isCached('blockcategories.tpl', $this->getCacheId())) + { + $category = false; + if (method_exists($this->context->controller, 'getCategory') && ($category = $this->context->controller->getCategory())) + $this->context->cookie->last_visited_category = $category->id; + elseif (method_exists($this->context->controller, 'getProduct') && ($product = $this->context->controller->getProduct())) { - // Get all groups for this customer and concatenate them as a string: "1,2,3..." - $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id)); + if (!isset($this->context->cookie->last_visited_category) + || !Product::idIsOnCategoryId($product->id, array(array('id_category' => $this->context->cookie->last_visited_category))) + || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)) + $this->context->cookie->last_visited_category = (int)$product->id_category_default; + + $category = new Category($this->context->cookie->last_visited_category, $this->context->language->id); + } + + $cacheId = $this->getCacheId($category ? $category->id : null); + if (!$this->isCached('blockcategories.tpl', $cacheId)) + { + $range = ''; $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH'); - if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite - FROM `'._DB_PREFIX_.'category` c - INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').') - INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.') - WHERE (c.`active` = 1 OR c.`id_category` = '.(int)Configuration::get('PS_HOME_CATEGORY').') - AND c.`id_category` != '.(int)Configuration::get('PS_ROOT_CATEGORY').' - '.((int)$maxdepth != 0 ? ' AND `level_depth` <= '.(int)$maxdepth : '').' - AND c.id_category IN (SELECT id_category FROM `'._DB_PREFIX_.'category_group` WHERE `id_group` IN ('.pSQL($groups).')) - ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC'))) - return; + if ($category) + { + if ($maxdepth > 0) + $maxdepth += $category->level_depth; + $range = 'AND nleft >= '.$category->nleft.' AND nright <= '.$category->nright; + } - $resultParents = array(); $resultIds = array(); - + $resultParents = array(); + $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 + INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').') + INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.') + WHERE (c.`active` = 1 OR c.`id_category` = '.(int)Configuration::get('PS_HOME_CATEGORY').') + AND c.`id_category` != '.(int)Configuration::get('PS_ROOT_CATEGORY').' + '.((int)$maxdepth != 0 ? ' AND `level_depth` <= '.(int)$maxdepth : '').' + '.$range.' + AND c.id_category IN ( + SELECT id_category + FROM `'._DB_PREFIX_.'category_group` + WHERE `id_group` IN ('.pSQL(implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id))).') + ) + ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')); foreach ($result as &$row) { $resultParents[$row['id_parent']][] = &$row; $resultIds[$row['id_category']] = &$row; } - $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH')); - unset($resultParents, $resultIds); - - $id_category = (int)Tools::getValue('id_category'); - $id_product = (int)Tools::getValue('id_product'); - - $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false); - if (Tools::isSubmit('id_category')) - { - $this->context->cookie->last_visited_category = $id_category; - $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category); - } - if (Tools::isSubmit('id_product')) - { - if (!isset($this->context->cookie->last_visited_category) - || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category))) - || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)) - { - $product = new Product($id_product); - if (isset($product) && Validate::isLoadedObject($product)) - $this->context->cookie->last_visited_category = (int)$product->id_category_default; - } - $this->smarty->assign('currentCategoryId', (int)$this->context->cookie->last_visited_category); - } + $blockCategTree = $this->getTree($resultParents, $resultIds, $maxdepth, (isset($category) ? $category->id : null)); $this->smarty->assign('blockCategTree', $blockCategTree); + if ($category) + $this->smarty->assign(array('currentCategory' => $category, 'currentCategoryId' => $category->id)); + + $this->smarty->assign('isDhtml', Configuration::get('BLOCK_CATEG_DHTML')); if (file_exists(_PS_THEME_DIR_.'modules/blockcategories/blockcategories.tpl')) $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl'); else $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl'); - $this->smarty->assign('isDhtml', $isDhtml); } - $display = $this->display(__FILE__, 'blockcategories.tpl', $this->getCacheId()); - return $display; + return $this->display(__FILE__, 'blockcategories.tpl', $cacheId); } protected function getCacheId($name = null) { - parent::getCacheId($name); - - $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id)); - $id_product = (int)Tools::getValue('id_product', 0); - $id_category = (int)Tools::getValue('id_category', 0); - $id_lang = (int)$this->context->language->id; - return 'blockcategories|'.(int)Tools::usingSecureMode().'|'.$this->context->shop->id.'|'.$groups.'|'.$id_lang.'|'.$id_product.'|'.$id_category; + $name = ($name ? $name.'|' : '').implode('-', Customer::getGroupsStatic($this->context->customer->id)); + return parent::getCacheId($name); } public function hookFooter($params) { - // Get all groups for this customer and concatenate them as a string: "1,2,3..." if (!$this->isCached('blockcategories_footer.tpl', $this->getCacheId())) { $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH'); + // Get all groups for this customer and concatenate them as a string: "1,2,3..." $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id)); if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite diff --git a/modules/blockcategories/blockcategories.tpl b/modules/blockcategories/blockcategories.tpl index f9f62be46..86acc7807 100644 --- a/modules/blockcategories/blockcategories.tpl +++ b/modules/blockcategories/blockcategories.tpl @@ -24,8 +24,9 @@ *} +{if $blockCategTree && $blockCategTree.children|@count}
{l s='Categories' mod='blockcategories'}
+