7
7
8
8
use Magento \Framework \Data \Tree \Node ;
9
9
use Magento \Store \Model \Store ;
10
+ use Magento \Framework \Registry ;
11
+ use Magento \Catalog \Model \ResourceModel \Category \Tree ;
12
+ use Magento \Catalog \Model \CategoryFactory ;
13
+ use Magento \Backend \Block \Template \Context ;
14
+ use Magento \Catalog \Model \Category ;
10
15
11
16
/**
12
17
* Class AbstractCategory
@@ -16,17 +21,17 @@ class AbstractCategory extends \Magento\Backend\Block\Template
16
21
/**
17
22
* Core registry
18
23
*
19
- * @var \Magento\Framework\ Registry
24
+ * @var Registry
20
25
*/
21
26
protected $ _coreRegistry = null ;
22
27
23
28
/**
24
- * @var \Magento\Catalog\Model\ResourceModel\Category\ Tree
29
+ * @var Tree
25
30
*/
26
31
protected $ _categoryTree ;
27
32
28
33
/**
29
- * @var \Magento\Catalog\Model\ CategoryFactory
34
+ * @var CategoryFactory
30
35
*/
31
36
protected $ _categoryFactory ;
32
37
@@ -36,17 +41,17 @@ class AbstractCategory extends \Magento\Backend\Block\Template
36
41
protected $ _withProductCount ;
37
42
38
43
/**
39
- * @param \Magento\Backend\Block\Template\ Context $context
40
- * @param \Magento\Catalog\Model\ResourceModel\Category\ Tree $categoryTree
41
- * @param \Magento\Framework\ Registry $registry
42
- * @param \Magento\Catalog\Model\ CategoryFactory $categoryFactory
44
+ * @param Context $context
45
+ * @param Tree $categoryTree
46
+ * @param Registry $registry
47
+ * @param CategoryFactory $categoryFactory
43
48
* @param array $data
44
49
*/
45
50
public function __construct (
46
- \ Magento \ Backend \ Block \ Template \ Context $ context ,
47
- \ Magento \ Catalog \ Model \ ResourceModel \ Category \ Tree $ categoryTree ,
48
- \ Magento \ Framework \ Registry $ registry ,
49
- \ Magento \ Catalog \ Model \ CategoryFactory $ categoryFactory ,
51
+ Context $ context ,
52
+ Tree $ categoryTree ,
53
+ Registry $ registry ,
54
+ CategoryFactory $ categoryFactory ,
50
55
array $ data = []
51
56
) {
52
57
$ this ->_categoryTree = $ categoryTree ;
@@ -67,36 +72,47 @@ public function getCategory()
67
72
}
68
73
69
74
/**
75
+ * Get category id
76
+ *
70
77
* @return int|string|null
71
78
*/
72
79
public function getCategoryId ()
73
80
{
74
81
if ($ this ->getCategory ()) {
75
- return $ this ->getCategory ()->getId ();
82
+ return $ this ->getCategory ()
83
+ ->getId ();
76
84
}
77
- return \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ;
85
+ return Category::TREE_ROOT_ID ;
78
86
}
79
87
80
88
/**
89
+ * Get category name
90
+ *
81
91
* @return string
82
92
*/
83
93
public function getCategoryName ()
84
94
{
85
- return $ this ->getCategory ()->getName ();
95
+ return $ this ->getCategory ()
96
+ ->getName ();
86
97
}
87
98
88
99
/**
100
+ * Get category path
101
+ *
89
102
* @return mixed
90
103
*/
91
104
public function getCategoryPath ()
92
105
{
93
106
if ($ this ->getCategory ()) {
94
- return $ this ->getCategory ()->getPath ();
107
+ return $ this ->getCategory ()
108
+ ->getPath ();
95
109
}
96
- return \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ;
110
+ return Category::TREE_ROOT_ID ;
97
111
}
98
112
99
113
/**
114
+ * Check store root category
115
+ *
100
116
* @return bool
101
117
*/
102
118
public function hasStoreRootCategory ()
@@ -109,15 +125,20 @@ public function hasStoreRootCategory()
109
125
}
110
126
111
127
/**
128
+ * Get store from request
129
+ *
112
130
* @return Store
113
131
*/
114
132
public function getStore ()
115
133
{
116
- $ storeId = (int )$ this ->getRequest ()->getParam ('store ' );
134
+ $ storeId = (int )$ this ->getRequest ()
135
+ ->getParam ('store ' );
117
136
return $ this ->_storeManager ->getStore ($ storeId );
118
137
}
119
138
120
139
/**
140
+ * Get root category for tree
141
+ *
121
142
* @param mixed|null $parentNodeCategory
122
143
* @param int $recursionLevel
123
144
* @return Node|array|null
@@ -130,13 +151,14 @@ public function getRoot($parentNodeCategory = null, $recursionLevel = 3)
130
151
}
131
152
$ root = $ this ->_coreRegistry ->registry ('root ' );
132
153
if ($ root === null ) {
133
- $ storeId = (int )$ this ->getRequest ()->getParam ('store ' );
154
+ $ storeId = (int )$ this ->getRequest ()
155
+ ->getParam ('store ' );
134
156
135
157
if ($ storeId ) {
136
158
$ store = $ this ->_storeManager ->getStore ($ storeId );
137
159
$ rootId = $ store ->getRootCategoryId ();
138
160
} else {
139
- $ rootId = \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ;
161
+ $ rootId = Category::TREE_ROOT_ID ;
140
162
}
141
163
142
164
$ tree = $ this ->_categoryTree ->load (null , $ recursionLevel );
@@ -149,10 +171,11 @@ public function getRoot($parentNodeCategory = null, $recursionLevel = 3)
149
171
150
172
$ root = $ tree ->getNodeById ($ rootId );
151
173
152
- if ($ root && $ rootId != \ Magento \ Catalog \ Model \Category:: TREE_ROOT_ID ) {
174
+ if ($ root ) {
153
175
$ root ->setIsVisible (true );
154
- } elseif ($ root && $ root ->getId () == \Magento \Catalog \Model \Category::TREE_ROOT_ID ) {
155
- $ root ->setName (__ ('Root ' ));
176
+ if ($ root ->getId () == Category::TREE_ROOT_ID ) {
177
+ $ root ->setName (__ ('Root ' ));
178
+ }
156
179
}
157
180
158
181
$ this ->_coreRegistry ->register ('root ' , $ root );
@@ -162,22 +185,28 @@ public function getRoot($parentNodeCategory = null, $recursionLevel = 3)
162
185
}
163
186
164
187
/**
188
+ * Get Default Store Id
189
+ *
165
190
* @return int
166
191
*/
167
192
protected function _getDefaultStoreId ()
168
193
{
169
- return \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID ;
194
+ return Store::DEFAULT_STORE_ID ;
170
195
}
171
196
172
197
/**
198
+ * Get category collection
199
+ *
173
200
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
174
201
*/
175
202
public function getCategoryCollection ()
176
203
{
177
- $ storeId = $ this ->getRequest ()->getParam ('store ' , $ this ->_getDefaultStoreId ());
204
+ $ storeId = $ this ->getRequest ()
205
+ ->getParam ('store ' , $ this ->_getDefaultStoreId ());
178
206
$ collection = $ this ->getData ('category_collection ' );
179
207
if ($ collection === null ) {
180
- $ collection = $ this ->_categoryFactory ->create ()->getCollection ();
208
+ $ collection = $ this ->_categoryFactory ->create ()
209
+ ->getCollection ();
181
210
182
211
$ collection ->addAttributeToSelect (
183
212
'name '
@@ -212,11 +241,11 @@ public function getRootByIds($ids)
212
241
if (null === $ root ) {
213
242
$ ids = $ this ->_categoryTree ->getExistingCategoryIdsBySpecifiedIds ($ ids );
214
243
$ tree = $ this ->_categoryTree ->loadByIds ($ ids );
215
- $ rootId = \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ;
244
+ $ rootId = Category::TREE_ROOT_ID ;
216
245
$ root = $ tree ->getNodeById ($ rootId );
217
- if ($ root && $ rootId != \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ) {
246
+ if ($ root && $ rootId != Category::TREE_ROOT_ID ) {
218
247
$ root ->setIsVisible (true );
219
- } elseif ($ root && $ root ->getId () == \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ) {
248
+ } elseif ($ root && $ root ->getId () == Category::TREE_ROOT_ID ) {
220
249
$ root ->setName (__ ('Root ' ));
221
250
}
222
251
@@ -227,6 +256,8 @@ public function getRootByIds($ids)
227
256
}
228
257
229
258
/**
259
+ * Get category node for tree
260
+ *
230
261
* @param mixed $parentNodeCategory
231
262
* @param int $recursionLevel
232
263
* @return Node
@@ -237,9 +268,9 @@ public function getNode($parentNodeCategory, $recursionLevel = 2)
237
268
$ node = $ this ->_categoryTree ->loadNode ($ nodeId );
238
269
$ node ->loadChildren ($ recursionLevel );
239
270
240
- if ($ node && $ nodeId != \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ) {
271
+ if ($ node && $ nodeId != Category::TREE_ROOT_ID ) {
241
272
$ node ->setIsVisible (true );
242
- } elseif ($ node && $ node ->getId () == \ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ) {
273
+ } elseif ($ node && $ node ->getId () == Category::TREE_ROOT_ID ) {
243
274
$ node ->setName (__ ('Root ' ));
244
275
}
245
276
@@ -249,17 +280,26 @@ public function getNode($parentNodeCategory, $recursionLevel = 2)
249
280
}
250
281
251
282
/**
283
+ * Get category save url
284
+ *
252
285
* @param array $args
253
286
* @return string
254
287
*/
255
288
public function getSaveUrl (array $ args = [])
256
289
{
257
- $ params = ['_current ' => false , '_query ' => false , 'store ' => $ this ->getStore ()->getId ()];
290
+ $ params = [
291
+ '_current ' => false ,
292
+ '_query ' => false ,
293
+ 'store ' => $ this ->getStore ()
294
+ ->getId ()
295
+ ];
258
296
$ params = array_merge ($ params , $ args );
259
297
return $ this ->getUrl ('catalog/*/save ' , $ params );
260
298
}
261
299
262
300
/**
301
+ * Get category edit url
302
+ *
263
303
* @return string
264
304
*/
265
305
public function getEditUrl ()
@@ -279,7 +319,7 @@ public function getRootIds()
279
319
{
280
320
$ ids = $ this ->getData ('root_ids ' );
281
321
if ($ ids === null ) {
282
- $ ids = [\ Magento \ Catalog \ Model \ Category::TREE_ROOT_ID ];
322
+ $ ids = [Category::TREE_ROOT_ID ];
283
323
foreach ($ this ->_storeManager ->getGroups () as $ store ) {
284
324
$ ids [] = $ store ->getRootCategoryId ();
285
325
}
0 commit comments