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