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_Customer_Model_Customer_Api extends Mage_Customer_Model_Api_Resource
35: {
36: protected $_mapAttributes = array(
37: 'customer_id' => 'entity_id'
38: );
39: 40: 41: 42: 43: 44: 45:
46: protected function _prepareData($data)
47: {
48: foreach ($this->_mapAttributes as $attributeAlias=>$attributeCode) {
49: if(isset($data[$attributeAlias]))
50: {
51: $data[$attributeCode] = $data[$attributeAlias];
52: unset($data[$attributeAlias]);
53: }
54: }
55: return $data;
56: }
57:
58: 59: 60: 61: 62: 63:
64: public function create($customerData)
65: {
66: $customerData = $this->_prepareData($customerData);
67: try {
68: $customer = Mage::getModel('customer/customer')
69: ->setData($customerData)
70: ->save();
71: } catch (Mage_Core_Exception $e) {
72: $this->_fault('data_invalid', $e->getMessage());
73: }
74: return $customer->getId();
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: public function info($customerId, $attributes = null)
85: {
86: $customer = Mage::getModel('customer/customer')->load($customerId);
87:
88: if (!$customer->getId()) {
89: $this->_fault('not_exists');
90: }
91:
92: if (!is_null($attributes) && !is_array($attributes)) {
93: $attributes = array($attributes);
94: }
95:
96: $result = array();
97:
98: foreach ($this->_mapAttributes as $attributeAlias=>$attributeCode) {
99: $result[$attributeAlias] = $customer->getData($attributeCode);
100: }
101:
102: foreach ($this->getAllowedAttributes($customer, $attributes) as $attributeCode=>$attribute) {
103: $result[$attributeCode] = $customer->getData($attributeCode);
104: }
105:
106: return $result;
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function items($filters)
116: {
117: $collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
118:
119: $apiHelper = Mage::helper('api');
120: $filters = $apiHelper->parseFilters($filters, $this->_mapAttributes);
121: try {
122: foreach ($filters as $field => $value) {
123: $collection->addFieldToFilter($field, $value);
124: }
125: } catch (Mage_Core_Exception $e) {
126: $this->_fault('filters_invalid', $e->getMessage());
127: }
128: $result = array();
129: foreach ($collection as $customer) {
130: $data = $customer->toArray();
131: $row = array();
132: foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
133: $row[$attributeAlias] = (isset($data[$attributeCode]) ? $data[$attributeCode] : null);
134: }
135: foreach ($this->getAllowedAttributes($customer) as $attributeCode => $attribute) {
136: if (isset($data[$attributeCode])) {
137: $row[$attributeCode] = $data[$attributeCode];
138: }
139: }
140: $result[] = $row;
141: }
142:
143: return $result;
144: }
145:
146: 147: 148: 149: 150: 151: 152:
153: public function update($customerId, $customerData)
154: {
155: $customerData = $this->_prepareData($customerData);
156:
157: $customer = Mage::getModel('customer/customer')->load($customerId);
158:
159: if (!$customer->getId()) {
160: $this->_fault('not_exists');
161: }
162:
163: foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
164: if (isset($customerData[$attributeCode])) {
165: $customer->setData($attributeCode, $customerData[$attributeCode]);
166: }
167: }
168:
169: $customer->save();
170: return true;
171: }
172:
173: 174: 175: 176: 177: 178:
179: public function delete($customerId)
180: {
181: $customer = Mage::getModel('customer/customer')->load($customerId);
182:
183: if (!$customer->getId()) {
184: $this->_fault('not_exists');
185: }
186:
187: try {
188: $customer->delete();
189: } catch (Mage_Core_Exception $e) {
190: $this->_fault('not_deleted', $e->getMessage());
191: }
192:
193: return true;
194: }
195:
196: }
197: