Mage_Catalog_Model_Category Class Reference

Inheritance diagram for Mage_Catalog_Model_Category:

Mage_Catalog_Model_Abstract Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 getUrlInstance ()
 getUrlRewrite ()
 getTreeModel ()
 getTreeModelInstance ()
 getDefaultAttributeSetId ()
 getProductCollection ()
 getAttributes ($noDesignAttributes=false)
 getProductsPosition ()
 getStoreIds ()
 getLayoutUpdateHandle ()
 getStoreId ()
 setStoreId ($storeId)
 getUrl ()
 getCategoryIdUrl ()
 formatUrlKey ($str)
 getImageUrl ()
 getUrlPath ()
 getParentCategory ()
 getParentId ()
 getParentIds ()
 getCustomDesignDate ()
 getDesignAttributes ()
 getAllChildren ($asArray=false)
 getChildren ()
 getPathInStore ()
 checkId ($id)
 getPathIds ()
 getLevel ()
 verifyIds (array $ids)
 hasChildren ()
 getRequestPath ()
 getName ()
 getAnchorsAbove ()
 getProductCount ()
 getCategories ($parent, $recursionLevel=0, $sorted=false, $asCollection=false, $toLoad=true)
 getParentCategories ()
 getChildrenCategories ()
 isInRootCategoryList ()
 getAvailableSortBy ()
 getAvailableSortByOptions ()
 getDefaultSortBy ()

Public Attributes

const DM_PRODUCT = 'PRODUCTS'
const DM_PAGE = 'PAGE'
const DM_MIXED = 'PRODUCTS_AND_PAGE'
const TREE_ROOT_ID = 1
const CACHE_TAG = 'catalog_category'

Protected Member Functions

 _construct ()
 _beforeDelete ()

Protected Attributes

 $_eventPrefix = 'catalog_category'
 $_eventObject = 'category'
 $_cacheTag = self::CACHE_TAG
 $_useFlatResource = false
 $_treeModel = null

Static Protected Attributes

static $_url
static $_urlRewrite


Detailed Description

Definition at line 35 of file Category.php.


Member Function Documentation

_beforeDelete (  )  [protected]

Before delete process

Returns:
Mage_Catalog_Model_Category

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 653 of file Category.php.

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     }

_construct (  )  [protected]

Initialize resource mode

Reimplemented from Varien_Object.

Definition at line 112 of file Category.php.

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     }

checkId ( id  ) 

Check category id exising

Parameters:
int $id
Returns:
bool

Definition at line 573 of file Category.php.

00574     {
00575         return $this->_getResource()->checkId($id);
00576     }

formatUrlKey ( str  ) 

Format URL key from name or defined key

Parameters:
string $str
Returns:
string

Definition at line 380 of file Category.php.

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     }

getAllChildren ( asArray = false  ) 

Get all children categories IDs

Parameters:
boolean $asArray return resul as array instead of comma-separated list of IDs
Returns:
array|string

Definition at line 509 of file Category.php.

00510     {
00511         $children = $this->getResource()->getAllChildren($this);
00512         if ($asArray) {
00513             return $children;
00514         }
00515         else {
00516             return implode(',', $children);
00517         }
00518 
00519 //        $this->getTreeModelInstance()->load();
00520 //        $children = $this->getTreeModelInstance()->getChildren($this->getId());
00521 //
00522 //        $myId = array($this->getId());
00523 //        if (is_array($children)) {
00524 //            $children = array_merge($myId, $children);
00525 //        }
00526 //        else {
00527 //            $children = $myId;
00528 //        }
00529 //        if ($asArray) {
00530 //            return $children;
00531 //        }
00532 //        else {
00533 //            return implode(',', $children);
00534 //        }
00535     }

getAnchorsAbove (  ) 

Retrieve anchors above

Returns:
array

Definition at line 667 of file Category.php.

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     }

getAttributes ( noDesignAttributes = false  ) 

Retrieve all customer attributes

Todo:
Use with Flat Resource
Returns:
array

Definition at line 213 of file Category.php.

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     }

getAvailableSortBy (  ) 

Retrieve Available int Product Listing sort by

Returns:
null|array

Definition at line 758 of file Category.php.

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     }

getAvailableSortByOptions (  ) 

Retrieve Available Product Listing Sort By code as key, value - name

Returns:
array

Definition at line 776 of file Category.php.

00776                                                 {
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     }

getCategories ( parent,
recursionLevel = 0,
sorted = false,
asCollection = false,
toLoad = true 
)

