00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Customer_Model_Address_Api extends Mage_Customer_Model_Api_Resource
00035 {
00036 protected $_mapAttributes = array(
00037 'customer_id' => 'entity_id'
00038 );
00039
00040 public function __construct()
00041 {
00042 $this->_ignoredAttributeCodes[] = 'parent_id';
00043 }
00044
00045
00046
00047
00048
00049
00050
00051 public function items($customerId)
00052 {
00053 $customer = Mage::getModel('customer/customer')
00054 ->load($customerId);
00055
00056
00057 if (!$customer->getId()) {
00058 $this->_fault('customer_not_exists');
00059 }
00060
00061 $result = array();
00062 foreach ($customer->getAddresses() as $address) {
00063 $data = $address->toArray();
00064 $row = array();
00065
00066 foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
00067 $row[$attributeAlias] = isset($data[$attributeCode]) ? $data[$attributeCode] : null;
00068 }
00069
00070 foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
00071 if (isset($data[$attributeCode])) {
00072 $row[$attributeCode] = $data[$attributeCode];
00073 }
00074 }
00075
00076 $row['is_default_billing'] = $customer->getDefaultBillingAddress() == $address->getId();
00077 $row['is_default_shipping'] = $customer->getDefaultShippingAddress() == $address->getId();
00078
00079 $result[] = $row;
00080
00081 }
00082
00083 return $result;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093 public function create($customerId, $addressData)
00094 {
00095 $customer = Mage::getModel('customer/customer')
00096 ->load($customerId);
00097
00098
00099 if (!$customer->getId()) {
00100 $this->_fault('customer_not_exists');
00101 }
00102
00103 $address = Mage::getModel('customer/address');
00104
00105 foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
00106 if (isset($addressData[$attributeCode])) {
00107 $address->setData($attributeCode, $addressData[$attributeCode]);
00108 }
00109 }
00110
00111 if (isset($addressData['is_default_billing'])) {
00112 $address->setIsDefaultBilling($addressData['is_default_billing']);
00113 }
00114
00115 if (isset($addressData['is_default_shipping'])) {
00116 $address->setIsDefaultShipping($addressData['is_default_shipping']);
00117 }
00118
00119 $address->setCustomerId($customer->getId());
00120
00121 $valid = $address->validate();
00122
00123 if (is_array($valid)) {
00124 $this->_fault('data_invalid', implode("\n", $valid));
00125 }
00126
00127 try {
00128 $address->save();
00129 } catch (Mage_Core_Exception $e) {
00130 $this->_fault('data_invalid', $e->getMessage());
00131 }
00132
00133 return $address->getId();
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 public function info($addressId)
00143 {
00144 $address = Mage::getModel('customer/address')
00145 ->load($addressId);
00146
00147 if (!$address->getId()) {
00148 $this->_fault('not_exists');
00149 }
00150
00151 $result = array();
00152
00153 foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
00154 $result[$attributeAlias] = $address->getData($attributeCode);
00155 }
00156
00157 foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
00158 $result[$attributeCode] = $address->getData($attributeCode);
00159 }
00160
00161
00162 if ($customer = $address->getCustomer()) {
00163 $result['is_default_billing'] = $customer->getDefaultBillingAddress() == $address->getId();
00164 $result['is_default_shipping'] = $customer->getDefaultShippingAddress() == $address->getId();
00165 }
00166
00167 return $result;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 public function update($addressId, $addressData)
00178 {
00179 $address = Mage::getModel('customer/address')
00180 ->load($addressId);
00181
00182 if (!$address->getId()) {
00183 $this->_fault('not_exists');
00184 }
00185
00186 foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
00187 if (isset($addressData[$attributeCode])) {
00188 $address->setData($attributeCode, $addressData[$attributeCode]);
00189 }
00190 }
00191
00192 if (isset($addressData['is_default_billing'])) {
00193 $address->setIsDefaultBilling($addressData['is_default_billing']);
00194 }
00195
00196 if (isset($addressData['is_default_shipping'])) {
00197 $address->setIsDefaultShipping($addressData['is_default_shipping']);
00198 }
00199
00200 $valid = $address->validate();
00201 if (is_array($valid)) {
00202 $this->_fault('data_invalid', implode("\n", $valid));
00203 }
00204
00205 try {
00206 $address->save();
00207 } catch (Mage_Core_Exception $e) {
00208 $this->_fault('data_invalid', $e->getMessage());
00209 }
00210
00211 return true;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 public function delete($addressId)
00221 {
00222 $address = Mage::getModel('customer/address')
00223 ->load($addressId);
00224
00225 if (!$address->getId()) {
00226 $this->_fault('not_exists');
00227 }
00228
00229 try {
00230 $address->delete();
00231 } catch (Mage_Core_Exception $e) {
00232 $this->_fault('not_deleted', $e->getMessage());
00233 }
00234
00235 return true;
00236 }
00237 }