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_Product_Attribute_Set_Api extends Mage_Api_Model_Resource_Abstract
35: {
36: 37: 38: 39: 40:
41: public function items()
42: {
43: $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
44: $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
45: ->setEntityTypeFilter($entityType->getId());
46:
47: $result = array();
48: foreach ($collection as $attributeSet) {
49: $result[] = array(
50: 'set_id' => $attributeSet->getId(),
51: 'name' => $attributeSet->getAttributeSetName()
52: );
53:
54: }
55:
56: return $result;
57: }
58:
59: 60: 61: 62: 63: 64: 65:
66: public function create($attributeSetName, $skeletonSetId)
67: {
68:
69: if (!Mage::getModel('eav/entity_attribute_set')->load($skeletonSetId)->getId()){
70: $this->_fault('invalid_skeleton_set_id');
71: }
72:
73: $entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
74:
75: $attributeSet = Mage::getModel('eav/entity_attribute_set')
76: ->setEntityTypeId($entityTypeId)
77: ->setAttributeSetName($attributeSetName);
78: try {
79:
80: $attributeSet->validate();
81:
82: $attributeSet->save();
83: $attributeSet->initFromSkeleton($skeletonSetId)->save();
84: } catch (Mage_Eav_Exception $e) {
85: $this->_fault('invalid_data', $e->getMessage());
86: } catch (Exception $e) {
87: $this->_fault('create_attribute_set_error', $e->getMessage());
88: }
89: return (int)$attributeSet->getId();
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function remove($attributeSetId, $forceProductsRemove = false)
100: {
101:
102: if (!$forceProductsRemove) {
103:
104: $catalogProductsCollection = Mage::getModel('catalog/product')->getCollection()
105: ->addFieldToFilter('attribute_set_id', $attributeSetId);
106: if (count($catalogProductsCollection)) {
107: $this->_fault('attribute_set_has_related_products');
108: }
109: }
110: $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
111:
112: if (!$attributeSet->getId()){
113: $this->_fault('invalid_attribute_set_id');
114: }
115: try {
116: $attributeSet->delete();
117: } catch (Exception $e) {
118: $this->_fault('remove_attribute_set_error', $e->getMessage());
119: }
120: return true;
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130: 131:
132: public function attributeAdd($attributeId, $attributeSetId, $attributeGroupId = null, $sortOrder = '0')
133: {
134:
135:
136: $attribute = Mage::getModel('eav/entity_attribute')->load($attributeId);
137: if (!$attribute->getId()) {
138: $this->_fault('invalid_attribute_id');
139: }
140:
141:
142: $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
143: if (!$attributeSet->getId()) {
144: $this->_fault('invalid_attribute_set_id');
145: }
146: if (!empty($attributeGroupId)) {
147:
148: if (!Mage::getModel('eav/entity_attribute_group')->load($attributeGroupId)->getId()) {
149: $this->_fault('invalid_attribute_group_id');
150: }
151: } else {
152:
153: $attributeGroupId = $attributeSet->getDefaultGroupId();
154: }
155: $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet();
156: if ($attribute->getEntityAttributeId()) {
157: $this->_fault('attribute_is_already_in_set');
158: }
159: try {
160: $attribute->setEntityTypeId($attributeSet->getEntityTypeId())
161: ->setAttributeSetId($attributeSetId)
162: ->setAttributeGroupId($attributeGroupId)
163: ->setSortOrder($sortOrder)
164: ->save();
165: } catch (Exception $e) {
166: $this->_fault('add_attribute_error', $e->getMessage());
167: }
168: return true;
169: }
170:
171: 172: 173: 174: 175: 176: 177:
178: public function attributeRemove($attributeId, $attributeSetId)
179: {
180:
181:
182: $attribute = Mage::getModel('eav/entity_attribute')->load($attributeId);
183: if (!$attribute->getId()) {
184: $this->_fault('invalid_attribute_id');
185: }
186:
187:
188: $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
189: if (!$attributeSet->getId()) {
190: $this->_fault('invalid_attribute_set_id');
191: }
192:
193: $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet();
194: if (!$attribute->getEntityAttributeId()) {
195: $this->_fault('attribute_is_not_in_set');
196: }
197: try {
198:
199:
200: $attribute->deleteEntity();
201: } catch (Exception $e) {
202: $this->_fault('remove_attribute_error', $e->getMessage());
203: }
204:
205: return true;
206: }
207:
208: 209: 210: 211: 212: 213: 214:
215: public function groupAdd($attributeSetId, $groupName)
216: {
217:
218: $group = Mage::getModel('eav/entity_attribute_group');
219: $group->setAttributeSetId($attributeSetId)
220: ->setAttributeGroupName(
221: Mage::helper('catalog')->stripTags($groupName)
222: );
223: if ($group->itemExists()) {
224: $this->_fault('group_already_exists');
225: }
226: try {
227: $group->save();
228: } catch (Exception $e) {
229: $this->_fault('group_add_error', $e->getMessage());
230: }
231: return (int)$group->getId();
232: }
233:
234: 235: 236: 237: 238: 239: 240:
241: public function groupRename($groupId, $groupName)
242: {
243: $model = Mage::getModel('eav/entity_attribute_group')->load($groupId);
244:
245: if (!$model->getAttributeGroupName()) {
246: $this->_fault('invalid_attribute_group_id');
247: }
248:
249: $model->setAttributeGroupName(
250: Mage::helper('catalog')->stripTags($groupName)
251: );
252: try {
253: $model->save();
254: } catch (Exception $e) {
255: $this->_fault('group_rename_error', $e->getMessage());
256: }
257: return true;
258: }
259:
260: 261: 262: 263: 264: 265:
266: public function groupRemove($attributeGroupId)
267: {
268:
269: $group = Mage::getModel('catalog/product_attribute_group')->load($attributeGroupId);
270: if (!$group->getId()) {
271: $this->_fault('invalid_attribute_group_id');
272: }
273: if ($group->hasConfigurableAttributes()) {
274: $this->_fault('group_has_configurable_attributes');
275: }
276: if ($group->hasSystemAttributes()) {
277: $this->_fault('group_has_system_attributes');
278: }
279: try {
280: $group->delete();
281: } catch (Exception $e) {
282: $this->_fault('group_remove_error', $e->getMessage());
283: }
284: return true;
285: }
286:
287: }
288: