Mage_Catalog_Model_Category_Api Class Reference

Inheritance diagram for Mage_Catalog_Model_Category_Api:

Mage_Catalog_Model_Api_Resource Mage_Api_Model_Resource_Abstract Mage_Catalog_Model_Category_Api_V2

List of all members.

Public Member Functions

 __construct ()
 level ($website=null, $store=null, $categoryId=null)
 tree ($parentId=null, $store=null)
 info ($categoryId, $store=null, $attributes=null)
 create ($parentId, $categoryData, $store=null)
 update ($categoryId, $categoryData, $store=null)
 move ($categoryId, $parentId, $afterId=null)
 delete ($categoryId)
 assignedProducts ($categoryId, $store=null)
 assignProduct ($categoryId, $productId, $position=null)
 updateProduct ($categoryId, $productId, $position=null)
 removeProduct ($categoryId, $productId)

Protected Member Functions

 _nodeToArray (Varien_Data_Tree_Node $node)
 _initCategory ($categoryId, $store=null)
 _getProductId ($productId)


Detailed Description

Definition at line 34 of file Api.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 36 of file Api.php.

00037     {
00038         $this->_storeIdSessionField = 'category_store_id';
00039     }


Member Function Documentation

_getProductId ( productId  )  [protected]

Get prduct Id from sku or from product id

Parameters:
int|string $productId
Returns:
int

Definition at line 370 of file Api.php.

00371     {
00372         $product = Mage::getModel('catalog/product');
00373 
00374         $idBySku = $product->getIdBySku($productId);
00375         if ($idBySku) {
00376             $productId = $idBySku;
00377         }
00378 
00379         $product->load($productId);
00380 
00381         if (!$product->getId()) {
00382             $this->_fault('not_exists','Product not exists.');
00383         }
00384 
00385         return $productId;
00386     }

_initCategory ( categoryId,
store = null 
) [protected]

Initilize and return category model

Parameters:
int $categoryId
string|int $store
Returns:
Mage_Catalog_Model_Category

Definition at line 185 of file Api.php.

00186     {
00187         $category = Mage::getModel('catalog/category')
00188             ->setStoreId($this->_getStoreId($store))
00189             ->load($categoryId);
00190 
00191         if (!$category->getId()) {
00192             $this->_fault('not_exists');
00193         }
00194 
00195         return $category;
00196     }

_nodeToArray ( Varien_Data_Tree_Node node  )  [protected]

Convert node to array

Parameters:
Varien_Data_Tree_Node $node
Returns:
array

Definition at line 159 of file Api.php.

00160     {
00161         // Only basic category data
00162         $result = array();
00163         $result['category_id'] = $node->getId();
00164         $result['parent_id']   = $node->getParentId();
00165         $result['name']        = $node->getName();
00166         $result['is_active']   = $node->getIsActive();
00167         $result['position']    = $node->getPosition();
00168         $result['level']       = $node->getLevel();
00169         $result['children']    = array();
00170 
00171         foreach ($node->getChildren() as $child) {
00172             $result['children'][] = $this->_nodeToArray($child);
00173         }
00174 
00175         return $result;
00176     }

assignedProducts ( categoryId,
store = null 
)

Retrieve list of assigned products to category

Parameters:
int $categoryId
string|int $store
Returns:
array

Definition at line 396 of file Api.php.

00397     {
00398         $category = $this->_initCategory($categoryId);
00399 
00400         $collection = $category->setStoreId($this->_getStoreId($store))
00401             ->getProductCollection()
00402             ->setOrder('position', 'asc');
00403 
00404         $result = array();
00405 
00406         foreach ($collection as $product) {
00407             $result[] = array(
00408                 'product_id' => $product->getId(),
00409                 'type'       => $product->getTypeId(),
00410                 'set'        => $product->getAttributeSetId(),
00411                 'sku'        => $product->getSku(),
00412                 'position'   => $product->getPosition()
00413             );
00414         }
00415 
00416         return $result;
00417     }

assignProduct ( categoryId,
productId,
position = null 
)

Assign product to category

Parameters:
int $categoryId
int $productId
int $position
Returns:
boolean

Definition at line 427 of file Api.php.

00428     {
00429         $category = $this->_initCategory($categoryId);
00430         $positions = $category->getProductsPosition();
00431         $productId = $this->_getProductId($productId);
00432         $positions[$productId] = $position;
00433         $category->setPostedProducts($positions);
00434 
00435         try {
00436             $category->save();
00437         } catch (Mage_Core_Exception $e) {
00438             $this->_fault('data_invalid', $e->getMessage());
00439         }
00440 
00441         return true;
00442     }

create ( parentId,
categoryData,
store = null 
)

Create new category

Parameters:
int $parentId
array $categoryData
Returns:
int

Reimplemented in Mage_Catalog_Model_Category_Api_V2.

Definition at line 237 of file Api.php.

