33namespace OuterEdge \Menu \Helper ;
44
55use Magento \Framework \App \Helper \AbstractHelper ;
6+ use OuterEdge \Menu \Model \Menu as MenuModel ;
67use OuterEdge \Menu \Model \MenuFactory as MenuFactory ;
8+ use OuterEdge \Menu \Model \ResourceModel \Item \Collection as ItemCollection ;
79use OuterEdge \Menu \Model \ResourceModel \Item \CollectionFactory as ItemCollectionFactory ;
8- use OuterEdge \Menu \Model \Menu as MenuModel ;
10+ use OuterEdge \Menu \Model \Item ;
11+ use OuterEdge \Menu \Model \ItemFactory ;
912use Magento \Framework \Escaper ;
13+ use Magento \Store \Model \StoreManagerInterface ;
14+ use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
15+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
16+ use Magento \Framework \Data \Collection ;
1017
1118class Menu extends AbstractHelper
1219{
@@ -19,28 +26,60 @@ class Menu extends AbstractHelper
1926 * @var ItemCollectionFactory
2027 */
2128 private $ itemCollectionFactory ;
29+
30+ /**
31+ * @var ItemFactory
32+ */
33+ private $ itemFactory ;
2234
2335 /**
2436 * @var Escaper
2537 */
2638 private $ escaper ;
27-
39+
40+ /**
41+ * @var StoreManagerInterface
42+ */
43+ private $ storeManager ;
44+
45+ /**
46+ * @var CategoryCollectionFactory
47+ */
48+ private $ categoryCollectionFactory ;
49+
2850 /**
2951 * @param MenuFactory $menuFactory
3052 * @param ItemCollectionFactory $itemCollectionFactory
53+ * @param ItemFactory $itemFactory
3154 * @param Escaper $escaper
55+ * @param StoreManagerInterface $storeManager
56+ * @param CategoryCollectionFactory $categoryCollectionFactory
3257 * @codeCoverageIgnore
3358 */
3459 public function __construct (
3560 MenuFactory $ menuFactory ,
3661 ItemCollectionFactory $ itemCollectionFactory ,
37- Escaper $ escaper
62+ ItemFactory $ itemFactory ,
63+ Escaper $ escaper ,
64+ StoreManagerInterface $ storeManager ,
65+ CategoryCollectionFactory $ categoryCollectionFactory
3866 ) {
3967 $ this ->menuFactory = $ menuFactory ;
4068 $ this ->itemCollectionFactory = $ itemCollectionFactory ;
69+ $ this ->itemFactory = $ itemFactory ;
4170 $ this ->escaper = $ escaper ;
71+ $ this ->storeManager = $ storeManager ;
72+ $ this ->categoryCollectionFactory = $ categoryCollectionFactory ;
4273 }
43-
74+
75+ /**
76+ * Get menu html public wrapper
77+ *
78+ * @param MenuModel|int|string $menu
79+ * @param boolean $includeWrapper
80+ * @param boolean $loadByCode
81+ * @return string
82+ */
4483 public function getMenuHtml ($ menu , $ includeWrapper = false , $ loadByCode = false )
4584 {
4685 if ($ menu instanceof MenuModel) {
@@ -59,7 +98,14 @@ public function getMenuHtml($menu, $includeWrapper = false, $loadByCode = false)
5998
6099 return $ this ->_getMenuHtml ($ menuModel , $ includeWrapper );
61100 }
62-
101+
102+ /**
103+ * Get menu html protected
104+ *
105+ * @param MenuModel $menu
106+ * @param boolean $includeWrapper
107+ * @return string
108+ */
63109 protected function _getMenuHtml ($ menu , $ includeWrapper = false )
64110 {
65111 $ items = $ this ->itemCollectionFactory ->create ()
@@ -74,13 +120,24 @@ protected function _getMenuHtml($menu, $includeWrapper = false)
74120 }
75121 return $ menuHtml ;
76122 }
77-
123+
124+ /**
125+ * Add sub menu html to menu
126+ *
127+ * @param ItemCollection $items
128+ * @param int $level
129+ * @return string
130+ */
78131 protected function _addSubMenu ($ items , $ level = 0 )
79132 {
80133 $ html = '' ;
81134
82135 foreach ($ items as $ item ) {
83136 $ children = $ this ->_getItemChildren ($ item );
137+
138+ if ($ item ->getCategoryId () && $ item ->getUseSubcategories ()) {
139+ $ this ->_addSubcategoriesToChildren ($ children , $ item ->getCategoryId (), $ level + 1 );
140+ }
84141
85142 $ item ->setLevel ($ level );
86143 $ item ->setChildren ($ children ->count ());
@@ -111,7 +168,13 @@ protected function _addSubMenu($items, $level = 0)
111168
112169 return $ html ;
113170 }
114-
171+
172+ /**
173+ * Get item classes for menu item
174+ *
175+ * @param Item $item
176+ * @return string
177+ */
115178 protected function _getItemClasses ($ item )
116179 {
117180 $ classes = ['level ' . $ item ->getLevel ()];
@@ -126,11 +189,64 @@ protected function _getItemClasses($item)
126189 }
127190 return implode (' ' , $ classes );
128191 }
129-
192+
193+ /**
194+ * Get menu item children collection
195+ *
196+ * @param Item $item
197+ * @return ItemCollection
198+ */
130199 protected function _getItemChildren ($ item )
131200 {
132201 return $ this ->itemCollectionFactory ->create ()
133202 ->addFieldToFilter ('parent_id ' , ['eq ' => $ item ->getId ()])
134203 ->setOrder ('sort_order ' , 'ASC ' );
135204 }
205+
206+ /**
207+ * Add default magento categories to children collection
208+ *
209+ * @param ItemCollection $children
210+ * @param int $categoryId
211+ * @param int $level
212+ * @return void
213+ */
214+ protected function _addSubcategoriesToChildren (&$ children , $ categoryId , $ level )
215+ {
216+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
217+
218+ $ subCategories = $ this ->getSubCategories ($ storeId , $ categoryId );
219+ foreach ($ subCategories as $ subCategory ) {
220+ $ item = $ this ->itemFactory ->create ();
221+ $ item ->setData ([
222+ 'title ' => $ subCategory ->getName (),
223+ 'category_id ' => $ subCategory ->getId (),
224+ 'use_subcategories ' => true
225+ ]);
226+ $ children ->addItem ($ item );
227+ }
228+ }
229+
230+ /**
231+ * Load magento category collection from parent category
232+ *
233+ * @param int $storeId
234+ * @param int $categoryId
235+ * @return CategoryCollection
236+ */
237+ protected function getSubCategories ($ storeId , $ categoryId )
238+ {
239+ $ collection = $ this ->categoryCollectionFactory ->create ();
240+ $ collection ->setStoreId ($ storeId );
241+ $ collection ->addAttributeToSelect ('name ' );
242+ $ collection ->addFieldToFilter ('parent_id ' , $ categoryId );
243+ $ collection ->addAttributeToFilter ('include_in_menu ' , 1 );
244+ $ collection ->addIsActiveFilter ();
245+ $ collection ->addUrlRewriteToResult ();
246+ $ collection ->addOrder ('level ' , Collection::SORT_ORDER_ASC );
247+ $ collection ->addOrder ('position ' , Collection::SORT_ORDER_ASC );
248+ $ collection ->addOrder ('parent_id ' , Collection::SORT_ORDER_ASC );
249+ $ collection ->addOrder ('entity_id ' , Collection::SORT_ORDER_ASC );
250+ return $ collection ;
251+ }
136252}
0 commit comments