1: <?php
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: class Mage_Catalog_Model_Category_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: public function __construct()
37: {
38: $this->_storeIdSessionField = 'category_store_id';
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48:
49: public function level($website = null, $store = null, $categoryId = null)
50: {
51: $ids = array();
52: $storeId = Mage_Catalog_Model_Category::DEFAULT_STORE_ID;
53:
54:
55: if (null !== $website) {
56: try {
57: $website = Mage::app()->getWebsite($website);
58: if (null === $store) {
59: if (null === $categoryId) {
60: foreach ($website->getStores() as $store) {
61:
62: $ids[] = $store->getRootCategoryId();
63: }
64: } else {
65: $ids = $categoryId;
66: }
67: } elseif (in_array($store, $website->getStoreIds())) {
68: $storeId = Mage::app()->getStore($store)->getId();
69: $ids = (null === $categoryId)? $store->getRootCategoryId() : $categoryId;
70: } else {
71: $this->_fault('store_not_exists');
72: }
73: } catch (Mage_Core_Exception $e) {
74: $this->_fault('website_not_exists', $e->getMessage());
75: }
76: }
77: elseif (null !== $store) {
78:
79: if (null === $categoryId) {
80: try {
81: $store = Mage::app()->getStore($store);
82: $storeId = $store->getId();
83: $ids = $store->getRootCategoryId();
84: } catch (Mage_Core_Model_Store_Exception $e) {
85: $this->_fault('store_not_exists');
86: }
87: }
88:
89: else {
90: $storeId = $this->_getStoreId($store);
91: $ids = (int)$categoryId;
92: }
93: }
94:
95: else {
96: $ids = (null === $categoryId)? Mage_Catalog_Model_Category::TREE_ROOT_ID : $categoryId;
97: }
98:
99: $collection = Mage::getModel('catalog/category')->getCollection()
100: ->setStoreId($storeId)
101: ->addAttributeToSelect('name')
102: ->addAttributeToSelect('is_active');
103:
104: if (is_array($ids)) {
105: $collection->addFieldToFilter('entity_id', array('in' => $ids));
106: } else {
107: $collection->addFieldToFilter('parent_id', $ids);
108: }
109:
110:
111: $result = array();
112: foreach ($collection as $category) {
113:
114: $result[] = array(
115: 'category_id' => $category->getId(),
116: 'parent_id' => $category->getParentId(),
117: 'name' => $category->getName(),
118: 'is_active' => $category->getIsActive(),
119: 'position' => $category->getPosition(),
120: 'level' => $category->getLevel()
121: );
122: }
123:
124: return $result;
125: }
126:
127: 128: 129: 130: 131: 132: 133:
134: public function tree($parentId = null, $store = null)
135: {
136: if (is_null($parentId) && !is_null($store)) {
137: $parentId = Mage::app()->getStore($this->_getStoreId($store))->getRootCategoryId();
138: } elseif (is_null($parentId)) {
139: $parentId = 1;
140: }
141:
142:
143: $tree = Mage::getResourceSingleton('catalog/category_tree')
144: ->load();
145:
146: $root = $tree->getNodeById($parentId);
147:
148: if($root && $root->getId() == 1) {
149: $root->setName(Mage::helper('catalog')->__('Root'));
150: }
151:
152: $collection = Mage::getModel('catalog/category')->getCollection()
153: ->setStoreId($this->_getStoreId($store))
154: ->addAttributeToSelect('name')
155: ->addAttributeToSelect('is_active');
156:
157: $tree->addCollectionData($collection, true);
158:
159: return $this->_nodeToArray($root);
160: }
161:
162: 163: 164: 165: 166: 167:
168: protected function _nodeToArray(Varien_Data_Tree_Node $node)
169: {
170:
171: $result = array();
172: $result['category_id'] = $node->getId();
173: $result['parent_id'] = $node->getParentId();
174: $result['name'] = $node->getName();
175: $result['is_active'] = $node->getIsActive();
176: $result['position'] = $node->getPosition();
177: $result['level'] = $node->getLevel();
178: $result['children'] = array();
179:
180: foreach ($node->getChildren() as $child) {
181: $result['children'][] = $this->_nodeToArray($child);
182: }
183:
184: return $result;
185: }
186:
187: 188: 189: 190: 191: 192: 193:
194: protected function _initCategory($categoryId, $store = null)
195: {
196: $category = Mage::getModel('catalog/category')
197: ->setStoreId($this->_getStoreId($store))
198: ->load($categoryId);
199:
200: if (!$category->getId()) {
201: $this->_fault('not_exists');
202: }
203:
204: return $category;
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214:
215: public function info($categoryId, $store = null, $attributes = null)
216: {
217: $category = $this->_initCategory($categoryId, $store);
218:
219:
220: $result = array();
221: $result['category_id'] = $category->getId();
222:
223: $result['is_active'] = $category->getIsActive();
224: $result['position'] = $category->getPosition();
225: $result['level'] = $category->getLevel();
226:
227: foreach ($category->getAttributes() as $attribute) {
228: if ($this->_isAllowedAttribute($attribute, $attributes)) {
229: $result[$attribute->getAttributeCode()] = $category->getData($attribute->getAttributeCode());
230: }
231: }
232: $result['parent_id'] = $category->getParentId();
233: $result['children'] = $category->getChildren();
234: $result['all_children'] = $category->getAllChildren();
235:
236: return $result;
237: }
238:
239: 240: 241: 242: 243: 244: 245: 246:
247: public function create($parentId, $categoryData, $store = null)
248: {
249: $parent_category = $this->_initCategory($parentId, $store);
250:
251:
252: $category = Mage::getModel('catalog/category')
253: ->setStoreId($this->_getStoreId($store));
254:
255: $category->addData(array('path'=>implode('/', $parent_category->getPathIds())));
256: $category->setAttributeSetId($category->getDefaultAttributeSetId());
257:
258: $useConfig = array();
259: foreach ($category->getAttributes() as $attribute) {
260: if ($this->_isAllowedAttribute($attribute)
261: && isset($categoryData[$attribute->getAttributeCode()])
262: ) {
263:
264: $attrCode = $attribute->getAttributeCode();
265: $categoryDataValue = $categoryData[$attrCode];
266: if ('use_config' === $categoryDataValue ||
267: (is_array($categoryDataValue) &&
268: count($categoryDataValue) == 1 &&
269: 'use_config' === $categoryDataValue[0])
270: ) {
271: $useConfig[] = $attrCode;
272: $category->setData($attrCode, null);
273: } else {
274: $category->setData($attrCode, $categoryDataValue);
275: }
276: }
277: }
278:
279: $category->setParentId($parent_category->getId());
280:
281: 282: 283:
284: if (count($useConfig) > 0) {
285: $category->setData("use_post_data_config", $useConfig);
286: }
287:
288: try {
289: $validate = $category->validate();
290: if ($validate !== true) {
291: foreach ($validate as $code => $error) {
292: if ($error === true) {
293: Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $code));
294: } else {
295: Mage::throwException($error);
296: }
297: }
298: }
299:
300: $category->save();
301: }
302: catch (Mage_Core_Exception $e) {
303: $this->_fault('data_invalid', $e->getMessage());
304: }
305: catch (Exception $e) {
306: $this->_fault('data_invalid', $e->getMessage());
307: }
308:
309: return $category->getId();
310: }
311:
312: 313: 314: 315: 316: 317: 318: 319:
320: public function update($categoryId, $categoryData, $store = null)
321: {
322: $category = $this->_initCategory($categoryId, $store);
323:
324: foreach ($category->getAttributes() as $attribute) {
325: if ($this->_isAllowedAttribute($attribute)
326: && isset($categoryData[$attribute->getAttributeCode()])) {
327: $category->setData(
328: $attribute->getAttributeCode(),
329: $categoryData[$attribute->getAttributeCode()]
330: );
331: }
332: }
333:
334: try {
335: $validate = $category->validate();
336: if ($validate !== true) {
337: foreach ($validate as $code => $error) {
338: if ($error === true) {
339: Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $code));
340: }
341: else {
342: Mage::throwException($error);
343: }
344: }
345: }
346:
347: $category->save();
348: } catch (Mage_Core_Exception $e) {
349: $this->_fault('data_invalid', $e->getMessage());
350: } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
351: $this->_fault('data_invalid', $e->getMessage());
352: }
353:
354: return true;
355: }
356:
357: 358: 359: 360: 361: 362: 363: 364:
365: public function move($categoryId, $parentId, $afterId = null)
366: {
367: $category = $this->_initCategory($categoryId);
368: $parent_category = $this->_initCategory($parentId);
369:
370:
371: if ($afterId === null && $parent_category->hasChildren()) {
372: $parentChildren = $parent_category->getChildren();
373: $afterId = array_pop(explode(',', $parentChildren));
374: }
375:
376: if( strpos($parent_category->getPath(), $category->getPath()) === 0) {
377: $this->_fault('not_moved', "Operation do not allow to move a parent category to any of children category");
378: }
379:
380: try {
381: $category->move($parentId, $afterId);
382: } catch (Mage_Core_Exception $e) {
383: $this->_fault('not_moved', $e->getMessage());
384: }
385:
386: return true;
387: }
388:
389: 390: 391: 392: 393: 394:
395: public function delete($categoryId)
396: {
397: if (Mage_Catalog_Model_Category::TREE_ROOT_ID == $categoryId) {
398: $this->_fault('not_deleted', 'Cannot remove the system category.');
399: }
400:
401: $category = $this->_initCategory($categoryId);
402:
403: try {
404: $category->delete();
405: } catch (Mage_Core_Exception $e) {
406: $this->_fault('not_deleted', $e->getMessage());
407: }
408:
409: return true;
410: }
411:
412: 413: 414: 415: 416: 417: 418:
419: protected function _getProductId($productId, $identifierType = null)
420: {
421: $product = Mage::helper('catalog/product')->getProduct($productId, null, $identifierType);
422: if (!$product->getId()) {
423: $this->_fault('not_exists','Product not exists.');
424: }
425: return $product->getId();
426: }
427:
428:
429: 430: 431: 432: 433: 434: 435:
436: public function assignedProducts($categoryId, $store = null)
437: {
438: $category = $this->_initCategory($categoryId);
439:
440: $storeId = $this->_getStoreId($store);
441: $collection = $category->setStoreId($storeId)->getProductCollection();
442: ($storeId == 0)? $collection->addOrder('position', 'asc') : $collection->setOrder('position', 'asc');;
443:
444: $result = array();
445:
446: foreach ($collection as $product) {
447: $result[] = array(
448: 'product_id' => $product->getId(),
449: 'type' => $product->getTypeId(),
450: 'set' => $product->getAttributeSetId(),
451: 'sku' => $product->getSku(),
452: 'position' => $product->getCatIndexPosition()
453: );
454: }
455:
456: return $result;
457: }
458:
459: 460: 461: 462: 463: 464: 465: 466:
467: public function assignProduct($categoryId, $productId, $position = null, $identifierType = null)
468: {
469: $category = $this->_initCategory($categoryId);
470: $positions = $category->getProductsPosition();
471: $productId = $this->_getProductId($productId);
472: $positions[$productId] = $position;
473: $category->setPostedProducts($positions);
474:
475: try {
476: $category->save();
477: } catch (Mage_Core_Exception $e) {
478: $this->_fault('data_invalid', $e->getMessage());
479: }
480:
481: return true;
482: }
483:
484:
485: 486: 487: 488: 489: 490: 491: 492:
493: public function updateProduct($categoryId, $productId, $position = null, $identifierType = null)
494: {
495: $category = $this->_initCategory($categoryId);
496: $positions = $category->getProductsPosition();
497: $productId = $this->_getProductId($productId, $identifierType);
498: if (!isset($positions[$productId])) {
499: $this->_fault('product_not_assigned');
500: }
501: $positions[$productId] = $position;
502: $category->setPostedProducts($positions);
503:
504: try {
505: $category->save();
506: } catch (Mage_Core_Exception $e) {
507: $this->_fault('data_invalid', $e->getMessage());
508: }
509:
510: return true;
511: }
512:
513: 514: 515: 516: 517: 518: 519:
520: public function removeProduct($categoryId, $productId, $identifierType = null)
521: {
522: $category = $this->_initCategory($categoryId);
523: $positions = $category->getProductsPosition();
524: $productId = $this->_getProductId($productId, $identifierType);
525: if (!isset($positions[$productId])) {
526: $this->_fault('product_not_assigned');
527: }
528:
529: unset($positions[$productId]);
530: $category->setPostedProducts($positions);
531:
532: try {
533: $category->save();
534: } catch (Mage_Core_Exception $e) {
535: $this->_fault('data_invalid', $e->getMessage());
536: }
537:
538: return true;
539: }
540:
541: }
542: