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 class Mage_Catalog_Model_Category_Api extends Mage_Catalog_Model_Api_Resource
00035 {
00036 public function __construct()
00037 {
00038 $this->_storeIdSessionField = 'category_store_id';
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 public function level($website = null, $store = null, $categoryId = null)
00049 {
00050 $ids = array();
00051 $storeId = Mage_Catalog_Model_Category::DEFAULT_STORE_ID;
00052
00053
00054 if (null !== $website) {
00055 try {
00056 $website = Mage::app()->getWebsite($website);
00057 foreach ($website->getStores() as $store) {
00058
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
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
00077 else {
00078 $storeId = $this->_getStoreId($store);
00079 $ids = (int)$categoryId;
00080 }
00081 }
00082
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
00099 $result = array();
00100 foreach ($collection as $category) {
00101
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 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 public function tree($parentId = null, $store = null)
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
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 }
00152
00153
00154
00155
00156
00157
00158
00159 protected function _nodeToArray(Varien_Data_Tree_Node $node)
00160 {
00161
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 }
00177
00178
00179
00180
00181
00182
00183
00184
00185 protected function _initCategory($categoryId, $store = null)
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 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 public function info($categoryId, $store = null, $attributes = null)
00207 {
00208 $category = $this->_initCategory($categoryId, $store);
00209
00210
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 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 public function create($parentId, $categoryData, $store = null)
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
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 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 public function update($categoryId, $categoryData, $store = null)
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 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 public function move($categoryId, $parentId, $afterId = null)
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
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 }
00344
00345
00346
00347
00348
00349
00350
00351 public function delete($categoryId)
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 }
00363
00364
00365
00366
00367
00368
00369
00370 protected function _getProductId($productId)
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 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 public function assignedProducts($categoryId, $store = null)
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 }
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 public function assignProduct($categoryId, $productId, $position = null)
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 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 public function updateProduct($categoryId, $productId, $position = null)
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 }
00472
00473
00474
00475
00476
00477
00478
00479
00480 public function removeProduct($categoryId, $productId)
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 }
00500
00501 }