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) 2012 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: /**
28: * API2 class for customer address
29: *
30: * @category Mage
31: * @package Mage_Customer
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Customer_Model_Api2_Customer_Address extends Mage_Api2_Model_Resource
35: {
36: /**
37: * Resource specific method to retrieve attributes' codes. May be overriden in child.
38: *
39: * @return array
40: */
41: protected function _getResourceAttributes()
42: {
43: return $this->getEavAttributes(Mage_Api2_Model_Auth_User_Admin::USER_TYPE != $this->getUserType());
44: }
45:
46: /**
47: * Get customer address resource validator instance
48: *
49: * @return Mage_Customer_Model_Api2_Customer_Address_Validator
50: */
51: protected function _getValidator()
52: {
53: return Mage::getModel('customer/api2_customer_address_validator', array('resource' => $this));
54: }
55:
56: /**
57: * Is specified address a default billing address?
58: *
59: * @param Mage_Customer_Model_Address $address
60: * @return bool
61: */
62: protected function _isDefaultBillingAddress(Mage_Customer_Model_Address $address)
63: {
64: return $address->getCustomer()->getDefaultBilling() == $address->getId();
65: }
66:
67: /**
68: * Is specified address a default shipping address?
69: *
70: * @param Mage_Customer_Model_Address $address
71: * @return bool
72: */
73: protected function _isDefaultShippingAddress(Mage_Customer_Model_Address $address)
74: {
75: return $address->getCustomer()->getDefaultShipping() == $address->getId();
76: }
77:
78: /**
79: * Get region id by name or code
80: * If id is not found then return passed $region
81: *
82: * @param string $region
83: * @param string $countryId
84: * @return int|string
85: */
86: protected function _getRegionIdByNameOrCode($region, $countryId)
87: {
88: /** @var $collection Mage_Directory_Model_Resource_Region_Collection */
89: $collection = Mage::getResourceModel('directory/region_collection');
90:
91: $collection->getSelect()
92: ->reset() // to avoid locale usage
93: ->from(array('main_table' => $collection->getMainTable()), 'region_id');
94:
95: $collection->addCountryFilter($countryId)
96: ->addFieldToFilter(array('default_name', 'code'), array($region, $region));
97:
98: $id = $collection->getResource()->getReadConnection()->fetchOne($collection->getSelect());
99:
100: return $id ? (int)$id : $region;
101: }
102:
103: /**
104: * Load customer address by id
105: *
106: * @param int $id
107: * @return Mage_Customer_Model_Address
108: */
109: protected function _loadCustomerAddressById($id)
110: {
111: /* @var $address Mage_Customer_Model_Address */
112: $address = Mage::getModel('customer/address')->load($id);
113:
114: if (!$address->getId()) {
115: $this->_critical(self::RESOURCE_NOT_FOUND);
116: }
117: $address->addData($this->_getDefaultAddressesInfo($address));
118:
119: return $address;
120: }
121:
122: /**
123: * Load customer by id
124: *
125: * @param int $id
126: * @throws Mage_Api2_Exception
127: * @return Mage_Customer_Model_Customer
128: */
129: protected function _loadCustomerById($id)
130: {
131: /* @var $customer Mage_Customer_Model_Customer */
132: $customer = Mage::getModel('customer/customer')->load($id);
133: if (!$customer->getId()) {
134: $this->_critical(self::RESOURCE_NOT_FOUND);
135: }
136: return $customer;
137: }
138: }
139: