[*] FO : Changed category block behavior inside categories

This commit is contained in:
Damien Metzger
2013-10-31 12:20:59 +01:00
parent 534421fb6b
commit ff2954b1d2
4 changed files with 66 additions and 61 deletions
+56 -55
View File
@@ -33,7 +33,7 @@ class BlockCategories extends Module
{ {
$this->name = 'blockcategories'; $this->name = 'blockcategories';
$this->tab = 'front_office_features'; $this->tab = 'front_office_features';
$this->version = '2.1'; $this->version = '2.2';
$this->author = 'PrestaShop'; $this->author = 'PrestaShop';
parent::__construct(); parent::__construct();
@@ -107,11 +107,16 @@ class BlockCategories extends Module
if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth)) if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
foreach ($resultParents[$id_category] as $subcat) foreach ($resultParents[$id_category] as $subcat)
$children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1); $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
if (!isset($resultIds[$id_category])) if (!isset($resultIds[$id_category]))
return false; return false;
$return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), $return = array(
'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'id' => $id_category,
'children' => $children); '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; return $return;
} }
@@ -182,85 +187,81 @@ class BlockCategories extends Module
public function hookLeftColumn($params) 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..." if (!isset($this->context->cookie->last_visited_category)
$groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id)); || !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'); $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' if ($category)
SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite {
FROM `'._DB_PREFIX_.'category` c if ($maxdepth > 0)
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').') $maxdepth += $category->level_depth;
INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.') $range = 'AND nleft >= '.$category->nleft.' AND nright <= '.$category->nright;
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;
$resultParents = array();
$resultIds = 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) foreach ($result as &$row)
{ {
$resultParents[$row['id_parent']][] = &$row; $resultParents[$row['id_parent']][] = &$row;
$resultIds[$row['id_category']] = &$row; $resultIds[$row['id_category']] = &$row;
} }
$blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH')); $blockCategTree = $this->getTree($resultParents, $resultIds, $maxdepth, (isset($category) ? $category->id : null));
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);
}
$this->smarty->assign('blockCategTree', $blockCategTree); $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')) 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'); $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl');
else else
$this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl'); $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 $this->display(__FILE__, 'blockcategories.tpl', $cacheId);
return $display;
} }
protected function getCacheId($name = null) protected function getCacheId($name = null)
{ {
parent::getCacheId($name); $name = ($name ? $name.'|' : '').implode('-', Customer::getGroupsStatic($this->context->customer->id));
return 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;
} }
public function hookFooter($params) 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())) if (!$this->isCached('blockcategories_footer.tpl', $this->getCacheId()))
{ {
$maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH'); $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)); $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
+3 -1
View File
@@ -24,8 +24,9 @@
*} *}
<!-- Block categories module --> <!-- Block categories module -->
{if $blockCategTree && $blockCategTree.children|@count}
<div id="categories_block_left" class="block"> <div id="categories_block_left" class="block">
<h4 class="title_block">{l s='Categories' mod='blockcategories'}</h4> <h4 class="title_block">{if isset($currentCategory)}{$currentCategory->name|escape}{else}{l s='Categories' mod='blockcategories'}{/if}</h4>
<div class="block_content"> <div class="block_content">
<ul class="tree {if $isDhtml}dhtml{/if}"> <ul class="tree {if $isDhtml}dhtml{/if}">
{foreach from=$blockCategTree.children item=child name=blockCategTree} {foreach from=$blockCategTree.children item=child name=blockCategTree}
@@ -45,4 +46,5 @@
</script> </script>
</div> </div>
</div> </div>
{/if}
<!-- /Block categories module --> <!-- /Block categories module -->
+3 -3
View File
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<module> <module>
<name>blockcategories</name> <name>blockcategories</name>
<displayName><![CDATA[Bloc cat&eacute;gories]]></displayName> <displayName><![CDATA[Categories block]]></displayName>
<version><![CDATA[2.1]]></version> <version><![CDATA[2.2]]></version>
<description><![CDATA[Ajoute un bloc proposant une navigation au sein de vos cat&eacute;gories de produits]]></description> <description><![CDATA[Adds a block featuring product categories.]]></description>
<author><![CDATA[PrestaShop]]></author> <author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab> <tab><![CDATA[front_office_features]]></tab>
<is_configurable>1</is_configurable> <is_configurable>1</is_configurable>
@@ -24,8 +24,9 @@
*} *}
<!-- Block categories module --> <!-- Block categories module -->
{if $blockCategTree && $blockCategTree.children|@count}
<div id="categories_block_left" class="block"> <div id="categories_block_left" class="block">
<p class="title_block">{l s='Categories' mod='blockcategories'}</p> <h4 class="title_block">{if isset($currentCategory)}{$currentCategory->name|escape}{else}{l s='Categories' mod='blockcategories'}{/if}</h4>
<div class="block_content"> <div class="block_content">
<ul class="tree {if $isDhtml}dhtml{/if}"> <ul class="tree {if $isDhtml}dhtml{/if}">
{foreach from=$blockCategTree.children item=child name=blockCategTree} {foreach from=$blockCategTree.children item=child name=blockCategTree}
@@ -45,4 +46,5 @@
</script> </script>
</div> </div>
</div> </div>
{/if}
<!-- /Block categories module --> <!-- /Block categories module -->