/
Category.php
1405 lines (1274 loc) · 38 KB
/
Category.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Convert\ConvertArray;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Profiler;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
/**
* Catalog category
*
* @method Category setAffectedProductIds(array $productIds)
* @method array getAffectedProductIds()
* @method Category setMovedCategoryId(array $productIds)
* @method int getMovedCategoryId()
* @method Category setAffectedCategoryIds(array $categoryIds)
* @method array getAffectedCategoryIds()
* @method Category setUrlKey(string $urlKey)
* @method Category setUrlPath(string $urlPath)
*
* @SuppressWarnings(PHPMD.LongVariable)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Category extends \Magento\Catalog\Model\AbstractModel implements
\Magento\Framework\DataObject\IdentityInterface,
\Magento\Catalog\Api\Data\CategoryInterface,
\Magento\Catalog\Api\Data\CategoryTreeInterface
{
/**
* Entity code.
* Can be used as part of method name for entity processing
*/
const ENTITY = 'catalog_category';
/**#@+
* Category display modes
*/
const DM_PRODUCT = 'PRODUCTS';
const DM_PAGE = 'PAGE';
const DM_MIXED = 'PRODUCTS_AND_PAGE';
/**#@-*/
/**
* Id of root category
*/
const ROOT_CATEGORY_ID = 0;
/**
* Id of category tree root
*/
const TREE_ROOT_ID = 1;
const CACHE_TAG = 'catalog_category';
/**#@+
* Constants
*/
const KEY_PARENT_ID = 'parent_id';
const KEY_NAME = 'name';
const KEY_IS_ACTIVE = 'is_active';
const KEY_POSITION = 'position';
const KEY_LEVEL = 'level';
const KEY_UPDATED_AT = 'updated_at';
const KEY_CREATED_AT = 'created_at';
const KEY_PATH = 'path';
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
const KEY_PRODUCT_COUNT = 'product_count';
const KEY_CHILDREN_DATA = 'children_data';
/**#@-*/
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'catalog_category';
/**
* Parameter name in event
*
* @var string
*/
protected $_eventObject = 'category';
/**
* Model cache tag for clear cache in after save and after delete
*
* @var string
*/
protected $_cacheTag = self::CACHE_TAG;
/**
* URL Model instance
*
* @var \Magento\Framework\UrlInterface
*/
protected $_url;
/**
* URL rewrite model
*
* @var \Magento\UrlRewrite\Model\UrlRewrite
*/
protected $_urlRewrite;
/**
* Use flat resource model flag
*
* @var bool
*/
protected $_useFlatResource = false;
/**
* Category design attributes
*
* @var string[]
*/
protected $_designAttributes = [
'custom_design',
'custom_design_from',
'custom_design_to',
'page_layout',
'custom_layout_update',
'custom_apply_to_products',
];
/**
* Attributes are that part of interface
*
* @var array
*/
protected $interfaceAttributes = [
'id',
self::KEY_PARENT_ID,
self::KEY_NAME,
self::KEY_IS_ACTIVE,
self::KEY_POSITION,
self::KEY_LEVEL,
self::KEY_UPDATED_AT,
self::KEY_CREATED_AT,
self::KEY_AVAILABLE_SORT_BY,
self::KEY_INCLUDE_IN_MENU,
self::KEY_CHILDREN_DATA,
];
/**
* Category tree model
*
* @var \Magento\Catalog\Model\ResourceModel\Category\Tree
*/
protected $_treeModel = null;
/**
* Core data
*
* @var \Magento\Framework\Filter\FilterManager
*/
protected $filter;
/**
* Catalog config
*
* @var \Magento\Catalog\Model\Config
*/
protected $_catalogConfig;
/**
* Product collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productCollectionFactory;
/**
* Store collection factory
*
* @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
*/
protected $_storeCollectionFactory;
/**
* Category tree factory
*
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
*/
protected $_categoryTreeFactory;
/**
* @var Indexer\Category\Flat\State
*/
protected $flatState;
/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator */
protected $categoryUrlPathGenerator;
/** @var UrlFinderInterface */
protected $urlFinder;
/** @var \Magento\Framework\Indexer\IndexerRegistry */
protected $indexerRegistry;
/**
* @var CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* @var \Magento\Framework\Api\MetadataServiceInterface
*/
protected $metadataService;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param AttributeValueFactory $customAttributeFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Api\CategoryAttributeRepositoryInterface $metadataService
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTreeResource
* @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory
* @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param Config $catalogConfig
* @param \Magento\Framework\Filter\FilterManager $filter
* @param Indexer\Category\Flat\State $flatState
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param UrlFinderInterface $urlFinder
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
* @param CategoryRepositoryInterface $categoryRepository
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
AttributeValueFactory $customAttributeFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Api\CategoryAttributeRepositoryInterface $metadataService,
\Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTreeResource,
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
\Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Config $catalogConfig,
\Magento\Framework\Filter\FilterManager $filter,
Indexer\Category\Flat\State $flatState,
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
UrlFinderInterface $urlFinder,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->metadataService = $metadataService;
$this->_treeModel = $categoryTreeResource;
$this->_categoryTreeFactory = $categoryTreeFactory;
$this->_storeCollectionFactory = $storeCollectionFactory;
$this->_url = $url;
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogConfig = $catalogConfig;
$this->filter = $filter;
$this->flatState = $flatState;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->urlFinder = $urlFinder;
$this->indexerRegistry = $indexerRegistry;
$this->categoryRepository = $categoryRepository;
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$storeManager,
$resource,
$resourceCollection,
$data
);
}
/**
* Initialize resource mode
*
* @return void
*/
protected function _construct()
{
// If Flat Index enabled then use it but only on frontend
if ($this->flatState->isAvailable()) {
$this->_init('Magento\Catalog\Model\ResourceModel\Category\Flat');
$this->_useFlatResource = true;
} else {
$this->_init('Magento\Catalog\Model\ResourceModel\Category');
}
}
/**
* {@inheritdoc}
*/
protected function getCustomAttributesCodes()
{
if ($this->customAttributesCodes === null) {
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
$this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes);
}
return $this->customAttributesCodes;
}
/**
* Get flat resource model flag
*
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getUseFlatResource()
{
return $this->_useFlatResource;
}
/**
* Retrieve URL instance
*
* @return \Magento\Framework\UrlInterface
*/
public function getUrlInstance()
{
return $this->_url;
}
/**
* Retrieve category tree model
*
* @return \Magento\Catalog\Model\ResourceModel\Category\Tree
*/
public function getTreeModel()
{
return $this->_categoryTreeFactory->create();
}
/**
* Enter description here...
*
* @return \Magento\Catalog\Model\ResourceModel\Category\Tree
*/
public function getTreeModelInstance()
{
return $this->_treeModel;
}
/**
* Move category
*
* @param int $parentId new parent category id
* @param null|int $afterCategoryId category id after which we have put current category
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function move($parentId, $afterCategoryId)
{
/**
* Validate new parent category id. (category model is used for backward
* compatibility in event params)
*/
try {
$parent = $this->categoryRepository->get($parentId, $this->getStoreId());
} catch (NoSuchEntityException $e) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'Sorry, but we can\'t find the new parent category you selected.'
),
$e
);
}
if (!$this->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Sorry, but we can\'t find the new category you selected.')
);
} elseif ($parent->getId() == $this->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'We can\'t move the category because the parent category name matches the child category name.'
)
);
}
/**
* Setting affected category ids for third party engine index refresh
*/
$this->setMovedCategoryId($this->getId());
$oldParentId = $this->getParentId();
$oldParentIds = $this->getParentIds();
$eventParams = [
$this->_eventObject => $this,
'parent' => $parent,
'category_id' => $this->getId(),
'prev_parent_id' => $oldParentId,
'parent_id' => $parentId,
];
$this->_getResource()->beginTransaction();
try {
$this->_eventManager->dispatch($this->_eventPrefix . '_move_before', $eventParams);
$this->getResource()->changeParent($this, $parent, $afterCategoryId);
$this->_eventManager->dispatch($this->_eventPrefix . '_move_after', $eventParams);
$this->_getResource()->commit();
// Set data for indexer
$this->setAffectedCategoryIds([$this->getId(), $oldParentId, $parentId]);
} catch (\Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
$this->_eventManager->dispatch('category_move', $eventParams);
if ($this->flatState->isFlatEnabled()) {
$flatIndexer = $this->indexerRegistry->get(Indexer\Category\Flat\State::INDEXER_ID);
if (!$flatIndexer->isScheduled()) {
$flatIndexer->reindexList([$this->getId(), $oldParentId, $parentId]);
}
}
$productIndexer = $this->indexerRegistry->get(Indexer\Category\Product::INDEXER_ID);
if (!$productIndexer->isScheduled()) {
$productIndexer->reindexList(array_merge($this->getPathIds(), $oldParentIds));
}
$this->_cacheManager->clean([self::CACHE_TAG]);
return $this;
}
/**
* Retrieve default attribute set id
*
* @return int
*/
public function getDefaultAttributeSetId()
{
return $this->getResource()->getEntityType()->getDefaultAttributeSetId();
}
/**
* Get category products collection
*
* @return \Magento\Framework\Data\Collection\AbstractDb
*/
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create()->setStoreId(
$this->getStoreId()
)->addCategoryFilter(
$this
);
return $collection;
}
/**
* Retrieve all customer attributes
*
* @param bool $noDesignAttributes
* @return array
* @todo Use with Flat Resource
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function getAttributes($noDesignAttributes = false)
{
$result = $this->getResource()->loadAllAttributes($this)->getSortedAttributes();
if ($noDesignAttributes) {
foreach ($result as $k => $a) {
if (in_array($k, $this->_designAttributes)) {
unset($result[$k]);
}
}
}
return $result;
}
/**
* Retrieve array of product id's for category
*
* The array returned has the following format:
* array($productId => $position)
*
* @return array
*/
public function getProductsPosition()
{
if (!$this->getId()) {
return [];
}
$array = $this->getData('products_position');
if ($array === null) {
$array = $this->getResource()->getProductsPosition($this);
$this->setData('products_position', $array);
}
return $array;
}
/**
* Retrieve array of store ids for category
*
* @return array
*/
public function getStoreIds()
{
if ($this->getInitialSetupFlag()) {
return [];
}
$storeIds = $this->getData('store_ids');
if ($storeIds) {
return $storeIds;
}
if (!$this->getId()) {
return [];
}
$nodes = [];
foreach ($this->getPathIds() as $id) {
$nodes[] = $id;
}
$storeIds = [];
$storeCollection = $this->_storeCollectionFactory->create()->loadByCategoryIds($nodes);
foreach ($storeCollection as $store) {
$storeIds[$store->getId()] = $store->getId();
}
$entityStoreId = $this->getStoreId();
if (!in_array($entityStoreId, $storeIds)) {
array_unshift($storeIds, $entityStoreId);
}
if (!in_array(0, $storeIds)) {
array_unshift($storeIds, 0);
}
$this->setData('store_ids', $storeIds);
return $storeIds;
}
/**
* Return store id.
*
* If store id is underfined for category return current active store id
*
* @return integer
*/
public function getStoreId()
{
if ($this->hasData('store_id')) {
return $this->_getData('store_id');
}
return $this->_storeManager->getStore()->getId();
}
/**
* Set store id
*
* @param int|string $storeId
* @return $this
*/
public function setStoreId($storeId)
{
if (!is_numeric($storeId)) {
$storeId = $this->_storeManager->getStore($storeId)->getId();
}
$this->setData('store_id', $storeId);
$this->getResource()->setStoreId($storeId);
return $this;
}
/**
* Get category url
*
* @return string
*/
public function getUrl()
{
$url = $this->_getData('url');
if ($url === null) {
Profiler::start('REWRITE: ' . __METHOD__, ['group' => 'REWRITE', 'method' => __METHOD__]);
if ($this->hasData('request_path') && $this->getRequestPath() != '') {
$this->setData('url', $this->getUrlInstance()->getDirectUrl($this->getRequestPath()));
Profiler::stop('REWRITE: ' . __METHOD__);
return $this->getData('url');
}
$rewrite = $this->urlFinder->findOneByData([
UrlRewrite::ENTITY_ID => $this->getId(),
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $this->getStoreId(),
]);
if ($rewrite) {
$this->setData('url', $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath()));
Profiler::stop('REWRITE: ' . __METHOD__);
return $this->getData('url');
}
$this->setData('url', $this->getCategoryIdUrl());
Profiler::stop('REWRITE: ' . __METHOD__);
return $this->getData('url');
}
return $url;
}
/**
* Retrieve category id URL
*
* @return string
*/
public function getCategoryIdUrl()
{
Profiler::start('REGULAR: ' . __METHOD__, ['group' => 'REGULAR', 'method' => __METHOD__]);
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
$url = $this->getUrlInstance()->getUrl('catalog/category/view', ['s' => $urlKey, 'id' => $this->getId()]);
Profiler::stop('REGULAR: ' . __METHOD__);
return $url;
}
/**
* Format URL key from name or defined key
*
* @param string $str
* @return string
*/
public function formatUrlKey($str)
{
return $this->filter->translitUrl($str);
}
/**
* Retrieve image URL
*
* @return string
*/
public function getImageUrl()
{
$url = false;
$image = $this->getImage();
if ($image) {
$url = $this->_storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'catalog/category/' . $image;
}
return $url;
}
/**
* Get parent category object
*
* @return \Magento\Catalog\Model\Category
*/
public function getParentCategory()
{
if (!$this->hasData('parent_category')) {
$this->setData('parent_category', $this->categoryRepository->get($this->getParentId()));
}
return $this->_getData('parent_category');
}
/**
* Get parent category identifier
*
* @return int
*/
public function getParentId()
{
$parentId = $this->getData(self::KEY_PARENT_ID);
if (isset($parentId)) {
return $parentId;
}
$parentIds = $this->getParentIds();
return intval(array_pop($parentIds));
}
/**
* Get all parent categories ids
*
* @return array
*/
public function getParentIds()
{
return array_diff($this->getPathIds(), [$this->getId()]);
}
/**
* Retrieve dates for custom design (from & to)
*
* @return array
*/
public function getCustomDesignDate()
{
$result = [];
$result['from'] = $this->getData('custom_design_from');
$result['to'] = $this->getData('custom_design_to');
return $result;
}
/**
* Retrieve design attributes array
*
* @return array
*/
public function getDesignAttributes()
{
$result = [];
foreach ($this->_designAttributes as $attrName) {
$result[] = $this->_getAttribute($attrName);
}
return $result;
}
/**
* Retrieve attribute by code
*
* @param string $attributeCode
* @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
*/
private function _getAttribute($attributeCode)
{
if (!$this->_useFlatResource) {
$attribute = $this->getResource()->getAttribute($attributeCode);
} else {
$attribute = $this->_catalogConfig->getAttribute(self::ENTITY, $attributeCode);
}
return $attribute;
}
/**
* Get all children categories IDs
*
* @param boolean $asArray return result as array instead of comma-separated list of IDs
* @return array|string
*/
public function getAllChildren($asArray = false)
{
$children = $this->getResource()->getAllChildren($this);
if ($asArray) {
return $children;
} else {
return implode(',', $children);
}
}
/**
* Retrieve children ids comma separated
*
* @return string
*/
public function getChildren()
{
return implode(',', $this->getResource()->getChildren($this, false));
}
/**
* Retrieve Stores where isset category Path
* Return comma separated string
*
* @return string
*/
public function getPathInStore()
{
$result = [];
$path = array_reverse($this->getPathIds());
foreach ($path as $itemId) {
if ($itemId == $this->_storeManager->getStore()->getRootCategoryId()) {
break;
}
$result[] = $itemId;
}
return implode(',', $result);
}
/**
* Check category id existing
*
* @param int $id
* @return bool
*/
public function checkId($id)
{
return $this->_getResource()->checkId($id);
}
/**
* Get array categories ids which are part of category path
* Result array contain id of current category because it is part of the path
*
* @return array
*/
public function getPathIds()
{
$ids = $this->getData('path_ids');
if ($ids === null) {
$ids = explode('/', $this->getPath());
$this->setData('path_ids', $ids);
}
return $ids;
}
/**
* Retrieve level
*
* @return int
*/
public function getLevel()
{
if (!$this->hasLevel()) {
return count(explode('/', $this->getPath())) - 1;
}
return $this->getData(self::KEY_LEVEL);
}
/**
* Verify category ids
*
* @param array $ids
* @return bool
*/
public function verifyIds(array $ids)
{
return $this->getResource()->verifyIds($ids);
}
/**
* Retrieve Is Category has children flag
*
* @return bool
*/
public function hasChildren()
{
return $this->_getResource()->getChildrenAmount($this) > 0;
}
/**
* Retrieve Request Path
*
* @return string
*/
public function getRequestPath()
{
return $this->_getData('request_path');
}
/**
* Retrieve Name data wrapper
*
* @return string
*/
public function getName()
{
return $this->_getData(self::KEY_NAME);
}
/**
* Before delete process
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return $this
*/
public function beforeDelete()
{
if ($this->getResource()->isForbiddenToDelete($this->getId())) {
throw new \Magento\Framework\Exception\LocalizedException(__('Can\'t delete root category.'));
}
return parent::beforeDelete();
}
/**
* Retrieve anchors above
*
* @return array
*/
public function getAnchorsAbove()
{
$anchors = [];
$path = $this->getPathIds();
if (in_array($this->getId(), $path)) {
unset($path[array_search($this->getId(), $path)]);
}
if ($this->_useFlatResource) {
$anchors = $this->_getResource()->getAnchorsAbove($path, $this->getStoreId());
} else {
if (!$this->_registry->registry('_category_is_anchor_attribute')) {
$model = $this->_getAttribute('is_anchor');
$this->_registry->register('_category_is_anchor_attribute', $model);
}
$isAnchorAttribute = $this->_registry->registry('_category_is_anchor_attribute');
if ($isAnchorAttribute) {
$anchors = $this->getResource()->findWhereAttributeIs($path, $isAnchorAttribute, 1);
}
}
return $anchors;
}
/**
* Retrieve count products of category
*
* @return int
*/
public function getProductCount()
{
if (!$this->hasProductCount()) {
$count = $this->_getResource()->getProductCount($this);
// load product count
$this->setData(self::KEY_PRODUCT_COUNT, $count);
}
return $this->getData(self::KEY_PRODUCT_COUNT);
}
/**
* Retrieve categories by parent
*
* @param int $parent
* @param int $recursionLevel
* @param bool $sorted
* @param bool $asCollection
* @param bool $toLoad
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true)
{
$categories = $this->getResource()->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
return $categories;
}
/**
* Return parent categories of current category
*
* @return \Magento\Framework\DataObject[]|\Magento\Catalog\Model\Category[]
*/
public function getParentCategories()
{
return $this->getResource()->getParentCategories($this);
}
/**
* Return children categories of current category
*
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection|\Magento\Catalog\Model\Category[]
*/
public function getChildrenCategories()
{
return $this->getResource()->getChildrenCategories($this);
}
/**
* Return parent category of current category with own custom design settings
*
* @return \Magento\Catalog\Model\Category
*/
public function getParentDesignCategory()
{
return $this->getResource()->getParentDesignCategory($this);
}
/**
* Check category is in Root Category list
*
* @return bool
*/
public function isInRootCategoryList()
{
// TODO there are bugs in resource models' methods, store_id set to model o/andr to resource model are ignored
return $this->getResource()->isInRootCategoryList($this);
}
/**
* Retrieve Available int Product Listing sort by
*
* @return null|array
*/
public function getAvailableSortBy()
{
$available = $this->getData(self::KEY_AVAILABLE_SORT_BY);
if (empty($available)) {