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: 35: 36: 37: 38: 39: 40: 41: 42:
43: class Mage_Directory_Model_Country extends Mage_Core_Model_Abstract
44: {
45: static public $_format = array();
46:
47: protected function _construct()
48: {
49: $this->_init('directory/country');
50: }
51:
52: public function loadByCode($code)
53: {
54: $this->_getResource()->loadByCode($this, $code);
55: return $this;
56: }
57:
58: public function getRegions()
59: {
60: return $this->getLoadedRegionCollection();
61: }
62:
63: public function getLoadedRegionCollection()
64: {
65: $collection = $this->getRegionCollection();
66: $collection->load();
67: return $collection;
68: }
69:
70: public function getRegionCollection()
71: {
72: $collection = Mage::getResourceModel('directory/region_collection');
73: $collection->addCountryFilter($this->getId());
74: return $collection;
75: }
76:
77: public function formatAddress(Varien_Object $address, $html=false)
78: {
79:
80: $address->getRegion();
81: $address->getCountry();
82:
83:
84:
85: $template = $this->getData('address_template_'.($html ? 'html' : 'plain'));
86: if (empty($template)) {
87: if (!$this->getId()) {
88: $template = '{{firstname}} {{lastname}}';
89: } elseif (!$html) {
90: $template = "{{firstname}} {{lastname}}
91: {{company}}
92: {{street1}}
93: {{street2}}
94: {{city}}, {{region}} {{postcode}}";
95: } else {
96: $template = "{{firstname}} {{lastname}}<br/>
97: {{street}}<br/>
98: {{city}}, {{region}} {{postcode}}<br/>
99: T: {{telephone}}";
100: }
101: }
102:
103: $filter = new Varien_Filter_Template_Simple();
104: $addressText = $filter->setData($address->getData())->filter($template);
105:
106: if ($html) {
107: $addressText = preg_replace('#(<br\s*/?>\s*){2,}#im', '<br/>', $addressText);
108: } else {
109: $addressText = preg_replace('#(\n\s*){2,}#m', "\n", $addressText);
110: }
111:
112: return $addressText;
113: }
114:
115: 116: 117: 118: 119:
120: public function getFormats()
121: {
122: if (!isset(self::$_format[$this->getId()]) && $this->getId()) {
123: self::$_format[$this->getId()] = Mage::getModel('directory/country_format')
124: ->getCollection()
125: ->setCountryFilter($this)
126: ->load();
127: }
128:
129: if (isset(self::$_format[$this->getId()])) {
130: return self::$_format[$this->getId()];
131: }
132:
133: return null;
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function getFormat($type)
143: {
144: if ($this->getFormats()) {
145: foreach ($this->getFormats() as $format) {
146: if ($format->getType()==$type) {
147: return $format;
148: }
149: }
150: }
151: return null;
152: }
153:
154: public function getName()
155: {
156: if(!$this->getData('name')) {
157: $this->setData(
158: 'name',
159: Mage::app()->getLocale()->getCountryTranslation($this->getId())
160: );
161: }
162: return $this->getData('name');
163: }
164:
165: }
166: