PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Customer/Model/Address/Config.php

https://bitbucket.org/acidel/buykoala
PHP | 169 lines | 80 code | 17 blank | 72 comment | 11 complexity | 2338b195260f27e38892d878deff3ba4 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_Customer
  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 config
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_Model_Address_Config extends Mage_Core_Model_Config_Base
  34. {
  35. const DEFAULT_ADDRESS_RENDERER = 'customer/address_renderer_default';
  36. const XML_PATH_ADDRESS_TEMPLATE = 'customer/address_templates/';
  37. const DEFAULT_ADDRESS_FORMAT = 'oneline';
  38. /**
  39. * Customer Address Templates per store
  40. *
  41. * @var array
  42. */
  43. protected $_types = array();
  44. /**
  45. * Current store instance
  46. *
  47. * @var Mage_Core_Model_Store
  48. */
  49. protected $_store = null;
  50. /**
  51. * Default types per store
  52. * Using for invalid code
  53. *
  54. * @var array
  55. */
  56. protected $_defaultTypes = array();
  57. public function setStore($store)
  58. {
  59. $this->_store = Mage::app()->getStore($store);
  60. return $this;
  61. }
  62. /**
  63. * Retrieve store
  64. *
  65. * @return Mage_Core_Model_Store
  66. */
  67. public function getStore()
  68. {
  69. if (is_null($this->_store)) {
  70. $this->_store = Mage::app()->getStore();
  71. }
  72. return $this->_store;
  73. }
  74. /**
  75. * Define node
  76. *
  77. */
  78. public function __construct()
  79. {
  80. parent::__construct(Mage::getConfig()->getNode()->global->customer->address);
  81. }
  82. /**
  83. * Retrieve address formats
  84. *
  85. * @return array
  86. */
  87. public function getFormats()
  88. {
  89. $store = $this->getStore();
  90. $storeId = $store->getId();
  91. if (!isset($this->_types[$storeId])) {
  92. $this->_types[$storeId] = array();
  93. foreach ($this->getNode('formats')->children() as $typeCode => $typeConfig) {
  94. $path = sprintf('%s%s', self::XML_PATH_ADDRESS_TEMPLATE, $typeCode);
  95. $type = new Varien_Object();
  96. $htmlEscape = strtolower($typeConfig->htmlEscape);
  97. $htmlEscape = $htmlEscape == 'false' || $htmlEscape == '0' || $htmlEscape == 'no'
  98. || !strlen($typeConfig->htmlEscape) ? false : true;
  99. $type->setCode($typeCode)
  100. ->setTitle((string)$typeConfig->title)
  101. ->setDefaultFormat(Mage::getStoreConfig($path, $store))
  102. ->setHtmlEscape($htmlEscape);
  103. $renderer = (string)$typeConfig->renderer;
  104. if (!$renderer) {
  105. $renderer = self::DEFAULT_ADDRESS_RENDERER;
  106. }
  107. $type->setRenderer(
  108. Mage::helper('customer/address')->getRenderer($renderer)->setType($type)
  109. );
  110. $this->_types[$storeId][] = $type;
  111. }
  112. }
  113. return $this->_types[$storeId];
  114. }
  115. /**
  116. * Retrieve default address format
  117. *
  118. * @return Varien_Object
  119. */
  120. protected function _getDefaultFormat()
  121. {
  122. $store = $this->getStore();
  123. $storeId = $store->getId();
  124. if(!isset($this->_defaultType[$storeId])) {
  125. $this->_defaultType[$storeId] = new Varien_Object();
  126. $this->_defaultType[$storeId]->setCode('default')
  127. ->setDefaultFormat('{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}'
  128. . '{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}, '
  129. . '{{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}');
  130. $this->_defaultType[$storeId]->setRenderer(
  131. Mage::helper('customer/address')
  132. ->getRenderer(self::DEFAULT_ADDRESS_RENDERER)->setType($this->_defaultType[$storeId])
  133. );
  134. }
  135. return $this->_defaultType[$storeId];
  136. }
  137. /**
  138. * Retrieve address format by code
  139. *
  140. * @param string $typeCode
  141. * @return Varien_Object
  142. */
  143. public function getFormatByCode($typeCode)
  144. {
  145. foreach($this->getFormats() as $type) {
  146. if($type->getCode()==$typeCode) {
  147. return $type;
  148. }
  149. }
  150. return $this->_getDefaultFormat();
  151. }
  152. }