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_Form extends Mage_Customer_Block_Address_Edit
35: {
36: 37: 38: 39: 40:
41: protected $_nameWidgetBlock;
42:
43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59:
60: protected $_customerFiledRenderer = array(
61: 'text' => 'xmlconnect/customer_form_renderer_text',
62: 'textarea' => 'xmlconnect/customer_form_renderer_textarea',
63: 'multiline' => 'xmlconnect/customer_form_renderer_multiline',
64: 'date' => 'xmlconnect/customer_form_renderer_date',
65: 'select' => 'xmlconnect/customer_form_renderer_select',
66: 'multiselect' => 'xmlconnect/customer_form_renderer_multiselect',
67: 'boolean' => 'xmlconnect/customer_form_renderer_boolean',
68: 'file' => 'xmlconnect/customer_form_renderer_file',
69: 'image' => 'xmlconnect/customer_form_renderer_image'
70: );
71:
72: 73: 74: 75: 76:
77: protected function _toHtml()
78: {
79: $address = $this->getAddress();
80:
81: 82: 83:
84: $addressId = (int)$this->getRequest()->getParam('id');
85: $billingChecked = $shippingChecked = false;
86:
87: if ($addressId && $address && $address->getId()) {
88: $defaultBillingAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
89: $defaultShippingAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
90:
91: $billingChecked = (int)$addressId == $defaultBillingAddressId;
92: $shippingChecked = (int)$addressId == $defaultShippingAddressId;
93:
94: $company = $address->getCompany();
95: $street1 = $address->getStreet(1);
96: $street2 = $address->getStreet(2);
97: $city = $address->getCity();
98: $regionId = $address->getRegionId();
99: $region = $address->getRegion();
100: $postcode = $address->getPostcode();
101: $countryId = $address->getCountryId();
102: $telephone = $address->getTelephone();
103: $fax = $address->getFax();
104: }
105:
106: $action = Mage::helper('xmlconnect')->getActionUrl('xmlconnect/customer/saveaddress');
107:
108:
109: $fromXmlObj = Mage::getModel('xmlconnect/simplexml_form', array(
110: 'xml_id' => 'address_form',
111: 'action' => $action,
112: 'use_container' => true
113: ));
114:
115: $contactInfoFieldset = $fromXmlObj->addFieldset('contact_info', array(
116: 'legend' => $this->__('Contact Information')
117: ))->setCustomAttributes(array('legend'));
118:
119: $this->_addCustomerContactInfo($contactInfoFieldset);
120:
121: $contactInfoFieldset->addField('company', 'text', array(
122: 'label' => $this->__('Company'),
123: 'value' => isset($company) ? $company : ''
124: ));
125: $contactInfoFieldset->addField('telephone', 'text', array(
126: 'label' => $this->__('Telephone'),
127: 'required' => 'true',
128: 'value' => isset($telephone) ? $telephone : ''
129: ));
130: $contactInfoFieldset->addField('fax', 'text', array(
131: 'label' => $this->__('Fax'),
132: 'value' => isset($fax) ? $fax : ''
133: ));
134:
135: $addressFieldset = $fromXmlObj->addFieldset('address_info', array('legend' => $this->__('Address')))
136: ->setCustomAttributes(array('legend'));
137:
138: $addressFieldset->addField('street', 'text', array(
139: 'name' => 'street[]',
140: 'label' => $this->__('Street Address'),
141: 'required' => 'true',
142: 'value' => isset($street1) ? $street1 : ''
143: ));
144: $addressFieldset->addField('street_2', 'text', array(
145: 'name' => 'street[]',
146: 'label' => $this->__('Street Address %s', 2),
147: 'value' => isset($street2) ? $street2 : ''
148: ));
149: $addressFieldset->addField('city', 'text', array(
150: 'label' => $this->__('City'),
151: 'required' => 'true',
152: 'value' => isset($city) ? $city : ''
153: ));
154:
155: $countryId = isset($countryId) ? $countryId : null;
156: $regionId = isset($regionId) ? $regionId : null;
157: $region = isset($region) ? $region : null;
158:
159: $addressFieldset->addField('country_id', 'countryListSelect', array(
160: 'label' => $this->__('Country'),
161: 'required' => 'true',
162: 'value' => array(
163: 'country_id' => $countryId,
164: 'region_id' => $regionId,
165: 'region' => $region
166: ),
167: 'old_format' => true
168: ));
169: $addressFieldset->addField('region', 'text', array(
170: 'label' => $this->__('State/Province'),
171: 'value' => isset($region) ? $region : ''
172: ));
173: $addressFieldset->addField('region_id', 'select', array(
174: 'label' => $this->__('State/Province'),
175: 'required' => 'true',
176: ));
177: $addressFieldset->addField('postcode', 'text', array(
178: 'label' => $this->__('Zip/Postal Code'),
179: 'required' => 'true',
180: 'value' => isset($postcode) ? $postcode : ''
181: ));
182: $addressFieldset->addField('default_billing', 'checkbox', array(
183: 'label' => $this->__('Use as my default billing address'),
184: 'value' => $billingChecked ? $billingChecked : 0
185: ));
186:
187: $addressFieldset->addField('default_shipping', 'checkbox', array(
188: 'label' => $this->__('Use as my default shipping address'),
189: 'value' => $shippingChecked ? $shippingChecked : 0
190: ));
191:
192: $this->_addCustomAddressAttributes($addressFieldset);
193:
194: return $fromXmlObj->getXml();
195: }
196:
197: 198: 199: 200: 201: 202:
203: protected function _addCustomerContactInfo(
204: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset
205: ) {
206: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_Customer'))) {
207: $this->setNameWidgetBlock(
208: $this->getLayout()->createBlock('customer/widget_name')->setObject(
209: $this->getAddress()->getFirstname() ? $this->getAddress() : $this->getCustomer()
210: ));
211:
212: if ($this->getNameWidgetBlock()->showPrefix()) {
213: $this->_addPrefix($contactInfoFieldset);
214: }
215:
216: $this->_addFirstName($contactInfoFieldset);
217:
218: if ($this->getNameWidgetBlock()->showMiddlename()) {
219: $this->_addMiddleName($contactInfoFieldset);
220: }
221:
222: $this->_addLastName($contactInfoFieldset);
223:
224: if ($this->getNameWidgetBlock()->showSuffix()) {
225: $this->_addSuffix($contactInfoFieldset);
226: }
227: } else {
228: $this->_addFirstName($contactInfoFieldset);
229: $this->_addLastName($contactInfoFieldset);
230: }
231: return $this;
232: }
233:
234: 235: 236: 237: 238: 239:
240: protected function _addPrefix(Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset)
241: {
242: $attributes = array();
243: $attributes += $contactInfoFieldset->checkAttribute(
244: 'value', $this->getNameWidgetBlock()->getObject()->getPrefix()
245: );
246:
247: $attributes += $contactInfoFieldset->checkAttribute(
248: 'required', (int)$this->getNameWidgetBlock()->isPrefixRequired()
249: );
250:
251: if ($this->getNameWidgetBlock()->getPrefixOptions() === false) {
252: $contactInfoFieldset->addField($this->getNameWidgetBlock()->getFieldId('prefix'), 'text', array(
253: 'label' => $this->__('Prefix'),
254: 'name' => $this->getNameWidgetBlock()->getFieldName('prefix')
255: ) + $attributes);
256: } else {
257: $contactInfoFieldset->addField($this->getNameWidgetBlock()->getFieldId('prefix'), 'select', array(
258: 'label' => $this->__('Prefix'),
259: 'name' => $this->getNameWidgetBlock()->getFieldName('prefix'),
260: 'options' => $this->getNameWidgetBlock()->getPrefixOptions()
261: ) + $attributes);
262: }
263: return $this;
264: }
265:
266: 267: 268: 269: 270: 271:
272: protected function _addSuffix(
273: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset
274: )
275: {
276: $attributes = array();
277: $attributes += $contactInfoFieldset->checkAttribute(
278: 'value', $this->getNameWidgetBlock()->getObject()->getSuffix()
279: );
280:
281: $attributes += $contactInfoFieldset->checkAttribute(
282: 'required', (int)$this->getNameWidgetBlock()->isSuffixRequired()
283: );
284:
285: if ($this->getNameWidgetBlock()->getSuffixOptions() === false) {
286: $contactInfoFieldset->addField($this->getNameWidgetBlock()->getFieldId('suffix'), 'text', array(
287: 'label' => $this->__('Suffix'),
288: 'name' => $this->getNameWidgetBlock()->getFieldName('suffix')
289: ) + $attributes);
290: } else {
291: $contactInfoFieldset->addField($this->getNameWidgetBlock()->getFieldId('suffix'), 'select', array(
292: 'label' => $this->__('Suffix'),
293: 'name' => $this->getNameWidgetBlock()->getFieldName('suffix'),
294: 'options' => $this->getNameWidgetBlock()->getSuffixOptions()
295: ) + $attributes);
296: }
297: return $this;
298: }
299:
300: 301: 302: 303: 304: 305:
306: protected function _addMiddleName(
307: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset
308: )
309: {
310: $attributes = array();
311: $attributes += $contactInfoFieldset->checkAttribute(
312: 'value',
313: $this->getNameWidgetBlock()->getObject()->getMiddlename()
314: );
315:
316: $contactInfoFieldset->addField($this->getNameWidgetBlock()->getFieldId('middlename'), 'text', array(
317: 'label' => $this->__('M.I.'),
318: 'name' => $this->getNameWidgetBlock()->getFieldName('middlename')
319: ) + $attributes);
320:
321: return $this;
322: }
323:
324: 325: 326: 327: 328: 329:
330: protected function _addFirstName(
331: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset
332: ) {
333: $firstName = $this->getAddress()->getFirstname();
334: $contactInfoFieldset->addField('firstname', 'text', array(
335: 'label' => $this->__('First Name'),
336: 'required' => 'true',
337: 'value' => isset($firstName) ? $firstName : ''
338: ));
339: return $this;
340: }
341:
342: 343: 344: 345: 346: 347:
348: protected function _addLastName(
349: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $contactInfoFieldset
350: ) {
351: $lastName = $this->getAddress()->getLastname();
352: $contactInfoFieldset->addField('lastname', 'text', array(
353: 'label' => $this->__('Last Name'),
354: 'required' => 'true',
355: 'value' => isset($lastName) ? $lastName : ''
356: ));
357: return $this;
358: }
359:
360: 361: 362: 363: 364: 365:
366: protected function _addCustomAddressAttributes(
367: Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset $addressFieldset
368: ) {
369: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_Customer'))) {
370:
371: $addressAttrBlock = $this->getLayout()
372: ->addBlock('enterprise_customer/form', 'customer_address_attr');
373: $addressAttrBlock->setFormCode('customer_address_edit');
374: $addressAttrBlock->setEntity($this->getAddress());
375:
376: foreach ($this->_customerFiledRenderer as $type => $rendererBlock) {
377: $addressAttrBlock->addRenderer($type, $rendererBlock, 'customer/form/renderer/text.phtml');
378: }
379:
380: if ($addressAttrBlock->hasUserDefinedAttributes()) {
381: foreach ($addressAttrBlock->getUserDefinedAttributes() as $attribute) {
382: $type = $attribute->getFrontendInput();
383: $block = $addressAttrBlock->getRenderer($type);
384: if ($block) {
385: $block->setAttributeObject($attribute)->setEntity($addressAttrBlock->getEntity())
386: ->addFieldToXmlObj($addressFieldset);
387: }
388: }
389: }
390: }
391: return $this;
392: }
393:
394: 395: 396: 397: 398:
399: public function getNameWidgetBlock()
400: {
401: return $this->_nameWidgetBlock;
402: }
403:
404: 405: 406: 407: 408: 409:
410: public function setNameWidgetBlock($nameWidgetBlock)
411: {
412: $this->_nameWidgetBlock = $nameWidgetBlock;
413: return $this;
414: }
415:
416: 417: 418: 419: 420:
421: public function getCustomerFiledRenderer()
422: {
423: return $this->_customerFiledRenderer;
424: }
425:
426: 427: 428: 429: 430: 431:
432: public function setCustomerFiledRenderer($customerFiledRenderer)
433: {
434: $this->_customerFiledRenderer = $customerFiledRenderer;
435: return $this;
436: }
437: }
438: