00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract
00036 {
00037
00038
00039
00040 const DM_PRODUCT = 'PRODUCTS';
00041 const DM_PAGE = 'PAGE';
00042 const DM_MIXED = 'PRODUCTS_AND_PAGE';
00043 const TREE_ROOT_ID = 1;
00044
00045 const CACHE_TAG = 'catalog_category';
00046
00047
00048
00049
00050
00051
00052 protected $_eventPrefix = 'catalog_category';
00053
00054
00055
00056
00057
00058
00059 protected $_eventObject = 'category';
00060
00061
00062
00063
00064 protected $_cacheTag = self::CACHE_TAG;
00065
00066
00067
00068
00069
00070
00071 protected static $_url;
00072
00073
00074
00075
00076
00077
00078 protected static $_urlRewrite;
00079
00080
00081
00082
00083
00084
00085 protected $_useFlatResource = false;
00086
00087
00088
00089
00090
00091
00092 private $_designAttributes = array(
00093 'custom_design',
00094 'custom_design_apply',
00095 'custom_design_from',
00096 'custom_design_to',
00097 'page_layout',
00098 'custom_layout_update'
00099 );
00100
00101
00102
00103
00104
00105
00106 protected $_treeModel = null;
00107
00108
00109
00110
00111
00112 protected function _construct()
00113 {
00114 if (Mage::helper('catalog/category_flat')->isEnabled()) {
00115 $this->_init('catalog/category_flat');
00116 $this->_useFlatResource = true;
00117 } else {
00118 $this->_init('catalog/category');
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127 public function getUrlInstance()
00128 {
00129 if (!self::$_url) {
00130 self::$_url = Mage::getModel('core/url');
00131 }
00132 return self::$_url;
00133 }
00134
00135
00136
00137
00138
00139
00140 public function getUrlRewrite()
00141 {
00142 if (!self::$_urlRewrite) {
00143 self::$_urlRewrite = Mage::getModel('core/url_rewrite');
00144 }
00145 return self::$_urlRewrite;
00146 }
00147
00148
00149
00150
00151
00152
00153 public function getTreeModel()
00154 {
00155 return Mage::getResourceModel('catalog/category_tree');
00156 }
00157
00158
00159
00160
00161
00162
00163 public function getTreeModelInstance()
00164 {
00165 if (is_null($this->_treeModel)) {
00166 $this->_treeModel = Mage::getResourceSingleton('catalog/category_tree');
00167 }
00168 return $this->_treeModel;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 public function getDefaultAttributeSetId()
00190 {
00191 return $this->getResource()->getEntityType()->getDefaultAttributeSetId();
00192 }
00193
00194
00195
00196
00197
00198
00199 public function getProductCollection()
00200 {
00201 $collection = Mage::getResourceModel('catalog/product_collection')
00202 ->setStoreId($this->getStoreId())
00203 ->addCategoryFilter($this);
00204 return $collection;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 public function getAttributes($noDesignAttributes = false)
00214 {
00215 $result = $this->getResource()
00216 ->loadAllAttributes($this)
00217 ->getSortedAttributes();
00218
00219 if ($noDesignAttributes){
00220 foreach ($result as $k=>$a){
00221 if (in_array($k, $this->_designAttributes)) {
00222 unset($result[$k]);
00223 }
00224 }
00225 }
00226
00227 return $result;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 public function getProductsPosition()
00238 {
00239 if (!$this->getId()) {
00240 return array();
00241 }
00242
00243 $array = $this->getData('products_position');
00244 if (is_null($array)) {
00245 $array = $this->getResource()->getProductsPosition($this);
00246 $this->setData('products_position', $array);
00247 }
00248 return $array;
00249 }
00250
00251
00252
00253
00254
00255
00256 public function getStoreIds()
00257 {
00258 if ($this->getInitialSetupFlag()) {
00259 return array();
00260 }
00261
00262 if ($storeIds = $this->getData('store_ids')) {
00263 return $storeIds;
00264 }
00265 $storeIds = $this->getResource()->getStoreIds($this);
00266 $this->setData('store_ids', $storeIds);
00267 return $storeIds;
00268 }
00269
00270
00271
00272
00273
00274
00275 public function getLayoutUpdateHandle()
00276 {
00277 $layout = 'catalog_category_';
00278 if ($this->getIsAnchor()) {
00279 $layout .= 'layered';
00280 }
00281 else {
00282 $layout .= 'default';
00283 }
00284 return $layout;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 public function getStoreId()
00295 {
00296 if ($this->hasData('store_id')) {
00297 return $this->_getData('store_id');
00298 }
00299 return Mage::app()->getStore()->getId();
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 public function setStoreId($storeId)
00309 {
00310 if (!is_numeric($storeId)) {
00311 $storeId = Mage::app($storeId)->getStore()->getId();
00312 }
00313 $this->setData('store_id', $storeId);
00314 $this->getResource()->setStoreId($storeId);
00315 return $this;
00316 }
00317
00318
00319
00320
00321
00322
00323 public function getUrl()
00324 {
00325 $url = $this->_getData('url');
00326 if (is_null($url)) {
00327 Varien_Profiler::start('REWRITE: '.__METHOD__);
00328
00329 if ($this->hasData('request_path') && $this->getRequestPath() != '') {
00330 $this->setData('url', $this->getUrlInstance()->getDirectUrl($this->getRequestPath()));
00331 return $this->getData('url');
00332 }
00333
00334 Varien_Profiler::stop('REWRITE: '.__METHOD__);
00335
00336 $rewrite = $this->getUrlRewrite();
00337 if ($this->getStoreId()) {
00338 $rewrite->setStoreId($this->getStoreId());
00339 }
00340 $idPath = 'category/' . $this->getId();
00341 $rewrite->loadByIdPath($idPath);
00342
00343 if ($rewrite->getId()) {
00344 $this->setData('url', $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath()));
00345 Varien_Profiler::stop('REWRITE: '.__METHOD__);
00346 return $this->getData('url');
00347 }
00348
00349 Varien_Profiler::stop('REWRITE: '.__METHOD__);
00350
00351 $this->setData('url', $this->getCategoryIdUrl());
00352 return $this->getData('url');
00353 }
00354 return $url;
00355 }
00356
00357
00358
00359
00360
00361
00362 public function getCategoryIdUrl()
00363 {
00364 Varien_Profiler::start('REGULAR: '.__METHOD__);
00365 $urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
00366 $url = $this->getUrlInstance()->getUrl('catalog/category/view', array(
00367 's'=>$urlKey,
00368 'id'=>$this->getId(),
00369 ));
00370 Varien_Profiler::stop('REGULAR: '.__METHOD__);
00371 return $url;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 public function formatUrlKey($str)
00381 {
00382 $str = Mage::helper('core')->removeAccents($str);
00383 $urlKey = preg_replace('#[^0-9a-z]+#i', '-', $str);
00384 $urlKey = strtolower($urlKey);
00385 $urlKey = trim($urlKey, '-');
00386 return $urlKey;
00387 }
00388
00389
00390
00391
00392
00393
00394 public function getImageUrl()
00395 {
00396 $url = false;
00397 if ($image = $this->getImage()) {
00398 $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
00399 }
00400 return $url;
00401 }
00402
00403
00404
00405
00406
00407
00408 public function getUrlPath()
00409 {
00410 if ($path = $this->getData('url_path')) {
00411 return $path;
00412 }
00413
00414 $path = $this->getUrlKey();
00415
00416 if ($this->getParentId()) {
00417 $parentPath = Mage::getModel('catalog/category')->load($this->getParentId())->getCategoryPath();
00418 $path = $parentPath.'/'.$path;
00419 }
00420
00421 $this->setUrlPath($path);
00422
00423 return $path;
00424 }
00425
00426
00427
00428
00429
00430
00431 public function getParentCategory()
00432 {
00433 return Mage::getModel('catalog/category')->load($this->getParentId());
00434 }
00435
00436
00437
00438
00439
00440
00441 public function getParentId()
00442 {
00443 $parentIds = $this->getParentIds();
00444 return intval(array_pop($parentIds));
00445 }
00446
00447
00448
00449
00450
00451
00452 public function getParentIds()
00453 {
00454 return array_diff($this->getPathIds(), array($this->getId()));
00455 }
00456
00457
00458
00459
00460
00461
00462 public function getCustomDesignDate()
00463 {
00464 $result = array();
00465 $result['from'] = $this->getData('custom_design_from');
00466 $result['to'] = $this->getData('custom_design_to');
00467
00468 return $result;
00469 }
00470
00471
00472
00473
00474
00475
00476 public function getDesignAttributes()
00477 {
00478 $result = array();
00479 foreach ($this->_designAttributes as $attrName) {
00480 $result[] = $this->_getAttribute($attrName);
00481 }
00482 return $result;
00483 }
00484
00485
00486
00487
00488
00489
00490
00491 private function _getAttribute($attributeCode)
00492 {
00493 if (!$this->_useFlatResource) {
00494 $attribute = $this->getResource()->getAttribute($attributeCode);
00495 }
00496 else {
00497 $attribute = Mage::getSingleton('catalog/config')
00498 ->getAttribute('catalog_category', $attributeCode);
00499 }
00500 return $attribute;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509 public function getAllChildren($asArray = false)
00510 {
00511 $children = $this->getResource()->getAllChildren($this);
00512 if ($asArray) {
00513 return $children;
00514 }
00515 else {
00516 return implode(',', $children);
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 }
00536
00537
00538
00539
00540
00541
00542 public function getChildren()
00543 {
00544 return implode(',', $this->getResource()->getChildren($this, false));
00545 }
00546
00547
00548
00549
00550
00551
00552
00553 public function getPathInStore()
00554 {
00555 $result = array();
00556
00557 $path = array_reverse($this->getPathIds());
00558 foreach ($path as $itemId) {
00559 if ($itemId == Mage::app()->getStore()->getRootCategoryId()) {
00560 break;
00561 }
00562 $result[] = $itemId;
00563 }
00564 return implode(',', $result);
00565 }
00566
00567
00568
00569
00570
00571
00572
00573 public function checkId($id)
00574 {
00575 return $this->_getResource()->checkId($id);
00576 }
00577
00578
00579
00580
00581
00582
00583
00584 public function getPathIds()
00585 {
00586 $ids = $this->getData('path_ids');
00587 if (is_null($ids)) {
00588 $ids = explode('/', $this->getPath());
00589 $this->setData('path_ids', $ids);
00590 }
00591 return $ids;
00592 }
00593
00594
00595
00596
00597
00598
00599 public function getLevel()
00600 {
00601 if (!$this->hasLevel()) {
00602 return count(explode('/', $this->getPath())) - 1;
00603 }
00604 return $this->getData('level');
00605 }
00606
00607
00608
00609
00610
00611
00612
00613 public function verifyIds(array $ids)
00614 {
00615 return $this->getResource()->verifyIds($ids);
00616 }
00617
00618
00619
00620
00621
00622
00623 public function hasChildren()
00624 {
00625 return $this->_getResource()->getChildrenAmount($this) > 0;
00626 }
00627
00628
00629
00630
00631
00632
00633 public function getRequestPath()
00634 {
00635 return $this->_getData('request_path');
00636 }
00637
00638
00639
00640
00641
00642
00643 public function getName()
00644 {
00645 return $this->_getData('name');
00646 }
00647
00648
00649
00650
00651
00652
00653 protected function _beforeDelete()
00654 {
00655 $this->_protectFromNonAdmin();
00656 if ($this->getResource()->isForbiddenToDelete($this->getId())) {
00657 Mage::throwException("Can't delete root category.");
00658 }
00659 return parent::_beforeDelete();
00660 }
00661
00662
00663
00664
00665
00666
00667 public function getAnchorsAbove()
00668 {
00669 $anchors = array();
00670 $path = $this->getPathIds();
00671
00672 if (in_array($this->getId(), $path)) {
00673 unset($path[array_search($this->getId(), $path)]);
00674 }
00675
00676 if ($this->_useFlatResource) {
00677 $anchors = $this->_getResource()->getAnchorsAbove($path, $this->getStoreId());
00678 }
00679 else {
00680 if (!Mage::registry('_category_is_anchor_attribute')) {
00681 $model = $this->_getAttribute('is_anchor');
00682 Mage::register('_category_is_anchor_attribute', $model);
00683 }
00684
00685 if ($isAnchorAttribute = Mage::registry('_category_is_anchor_attribute')) {
00686 $anchors = $this->getResource()->findWhereAttributeIs($path, $isAnchorAttribute, 1);
00687 }
00688 }
00689 return $anchors;
00690 }
00691
00692
00693
00694
00695
00696
00697 public function getProductCount()
00698 {
00699 if (!$this->hasProductCount()) {
00700 $count = $this->_getResource()->getProductCount($this);
00701 $this->setData('product_count', $count);
00702 }
00703 return $this->getData('product_count');
00704 }
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716 public function getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
00717 {
00718 $categories = $this->getResource()
00719 ->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
00720 return $categories;
00721 }
00722
00723
00724
00725
00726
00727
00728 public function getParentCategories()
00729 {
00730 return $this->getResource()->getParentCategories($this);
00731 }
00732
00733
00734
00735
00736
00737
00738 public function getChildrenCategories()
00739 {
00740 return $this->getResource()->getChildrenCategories($this);
00741 }
00742
00743
00744
00745
00746
00747
00748 public function isInRootCategoryList()
00749 {
00750 return $this->getResource()->isInRootCategoryList($this);
00751 }
00752
00753
00754
00755
00756
00757
00758 public function getAvailableSortBy()
00759 {
00760 $available = $this->getData('available_sort_by');
00761 if (empty($available)) {
00762 return array();
00763 }
00764 if ($available && !is_array($available)) {
00765 $available = split(',', $available);
00766 }
00767 return $available;
00768 }
00769
00770
00771
00772
00773
00774
00775
00776 public function getAvailableSortByOptions() {
00777 $availableSortBy = array();
00778 $defaultSortBy = Mage::getSingleton('catalog/config')
00779 ->getAttributeUsedForSortByArray();
00780 if ($this->getAvailableSortBy()) {
00781 foreach ($this->getAvailableSortBy() as $sortBy) {
00782 if (isset($defaultSortBy[$sortBy])) {
00783 $availableSortBy[$sortBy] = $defaultSortBy[$sortBy];
00784 }
00785 }
00786 }
00787
00788 if (!$availableSortBy) {
00789 $availableSortBy = $defaultSortBy;
00790 }
00791
00792 return $availableSortBy;
00793 }
00794
00795
00796
00797
00798
00799
00800 public function getDefaultSortBy() {
00801 if (!$sortBy = $this->getData('default_sort_by')) {
00802 $sortBy = Mage::getSingleton('catalog/config')
00803 ->getProductListDefaultSortBy($this->getStoreId());
00804 }
00805 $available = $this->getAvailableSortByOptions();
00806 if (!isset($available[$sortBy])) {
00807 $sortBy = array_keys($available);
00808 $sortBy = $sortBy[0];
00809 }
00810
00811 return $sortBy;
00812 }
00813 }