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_Customer_Model_Address_Abstract extends Mage_Core_Model_Abstract
00033 {
00034 protected $_eventPrefix = 'customer_address';
00035 protected $_eventObject = 'customer_address';
00036
00037
00038
00039
00040
00041 static protected $_countryModels = array();
00042
00043
00044
00045
00046
00047
00048 static protected $_regionModels = array();
00049
00050 public function getName()
00051 {
00052 $name = '';
00053 if ($this->getPrefix()) {
00054 $name .= $this->getPrefix() . ' ';
00055 }
00056 $name .= $this->getFirstname();
00057 if ($this->getMiddlename()) {
00058 $name .= ' ' . $this->getMiddlename();
00059 }
00060 $name .= ' ' . $this->getLastname();
00061 if ($this->getSuffix()) {
00062 $name .= ' ' . $this->getSuffix();
00063 }
00064 return $name;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 public function getStreet($line=0)
00074 {
00075 $street = parent::getData('street');
00076 if (-1 === $line) {
00077 return $street;
00078 } else {
00079 $arr = is_array($street) ? $street : explode("\n", $street);
00080 if (0 === $line || $line === null) {
00081 return $arr;
00082 } elseif (isset($arr[$line-1])) {
00083 return $arr[$line-1];
00084 } else {
00085 return '';
00086 }
00087 }
00088 }
00089
00090 public function getStreet1()
00091 {
00092 return $this->getStreet(1);
00093 }
00094
00095 public function getStreet2()
00096 {
00097 return $this->getStreet(2);
00098 }
00099
00100 public function getStreet3()
00101 {
00102 return $this->getStreet(3);
00103 }
00104
00105 public function getStreet4()
00106 {
00107 return $this->getStreet(4);
00108 }
00109
00110 public function getStreetFull()
00111 {
00112 return $this->getData('street');
00113 }
00114
00115 public function setStreetFull($street)
00116 {
00117 return $this->setStreet($street);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 public function setStreet($street)
00127 {
00128 if (is_array($street)) {
00129 $street = trim(implode("\n", $street));
00130 }
00131 $this->setData('street', $street);
00132 return $this;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function explodeStreetAddress()
00142 {
00143 $streetLines = $this->getStreet();
00144 foreach ($streetLines as $i=>$line) {
00145 $this->setData('street'.($i+1), $line);
00146 }
00147 return $this;
00148 }
00149
00150
00151
00152
00153 public function implodeStreetAddress()
00154 {
00155 $this->setStreet($this->getData('street'));
00156 return $this;
00157 }
00158
00159
00160
00161
00162
00163
00164 public function getRegion()
00165 {
00166 $regionId = $this->getData('region_id');
00167 $region = $this->getData('region');
00168
00169 if ($regionId) {
00170 if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
00171 $region = $this->getRegionModel($regionId)->getName();
00172 $this->setData('region', $region);
00173 }
00174 }
00175
00176 if (!empty($region) && is_string($region)) {
00177 $this->setData('region', $region);
00178 }
00179 elseif (!$regionId && is_numeric($region)) {
00180 if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
00181 $this->setData('region', $this->getRegionModel($region)->getName());
00182 $this->setData('region_id', $region);
00183 }
00184 }
00185 elseif ($regionId && !$region) {
00186 if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
00187 $this->setData('region', $this->getRegionModel($regionId)->getName());
00188 }
00189 }
00190
00191 return $this->getData('region');
00192 }
00193
00194
00195
00196
00197
00198 public function getRegionCode()
00199 {
00200 $regionId = $this->getData('region_id');
00201 $region = $this->getData('region');
00202
00203 if (!$regionId && is_numeric($region)) {
00204 if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
00205 $this->setData('region_code', $this->getRegionModel($region)->getCode());
00206 }
00207 }
00208 elseif ($regionId) {
00209 if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
00210 $this->setData('region_code', $this->getRegionModel($regionId)->getCode());
00211 }
00212 }
00213 elseif (is_string($region)) {
00214 $this->setData('region_code', $region);
00215 }
00216 return $this->getData('region_code');
00217 }
00218
00219 public function getRegionId()
00220 {
00221 $regionId = $this->getData('region_id');
00222 $region = $this->getData('region');
00223 if (!$regionId) {
00224 if (is_numeric($region)) {
00225 $this->setData('region_id', $region);
00226 $this->unsRegion();
00227 } else {
00228 $regionModel = Mage::getModel('directory/region')->loadByCode($this->getRegionCode(), $this->getCountryId());
00229 $this->setData('region_id', $regionModel->getId());
00230 }
00231 }
00232 return $this->getData('region_id');
00233 }
00234
00235 public function getCountry()
00236 {
00237
00238
00239
00240
00241 $country = $this->getCountryId();
00242 return $country ? $country : $this->getData('country');
00243 }
00244
00245
00246
00247
00248
00249
00250 public function getCountryModel()
00251 {
00252 if(!isset(self::$_countryModels[$this->getCountryId()])) {
00253 self::$_countryModels[$this->getCountryId()] = Mage::getModel('directory/country')->load($this->getCountryId());
00254 }
00255
00256 return self::$_countryModels[$this->getCountryId()];
00257 }
00258
00259
00260
00261
00262
00263
00264 public function getRegionModel($region=null)
00265 {
00266 if(is_null($region)) {
00267 $region = $this->getRegionId();
00268 }
00269
00270 if(!isset(self::$_regionModels[$region])) {
00271 self::$_regionModels[$region] = Mage::getModel('directory/region')->load($region);
00272 }
00273
00274 return self::$_regionModels[$region];
00275 }
00276
00277
00278
00279
00280 public function getHtmlFormat()
00281 {
00282 return $this->getConfig()->getFormatByCode('html');
00283 }
00284
00285
00286
00287
00288 public function getFormated($html=false)
00289 {
00290 return $this->format($html ? 'html' : 'text');
00291 }
00292
00293 public function format($type)
00294 {
00295 if(!($formatType = $this->getConfig()->getFormatByCode($type))
00296 || !$formatType->getRenderer()) {
00297 return null;
00298 }
00299
00300 return $formatType->getRenderer()->render($this);
00301 }
00302
00303
00304
00305
00306
00307
00308 public function getConfig()
00309 {
00310 return Mage::getSingleton('customer/address_config');
00311 }
00312
00313 protected function _beforeSave()
00314 {
00315 parent::_beforeSave();
00316 $this->getRegion();
00317 return $this;
00318 }
00319
00320
00321
00322
00323
00324
00325 public function validate()
00326 {
00327 $errors = array();
00328 $helper = Mage::helper('customer');
00329 $this->implodeStreetAddress();
00330 if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
00331 $errors[] = $helper->__('Please enter first name.');
00332 }
00333
00334 if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
00335 $errors[] = $helper->__('Please enter last name.');
00336 }
00337
00338 if (!Zend_Validate::is($this->getStreet(1), 'NotEmpty')) {
00339 $errors[] = $helper->__('Please enter street.');
00340 }
00341
00342 if (!Zend_Validate::is($this->getCity(), 'NotEmpty')) {
00343 $errors[] = $helper->__('Please enter city.');
00344 }
00345
00346 if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
00347 $errors[] = $helper->__('Please enter telephone.');
00348 }
00349
00350 if (!Zend_Validate::is($this->getPostcode(), 'NotEmpty')) {
00351 $errors[] = $helper->__('Please enter zip/postal code.');
00352 }
00353
00354 if (!Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
00355 $errors[] = $helper->__('Please enter country.');
00356 }
00357
00358 if ($this->getCountryModel()->getRegionCollection()->getSize()
00359 && !Zend_Validate::is($this->getRegionId(), 'NotEmpty')) {
00360 $errors[] = $helper->__('Please enter state/province.');
00361 }
00362
00363 if (empty($errors)) {
00364 return true;
00365 }
00366 return $errors;
00367 }
00368 }