-
Notifications
You must be signed in to change notification settings - Fork 6
/
treeNode.php
253 lines (220 loc) · 8.23 KB
/
treeNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
ini_set('display_startup_errors', 1);ini_set('display_errors', 1); error_reporting(-1);
$file = dirname(__FILE__) . '/safira/app/bootstrap.php';
$file = str_replace('/public_html/', '/public_htmls/', $file);
require $file;
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
use Magento\Backend\Model\Menu;
use Magento\Framework\Data\Tree\Node;
use Magento\Framework\Data\Tree\Node\Collection;
use Magento\Framework\Data\Tree\NodeFactory;
use Magento\Framework\Data\TreeFactory;
class CategoryTree extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
/**
* Top menu data tree
*
* @var Node
*/
protected $_menu;
/**
* @var NodeFactory
*/
private $nodeFactory;
/**
* @var TreeFactory
*/
private $treeFactory;
/**
* Catalog category
*
* @var \Magento\Catalog\Helper\Category
*/
protected $catalogCategory;
/**
* @var \Magento\Catalog\Model\Layer\Resolver
*/
private $layerResolver;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
public function launch()
{
$this->_state->setAreaCode('frontend');
$this->nodeFactory = $this->_objectManager->get('\Magento\Framework\Data\Tree\NodeFactory');
$this->treeFactory = $this->_objectManager->get('\Magento\Framework\Data\TreeFactory');
$this->storeManager = $this->_objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$this->catalogCategory = $this->_objectManager->create('\Magento\Catalog\Helper\Category');
$this->layerResolver = $this->_objectManager->create('\Magento\Catalog\Model\Layer\Resolver');
$rootId = $this->storeManager->getStore()->getRootCategoryId();
$storeId = $this->storeManager->getStore()->getId();
echo "Root Id = $rootId and Store Id = $storeId";
$menu = $this->getTreeMenu($storeId, $rootId);
$rootLevel = 0;
$this->removeChildrenWithoutActiveParent($menu, $rootLevel);
$html = $this->getTreeCategories($menu, 'root-class');
echo $html;
//the method must end with this line
return $this->_response;
}
public function getTreeMenu($storeId, $rootId)
{
$collection = $this->getCategoryTree($storeId, $rootId);
$currentCategory = $this->getCurrentCategory();
$mapping = [$rootId => $this->getMenu()]; // use nodes stack to avoid recursion
foreach ($collection as $category) {
$categoryParentId = $category->getParentId();
if (!isset($mapping[$categoryParentId])) {
$parentIds = $category->getParentIds();
foreach ($parentIds as $parentId) {
if (isset($mapping[$parentId])) {
$categoryParentId = $parentId;
}
}
}
/** @var Node $parentCategoryNode */
$parentCategoryNode = $mapping[$categoryParentId];
$categoryNode = new Node(
$this->getCategoryAsArray(
$category,
$currentCategory,
$category->getParentId() == $categoryParentId
),
'id',
$parentCategoryNode->getTree(),
$parentCategoryNode
);
$parentCategoryNode->addChild($categoryNode);
$mapping[$category->getId()] = $categoryNode; //add node in stack
}
$menu = isset($mapping[$rootId]) ? $mapping[$rootId]->getChildren() : [];
return $menu;
}
public function getTreeCategories($categories, $itemPositionClassPrefix) // include Magic_Label and Maximal Depth
{
$html = '';
$counter = 1;
foreach($categories as $category) {
$level = $category->getLevel();
$catChild = $category->getChildren();
$childLevel = $this->getChildLevel($level);
$this->removeChildrenWithoutActiveParent($catChild, $childLevel);
$childHtml = $this->getTreeCategories($catChild, $itemPositionClassPrefix);
$childClass = $childHtml ? ' hasChild parent ' : ' ';
$childClass .= $itemPositionClassPrefix . '-' .$counter;
$childClass .= ' category-item ';
$html .= '<li class="level' . ($level -2) . $childClass . '"><a href="' . $category->getUrl() . '"><span>' . $category->getName() . "</span></a>\n";
$html .= $childHtml;
$html .= '</li>';
$counter++;
}
if($html) $html = '<ul class="level' .($level -3). ' submenu">' . $html . '</ul>';
return $html;
}
/**
* Get menu object.
*
* Creates Tree root node object.
* The creation logic was moved from class constructor into separate method.
*
* @return Node
* @since 100.1.0
*/
public function getMenu()
{
if (!$this->_menu) {
$this->_menu = $this->nodeFactory->create(
[
'data' => [],
'idField' => 'root',
'tree' => $this->treeFactory->create()
]
);
}
return $this->_menu;
}
protected function getCategoryTree($storeId, $rootId)
{
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
$collection = $this->_objectManager->create('\Magento\Catalog\Model\ResourceModel\Category\Collection');
$collection->setStoreId($storeId);
$collection->addAttributeToSelect('name');
$collection->addFieldToFilter('path', ['like' => '1/' . $rootId . '/%']); //load only from store root
$collection->addAttributeToFilter('include_in_menu', 1);
$collection->addIsActiveFilter();
$collection->addNavigationMaxDepthFilter();
$collection->addUrlRewriteToResult();
$collection->addOrder('level', 'ASC');
$collection->addOrder('position', 'ASC');
$collection->addOrder('parent_id', 'ASC');
$collection->addOrder('entity_id', 'ASC');
return $collection;
}
/**
* Convert category to array
*
* @param \Magento\Catalog\Model\Category $category
* @param \Magento\Catalog\Model\Category $currentCategory
* @param bool $isParentActive
* @return array
*/
private function getCategoryAsArray($category, $currentCategory, $isParentActive)
{
$categoryId = $category->getId();
return [
'name' => $category->getName(),
'id' => 'category-node-' . $categoryId,
'url' => $this->catalogCategory->getCategoryUrl($category),
'has_active' => in_array((string)$categoryId, explode('/', (string)$currentCategory->getPath()), true),
'is_active' => $categoryId == $currentCategory->getId(),
'is_category' => true,
'is_parent_active' => $isParentActive,
'entity_id' => $categoryId,
'level' => $category->getData('level')
];
}
/**
* Get current Category from catalog layer
*
* @return \Magento\Catalog\Model\Category
*/
private function getCurrentCategory()
{
$catalogLayer = $this->layerResolver->get();
if (!$catalogLayer) {
return null;
}
return $catalogLayer->getCurrentCategory();
}
/**
* Remove children from collection when the parent is not active
*
* @param Collection $children
* @param int $childLevel
* @return void
*/
private function removeChildrenWithoutActiveParent(Collection $children, int $childLevel): void
{
/** @var Node $child */
foreach ($children as $child) {
if ($childLevel === 0 && $child->getData('is_parent_active') === false) {
$children->delete($child);
}
}
}
/**
* Retrieve child level based on parent level
*
* @param int $parentLevel
*
* @return int
*/
private function getChildLevel($parentLevel): int
{
return $parentLevel === null ? 0 : $parentLevel + 1;
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CategoryTree');
$bootstrap->run($app);