-
Notifications
You must be signed in to change notification settings - Fork 0
/
Category.php
executable file
·957 lines (864 loc) · 24.2 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
<?php
/**
* Magento Enterprise Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Enterprise Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/enterprise-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/enterprise-edition
*/
/**
* Catalog category
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract
{
/**
* 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';
const TREE_ROOT_ID = 1;
const CACHE_TAG = 'catalog_category';
/**
* 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
*/
protected $_cacheTag = self::CACHE_TAG;
/**
* URL Model instance
*
* @var Mage_Core_Model_Url
*/
protected static $_url;
/**
* URL rewrite model
*
* @var Mage_Core_Model_Url_Rewrite
*/
protected static $_urlRewrite;
/**
* Use flat resource model flag
*
* @var bool
*/
protected $_useFlatResource = false;
/**
* Category design attributes
*
* @var array
*/
private $_designAttributes = array(
'custom_design',
'custom_design_from',
'custom_design_to',
'page_layout',
'custom_layout_update',
'custom_apply_to_products'
);
/**
* Category tree model
*
* @var Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree
*/
protected $_treeModel = null;
/**
* Category Url instance
*
* @var Mage_Catalog_Model_Category_Url
*/
protected $_urlModel;
/**
* Initialize resource mode
*
* @return void
*/
protected function _construct()
{
// If Flat Data enabled then use it but only on frontend
$flatHelper = Mage::helper('catalog/category_flat');
if ($flatHelper->isAvailable() && !Mage::app()->getStore()->isAdmin() && $flatHelper->isBuilt(true)
&& !$this->getDisableFlat()
) {
$this->_init('catalog/category_flat');
$this->_useFlatResource = true;
} else {
$this->_init('catalog/category');
}
}
/**
* Retrieve URL instance
*
* @return Mage_Core_Model_Url
*/
public function getUrlInstance()
{
if (!self::$_url) {
self::$_url = Mage::getModel('core/url');
}
return self::$_url;
}
/**
* Get url rewrite model
*
* @return Mage_Core_Model_Url_Rewrite
*/
public function getUrlRewrite()
{
if (!self::$_urlRewrite) {
self::$_urlRewrite = Mage::getSingleton('core/factory')->getUrlRewriteInstance();
}
return self::$_urlRewrite;
}
/**
* Retrieve category tree model
*
* @return Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree
*/
public function getTreeModel()
{
return Mage::getResourceModel('catalog/category_tree');
}
/**
* Enter description here...
*
* @return Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree
*/
public function getTreeModelInstance()
{
if (is_null($this->_treeModel)) {
$this->_treeModel = Mage::getResourceSingleton('catalog/category_tree');
}
return $this->_treeModel;
}
/**
* Move category
*
* @param int $parentId new parent category id
* @param int $afterCategoryId category id after which we have put current category
* @return Mage_Catalog_Model_Category
*/
public function move($parentId, $afterCategoryId)
{
/**
* Validate new parent category id. (category model is used for backward
* compatibility in event params)
*/
$parent = Mage::getModel('catalog/category')
->setStoreId($this->getStoreId())
->load($parentId);
if (!$parent->getId()) {
Mage::throwException(
Mage::helper('catalog')->__('Category move operation is not possible: the new parent category was not found.')
);
}
if (!$this->getId()) {
Mage::throwException(
Mage::helper('catalog')->__('Category move operation is not possible: the current category was not found.')
);
} elseif ($parent->getId() == $this->getId()) {
Mage::throwException(
Mage::helper('catalog')->__('Category move operation is not possible: parent category is equal to child category.')
);
}
/**
* Setting affected category ids for third party engine index refresh
*/
$this->setMovedCategoryId($this->getId());
$eventParams = array(
$this->_eventObject => $this,
'parent' => $parent,
'category_id' => $this->getId(),
'prev_parent_id'=> $this->getParentId(),
'parent_id' => $parentId
);
$moveComplete = false;
$this->_getResource()->beginTransaction();
try {
/**
* catalog_category_tree_move_before and catalog_category_tree_move_after
* events declared for backward compatibility
*/
Mage::dispatchEvent('catalog_category_tree_move_before', $eventParams);
Mage::dispatchEvent($this->_eventPrefix.'_move_before', $eventParams);
$this->getResource()->changeParent($this, $parent, $afterCategoryId);
Mage::dispatchEvent($this->_eventPrefix.'_move_after', $eventParams);
Mage::dispatchEvent('catalog_category_tree_move_after', $eventParams);
$this->_getResource()->commit();
// Set data for indexer
$this->setAffectedCategoryIds(array($this->getId(), $this->getParentId(), $parentId));
$moveComplete = true;
} catch (Exception $e) {
$this->_getResource()->rollBack();
throw $e;
}
if ($moveComplete) {
Mage::dispatchEvent('category_move', $eventParams);
Mage::getSingleton('index/indexer')->processEntityAction(
$this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
);
Mage::app()->cleanCache(array(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 Varien_Data_Collection_Db
*/
public function getProductCollection()
{
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this);
return $collection;
}
/**
* Retrieve all customer attributes
*
* @todo Use with Flat Resource
* @return array
*/
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
*
* array($productId => $position)
*
* @return array
*/
public function getProductsPosition()
{
if (!$this->getId()) {
return array();
}
$array = $this->getData('products_position');
if (is_null($array)) {
$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 array();
}
if ($storeIds = $this->getData('store_ids')) {
return $storeIds;
}
if (!$this->getId()) {
return array();
}
$nodes = array();
foreach ($this->getPathIds() as $id) {
$nodes[] = $id;
}
$storeIds = array();
$storeCollection = Mage::getModel('core/store')->getCollection()->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;
}
/**
* Retrieve Layout Update Handle name
*
* @return string
*/
public function getLayoutUpdateHandle()
{
$layout = 'catalog_category_';
if ($this->getIsAnchor()) {
$layout .= 'layered';
}
else {
$layout .= 'default';
}
return $layout;
}
/**
* 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 Mage::app()->getStore()->getId();
}
/**
* Set store id
*
* @param integer $storeId
* @return Mage_Catalog_Model_Category
*/
public function setStoreId($storeId)
{
if (!is_numeric($storeId)) {
$storeId = Mage::app($storeId)->getStore()->getId();
}
$this->setData('store_id', $storeId);
$this->getResource()->setStoreId($storeId);
return $this;
}
/**
* Get category url
*
* @return string
*/
public function getUrl()
{
return $this->getUrlModel()->getCategoryUrl($this);
}
/**
* Get product url model
*
* @return Mage_Catalog_Model_Category_Url
*/
public function getUrlModel()
{
if ($this->_urlModel === null) {
$this->_urlModel = Mage::getSingleton('catalog/factory')->getCategoryUrlInstance();
}
return $this->_urlModel;
}
/**
* Retrieve category id URL
*
* @return string
*/
public function getCategoryIdUrl()
{
Varien_Profiler::start('REGULAR: '.__METHOD__);
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
$url = $this->getUrlInstance()->getUrl('catalog/category/view', array(
's'=>$urlKey,
'id'=>$this->getId(),
));
Varien_Profiler::stop('REGULAR: '.__METHOD__);
return $url;
}
/**
* Format URL key from name or defined key
*
* @param string $str
* @return string
*/
public function formatUrlKey($str)
{
$str = Mage::helper('catalog/product_url')->format($str);
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', $str);
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');
return $urlKey;
}
/**
* Retrieve image URL
*
* @return string
*/
public function getImageUrl()
{
$url = false;
if ($image = $this->getImage()) {
$url = Mage::getBaseUrl('media').'catalog/category/'.$image;
}
return $url;
}
/**
* Retrieve URL path
*
* @return string
*/
public function getUrlPath()
{
$path = $this->getData('url_path');
if ($path) {
return $path;
}
$path = $this->getUrlKey();
if ($this->getParentId()) {
$parentPath = Mage::getModel('catalog/category')->load($this->getParentId())->getCategoryPath();
$path = $parentPath.'/'.$path;
}
$this->setUrlPath($path);
return $path;
}
/**
* Get parent category object
*
* @return Mage_Catalog_Model_Category
*/
public function getParentCategory()
{
if (!$this->hasData('parent_category')) {
$this->setData('parent_category', Mage::getModel('catalog/category')->load($this->getParentId()));
}
return $this->_getData('parent_category');
}
/**
* Get parent category identifier
*
* @return int
*/
public function getParentId()
{
$parentIds = $this->getParentIds();
return intval(array_pop($parentIds));
}
/**
* Get all parent categories ids
*
* @return array
*/
public function getParentIds()
{
return array_diff($this->getPathIds(), array($this->getId()));
}
/**
* Retrieve dates for custom design (from & to)
*
* @return array
*/
public function getCustomDesignDate()
{
$result = array();
$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 = array();
foreach ($this->_designAttributes as $attrName) {
$result[] = $this->_getAttribute($attrName);
}
return $result;
}
/**
* Retrieve attribute by code
*
* @param string $attributeCode
* @return Mage_Eav_Model_Entity_Attribute_Abstract
*/
private function _getAttribute($attributeCode)
{
if (!$this->_useFlatResource) {
$attribute = $this->getResource()->getAttribute($attributeCode);
}
else {
$attribute = Mage::getSingleton('catalog/config')
->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);
}
// $this->getTreeModelInstance()->load();
// $children = $this->getTreeModelInstance()->getChildren($this->getId());
//
// $myId = array($this->getId());
// if (is_array($children)) {
// $children = array_merge($myId, $children);
// }
// else {
// $children = $myId;
// }
// 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 = array();
//$path = $this->getTreeModelInstance()->getPath($this->getId());
$path = array_reverse($this->getPathIds());
foreach ($path as $itemId) {
if ($itemId == Mage::app()->getStore()->getRootCategoryId()) {
break;
}
$result[] = $itemId;
}
return implode(',', $result);
}
/**
* Check category id exising
*
* @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 (is_null($ids)) {
$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('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()
{
if (!$this->_getData('request_path')) {
$this->getUrl();
}
return $this->_getData('request_path');
}
/**
* Retrieve Name data wrapper
*
* @return string
*/
public function getName()
{
return $this->_getData('name');
}
/**
* Before delete process
*
* @return Mage_Catalog_Model_Category
*/
protected function _beforeDelete()
{
$this->_protectFromNonAdmin();
if ($this->getResource()->isForbiddenToDelete($this->getId())) {
Mage::throwException("Can't delete root category.");
}
return parent::_beforeDelete();
}
/**
* Retrieve anchors above
*
* @return array
*/
public function getAnchorsAbove()
{
$anchors = array();
$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 (!Mage::registry('_category_is_anchor_attribute')) {
$model = $this->_getAttribute('is_anchor');
Mage::register('_category_is_anchor_attribute', $model);
}
if ($isAnchorAttribute = Mage::registry('_category_is_anchor_attribute')) {
$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('product_count', $count);
}
return $this->getData('product_count');
}
/**
* Retrieve categories by parent
*
* @param int $parent
* @param int $recursionLevel
* @param bool $sorted
* @param bool $asCollection
* @param bool $toLoad
* @return mixed
*/
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 array
*/
public function getParentCategories()
{
return $this->getResource()->getParentCategories($this);
}
/**
* Retuen children categories of current category
*
* @return array
*/
public function getChildrenCategories()
{
return $this->getResource()->getChildrenCategories($this);
}
/**
* Return children categories of current category
*
* @return array
*/
public function getChildrenCategoriesWithInactive()
{
return $this->getResource()->getChildrenCategoriesWithInactive($this);
}
/**
* Return parent category of current category with own custom design settings
*
* @return Mage_Catalog_Model_Category
*/
public function getParentDesignCategory()
{
return $this->getResource()->getParentDesignCategory($this);
}
/**
* Check category is in Root Category list
*
* @return bool
*/
public function isInRootCategoryList()
{
return $this->getResource()->isInRootCategoryList($this);
}
/**
* Retrieve Available int Product Listing sort by
*
* @return null|array
*/
public function getAvailableSortBy()
{
$available = $this->getData('available_sort_by');
if (empty($available)) {
return array();
}
if ($available && !is_array($available)) {
$available = explode(',', $available);
}
return $available;
}
/**
* Retrieve Available Product Listing Sort By
* code as key, value - name
*
* @return array
*/
public function getAvailableSortByOptions() {
$availableSortBy = array();
$defaultSortBy = Mage::getSingleton('catalog/config')
->getAttributeUsedForSortByArray();
if ($this->getAvailableSortBy()) {
foreach ($this->getAvailableSortBy() as $sortBy) {
if (isset($defaultSortBy[$sortBy])) {
$availableSortBy[$sortBy] = $defaultSortBy[$sortBy];
}
}
}
if (!$availableSortBy) {
$availableSortBy = $defaultSortBy;
}
return $availableSortBy;
}
/**
* Retrieve Product Listing Default Sort By
*
* @return string
*/
public function getDefaultSortBy() {
if (!$sortBy = $this->getData('default_sort_by')) {
$sortBy = Mage::getSingleton('catalog/config')
->getProductListDefaultSortBy($this->getStoreId());
}
$available = $this->getAvailableSortByOptions();
if (!isset($available[$sortBy])) {
$sortBy = array_keys($available);
$sortBy = $sortBy[0];
}
return $sortBy;
}
/**
* Validate attribute values
*
* @throws Mage_Eav_Model_Entity_Attribute_Exception
* @return bool|array
*/
public function validate()
{
return $this->_getResource()->validate($this);
}
/**
* Init indexing process after category save
*
* @return Mage_Catalog_Model_Category
*/
protected function _afterSave()
{
$result = parent::_afterSave();
Mage::getSingleton('index/indexer')->processEntityAction(
$this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
);
return $result;
}
}