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_Address_Api extends Mage_Customer_Model_Api_Resource
35: {
36: protected $_mapAttributes = array(
37: 'customer_address_id' => 'entity_id'
38: );
39:
40: public function __construct()
41: {
42: $this->_ignoredAttributeCodes[] = 'parent_id';
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function items($customerId)
52: {
53: $customer = Mage::getModel('customer/customer')
54: ->load($customerId);
55:
56:
57: if (!$customer->getId()) {
58: $this->_fault('customer_not_exists');
59: }
60:
61: $result = array();
62: foreach ($customer->getAddresses() as $address) {
63: $data = $address->toArray();
64: $row = array();
65:
66: foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
67: $row[$attributeAlias] = isset($data[$attributeCode]) ? $data[$attributeCode] : null;
68: }
69:
70: foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
71: if (isset($data[$attributeCode])) {
72: $row[$attributeCode] = $data[$attributeCode];
73: }
74: }
75:
76: $row['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
77: $row['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();
78:
79: $result[] = $row;
80:
81: }
82:
83: return $result;
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: public function create($customerId, $addressData)
94: {
95: $customer = Mage::getModel('customer/customer')
96: ->load($customerId);
97:
98:
99: if (!$customer->getId()) {
100: $this->_fault('customer_not_exists');
101: }
102:
103: $address = Mage::getModel('customer/address');
104:
105: foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
106: if (isset($addressData[$attributeCode])) {
107: $address->setData($attributeCode, $addressData[$attributeCode]);
108: }
109: }
110:
111: if (isset($addressData['is_default_billing'])) {
112: $address->setIsDefaultBilling($addressData['is_default_billing']);
113: }
114:
115: if (isset($addressData['is_default_shipping'])) {
116: $address->setIsDefaultShipping($addressData['is_default_shipping']);
117: }
118:
119: $address->setCustomerId($customer->getId());
120:
121: $valid = $address->validate();
122:
123: if (is_array($valid)) {
124: $this->_fault('data_invalid', implode("\n", $valid));
125: }
126:
127: try {
128: $address->save();
129: } catch (Mage_Core_Exception $e) {
130: $this->_fault('data_invalid', $e->getMessage());
131: }
132:
133: return $address->getId();
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function info($addressId)
143: {
144: $address = Mage::getModel('customer/address')
145: ->load($addressId);
146:
147: if (!$address->getId()) {
148: $this->_fault('not_exists');
149: }
150:
151: $result = array();
152:
153: foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
154: $result[$attributeAlias] = $address->getData($attributeCode);
155: }
156:
157: foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
158: $result[$attributeCode] = $address->getData($attributeCode);
159: }
160:
161:
162: if ($customer = $address->getCustomer()) {
163: $result['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
164: $result['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();
165: }
166:
167: return $result;
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: public function update($addressId, $addressData)
178: {
179: $address = Mage::getModel('customer/address')
180: ->load($addressId);
181:
182: if (!$address->getId()) {
183: $this->_fault('not_exists');
184: }
185:
186: foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
187: if (isset($addressData[$attributeCode])) {
188: $address->setData($attributeCode, $addressData[$attributeCode]);
189: }
190: }
191:
192: if (isset($addressData['is_default_billing'])) {
193: $address->setIsDefaultBilling($addressData['is_default_billing']);
194: }
195:
196: if (isset($addressData['is_default_shipping'])) {
197: $address->setIsDefaultShipping($addressData['is_default_shipping']);
198: }
199:
200: $valid = $address->validate();
201: if (is_array($valid)) {
202: $this->_fault('data_invalid', implode("\n", $valid));
203: }
204:
205: try {
206: $address->save();
207: } catch (Mage_Core_Exception $e) {
208: $this->_fault('data_invalid', $e->getMessage());
209: }
210:
211: return true;
212: }
213:
214: 215: 216: 217: 218: 219:
220: public function delete($addressId)
221: {
222: $address = Mage::getModel('customer/address')
223: ->load($addressId);
224:
225: if (!$address->getId()) {
226: $this->_fault('not_exists');
227: }
228:
229: try {
230: $address->delete();
231: } catch (Mage_Core_Exception $e) {
232: $this->_fault('not_deleted', $e->getMessage());
233: }
234:
235: return true;
236: }
237: }
238: