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_V2 extends Mage_Catalog_Model_Category_Api
35: {
36: 37: 38: 39: 40: 41: 42: 43:
44: public function info($categoryId, $store = null, $attributes = null)
45: {
46: $category = $this->_initCategory($categoryId, $store);
47:
48:
49: $result = array();
50: $result['category_id'] = $category->getId();
51:
52: $result['is_active'] = $category->getIsActive();
53: $result['position'] = $category->getPosition();
54: $result['level'] = $category->getLevel();
55:
56: foreach ($category->getAttributes() as $attribute) {
57: if ($this->_isAllowedAttribute($attribute, $attributes)) {
58: $result[$attribute->getAttributeCode()] = $category->getDataUsingMethod($attribute->getAttributeCode());
59: }
60: }
61: $result['parent_id'] = $category->getParentId();
62: $result['children'] = $category->getChildren();
63: $result['all_children'] = $category->getAllChildren();
64:
65: return $result;
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: public function create($parentId, $categoryData, $store = null)
76: {
77: $parent_category = $this->_initCategory($parentId, $store);
78:
79:
80: $category = Mage::getModel('catalog/category')
81: ->setStoreId($this->_getStoreId($store));
82:
83: $category->addData(array('path'=>implode('/',$parent_category->getPathIds())));
84:
85: $category ->setAttributeSetId($category->getDefaultAttributeSetId());
86:
87:
88: foreach ($category->getAttributes() as $attribute) {
89: $_attrCode = $attribute->getAttributeCode();
90: if ($this->_isAllowedAttribute($attribute)
91: && isset($categoryData->$_attrCode)) {
92: $category->setData(
93: $attribute->getAttributeCode(),
94: $categoryData->$_attrCode
95: );
96: }
97: }
98: $category->setParentId($parent_category->getId());
99: try {
100: $validate = $category->validate();
101: if ($validate !== true) {
102: foreach ($validate as $code => $error) {
103: if ($error === true) {
104: Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $code));
105: }
106: else {
107: Mage::throwException($error);
108: }
109: }
110: }
111:
112: $category->save();
113: }
114: catch (Mage_Core_Exception $e) {
115: $this->_fault('data_invalid', $e->getMessage());
116: }
117:
118: return $category->getId();
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128:
129: public function update($categoryId, $categoryData, $store = null)
130: {
131: $category = $this->_initCategory($categoryId, $store);
132:
133: foreach ($category->getAttributes() as $attribute) {
134: $_attrCode = $attribute->getAttributeCode();
135: if ($this->_isAllowedAttribute($attribute)
136: && isset($categoryData->$_attrCode)) {
137: $category->setData(
138: $attribute->getAttributeCode(),
139: $categoryData->$_attrCode
140: );
141: }
142: }
143:
144: try {
145: $validate = $category->validate();
146: if ($validate !== true) {
147: foreach ($validate as $code => $error) {
148: if ($error === true) {
149: Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $code));
150: }
151: else {
152: Mage::throwException($error);
153: }
154: }
155: }
156: $category->save();
157: } catch (Mage_Core_Exception $e) {
158: $this->_fault('data_invalid', $e->getMessage());
159: } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
160: $this->_fault('data_invalid', $e->getMessage());
161: }
162:
163: return true;
164: }
165: }
166: