PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Block/Checkout/Address/Form.php

https://bitbucket.org/acidel/buykoala
PHP | 212 lines | 138 code | 25 blank | 49 comment | 15 complexity | 0bbebaf7a0f1d02fe67abe0bdccd8e3a 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_XmlConnect
  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 form xml renderer for onepage checkout
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Checkout_Address_Form extends Mage_Core_Block_Template
  34. {
  35. /**
  36. * Render customer address form xml
  37. *
  38. * @return string
  39. */
  40. protected function _toHtml()
  41. {
  42. /** @var $xmlModel Mage_XmlConnect_Model_Simplexml_Element */
  43. $xmlModel = Mage::getModel('xmlconnect/simplexml_element', '<form></form>');
  44. $xmlModel->addAttribute('name', 'address_form');
  45. $xmlModel->addAttribute('method', 'post');
  46. $addressType = $this->getType();
  47. if (!$addressType) {
  48. $addressType = 'billing';
  49. }
  50. $isAllowedGuestCheckout = Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout();
  51. $countries = $this->_getCountryOptions();
  52. $xmlModel->addField($addressType . '[firstname]', 'text', array(
  53. 'label' => $this->__('First Name'),
  54. 'required' => 'true',
  55. 'value' => ''
  56. ));
  57. $xmlModel->addField($addressType . '[lastname]', 'text', array(
  58. 'label' => $this->__('Last Name'),
  59. 'required' => 'true',
  60. 'value' => ''
  61. ));
  62. $xmlModel->addField($addressType . '[company]', 'text', array(
  63. 'label' => $this->__('Company'),
  64. 'required' => 'true',
  65. 'value' => ''
  66. ));
  67. if ($isAllowedGuestCheckout && !Mage::getSingleton('customer/session')->isLoggedIn()
  68. && $addressType == 'billing'
  69. ) {
  70. $emailField = $xmlModel->addField($addressType . '[email]', 'text', array(
  71. 'label' => $this->__('Email Address'),
  72. 'required' => 'true',
  73. 'value' => ''
  74. ));
  75. $emailValidator = $emailField->addChild('validators')->addChild('validator');
  76. $emailValidator->addAttribute('type', 'email');
  77. $emailValidator->addAttribute('message', $this->__('Wrong email format'));
  78. }
  79. $xmlModel->addField($addressType . '[street][]', 'text', array(
  80. 'label' => $this->__('Address'),
  81. 'required' => 'true',
  82. 'value' => ''
  83. ));
  84. $xmlModel->addField($addressType . '[street][]', 'text', array(
  85. 'label' => $this->__('Address 2'),
  86. 'value' => ''
  87. ));
  88. $xmlModel->addField($addressType . '[city]', 'text', array(
  89. 'label' => $this->__('City'),
  90. 'required' => 'true',
  91. 'value' => ''
  92. ));
  93. $countryOptionsXml = $xmlModel->addField($addressType . '[country_id]', 'select', array(
  94. 'label' => $this->__('Country'),
  95. 'required' => 'true',
  96. 'value' => ''
  97. ))->addChild('values');
  98. foreach ($countries as $data) {
  99. $regions = array();
  100. if ($data['value']) {
  101. $regions = $this->_getRegionOptions($data['value']);
  102. }
  103. $regionStr = (!empty($regions) ? 'region_id' : 'region');
  104. $countryXml = $countryOptionsXml->addCustomChild('item', null, array('relation' => $regionStr));
  105. $countryXml->addCustomChild('label', (string)$data['label']);
  106. $countryXml->addCustomChild('value', (string)$data['value']);
  107. if (!empty($regions)) {
  108. $regionXml = $countryXml->addChild('regions');
  109. foreach ($regions as $_data) {
  110. $regionItemXml = $regionXml->addChild('region_item');
  111. $regionItemXml->addCustomChild('label', (string)$_data['label']);
  112. $regionItemXml->addCustomChild('value', (string)$_data['value']);
  113. }
  114. }
  115. }
  116. $xmlModel->addField($addressType . '[region]', 'text', array(
  117. 'label' => $this->__('State/Province'),
  118. 'value' => ''
  119. ));
  120. $xmlModel->addField($addressType . '[region_id]', 'select', array(
  121. 'label' => $this->__('State/Province'),
  122. 'required' => 'true',
  123. 'value' => ''
  124. ));
  125. $xmlModel->addField($addressType . '[postcode]', 'text', array(
  126. 'label' => $this->__('Zip/Postal Code'),
  127. 'required' => 'true',
  128. 'value' => ''
  129. ));
  130. $xmlModel->addField($addressType . '[telephone]', 'text', array(
  131. 'label' => $this->__('Telephone'),
  132. 'required' => 'true',
  133. 'value' => ''
  134. ));
  135. $xmlModel->addField($addressType . '[fax]', 'text', array(
  136. 'label' => $this->__('Alternate Number'),
  137. 'value' => ''
  138. ));
  139. $xmlModel->addField($addressType . '[save_in_address_book]', 'checkbox', array(
  140. 'label' => $this->__('Save in address book'),
  141. ));
  142. return $xmlModel->asNiceXml();
  143. }
  144. /**
  145. * Retrieve regions by country
  146. *
  147. * @param string $countryId
  148. * @return array
  149. */
  150. protected function _getRegionOptions($countryId)
  151. {
  152. $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . Mage::app()->getStore()->getId() . $countryId;
  153. $cache = Mage::app()->loadCache($cacheKey);
  154. if (Mage::app()->useCache('config') && $cache) {
  155. $options = unserialize($cache);
  156. } else {
  157. $collection = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countryId)
  158. ->load();
  159. $options = $collection->toOptionArray();
  160. if (Mage::app()->useCache('config')) {
  161. Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
  162. }
  163. }
  164. return $options;
  165. }
  166. /**
  167. * Retrieve countries
  168. *
  169. * @return array
  170. */
  171. protected function _getCountryOptions()
  172. {
  173. $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
  174. $cache = Mage::app()->loadCache($cacheKey);
  175. if (Mage::app()->useCache('config') && $cache) {
  176. $options = unserialize($cache);
  177. } else {
  178. /** @var $collection Mage_Directory_Model_Mysql4_Country_Collection */
  179. $collection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
  180. $options = $collection->toOptionArray(false);
  181. if (Mage::app()->useCache('config')) {
  182. Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
  183. }
  184. }
  185. return $options;
  186. }
  187. }