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_Checkout_Address_Form extends Mage_Core_Block_Template
35: {
36: 37: 38: 39: 40:
41: protected function _toHtml()
42: {
43:
44: $xmlModel = Mage::getModel('xmlconnect/simplexml_element', '<form></form>');
45: $xmlModel->addAttribute('name', 'address_form');
46: $xmlModel->addAttribute('method', 'post');
47:
48: $addressType = $this->getType();
49: if (!$addressType) {
50: $addressType = 'billing';
51: }
52:
53: $isAllowedGuestCheckout = Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout();
54:
55: $countries = $this->_getCountryOptions();
56:
57: $xmlModel->addField($addressType . '[firstname]', 'text', array(
58: 'label' => $this->__('First Name'),
59: 'required' => 'true',
60: 'value' => ''
61: ));
62:
63: $xmlModel->addField($addressType . '[lastname]', 'text', array(
64: 'label' => $this->__('Last Name'),
65: 'required' => 'true',
66: 'value' => ''
67: ));
68:
69: $xmlModel->addField($addressType . '[company]', 'text', array(
70: 'label' => $this->__('Company'),
71: 'required' => 'true',
72: 'value' => ''
73: ));
74:
75: if ($isAllowedGuestCheckout && !Mage::getSingleton('customer/session')->isLoggedIn()
76: && $addressType == 'billing'
77: ) {
78: $emailField = $xmlModel->addField($addressType . '[email]', 'text', array(
79: 'label' => $this->__('Email Address'),
80: 'required' => 'true',
81: 'value' => ''
82: ));
83: $emailValidator = $emailField->addChild('validators')->addChild('validator');
84: $emailValidator->addAttribute('type', 'email');
85: $emailValidator->addAttribute('message', $this->__('Wrong email format'));
86: }
87:
88: $xmlModel->addField($addressType . '[street][]', 'text', array(
89: 'label' => $this->__('Address'),
90: 'required' => 'true',
91: 'value' => ''
92: ));
93:
94: $xmlModel->addField($addressType . '[street][]', 'text', array(
95: 'label' => $this->__('Address 2'),
96: 'value' => ''
97: ));
98:
99: $xmlModel->addField($addressType . '[city]', 'text', array(
100: 'label' => $this->__('City'),
101: 'required' => 'true',
102: 'value' => ''
103: ));
104:
105: $countryOptionsXml = $xmlModel->addField($addressType . '[country_id]', 'select', array(
106: 'label' => $this->__('Country'),
107: 'required' => 'true',
108: 'value' => ''
109: ))->addChild('values');
110:
111: foreach ($countries as $data) {
112: $regions = array();
113:
114: if ($data['value']) {
115: $regions = $this->_getRegionOptions($data['value']);
116: }
117:
118: $regionStr = (!empty($regions) ? 'region_id' : 'region');
119:
120: $countryXml = $countryOptionsXml->addCustomChild('item', null, array('relation' => $regionStr));
121: $countryXml->addCustomChild('label', (string)$data['label']);
122: $countryXml->addCustomChild('value', (string)$data['value']);
123: if (!empty($regions)) {
124: $regionXml = $countryXml->addChild('regions');
125: foreach ($regions as $_data) {
126: $regionItemXml = $regionXml->addChild('region_item');
127: $regionItemXml->addCustomChild('label', (string)$_data['label']);
128: $regionItemXml->addCustomChild('value', (string)$_data['value']);
129: }
130: }
131: }
132:
133: $xmlModel->addField($addressType . '[region]', 'text', array(
134: 'label' => $this->__('State/Province'),
135: 'value' => ''
136: ));
137:
138: $xmlModel->addField($addressType . '[region_id]', 'select', array(
139: 'label' => $this->__('State/Province'),
140: 'required' => 'true',
141: 'value' => ''
142: ));
143:
144: $xmlModel->addField($addressType . '[postcode]', 'text', array(
145: 'label' => $this->__('Zip/Postal Code'),
146: 'required' => 'true',
147: 'value' => ''
148: ));
149:
150: $xmlModel->addField($addressType . '[telephone]', 'text', array(
151: 'label' => $this->__('Telephone'),
152: 'required' => 'true',
153: 'value' => ''
154: ));
155:
156: $xmlModel->addField($addressType . '[fax]', 'text', array(
157: 'label' => $this->__('Fax'),
158: 'value' => ''
159: ));
160:
161: $xmlModel->addField($addressType . '[save_in_address_book]', 'checkbox', array(
162: 'label' => $this->__('Save in address book'),
163: ));
164:
165: return $xmlModel->asNiceXml();
166: }
167:
168: 169: 170: 171: 172: 173:
174: protected function _getRegionOptions($countryId)
175: {
176: $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . Mage::app()->getStore()->getId() . $countryId;
177: $cache = Mage::app()->loadCache($cacheKey);
178: if (Mage::app()->useCache('config') && $cache) {
179: $options = unserialize($cache);
180: } else {
181: $collection = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countryId)
182: ->load();
183: $options = $collection->toOptionArray();
184: if (Mage::app()->useCache('config')) {
185: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
186: }
187: }
188: return $options;
189: }
190:
191: 192: 193: 194: 195:
196: protected function _getCountryOptions()
197: {
198: $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
199: $cache = Mage::app()->loadCache($cacheKey);
200: if (Mage::app()->useCache('config') && $cache) {
201: $options = unserialize($cache);
202: } else {
203:
204: $collection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
205: $options = $collection->toOptionArray(false);
206: if (Mage::app()->useCache('config')) {
207: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
208: }
209: }
210: return $options;
211: }
212: }
213: