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_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39: 40:
41: protected $_entityTypeId;
42:
43: 44: 45:
46: public function __construct()
47: {
48: $this->_storeIdSessionField = 'product_store_id';
49: $this->_ignoredAttributeCodes[] = 'type_id';
50: $this->_ignoredAttributeTypes[] = 'gallery';
51: $this->_ignoredAttributeTypes[] = 'media_image';
52: $this->_entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
53: }
54:
55: 56: 57: 58: 59: 60:
61: public function items($setId)
62: {
63: $attributes = Mage::getModel('catalog/product')->getResource()
64: ->loadAllAttributes()
65: ->getSortedAttributes($setId);
66: $result = array();
67:
68: foreach ($attributes as $attribute) {
69:
70: if ((!$attribute->getId() || $attribute->isInSet($setId))
71: && $this->_isAllowedAttribute($attribute)) {
72:
73: if (!$attribute->getId() || $attribute->isScopeGlobal()) {
74: $scope = 'global';
75: } elseif ($attribute->isScopeWebsite()) {
76: $scope = 'website';
77: } else {
78: $scope = 'store';
79: }
80:
81: $result[] = array(
82: 'attribute_id' => $attribute->getId(),
83: 'code' => $attribute->getAttributeCode(),
84: 'type' => $attribute->getFrontendInput(),
85: 'required' => $attribute->getIsRequired(),
86: 'scope' => $scope
87: );
88: }
89: }
90:
91: return $result;
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: public function options($attributeId, $store = null)
102: {
103: $storeId = $this->_getStoreId($store);
104: $attribute = Mage::getModel('catalog/product')
105: ->setStoreId($storeId)
106: ->getResource()
107: ->getAttribute($attributeId);
108:
109:
110: if (!$attribute) {
111: $this->_fault('not_exists');
112: }
113: $options = array();
114: if ($attribute->usesSource()) {
115: foreach ($attribute->getSource()->getAllOptions() as $optionId => $optionValue) {
116: if (is_array($optionValue)) {
117: $options[] = $optionValue;
118: } else {
119: $options[] = array(
120: 'value' => $optionId,
121: 'label' => $optionValue
122: );
123: }
124: }
125: }
126:
127: return $options;
128: }
129:
130: 131: 132: 133: 134:
135: public function types()
136: {
137: return Mage::getModel('catalog/product_attribute_source_inputtype')->toOptionArray();
138: }
139:
140: 141: 142: 143: 144: 145:
146: public function create($data)
147: {
148:
149: $model = Mage::getModel('catalog/resource_eav_attribute');
150:
151: $helper = Mage::helper('catalog/product');
152:
153: if (empty($data['attribute_code']) || !is_array($data['frontend_label'])) {
154: $this->_fault('invalid_parameters');
155: }
156:
157:
158: if (!preg_match('/^[a-z][a-z_0-9]{0,254}$/', $data['attribute_code'])) {
159: $this->_fault('invalid_code');
160: }
161:
162:
163: $allowedTypes = array();
164: foreach ($this->types() as $type) {
165: $allowedTypes[] = $type['value'];
166: }
167: if (!in_array($data['frontend_input'], $allowedTypes)) {
168: $this->_fault('invalid_frontend_input');
169: }
170:
171: $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
172: $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
173: if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
174: $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
175: }
176:
177: $this->_prepareDataForSave($data);
178:
179: $model->addData($data);
180: $model->setEntityTypeId($this->_entityTypeId);
181: $model->setIsUserDefined(1);
182:
183: try {
184: $model->save();
185:
186: Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
187: } catch (Exception $e) {
188: $this->_fault('unable_to_save', $e->getMessage());
189: }
190:
191: return (int) $model->getId();
192: }
193:
194: 195: 196: 197: 198: 199: 200:
201: public function update($attribute, $data)
202: {
203: $model = $this->_getAttribute($attribute);
204:
205: if ($model->getEntityTypeId() != $this->_entityTypeId) {
206: $this->_fault('can_not_edit');
207: }
208:
209: $data['attribute_code'] = $model->getAttributeCode();
210: $data['is_user_defined'] = $model->getIsUserDefined();
211: $data['frontend_input'] = $model->getFrontendInput();
212:
213: $this->_prepareDataForSave($data);
214:
215: $model->addData($data);
216: try {
217: $model->save();
218:
219: Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
220: return true;
221: } catch (Exception $e) {
222: $this->_fault('unable_to_save', $e->getMessage());
223: }
224: }
225:
226: 227: 228: 229: 230: 231:
232: public function remove($attribute)
233: {
234: $model = $this->_getAttribute($attribute);
235:
236: if ($model->getEntityTypeId() != $this->_entityTypeId) {
237: $this->_fault('can_not_delete');
238: }
239:
240: try {
241: $model->delete();
242: return true;
243: } catch (Exception $e) {
244: $this->_fault('can_not_delete', $e->getMessage());
245: }
246: }
247:
248: 249: 250: 251: 252: 253:
254: public function info($attribute)
255: {
256: $model = $this->_getAttribute($attribute);
257:
258: if ($model->isScopeGlobal()) {
259: $scope = 'global';
260: } elseif ($model->isScopeWebsite()) {
261: $scope = 'website';
262: } else {
263: $scope = 'store';
264: }
265:
266: $frontendLabels = array(
267: array(
268: 'store_id' => 0,
269: 'label' => $model->getFrontendLabel()
270: )
271: );
272: foreach ($model->getStoreLabels() as $store_id => $label) {
273: $frontendLabels[] = array(
274: 'store_id' => $store_id,
275: 'label' => $label
276: );
277: }
278:
279: $result = array(
280: 'attribute_id' => $model->getId(),
281: 'attribute_code' => $model->getAttributeCode(),
282: 'frontend_input' => $model->getFrontendInput(),
283: 'default_value' => $model->getDefaultValue(),
284: 'is_unique' => $model->getIsUnique(),
285: 'is_required' => $model->getIsRequired(),
286: 'apply_to' => $model->getApplyTo(),
287: 'is_configurable' => $model->getIsConfigurable(),
288: 'is_searchable' => $model->getIsSearchable(),
289: 'is_visible_in_advanced_search' => $model->getIsVisibleInAdvancedSearch(),
290: 'is_comparable' => $model->getIsComparable(),
291: 'is_used_for_promo_rules' => $model->getIsUsedForPromoRules(),
292: 'is_visible_on_front' => $model->getIsVisibleOnFront(),
293: 'used_in_product_listing' => $model->getUsedInProductListing(),
294: 'frontend_label' => $frontendLabels
295: );
296: if ($model->getFrontendInput() != 'price') {
297: $result['scope'] = $scope;
298: }
299:
300:
301: switch ($model->getFrontendInput()) {
302: case 'text':
303: $result['additional_fields'] = array(
304: 'frontend_class' => $model->getFrontendClass(),
305: 'is_html_allowed_on_front' => $model->getIsHtmlAllowedOnFront(),
306: 'used_for_sort_by' => $model->getUsedForSortBy()
307: );
308: break;
309: case 'textarea':
310: $result['additional_fields'] = array(
311: 'is_wysiwyg_enabled' => $model->getIsWysiwygEnabled(),
312: 'is_html_allowed_on_front' => $model->getIsHtmlAllowedOnFront(),
313: );
314: break;
315: case 'date':
316: case 'boolean':
317: $result['additional_fields'] = array(
318: 'used_for_sort_by' => $model->getUsedForSortBy()
319: );
320: break;
321: case 'multiselect':
322: $result['additional_fields'] = array(
323: 'is_filterable' => $model->getIsFilterable(),
324: 'is_filterable_in_search' => $model->getIsFilterableInSearch(),
325: 'position' => $model->getPosition()
326: );
327: break;
328: case 'select':
329: case 'price':
330: $result['additional_fields'] = array(
331: 'is_filterable' => $model->getIsFilterable(),
332: 'is_filterable_in_search' => $model->getIsFilterableInSearch(),
333: 'position' => $model->getPosition(),
334: 'used_for_sort_by' => $model->getUsedForSortBy()
335: );
336: break;
337: default:
338: $result['additional_fields'] = array();
339: break;
340: }
341:
342:
343: $options = $this->options($model->getId());
344:
345: if ($model->getFrontendInput() != 'boolean') {
346: array_shift($options);
347: }
348:
349: if (count($options) > 0) {
350: $result['options'] = $options;
351: }
352:
353: return $result;
354: }
355:
356: 357: 358: 359: 360: 361: 362:
363: public function addOption($attribute, $data)
364: {
365: $model = $this->_getAttribute($attribute);
366:
367: if (!$model->usesSource()) {
368: $this->_fault('invalid_frontend_input');
369: }
370:
371:
372: $helperCatalog = Mage::helper('catalog');
373:
374: $optionLabels = array();
375: foreach ($data['label'] as $label) {
376: $storeId = $label['store_id'];
377: $labelText = $helperCatalog->stripTags($label['value']);
378: if (is_array($storeId)) {
379: foreach ($storeId as $multiStoreId) {
380: $optionLabels[$multiStoreId] = $labelText;
381: }
382: } else {
383: $optionLabels[$storeId] = $labelText;
384: }
385: }
386:
387:
388:
389: $modelData = array(
390: 'option' => array(
391: 'value' => array(
392: 'option_1' => $optionLabels
393: ),
394: 'order' => array(
395: 'option_1' => (int) $data['order']
396: )
397: )
398: );
399: if ($data['is_default']) {
400: $modelData['default'][] = 'option_1';
401: }
402:
403: $model->addData($modelData);
404: try {
405: $model->save();
406: } catch (Exception $e) {
407: $this->_fault('unable_to_add_option', $e->getMessage());
408: }
409:
410: return true;
411: }
412:
413: 414: 415: 416: 417: 418: 419:
420: public function removeOption($attribute, $optionId)
421: {
422: $model = $this->_getAttribute($attribute);
423:
424: if (!$model->usesSource()) {
425: $this->_fault('invalid_frontend_input');
426: }
427:
428:
429:
430:
431: $modelData = array(
432: 'option' => array(
433: 'value' => array(
434: $optionId => array()
435: ),
436: 'delete' => array(
437: $optionId => '1'
438: )
439: )
440: );
441: $model->addData($modelData);
442: try {
443: $model->save();
444: } catch (Exception $e) {
445: $this->_fault('unable_to_remove_option', $e->getMessage());
446: }
447:
448: return true;
449: }
450:
451: 452: 453: 454: 455: 456:
457: protected function _prepareDataForSave(&$data)
458: {
459:
460: $helperCatalog = Mage::helper('catalog');
461:
462: if ($data['scope'] == 'global') {
463: $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL;
464: } else if ($data['scope'] == 'website') {
465: $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE;
466: } else {
467: $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE;
468: }
469: if (!isset($data['is_configurable'])) {
470: $data['is_configurable'] = 0;
471: }
472: if (!isset($data['is_filterable'])) {
473: $data['is_filterable'] = 0;
474: }
475: if (!isset($data['is_filterable_in_search'])) {
476: $data['is_filterable_in_search'] = 0;
477: }
478: if (!isset($data['apply_to'])) {
479: $data['apply_to'] = array();
480: }
481:
482: if (isset($data['frontend_label']) && is_array($data['frontend_label'])) {
483: $labels = array();
484: foreach ($data['frontend_label'] as $label) {
485: $storeId = $label['store_id'];
486: $labelText = $helperCatalog->stripTags($label['label']);
487: $labels[$storeId] = $labelText;
488: }
489: $data['frontend_label'] = $labels;
490: }
491:
492: if (isset($data['additional_fields']) && is_array($data['additional_fields'])) {
493: $data = array_merge($data, $data['additional_fields']);
494: unset($data['additional_fields']);
495: }
496:
497: if (!empty($data['default_value'])) {
498: $data['default_value'] = $helperCatalog->stripTags($data['default_value']);
499: }
500: }
501:
502: 503: 504: 505: 506: 507:
508: protected function _getAttribute($attribute)
509: {
510: $model = Mage::getResourceModel('catalog/eav_attribute')
511: ->setEntityTypeId($this->_entityTypeId);
512:
513: if (is_numeric($attribute)) {
514: $model->load(intval($attribute));
515: } else {
516: $model->load($attribute, 'attribute_code');
517: }
518:
519: if (!$model->getId()) {
520: $this->_fault('not_exists');
521: }
522:
523: return $model;
524: }
525:
526: }
527: