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: abstract class Mage_Customer_Model_Api2_Customer_Address_Rest extends Mage_Customer_Model_Api2_Customer_Address
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _create(array $data)
44: {
45:
46: $customer = $this->_loadCustomerById($this->getRequest()->getParam('customer_id'));
47: $validator = $this->_getValidator();
48:
49: $data = $validator->filter($data);
50: if (!$validator->isValidData($data) || !$validator->isValidDataForCreateAssociationWithCountry($data)) {
51: foreach ($validator->getErrors() as $error) {
52: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
53: }
54: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
55: }
56:
57: if (isset($data['region']) && isset($data['country_id'])) {
58: $data['region'] = $this->_getRegionIdByNameOrCode($data['region'], $data['country_id']);
59: }
60:
61:
62: $address = Mage::getModel('customer/address');
63: $address->setData($data);
64: $address->setCustomer($customer);
65:
66: try {
67: $address->save();
68: } catch (Mage_Core_Exception $e) {
69: $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
70: } catch (Exception $e) {
71: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
72: }
73: return $this->_getLocation($address);
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function _retrieve()
83: {
84:
85: $address = $this->_loadCustomerAddressById($this->getRequest()->getParam('id'));
86: $addressData = $address->getData();
87: $addressData['street'] = $address->getStreet();
88: return $addressData;
89: }
90:
91: 92: 93: 94: 95:
96: protected function _retrieveCollection()
97: {
98: $data = array();
99:
100: foreach ($this->_getCollectionForRetrieve() as $address) {
101: $addressData = $address->getData();
102: $addressData['street'] = $address->getStreet();
103: $data[] = array_merge($addressData, $this->_getDefaultAddressesInfo($address));
104: }
105: return $data;
106: }
107:
108: 109: 110: 111: 112:
113: protected function _getCollectionForRetrieve()
114: {
115:
116: $customer = $this->_loadCustomerById($this->getRequest()->getParam('customer_id'));
117:
118:
119: $collection = $customer->getAddressesCollection();
120:
121: $this->_applyCollectionModifiers($collection);
122: return $collection;
123: }
124:
125: 126: 127: 128: 129: 130:
131: protected function _getDefaultAddressesInfo(Mage_Customer_Model_Address $address)
132: {
133: return array(
134: 'is_default_billing' => (int)$this->_isDefaultBillingAddress($address),
135: 'is_default_shipping' => (int)$this->_isDefaultShippingAddress($address)
136: );
137: }
138:
139: 140: 141: 142: 143: 144:
145: protected function _update(array $data)
146: {
147:
148: $address = $this->_loadCustomerAddressById($this->getRequest()->getParam('id'));
149: $validator = $this->_getValidator();
150:
151: $data = $validator->filter($data);
152: if (!$validator->isValidData($data, true)
153: || !$validator->isValidDataForChangeAssociationWithCountry($address, $data)) {
154: foreach ($validator->getErrors() as $error) {
155: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
156: }
157: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
158: }
159: if (isset($data['region'])) {
160: $data['region'] = $this->_getRegionIdByNameOrCode(
161: $data['region'], isset($data['country_id']) ? $data['country_id'] : $address->getCountryId()
162: );
163: $data['region_id'] = null;
164: }
165: $address->addData($data);
166:
167: try {
168: $address->save();
169: } catch (Mage_Core_Exception $e) {
170: $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
171: } catch (Exception $e) {
172: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
173: }
174: }
175:
176: 177: 178:
179: protected function _delete()
180: {
181:
182: $address = $this->_loadCustomerAddressById($this->getRequest()->getParam('id'));
183:
184: if ($this->_isDefaultBillingAddress($address) || $this->_isDefaultShippingAddress($address)) {
185: $this->_critical(
186: 'Address is default for customer so is not allowed to be deleted',
187: Mage_Api2_Model_Server::HTTP_BAD_REQUEST
188: );
189: }
190: try {
191: $address->delete();
192: } catch (Mage_Core_Exception $e) {
193: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
194: } catch (Exception $e) {
195: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
196: }
197: }
198: }
199: