Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

[+] MO: Add flat category list option #233

Merged
merged 6 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 13 additions & 11 deletions modules/cttopmenu/classes/CTTopMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ class CTTopMenuItem extends ObjectModel
/**
* Menu item types
*/
const TYPE_CUSTOM_LINK = 0;
const TYPE_PRODUCT = 1;
const TYPE_CATEGORY = 2;
const TYPE_CATEGORY_TREE = 3;
const TYPE_CMS = 4;
const TYPE_CMS_CATEGORY = 5;
const TYPE_CMS_CATEGORY_TREE = 6;
const TYPE_MANUFACTURER = 7;
const TYPE_MANUFACTURER_LIST = 8;
const TYPE_SUPPLIER = 9;
const TYPE_SUPPLIER_LIST = 10;
const TYPE_CUSTOM_LINK = 0;
const TYPE_PRODUCT = 1;
const TYPE_CATEGORY = 2;
const TYPE_CATEGORY_TREE = 3;
const TYPE_CATEGORY_FLAT_TREE = 11;
const TYPE_CMS = 4;
const TYPE_CMS_CATEGORY = 5;
const TYPE_CMS_CATEGORY_TREE = 6;
const TYPE_MANUFACTURER = 7;
const TYPE_MANUFACTURER_LIST = 8;
const TYPE_SUPPLIER = 9;
const TYPE_SUPPLIER_LIST = 10;

/** @var int */
public $type = 0;
Expand Down Expand Up @@ -72,6 +73,7 @@ class CTTopMenuItem extends ObjectModel
self::TYPE_PRODUCT,
self::TYPE_CATEGORY,
self::TYPE_CATEGORY_TREE,
self::TYPE_CATEGORY_FLAT_TREE,
self::TYPE_CMS,
self::TYPE_CMS_CATEGORY,
self::TYPE_CMS_CATEGORY_TREE,
Expand Down
4 changes: 2 additions & 2 deletions modules/cttopmenu/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<module>
<name>cttopmenu</name>
<displayName><![CDATA[Community Theme Top Menu]]></displayName>
<version><![CDATA[1.0.0]]></version>
<version><![CDATA[1.1.0]]></version>
<description><![CDATA[Adds a new horizontal menu to the top of your e-commerce website.]]></description>
<author><![CDATA[PrestaShop Community]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function __construct()
'tree_max_depth' => true,
),
),
CTTopMenuItem::TYPE_CATEGORY_FLAT_TREE => array(
'name' => $this->l('Category Flat Tree'),
'entity_name' => $this->l('Category'),
'fields' => array(
'entity_id' => true,
'tree_max_depth' => true,
),
),
CTTopMenuItem::TYPE_CMS => array(
'name' => $this->l('CMS Link'),
'entity_name' => $this->l('CMS'),
Expand Down Expand Up @@ -100,14 +108,9 @@ public function __construct()
);

// Write ID column so we can use this array as option list in fields form
$this->menuItemTypes = array_map(
function ($value, $key) {
$value['id'] = $key;
return $value;
},
$this->menuItemTypes,
array_keys($this->menuItemTypes)
);
foreach ($this->menuItemTypes as $menuItemTypeKey => &$menuItemType) {
$menuItemType['id'] = $menuItemTypeKey;
}
}

/**
Expand Down
52 changes: 52 additions & 0 deletions modules/cttopmenu/cttopmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ public function buildMenuItem(array $menuItem, $id_shop, $id_lang)
return $this->buildCategoryLink($menuItem, $id_shop, $id_lang);
case CTTopMenuItem::TYPE_CATEGORY_TREE:
return $this->buildCategoryTree($menuItem, $id_shop, $id_lang);
case CTTopMenuItem::TYPE_CATEGORY_FLAT_TREE:
return $this->buildCategoryFlatTree($menuItem, $id_shop, $id_lang);
case CTTopMenuItem::TYPE_CMS:
return $this->buildCmsLink($menuItem, $id_shop, $id_lang);
case CTTopMenuItem::TYPE_CMS_CATEGORY:
Expand Down Expand Up @@ -590,6 +592,35 @@ protected function buildCategoryTreeItem(array $category, $id_shop, $id_lang, $d
);
}

/**
* Builds a menu tree where subcategories of a given category
* are a flattened
*
* @param array $menuItem
* @param int $id_shop
* @param int $id_lang
*
* @return array
*/
protected function buildCategoryFlatTree(array $menuItem, $id_shop, $id_lang)
{
$tree = $this->buildCategoryTree($menuItem, $id_shop, $id_lang);
$tree['type'] = 'category-flat-tree';

$treeSubItems = array();
foreach ($tree['sub_items'] as $subItemKey => $subItemTree) {
$treeSubItems = array_merge($treeSubItems, $this->flattenMenuItemTree($subItemTree));
}

usort($treeSubItems, function ($a, $b) {
return ($a['name'] < $b['name']) ? -1 : 1;
});

$tree['sub_items'] = $treeSubItems;

return $tree;
}

/**
* Builds menu item
*
Expand Down Expand Up @@ -1015,4 +1046,25 @@ protected function buildSupplierList(array $menuItem, $id_lang)
'sub_items' => $subItems,
);
}

/**
* Flattens the menu tree to a flat list (array)
*
* @param array $menuItem
*
* @return array
*/
protected function flattenMenuItemTree(array $menuItem)
{
$menuItemList = array();

foreach ($menuItem['sub_items'] as $menuSubItem) {
$menuItemList = array_merge($menuItemList, $this->flattenMenuItemTree($menuSubItem));
}

$menuItem['sub_items'] = array();
$menuItemList[] = $menuItem;

return $menuItemList;
}
}
2 changes: 1 addition & 1 deletion modules/cttopmenu/views/templates/hook/tree_item.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<li>
<a{if !empty($item.id)} id="cttm-item-{$item.id|escape:'html':'UTF-8'}"{/if} href="{$item.url|escape:'html':'UTF-8'}" class="cttm-item-{$item.type|escape:'html':'UTF-8'}{if !empty($item.entity_id)} cttm-entity-{$item.entity_id|escape:'html':'UTF-8'}{/if}" title="{$item.title|escape:'html':'UTF-8'}"{if $item.no_follow} rel="nofollow"{/if}>
<a{if !empty($item.id)} id="cttm-item-{$item.id|escape:'html':'UTF-8'}"{/if} href="{if !empty($item.url)}{$item.url|escape:'html':'UTF-8'}{else}#{/if}" class="cttm-item-{$item.type|escape:'html':'UTF-8'}{if !empty($item.entity_id)} cttm-entity-{$item.entity_id|escape:'html':'UTF-8'}{/if}" title="{$item.title|escape:'html':'UTF-8'}"{if $item.no_follow} rel="nofollow"{/if}>
{if !empty($item.icon)}<i class="icon icon-{$item.icon|escape:'html':'UTF-8'}"></i>{/if}
{$item.name|escape:'html':'UTF-8'}
</a>
Expand Down