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: class Mage_Customer_Model_Address_Abstract extends Mage_Core_Model_Abstract
35: {
36: 37: 38:
39: const TYPE_BILLING = 'billing';
40: const TYPE_SHIPPING = 'shipping';
41:
42: 43: 44: 45: 46:
47: protected $_eventPrefix = 'customer_address';
48:
49: 50: 51: 52: 53:
54: protected $_eventObject = 'customer_address';
55:
56: 57: 58: 59: 60:
61: static protected $_countryModels = array();
62:
63: 64: 65: 66: 67:
68: static protected $_regionModels = array();
69:
70: 71: 72: 73: 74:
75: public function getName()
76: {
77: $name = '';
78: $config = Mage::getSingleton('eav/config');
79: if ($config->getAttribute('customer_address', 'prefix')->getIsVisible() && $this->getPrefix()) {
80: $name .= $this->getPrefix() . ' ';
81: }
82: $name .= $this->getFirstname();
83: if ($config->getAttribute('customer_address', 'middlename')->getIsVisible() && $this->getMiddlename()) {
84: $name .= ' ' . $this->getMiddlename();
85: }
86: $name .= ' ' . $this->getLastname();
87: if ($config->getAttribute('customer_address', 'suffix')->getIsVisible() && $this->getSuffix()) {
88: $name .= ' ' . $this->getSuffix();
89: }
90: return $name;
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function getStreet($line=0)
100: {
101: $street = parent::getData('street');
102: if (-1 === $line) {
103: return $street;
104: } else {
105: $arr = is_array($street) ? $street : explode("\n", $street);
106: if (0 === $line || $line === null) {
107: return $arr;
108: } elseif (isset($arr[$line-1])) {
109: return $arr[$line-1];
110: } else {
111: return '';
112: }
113: }
114: }
115:
116: public function getStreet1()
117: {
118: return $this->getStreet(1);
119: }
120:
121: public function getStreet2()
122: {
123: return $this->getStreet(2);
124: }
125:
126: public function getStreet3()
127: {
128: return $this->getStreet(3);
129: }
130:
131: public function getStreet4()
132: {
133: return $this->getStreet(4);
134: }
135:
136: public function getStreetFull()
137: {
138: return $this->getData('street');
139: }
140:
141: public function setStreetFull($street)
142: {
143: return $this->setStreet($street);
144: }
145:
146: 147: 148: 149: 150: 151:
152: public function setStreet($street)
153: {
154: if (is_array($street)) {
155: $street = trim(implode("\n", $street));
156: }
157: $this->setData('street', $street);
158: return $this;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function explodeStreetAddress()
168: {
169: $streetLines = $this->getStreet();
170: foreach ($streetLines as $i=>$line) {
171: $this->setData('street'.($i+1), $line);
172: }
173: return $this;
174: }
175:
176: 177: 178:
179: public function implodeStreetAddress()
180: {
181: $this->setStreet($this->getData('street'));
182: return $this;
183: }
184:
185: 186: 187: 188: 189:
190: public function getRegion()
191: {
192: $regionId = $this->getData('region_id');
193: $region = $this->getData('region');
194:
195: if ($regionId) {
196: if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
197: $region = $this->getRegionModel($regionId)->getName();
198: $this->setData('region', $region);
199: }
200: }
201:
202: if (!empty($region) && is_string($region)) {
203: $this->setData('region', $region);
204: }
205: elseif (!$regionId && is_numeric($region)) {
206: if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
207: $this->setData('region', $this->getRegionModel($region)->getName());
208: $this->setData('region_id', $region);
209: }
210: }
211: elseif ($regionId && !$region) {
212: if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
213: $this->setData('region', $this->getRegionModel($regionId)->getName());
214: }
215: }
216:
217: return $this->getData('region');
218: }
219:
220: 221: 222: 223:
224: public function getRegionCode()
225: {
226: $regionId = $this->getData('region_id');
227: $region = $this->getData('region');
228:
229: if (!$regionId && is_numeric($region)) {
230: if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
231: $this->setData('region_code', $this->getRegionModel($region)->getCode());
232: }
233: }
234: elseif ($regionId) {
235: if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
236: $this->setData('region_code', $this->getRegionModel($regionId)->getCode());
237: }
238: }
239: elseif (is_string($region)) {
240: $this->setData('region_code', $region);
241: }
242: return $this->getData('region_code');
243: }
244:
245: public function getRegionId()
246: {
247: $regionId = $this->getData('region_id');
248: $region = $this->getData('region');
249: if (!$regionId) {
250: if (is_numeric($region)) {
251: $this->setData('region_id', $region);
252: $this->unsRegion();
253: } else {
254: $regionModel = Mage::getModel('directory/region')
255: ->loadByCode($this->getRegionCode(), $this->getCountryId());
256: $this->setData('region_id', $regionModel->getId());
257: }
258: }
259: return $this->getData('region_id');
260: }
261:
262: public function getCountry()
263: {
264: 265: 266: 267: 268:
269: $country = $this->getCountryId();
270: return $country ? $country : $this->getData('country');
271: }
272:
273: 274: 275: 276: 277:
278: public function getCountryModel()
279: {
280: if(!isset(self::$_countryModels[$this->getCountryId()])) {
281: self::$_countryModels[$this->getCountryId()] = Mage::getModel('directory/country')
282: ->load($this->getCountryId());
283: }
284:
285: return self::$_countryModels[$this->getCountryId()];
286: }
287:
288: 289: 290: 291: 292:
293: public function getRegionModel($region=null)
294: {
295: if(is_null($region)) {
296: $region = $this->getRegionId();
297: }
298:
299: if(!isset(self::$_regionModels[$region])) {
300: self::$_regionModels[$region] = Mage::getModel('directory/region')->load($region);
301: }
302:
303: return self::$_regionModels[$region];
304: }
305:
306: 307: 308:
309: public function getHtmlFormat()
310: {
311: return $this->getConfig()->getFormatByCode('html');
312: }
313:
314: 315: 316:
317: public function getFormated($html=false)
318: {
319: return $this->format($html ? 'html' : 'text');
320:
321: }
322:
323: public function format($type)
324: {
325: if(!($formatType = $this->getConfig()->getFormatByCode($type))
326: || !$formatType->getRenderer()) {
327: return null;
328: }
329: Mage::dispatchEvent('customer_address_format', array('type' => $formatType, 'address' => $this));
330: return $formatType->getRenderer()->render($this);
331: }
332:
333: 334: 335: 336: 337:
338: public function getConfig()
339: {
340: return Mage::getSingleton('customer/address_config');
341: }
342:
343: protected function _beforeSave()
344: {
345: parent::_beforeSave();
346: $this->getRegion();
347: return $this;
348: }
349:
350: 351: 352: 353: 354:
355: public function validate()
356: {
357: $errors = array();
358: $this->implodeStreetAddress();
359: if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
360: $errors[] = Mage::helper('customer')->__('Please enter the first name.');
361: }
362:
363: if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
364: $errors[] = Mage::helper('customer')->__('Please enter the last name.');
365: }
366:
367: if (!Zend_Validate::is($this->getStreet(1), 'NotEmpty')) {
368: $errors[] = Mage::helper('customer')->__('Please enter the street.');
369: }
370:
371: if (!Zend_Validate::is($this->getCity(), 'NotEmpty')) {
372: $errors[] = Mage::helper('customer')->__('Please enter the city.');
373: }
374:
375: if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
376: $errors[] = Mage::helper('customer')->__('Please enter the telephone number.');
377: }
378:
379: $_havingOptionalZip = Mage::helper('directory')->getCountriesWithOptionalZip();
380: if (!in_array($this->getCountryId(), $_havingOptionalZip)
381: && !Zend_Validate::is($this->getPostcode(), 'NotEmpty')
382: ) {
383: $errors[] = Mage::helper('customer')->__('Please enter the zip/postal code.');
384: }
385:
386: if (!Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
387: $errors[] = Mage::helper('customer')->__('Please enter the country.');
388: }
389:
390: if ($this->getCountryModel()->getRegionCollection()->getSize()
391: && !Zend_Validate::is($this->getRegionId(), 'NotEmpty')
392: && Mage::helper('directory')->isRegionRequired($this->getCountryId())
393: ) {
394: $errors[] = Mage::helper('customer')->__('Please enter the state/province.');
395: }
396:
397: if (empty($errors) || $this->getShouldIgnoreValidation()) {
398: return true;
399: }
400: return $errors;
401: }
402: }
403: