/app/code/core/Mage/back_Customer/Model/Address/Api.php

https://bitbucket.org/acidel/buykoala · PHP · 237 lines · 130 code · 42 blank · 65 comment · 19 complexity · d2fae472cc2c7c135a3016f9c828df35 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer address api
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_Model_Address_Api extends Mage_Customer_Model_Api_Resource
  34. {
  35. protected $_mapAttributes = array(
  36. 'customer_address_id' => 'entity_id'
  37. );
  38. public function __construct()
  39. {
  40. $this->_ignoredAttributeCodes[] = 'parent_id';
  41. }
  42. /**
  43. * Retrive customer addresses list
  44. *
  45. * @param int $customerId
  46. * @return array
  47. */
  48. public function items($customerId)
  49. {
  50. $customer = Mage::getModel('customer/customer')
  51. ->load($customerId);
  52. /* @var $customer Mage_Customer_Model_Customer */
  53. if (!$customer->getId()) {
  54. $this->_fault('customer_not_exists');
  55. }
  56. $result = array();
  57. foreach ($customer->getAddresses() as $address) {
  58. $data = $address->toArray();
  59. $row = array();
  60. foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
  61. $row[$attributeAlias] = isset($data[$attributeCode]) ? $data[$attributeCode] : null;
  62. }
  63. foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
  64. if (isset($data[$attributeCode])) {
  65. $row[$attributeCode] = $data[$attributeCode];
  66. }
  67. }
  68. $row['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
  69. $row['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();
  70. $result[] = $row;
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Create new address for customer
  76. *
  77. * @param int $customerId
  78. * @param array $addressData
  79. * @return int
  80. */
  81. public function create($customerId, $addressData)
  82. {
  83. $customer = Mage::getModel('customer/customer')
  84. ->load($customerId);
  85. /* @var $customer Mage_Customer_Model_Customer */
  86. if (!$customer->getId()) {
  87. $this->_fault('customer_not_exists');
  88. }
  89. $address = Mage::getModel('customer/address');
  90. foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
  91. if (isset($addressData[$attributeCode])) {
  92. $address->setData($attributeCode, $addressData[$attributeCode]);
  93. }
  94. }
  95. if (isset($addressData['is_default_billing'])) {
  96. $address->setIsDefaultBilling($addressData['is_default_billing']);
  97. }
  98. if (isset($addressData['is_default_shipping'])) {
  99. $address->setIsDefaultShipping($addressData['is_default_shipping']);
  100. }
  101. $address->setCustomerId($customer->getId());
  102. $valid = $address->validate();
  103. if (is_array($valid)) {
  104. $this->_fault('data_invalid', implode("\n", $valid));
  105. }
  106. try {
  107. $address->save();
  108. } catch (Mage_Core_Exception $e) {
  109. $this->_fault('data_invalid', $e->getMessage());
  110. }
  111. return $address->getId();
  112. }
  113. /**
  114. * Retrieve address data
  115. *
  116. * @param int $addressId
  117. * @return array
  118. */
  119. public function info($addressId)
  120. {
  121. $address = Mage::getModel('customer/address')
  122. ->load($addressId);
  123. if (!$address->getId()) {
  124. $this->_fault('not_exists');
  125. }
  126. $result = array();
  127. foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
  128. $result[$attributeAlias] = $address->getData($attributeCode);
  129. }
  130. foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
  131. $result[$attributeCode] = $address->getData($attributeCode);
  132. }
  133. if ($customer = $address->getCustomer()) {
  134. $result['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
  135. $result['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Update address data
  141. *
  142. * @param int $addressId
  143. * @param array $addressData
  144. * @return boolean
  145. */
  146. public function update($addressId, $addressData)
  147. {
  148. $address = Mage::getModel('customer/address')
  149. ->load($addressId);
  150. if (!$address->getId()) {
  151. $this->_fault('not_exists');
  152. }
  153. foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
  154. if (isset($addressData[$attributeCode])) {
  155. $address->setData($attributeCode, $addressData[$attributeCode]);
  156. }
  157. }
  158. if (isset($addressData['is_default_billing'])) {
  159. $address->setIsDefaultBilling($addressData['is_default_billing']);
  160. }
  161. if (isset($addressData['is_default_shipping'])) {
  162. $address->setIsDefaultShipping($addressData['is_default_shipping']);
  163. }
  164. $valid = $address->validate();
  165. if (is_array($valid)) {
  166. $this->_fault('data_invalid', implode("\n", $valid));
  167. }
  168. try {
  169. $address->save();
  170. } catch (Mage_Core_Exception $e) {
  171. $this->_fault('data_invalid', $e->getMessage());
  172. }
  173. return true;
  174. }
  175. /**
  176. * Delete address
  177. *
  178. * @param int $addressId
  179. * @return boolean
  180. */
  181. public function delete($addressId)
  182. {
  183. $address = Mage::getModel('customer/address')
  184. ->load($addressId);
  185. if (!$address->getId()) {
  186. $this->_fault('not_exists');
  187. }
  188. try {
  189. $address->delete();
  190. } catch (Mage_Core_Exception $e) {
  191. $this->_fault('not_deleted', $e->getMessage());
  192. }
  193. return true;
  194. }
  195. } // Class Mage_Customer_Model_Address_Api End