Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from outeredge/use-subcategories
Browse files Browse the repository at this point in the history
use subcategories option added to menu items
  • Loading branch information
minlare authored Jan 16, 2018
2 parents bfe667d + 63a7da0 commit 4cf4968
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 21 deletions.
38 changes: 27 additions & 11 deletions Api/Data/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ interface ItemInterface
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
const ITEM_ID = 'item_id';
const MENU_ID = 'menu_id';
const PARENT_ID = 'parent_id';
const TITLE = 'title';
const DESCRIPTION = 'description';
const URL = 'url';
const IMAGE = 'image';
const PRODUCT_ID = 'product_id';
const CATEGORY_ID = 'category_id';
const PAGE_ID = 'page_id';
const SORT_ORDER = 'sort_order';
const ITEM_ID = 'item_id';
const MENU_ID = 'menu_id';
const PARENT_ID = 'parent_id';
const TITLE = 'title';
const DESCRIPTION = 'description';
const URL = 'url';
const IMAGE = 'image';
const PRODUCT_ID = 'product_id';
const CATEGORY_ID = 'category_id';
const USE_SUBCATEGORIES = 'use_subcategories';
const PAGE_ID = 'page_id';
const SORT_ORDER = 'sort_order';
/**#@-*/

/**
Expand Down Expand Up @@ -86,6 +87,13 @@ public function getProductId();
* @return int|null
*/
public function getCategoryId();

/**
* Get use subcategories
*
* @return int|null
*/
public function getUseSubcategories();

/**
* Get page ID
Expand Down Expand Up @@ -172,6 +180,14 @@ public function setProductId($productId);
* @return $this
*/
public function setCategoryId($categoryId);

/**
* Set use subcategories
*
* @param int $useSubcategories
* @return $this
*/
public function setUseSubcategories($useSubcategories);

/**
* Set page ID
Expand Down
132 changes: 124 additions & 8 deletions Helper/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
namespace OuterEdge\Menu\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use OuterEdge\Menu\Model\Menu as MenuModel;
use OuterEdge\Menu\Model\MenuFactory as MenuFactory;
use OuterEdge\Menu\Model\ResourceModel\Item\Collection as ItemCollection;
use OuterEdge\Menu\Model\ResourceModel\Item\CollectionFactory as ItemCollectionFactory;
use OuterEdge\Menu\Model\Menu as MenuModel;
use OuterEdge\Menu\Model\Item;
use OuterEdge\Menu\Model\ItemFactory;
use Magento\Framework\Escaper;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\Data\Collection;

class Menu extends AbstractHelper
{
Expand All @@ -19,28 +26,60 @@ class Menu extends AbstractHelper
* @var ItemCollectionFactory
*/
private $itemCollectionFactory;

/**
* @var ItemFactory
*/
private $itemFactory;

/**
* @var Escaper
*/
private $escaper;


/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var CategoryCollectionFactory
*/
private $categoryCollectionFactory;

/**
* @param MenuFactory $menuFactory
* @param ItemCollectionFactory $itemCollectionFactory
* @param ItemFactory $itemFactory
* @param Escaper $escaper
* @param StoreManagerInterface $storeManager
* @param CategoryCollectionFactory $categoryCollectionFactory
* @codeCoverageIgnore
*/
public function __construct(
MenuFactory $menuFactory,
ItemCollectionFactory $itemCollectionFactory,
Escaper $escaper
ItemFactory $itemFactory,
Escaper $escaper,
StoreManagerInterface $storeManager,
CategoryCollectionFactory $categoryCollectionFactory
) {
$this->menuFactory = $menuFactory;
$this->itemCollectionFactory = $itemCollectionFactory;
$this->itemFactory = $itemFactory;
$this->escaper = $escaper;
$this->storeManager = $storeManager;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}


/**
* Get menu html public wrapper
*
* @param MenuModel|int|string $menu
* @param boolean $includeWrapper
* @param boolean $loadByCode
* @return string
*/
public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)
{
if ($menu instanceof MenuModel) {
Expand All @@ -59,7 +98,14 @@ public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)

return $this->_getMenuHtml($menuModel, $includeWrapper);
}


/**
* Get menu html protected
*
* @param MenuModel $menu
* @param boolean $includeWrapper
* @return string
*/
protected function _getMenuHtml($menu, $includeWrapper = false)
{
$items = $this->itemCollectionFactory->create()
Expand All @@ -74,13 +120,24 @@ protected function _getMenuHtml($menu, $includeWrapper = false)
}
return $menuHtml;
}


/**
* Add sub menu html to menu
*
* @param ItemCollection $items
* @param int $level
* @return string
*/
protected function _addSubMenu($items, $level = 0)
{
$html = '';

foreach ($items as $item) {
$children = $this->_getItemChildren($item);

if ($item->getCategoryId() && $item->getUseSubcategories()) {
$this->_addSubcategoriesToChildren($children, $item->getCategoryId(), $level + 1);
}

$item->setLevel($level);
$item->setChildren($children->count());
Expand Down Expand Up @@ -111,7 +168,13 @@ protected function _addSubMenu($items, $level = 0)

return $html;
}


/**
* Get item classes for menu item
*
* @param Item $item
* @return string
*/
protected function _getItemClasses($item)
{
$classes = ['level' . $item->getLevel()];
Expand All @@ -126,11 +189,64 @@ protected function _getItemClasses($item)
}
return implode(' ', $classes);
}


/**
* Get menu item children collection
*
* @param Item $item
* @return ItemCollection
*/
protected function _getItemChildren($item)
{
return $this->itemCollectionFactory->create()
->addFieldToFilter('parent_id', ['eq' => $item->getId()])
->setOrder('sort_order', 'ASC');
}

/**
* Add default magento categories to children collection
*
* @param ItemCollection $children
* @param int $categoryId
* @param int $level
* @return void
*/
protected function _addSubcategoriesToChildren(&$children, $categoryId, $level)
{
$storeId = $this->storeManager->getStore()->getId();

$subCategories = $this->getSubCategories($storeId, $categoryId);
foreach ($subCategories as $subCategory) {
$item = $this->itemFactory->create();
$item->setData([
'title' => $subCategory->getName(),
'category_id' => $subCategory->getId(),
'use_subcategories' => true
]);
$children->addItem($item);
}
}

/**
* Load magento category collection from parent category
*
* @param int $storeId
* @param int $categoryId
* @return CategoryCollection
*/
protected function getSubCategories($storeId, $categoryId)
{
$collection = $this->categoryCollectionFactory->create();
$collection->setStoreId($storeId);
$collection->addAttributeToSelect('name');
$collection->addFieldToFilter('parent_id', $categoryId);
$collection->addAttributeToFilter('include_in_menu', 1);
$collection->addIsActiveFilter();
$collection->addUrlRewriteToResult();
$collection->addOrder('level', Collection::SORT_ORDER_ASC);
$collection->addOrder('position', Collection::SORT_ORDER_ASC);
$collection->addOrder('parent_id', Collection::SORT_ORDER_ASC);
$collection->addOrder('entity_id', Collection::SORT_ORDER_ASC);
return $collection;
}
}
21 changes: 21 additions & 0 deletions Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ public function getCategoryId()
{
return parent::getData(self::CATEGORY_ID);
}

/**
* Get use subcategories
*
* @return int
*/
public function getUseSubcategories()
{
return parent::getData(self::USE_SUBCATEGORIES);
}

/**
* Get page ID
Expand Down Expand Up @@ -259,6 +269,17 @@ public function setCategoryId($categoryId)
{
return $this->setData(self::CATEGORY_ID, $categoryId);
}

/**
* Set use subcategories
*
* @param int $useSubcategories
* @return ItemInterface
*/
public function setUseSubcategories($useSubcategories)
{
return $this->setData(self::USE_SUBCATEGORIES, $useSubcategories);
}

/**
* Set page ID
Expand Down
3 changes: 3 additions & 0 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class InstallSchema implements InstallSchemaInterface
{
/**
* {@inheritdoc}
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
Expand Down
52 changes: 52 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace OuterEdge\Menu\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.1', '<')) {
$this->addUseSubcategoriesColumnToItemTable($setup);
}

$setup->endSetup();
}

/**
* Add the column 'use_subcategories' to the MenuItem table
* It allows to use subcategory tree on a menu item
*
* @param SchemaSetupInterface $setup
*
* @return void
*/
private function addUseSubcategoriesColumnToItemTable(SchemaSetupInterface $setup)
{
$connection = $setup->getConnection();

$tableItem = $setup->getTable('menu_item');
if ($connection->isTableExists($tableItem) == true) {
$connection->addColumn(
$tableItem,
'use_subcategories',
[
'type' => Table::TYPE_SMALLINT,
'nullable' => false,
'default' => 0,
'comment' => 'Use Subcategory Tree'
]
);
}
}
}
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="OuterEdge_Menu" setup_version="1.0.0"/>
<module name="OuterEdge_Menu" setup_version="1.0.1"/>
</config>
Loading

0 comments on commit 4cf4968

Please sign in to comment.