00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Directory_Model_Country extends Mage_Core_Model_Abstract
00033 {
00034 static public $_format = array();
00035
00036 protected function _construct()
00037 {
00038 $this->_init('directory/country');
00039 }
00040
00041 public function loadByCode($code)
00042 {
00043 $this->_getResource()->loadByCode($this, $code);
00044 return $this;
00045 }
00046
00047 public function getRegions()
00048 {
00049 return $this->getLoadedRegionCollection();
00050 }
00051
00052 public function getLoadedRegionCollection()
00053 {
00054 $collection = $this->getRegionCollection();
00055 $collection->load();
00056 return $collection;
00057 }
00058
00059 public function getRegionCollection()
00060 {
00061 $collection = Mage::getResourceModel('directory/region_collection');
00062 $collection->addCountryFilter($this->getId());
00063 return $collection;
00064 }
00065
00066 public function formatAddress(Varien_Object $address, $html=false)
00067 {
00068
00069 $address->getRegion();
00070 $address->getCountry();
00071
00072
00073
00074 $template = $this->getData('address_template_'.($html ? 'html' : 'plain'));
00075 if (empty($template)) {
00076 if (!$this->getId()) {
00077 $template = '{{firstname}} {{lastname}}';
00078 } elseif (!$html) {
00079 $template = "{{firstname}} {{lastname}}
00080 {{company}}
00081 {{street1}}
00082 {{street2}}
00083 {{city}}, {{region}} {{postcode}}";
00084 } else {
00085 $template = "{{firstname}} {{lastname}}<br/>
00086 {{street}}<br/>
00087 {{city}}, {{region}} {{postcode}}<br/>
00088 T: {{telephone}}";
00089 }
00090 }
00091
00092 $filter = new Varien_Filter_Template_Simple();
00093 $addressText = $filter->setData($address->getData())->filter($template);
00094
00095 if ($html) {
00096 $addressText = preg_replace('#(<br\s*/?>\s*){2,}#im', '<br/>', $addressText);
00097 } else {
00098 $addressText = preg_replace('#(\n\s*){2,}#m', "\n", $addressText);
00099 }
00100
00101 return $addressText;
00102 }
00103
00104
00105
00106
00107
00108
00109 public function getFormats()
00110 {
00111 if (!isset(self::$_format[$this->getId()]) && $this->getId()) {
00112 self::$_format[$this->getId()] = Mage::getModel('directory/country_format')
00113 ->getCollection()
00114 ->setCountryFilter($this)
00115 ->load();
00116 }
00117
00118 if (isset(self::$_format[$this->getId()])) {
00119 return self::$_format[$this->getId()];
00120 }
00121
00122 return null;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 public function getFormat($type)
00132 {
00133 if ($this->getFormats()) {
00134 foreach ($this->getFormats() as $format) {
00135 if ($format->getType()==$type) {
00136 return $format;
00137 }
00138 }
00139 }
00140 return null;
00141 }
00142
00143 public function getName()
00144 {
00145 if(!$this->getData('name')) {
00146 $this->setData(
00147 'name',
00148 Mage::app()->getLocale()->getCountryTranslation($this->getId())
00149 );
00150 }
00151 return $this->getData('name');
00152 }
00153
00154 }