00238     {
00239          $parent_category = $this->_initCategory($parentId);
00240 
00241         $category = Mage::getModel('catalog/category')
00242             ->setStoreId($this->_getStoreId($store));
00243 
00244 
00245 
00246         $category->addData(array('path'=>implode('/',$parent_category->getPathIds())));
00247 
00248         $category ->setAttributeSetId($category->getDefaultAttributeSetId());
00249         /* @var $category Mage_Catalog_Model_Category */
00250 
00251         foreach ($category->getAttributes() as $attribute) {
00252             if ($this->_isAllowedAttribute($attribute)
00253                 && isset($categoryData[$attribute->getAttributeCode()])) {
00254                 $category->setData(
00255                     $attribute->getAttributeCode(),
00256                     $categoryData[$attribute->getAttributeCode()]
00257                 );
00258             }
00259         }
00260         $category->setParentId($parent_category->getId());
00261         try {
00262             $category->save();
00263         } catch (Mage_Core_Exception $e) {
00264             $this->_fault('data_invalid', $e->getMessage());
00265         }
00266 
00267         return $category->getId();
00268     }

delete ( categoryId  ) 

Delete category

Parameters:
int $categoryId
Returns:
boolean

Definition at line 351 of file Api.php.

00352     {
00353         $category = $this->_initCategory($categoryId);
00354 
00355         try {
00356             $category->delete();
00357         } catch (Mage_Core_Exception $e) {
00358             $this->_fault('not_deleted', $e->getMessage());
00359         }
00360 
00361         return true;
00362     }

info ( categoryId,
store = null,
attributes = null 
)

Retrieve category data

Parameters:
int $categoryId
string|int $store
array $attributes
Returns:
array

Reimplemented in Mage_Catalog_Model_Category_Api_V2.

Definition at line 206 of file Api.php.

00207     {
00208         $category = $this->_initCategory($categoryId, $store);
00209 
00210         // Basic category data
00211         $result = array();
00212         $result['category_id'] = $category->getId();
00213 
00214         $result['is_active']   = $category->getIsActive();
00215         $result['position']    = $category->getPosition();
00216         $result['level']       = $category->getLevel();
00217 
00218         foreach ($category->getAttributes() as $attribute) {
00219             if ($this->_isAllowedAttribute($attribute, $attributes)) {
00220                 $result[$attribute->getAttributeCode()] = $category->getData($attribute->getAttributeCode());
00221             }
00222         }
00223         $result['parent_id']   = $category->getParentId();
00224         $result['children']           = $category->getChildren();
00225         $result['all_children']       = $category->getAllChildren();
00226 
00227         return $result;
00228     }

level ( website = null,
store = null,
categoryId = null 
)

Retrive level of categories for category/store view/website

Parameters:
string|int $website
string|int $store
Returns:
array

Definition at line 48 of file Api.php.

00049     {
00050         $ids = array();
00051         $storeId = Mage_Catalog_Model_Category::DEFAULT_STORE_ID;
00052 
00053         // load root categories of website
00054         if (null !== $website) {
00055             try {
00056                 $website = Mage::app()->getWebsite($website);
00057                 foreach ($website->getStores() as $store) {
00058                     /* @var $store Mage_Core_Model_Store */
00059                     $ids[] = $store->getRootCategoryId();
00060                 }
00061             } catch (Mage_Core_Exception $e) {
00062                 $this->_fault('website_not_exists', $e->getMessage());
00063             }
00064         }
00065         elseif (null !== $store) {
00066             // load children of root category of store
00067             if (null === $categoryId) {
00068                 try {
00069                     $store = Mage::app()->getStore($store);
00070                     $storeId = $store->getId();
00071                     $ids = $store->getRootCategoryId();
00072                 } catch (Mage_Core_Model_Store_Exception $e) {
00073                     $this->_fault('store_not_exists');
00074                 }
00075             }
00076             // load children of specified category id
00077             else {
00078                 $storeId = $this->_getStoreId($store);
00079                 $ids = (int)$categoryId;
00080             }
00081         }
00082         // load all root categories
00083         else {
00084             $ids = Mage_Catalog_Model_Category::TREE_ROOT_ID;
00085         }
00086 
00087         $collection = Mage::getModel('catalog/category')->getCollection()
00088             ->setStoreId($storeId)
00089             ->addAttributeToSelect('name')
00090             ->addAttributeToSelect('is_active');
00091 
00092         if (is_array($ids)) {
00093             $collection->addFieldToFilter('entity_id', array('in' => $ids));
00094         } else {
00095             $collection->addFieldToFilter('parent_id', $ids);
00096         }
00097 
00098         // Only basic category data
00099         $result = array();
00100         foreach ($collection as $category) {
00101             /* @var $category Mage_Catalog_Model_Category */
00102             $result[] = array(
00103                 'category_id' => $category->getId(),
00104                 'parent_id'   => $category->getParentId(),
00105                 'name'        => $category->getName(),
00106                 'is_active'   => $category->getIsActive(),
00107                 'position'    => $category->getPosition(),
00108                 'level'       => $category->getLevel()
00109             );
00110         }
00111 
00112         return $result;
00113     }

move ( categoryId,
parentId,
afterId = null 
)

Move category in tree

Parameters:
int $categoryId
int $parentId
int $afterId
Returns:
boolean

Definition at line 309 of file Api.php.

00310     {
00311         $category = $this->_initCategory($categoryId);
00312         $parent_category = $this->_initCategory($parentId);
00313 
00314         $tree = Mage::getResourceModel('catalog/category_tree')
00315                 ->load();
00316 
00317         $node           = $tree->getNodeById($category->getId());
00318         $newParentNode  = $tree->getNodeById($parent_category->getId());
00319 
00320         if (!$node || !$node->getId()) {
00321             $this->_fault('not_exists');
00322         }
00323 
00324         // if $afterId is null - move category to the down
00325         if ($afterId === null && $parent_category->hasChildren()) {
00326             $parentChildren = $parent_category->getChildren();
00327             $afterId = array_pop(explode(',', $parentChildren));
00328         }
00329 
00330         $prevNode = $tree->getNodeById($afterId);
00331 
00332         if (!$prevNode || !$prevNode->getId()) {
00333             $prevNode = null;
00334         }
00335 
00336         try {
00337             $tree->move($node, $newParentNode, $prevNode);
00338         } catch (Mage_Core_Exception $e) {
00339             $this->_fault('not_moved', $e->getMessage());
00340         }
00341 
00342         return true;
00343     }

removeProduct ( categoryId,
productId 
)

Remove product assignment from category

Parameters:
int $categoryId
int $productId
Returns:
boolean

Definition at line 480 of file Api.php.

00481     {
00482         $category = $this->_initCategory($categoryId);
00483         $positions = $category->getProductsPosition();
00484         $productId = $this->_getProductId($productId);
00485         if (!isset($positions[$productId])) {
00486             $this->_fault('product_not_assigned');
00487         }
00488 
00489         unset($positions[$productId]);
00490         $category->setPostedProducts($positions);
00491 
00492         try {
00493             $category->save();
00494         } catch (Mage_Core_Exception $e) {
00495             $this->_fault('data_invalid', $e->getMessage());
00496         }
00497 
00498         return true;
00499     }

tree ( parentId = null,
store = null 
)

Retrieve category tree

Parameters:
int $parent
string|int $store
Returns:
array

Definition at line 122 of file Api.php.

00123     {
00124         $tree = Mage::getResourceSingleton('catalog/category_tree')
00125                 ->load();
00126 
00127         if (is_null($parentId) && !is_null($store)) {
00128             $parentId = Mage::app()->getStore($this->_getStoreId($store))->getRootCategoryId();
00129         } elseif (is_null($parentId)) {
00130             $parentId = 1;
00131         }
00132 
00133         $tree = Mage::getResourceSingleton('catalog/category_tree')
00134             ->load();
00135         /* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */
00136 
00137         $root = $tree->getNodeById($parentId);
00138 
00139         if($root && $root->getId() == 1) {
00140             $root->setName(Mage::helper('catalog')->__('Root'));
00141         }
00142 
00143         $collection = Mage::getModel('catalog/category')->getCollection()
00144             ->setStoreId($this->_getStoreId($store))
00145             ->addAttributeToSelect('name')
00146             ->addAttributeToSelect('is_active');
00147 
00148         $tree->addCollectionData($collection, true);
00149 
00150         return $this->_nodeToArray($root);
00151     }

update ( categoryId,
categoryData,
store = null 
)

Update category data

Parameters:
int $categoryId
array $categoryData
string|int $store
Returns:
boolean

Reimplemented in Mage_Catalog_Model_Category_Api_V2.

Definition at line 278 of file Api.php.

00279     {
00280         $category = $this->_initCategory($categoryId, $store);
00281 
00282         foreach ($category->getAttributes() as $attribute) {
00283             if ($this->_isAllowedAttribute($attribute)
00284                 && isset($categoryData[$attribute->getAttributeCode()])) {
00285                 $category->setData(
00286                     $attribute->getAttributeCode(),
00287                     $categoryData[$attribute->getAttributeCode()]
00288                 );
00289             }
00290         }
00291 
00292         try {
00293             $category->save();
00294         } catch (Mage_Core_Exception $e) {
00295             $this->_fault('data_invalid', $e->getMessage());
00296         }
00297 
00298         return true;
00299     }

updateProduct ( categoryId,
productId,
position = null 
)

Update product assignment

Parameters:
int $categoryId
int $productId
int $position
Returns:
boolean

Definition at line 453 of file Api.php.

00454     {
00455         $category = $this->_initCategory($categoryId);
00456         $positions = $category->getProductsPosition();
00457         $productId = $this->_getProductId($productId);
00458         if (!isset($positions[$productId])) {
00459             $this->_fault('product_not_assigned');
00460         }
00461         $positions[$productId] = $position;
00462         $category->setPostedProducts($positions);
00463 
00464         try {
00465             $category->save();
00466         } catch (Mage_Core_Exception $e) {
00467             $this->_fault('data_invalid', $e->getMessage());
00468         }
00469 
00470         return true;
00471     }


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