Retrieve categories by parent

Parameters:
int $parent
int $recursionLevel
bool $sorted
bool $asCollection
bool $toLoad
Returns:
mixed

Definition at line 716 of file Category.php.

00717     {
00718         $categories = $this->getResource()
00719             ->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
00720         return $categories;
00721     }

getCategoryIdUrl (  ) 

Retrieve category id URL

Returns:
string

Definition at line 362 of file Category.php.

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     }

getChildren (  ) 

Retrieve children ids comma separated

Returns:
string

Definition at line 542 of file Category.php.

00543     {
00544         return implode(',', $this->getResource()->getChildren($this, false));
00545     }

getChildrenCategories (  ) 

Retuen children categories of current category

Returns:
array

Definition at line 738 of file Category.php.

00739     {
00740         return $this->getResource()->getChildrenCategories($this);
00741     }

getCustomDesignDate (  ) 

Retrieve dates for custom design (from & to)

Returns:
array

Definition at line 462 of file Category.php.

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     }

getDefaultAttributeSetId (  ) 

Move category

Returns:
Mage_Catalog_Model_Category Retrieve default attribute set id

int

Definition at line 189 of file Category.php.

00190     {
00191         return $this->getResource()->getEntityType()->getDefaultAttributeSetId();
00192     }

getDefaultSortBy (  ) 

Retrieve Product Listing Default Sort By

Returns:
string

Definition at line 800 of file Category.php.

00800                                        {
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     }

getDesignAttributes (  ) 

Retrieve design attributes array

Returns:
array

Definition at line 476 of file Category.php.

00477     {
00478         $result = array();
00479         foreach ($this->_designAttributes as $attrName) {
00480             $result[] = $this->_getAttribute($attrName);
00481         }
00482         return $result;
00483     }

getImageUrl (  ) 

Retrieve image URL

Returns:
string

Definition at line 394 of file Category.php.

00395     {
00396         $url = false;
00397         if ($image = $this->getImage()) {
00398             $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
00399         }
00400         return $url;
00401     }

getLayoutUpdateHandle (  ) 

Retrieve Layout Update Handle name

Returns:
string

Definition at line 275 of file Category.php.

00276     {
00277         $layout = 'catalog_category_';
00278         if ($this->getIsAnchor()) {
00279             $layout .= 'layered';
00280         }
00281         else {
00282             $layout .= 'default';
00283         }
00284         return $layout;
00285     }

getLevel (  ) 

Retrieve level

Returns:
int

Definition at line 599 of file Category.php.

00600     {
00601         if (!$this->hasLevel()) {
00602             return count(explode('/', $this->getPath())) - 1;
00603         }
00604         return $this->getData('level');
00605     }

getName (  ) 

Retrieve Name data wraper

Returns:
string

Definition at line 643 of file Category.php.

00644     {
00645         return $this->_getData('name');
00646     }

getParentCategories (  ) 

Return parent categories of current category

Returns:
array

Definition at line 728 of file Category.php.

00729     {
00730         return $this->getResource()->getParentCategories($this);
00731     }

getParentCategory (  ) 

Get parent category object

Returns:
Mage_Catalog_Model_Category

Definition at line 431 of file Category.php.

00432     {
00433         return Mage::getModel('catalog/category')->load($this->getParentId());
00434     }

getParentId (  ) 

Get parent category identifier

Returns:
int

Definition at line 441 of file Category.php.

00442     {
00443         $parentIds = $this->getParentIds();
00444         return intval(array_pop($parentIds));
00445     }

getParentIds (  ) 

Get all parent categories ids

Returns:
array

Definition at line 452 of file Category.php.

00453     {
00454         return array_diff($this->getPathIds(), array($this->getId()));
00455     }

getPathIds (  ) 

Get array categories ids which are part of category path Result array contain id of current category because it is part of the path

Returns:
array

Definition at line 584 of file Category.php.

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     }

getPathInStore (  ) 

Retrieve Stores where isset category Path Return comma separated string

Returns:
string

Definition at line 553 of file Category.php.

00554     {
00555         $result = array();
00556         //$path = $this->getTreeModelInstance()->getPath($this->getId());
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     }

getProductCollection (  ) 

Get category products collection

Returns:
Varien_Data_Collection_Db

Definition at line 199 of file Category.php.

00200     {
00201         $collection = Mage::getResourceModel('catalog/product_collection')
00202             ->setStoreId($this->getStoreId())
00203             ->addCategoryFilter($this);
00204         return $collection;
00205     }

getProductCount (  ) 

Retrieve count products of category

Returns:
int

Definition at line 697 of file Category.php.

00698     {
00699         if (!$this->hasProductCount()) {
00700             $count = $this->_getResource()->getProductCount($this); // load product count
00701             $this->setData('product_count', $count);
00702         }
00703         return $this->getData('product_count');
00704     }

getProductsPosition (  ) 

Retrieve array of product id's for category

array($productId => $position)

Returns:
array

Definition at line 237 of file Category.php.

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     }

getRequestPath (  ) 

Retrieve Request Path

Returns:
string

Definition at line 633 of file Category.php.

00634     {
00635         return $this->_getData('request_path');
00636     }

getStoreId (  ) 

Return store id.

If store id is underfined for category return current active store id

Returns:
integer

Definition at line 294 of file Category.php.

00295     {
00296         if ($this->hasData('store_id')) {
00297             return $this->_getData('store_id');
00298         }
00299         return Mage::app()->getStore()->getId();
00300     }

getStoreIds (  ) 

Retrieve array of store ids for category

Returns:
array

Definition at line 256 of file Category.php.

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     }

getTreeModel (  ) 

Retrieve category tree model

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree

Definition at line 153 of file Category.php.

00154     {
00155         return Mage::getResourceModel('catalog/category_tree');
00156     }

getTreeModelInstance (  ) 

Enter description here...

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree

Definition at line 163 of file Category.php.

00164     {
00165         if (is_null($this->_treeModel)) {
00166             $this->_treeModel = Mage::getResourceSingleton('catalog/category_tree');
00167         }
00168         return $this->_treeModel;
00169     }

getUrl (  ) 

Get category url

Returns:
string

Definition at line 323 of file Category.php.

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     }

getUrlInstance (  ) 

Retrieve URL instance

Returns:
Mage_Core_Model_Url

Definition at line 127 of file Category.php.

00128     {
00129         if (!self::$_url) {
00130             self::$_url = Mage::getModel('core/url');
00131         }
00132         return self::$_url;
00133     }

getUrlPath (  ) 

Retrieve URL path

Returns:
string

Definition at line 408 of file Category.php.

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     }

getUrlRewrite (  ) 

Get url rewrite model

Returns:
Mage_Core_Model_Url_Rewrite

Definition at line 140 of file Category.php.

00141     {
00142         if (!self::$_urlRewrite) {
00143             self::$_urlRewrite = Mage::getModel('core/url_rewrite');
00144         }
00145         return self::$_urlRewrite;
00146     }

hasChildren (  ) 

Retrieve Is Category has children flag

Returns:
bool

Definition at line 623 of file Category.php.

00624     {
00625         return $this->_getResource()->getChildrenAmount($this) > 0;
00626     }

isInRootCategoryList (  ) 

Check category is in Root Category list

Returns:
bool

Definition at line 748 of file Category.php.

00749     {
00750         return $this->getResource()->isInRootCategoryList($this);
00751     }

setStoreId ( storeId  ) 

Set store id

Parameters:
integer $storeId
Returns:
Mage_Catalog_Model_Category

Definition at line 308 of file Category.php.

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     }

verifyIds ( array ids  ) 

Verify category ids

Parameters:
array $ids
Returns:
bool

Definition at line 613 of file Category.php.

00614     {
00615         return $this->getResource()->verifyIds($ids);
00616     }


Member Data Documentation

$_cacheTag = self::CACHE_TAG [protected]

Model cache tag for clear cache in after save and after delete

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 64 of file Category.php.

$_eventObject = 'category' [protected]

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 59 of file Category.php.

$_eventPrefix = 'catalog_category' [protected]

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 52 of file Category.php.

$_treeModel = null [protected]

Definition at line 106 of file Category.php.

$_url [static, protected]

Definition at line 71 of file Category.php.

$_urlRewrite [static, protected]

Definition at line 78 of file Category.php.

$_useFlatResource = false [protected]

Definition at line 85 of file Category.php.

const CACHE_TAG = 'catalog_category'

Definition at line 45 of file Category.php.

const DM_MIXED = 'PRODUCTS_AND_PAGE'

Definition at line 42 of file Category.php.

const DM_PAGE = 'PAGE'

Definition at line 41 of file Category.php.

const DM_PRODUCT = 'PRODUCTS'

Category display modes

Definition at line 40 of file Category.php.

const TREE_ROOT_ID = 1

Definition at line 43 of file Category.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:38 2009 for Magento by  doxygen 1.5.8