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_XmlConnect_Block_Customer_Address_List extends Mage_Core_Block_Template
35: {
36: 37: 38: 39: 40:
41: protected function _toHtml()
42: {
43: $addressXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<address></address>');
44: $customer = Mage::getSingleton('customer/session')->getCustomer();
45:
46: $_billingAddssesId = $customer->getDefaultBilling();
47: $_shippingAddssesId = $customer->getDefaultShipping();
48: $billingAddress = $customer->getAddressById($_billingAddssesId);
49: $shippingAddress = $customer->getAddressById($_shippingAddssesId);
50:
51: if ($billingAddress && $billingAddress->getId()) {
52: $item = $addressXmlObj->addChild('item');
53: $item->addAttribute('label', $this->__('Default Billing Address'));
54: $item->addAttribute('default_billing', 1);
55: $this->prepareAddressData($billingAddress, $item);
56: }
57: if ($shippingAddress && $shippingAddress->getId()) {
58: $item = $addressXmlObj->addChild('item');
59: $item->addAttribute('label', $this->__('Default Shipping Address'));
60: $item->addAttribute('default_shipping', 1);
61: $this->prepareAddressData($shippingAddress, $item);
62: }
63: $_additionalAddresses = $customer->getAdditionalAddresses();
64: if ($_additionalAddresses) {
65: foreach ($_additionalAddresses as $_address) {
66: $item = $addressXmlObj->addChild('item');
67: $item->addAttribute('label', $this->__('Additional Address'));
68: $item->addAttribute('additional', 1);
69: $this->prepareAddressData($_address, $item);
70: }
71: }
72:
73: return $addressXmlObj->asNiceXml();
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83:
84: public function prepareAddressData(
85: Mage_Customer_Model_Address $address, Mage_XmlConnect_Model_Simplexml_Element $item
86: ) {
87: if (!$address) {
88: return array();
89: }
90:
91: $attributes = Mage::helper('customer/address')->getAttributes();
92:
93: $data = array('entity_id' => $address->getId());
94:
95: foreach ($attributes as $attribute) {
96:
97: if (!$attribute->getIsVisible()) {
98: continue;
99: }
100: if ($attribute->getAttributeCode() == 'country_id') {
101: $data['country'] = $address->getCountryModel()->getName();
102: $data['country_id'] = $address->getCountryId();
103: } else if ($attribute->getAttributeCode() == 'region') {
104: $data['region'] = $address->getRegion();
105: } else {
106: $dataModel = Mage_Customer_Model_Attribute_Data::factory($attribute, $address);
107: $value = $dataModel->outputValue(Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE);
108: if ($attribute->getFrontendInput() == 'multiline') {
109: $values = $dataModel->outputValue(Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ARRAY);
110:
111: foreach ($values as $k => $v) {
112: $key = sprintf('%s%d', $attribute->getAttributeCode(), $k + 1);
113: $data[$key] = $v;
114: }
115: }
116: $data[$attribute->getAttributeCode()] = $value;
117: }
118: }
119:
120: foreach ($data as $key => $value) {
121: if (empty($value)) {
122: continue;
123: }
124: $item->addChild($key, $item->escapeXml($value));
125: }
126: }
127: }
128: