PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Customer/Model/Address/Abstract.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 460 lines | 260 code | 53 blank | 147 comment | 50 complexity | 5d9dee72eb717beae28d1922c057a11b MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Address abstract model
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_Model_Address_Abstract extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Possible customer address types
  37. */
  38. const TYPE_BILLING = 'billing';
  39. const TYPE_SHIPPING = 'shipping';
  40. /**
  41. * Prefix of model events
  42. *
  43. * @var string
  44. */
  45. protected $_eventPrefix = 'customer_address';
  46. /**
  47. * Name of event object
  48. *
  49. * @var string
  50. */
  51. protected $_eventObject = 'customer_address';
  52. /**
  53. * List of errors
  54. *
  55. * @var array
  56. */
  57. protected $_errors = array();
  58. /**
  59. * Directory country models
  60. *
  61. * @var array
  62. */
  63. static protected $_countryModels = array();
  64. /**
  65. * Directory region models
  66. *
  67. * @var array
  68. */
  69. static protected $_regionModels = array();
  70. /**
  71. * Get full customer name
  72. *
  73. * @return string
  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. * get address street
  94. *
  95. * @param int $line address line index
  96. * @return string
  97. */
  98. public function getStreet($line=0)
  99. {
  100. $street = parent::getData('street');
  101. if (-1 === $line) {
  102. return $street;
  103. } else {
  104. $arr = is_array($street) ? $street : explode("\n", $street);
  105. if (0 === $line || $line === null) {
  106. return $arr;
  107. } elseif (isset($arr[$line-1])) {
  108. return $arr[$line-1];
  109. } else {
  110. return '';
  111. }
  112. }
  113. }
  114. public function getStreet1()
  115. {
  116. return $this->getStreet(1);
  117. }
  118. public function getStreet2()
  119. {
  120. return $this->getStreet(2);
  121. }
  122. public function getStreet3()
  123. {
  124. return $this->getStreet(3);
  125. }
  126. public function getStreet4()
  127. {
  128. return $this->getStreet(4);
  129. }
  130. public function getStreetFull()
  131. {
  132. return $this->getData('street');
  133. }
  134. public function setStreetFull($street)
  135. {
  136. return $this->setStreet($street);
  137. }
  138. /**
  139. * set address street informa
  140. *
  141. * @param unknown_type $street
  142. * @return unknown
  143. */
  144. public function setStreet($street)
  145. {
  146. if (is_array($street)) {
  147. $street = trim(implode("\n", $street));
  148. }
  149. $this->setData('street', $street);
  150. return $this;
  151. }
  152. /**
  153. * Create fields street1, street2, etc.
  154. *
  155. * To be used in controllers for views data
  156. *
  157. */
  158. public function explodeStreetAddress()
  159. {
  160. $streetLines = $this->getStreet();
  161. foreach ($streetLines as $i=>$line) {
  162. $this->setData('street'.($i+1), $line);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * To be used when processing _POST
  168. */
  169. public function implodeStreetAddress()
  170. {
  171. $this->setStreet($this->getData('street'));
  172. return $this;
  173. }
  174. /**
  175. * Retrieve region name
  176. *
  177. * @return string
  178. */
  179. public function getRegion()
  180. {
  181. $regionId = $this->getData('region_id');
  182. $region = $this->getData('region');
  183. if ($regionId) {
  184. if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
  185. $region = $this->getRegionModel($regionId)->getName();
  186. $this->setData('region', $region);
  187. }
  188. }
  189. if (!empty($region) && is_string($region)) {
  190. $this->setData('region', $region);
  191. }
  192. elseif (!$regionId && is_numeric($region)) {
  193. if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
  194. $this->setData('region', $this->getRegionModel($region)->getName());
  195. $this->setData('region_id', $region);
  196. }
  197. }
  198. elseif ($regionId && !$region) {
  199. if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
  200. $this->setData('region', $this->getRegionModel($regionId)->getName());
  201. }
  202. }
  203. return $this->getData('region');
  204. }
  205. /**
  206. * Return 2 letter state code if available, otherwise full region name
  207. *
  208. */
  209. public function getRegionCode()
  210. {
  211. $regionId = $this->getData('region_id');
  212. $region = $this->getData('region');
  213. if (!$regionId && is_numeric($region)) {
  214. if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
  215. $this->setData('region_code', $this->getRegionModel($region)->getCode());
  216. }
  217. }
  218. elseif ($regionId) {
  219. if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
  220. $this->setData('region_code', $this->getRegionModel($regionId)->getCode());
  221. }
  222. }
  223. elseif (is_string($region)) {
  224. $this->setData('region_code', $region);
  225. }
  226. return $this->getData('region_code');
  227. }
  228. public function getRegionId()
  229. {
  230. $regionId = $this->getData('region_id');
  231. $region = $this->getData('region');
  232. if (!$regionId) {
  233. if (is_numeric($region)) {
  234. $this->setData('region_id', $region);
  235. $this->unsRegion();
  236. } else {
  237. $regionModel = Mage::getModel('directory/region')
  238. ->loadByCode($this->getRegionCode(), $this->getCountryId());
  239. $this->setData('region_id', $regionModel->getId());
  240. }
  241. }
  242. return $this->getData('region_id');
  243. }
  244. public function getCountry()
  245. {
  246. /*if ($this->getData('country_id') && !$this->getData('country')) {
  247. $this->setData('country', Mage::getModel('directory/country')
  248. ->load($this->getData('country_id'))->getIso2Code());
  249. }
  250. return $this->getData('country');*/
  251. $country = $this->getCountryId();
  252. return $country ? $country : $this->getData('country');
  253. }
  254. /**
  255. * Retrive country model
  256. *
  257. * @return Mage_Directory_Model_Country
  258. */
  259. public function getCountryModel()
  260. {
  261. if(!isset(self::$_countryModels[$this->getCountryId()])) {
  262. self::$_countryModels[$this->getCountryId()] = Mage::getModel('directory/country')
  263. ->load($this->getCountryId());
  264. }
  265. return self::$_countryModels[$this->getCountryId()];
  266. }
  267. /**
  268. * Retrive country model
  269. *
  270. * @return Mage_Directory_Model_Country
  271. */
  272. public function getRegionModel($region=null)
  273. {
  274. if(is_null($region)) {
  275. $region = $this->getRegionId();
  276. }
  277. if(!isset(self::$_regionModels[$region])) {
  278. self::$_regionModels[$region] = Mage::getModel('directory/region')->load($region);
  279. }
  280. return self::$_regionModels[$region];
  281. }
  282. /**
  283. * @deprecated for public function format
  284. */
  285. public function getHtmlFormat()
  286. {
  287. return $this->getConfig()->getFormatByCode('html');
  288. }
  289. /**
  290. * @deprecated for public function format
  291. */
  292. public function getFormated($html=false)
  293. {
  294. return $this->format($html ? 'html' : 'text');
  295. //Mage::getModel('directory/country')->load($this->getCountryId())->formatAddress($this, $html);
  296. }
  297. public function format($type)
  298. {
  299. if(!($formatType = $this->getConfig()->getFormatByCode($type))
  300. || !$formatType->getRenderer()) {
  301. return null;
  302. }
  303. Mage::dispatchEvent('customer_address_format', array('type' => $formatType, 'address' => $this));
  304. return $formatType->getRenderer()->render($this);
  305. }
  306. /**
  307. * Retrive address config object
  308. *
  309. * @return Mage_Customer_Model_Address_Config
  310. */
  311. public function getConfig()
  312. {
  313. return Mage::getSingleton('customer/address_config');
  314. }
  315. protected function _beforeSave()
  316. {
  317. parent::_beforeSave();
  318. $this->getRegion();
  319. return $this;
  320. }
  321. /**
  322. * Validate address attribute values
  323. *
  324. * @return array | bool
  325. */
  326. public function validate()
  327. {
  328. $this->_resetErrors();
  329. $this->implodeStreetAddress();
  330. $this->_basicCheck();
  331. Mage::dispatchEvent('customer_address_validation_after', array('address' => $this));
  332. $errors = $this->_getErrors();
  333. $this->_resetErrors();
  334. if (empty($errors) || $this->getShouldIgnoreValidation()) {
  335. return true;
  336. }
  337. return $errors;
  338. }
  339. /**
  340. * Perform basic validation
  341. *
  342. * @return void
  343. */
  344. protected function _basicCheck()
  345. {
  346. if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
  347. $this->addError(Mage::helper('customer')->__('Please enter the first name.'));
  348. }
  349. if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
  350. $this->addError(Mage::helper('customer')->__('Please enter the last name.'));
  351. }
  352. if (!Zend_Validate::is($this->getStreet(1), 'NotEmpty')) {
  353. $this->addError(Mage::helper('customer')->__('Please enter the street.'));
  354. }
  355. if (!Zend_Validate::is($this->getCity(), 'NotEmpty')) {
  356. $this->addError(Mage::helper('customer')->__('Please enter the city.'));
  357. }
  358. if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
  359. $this->addError(Mage::helper('customer')->__('Please enter the telephone number.'));
  360. }
  361. $_havingOptionalZip = Mage::helper('directory')->getCountriesWithOptionalZip();
  362. if (!in_array($this->getCountryId(), $_havingOptionalZip)
  363. && !Zend_Validate::is($this->getPostcode(), 'NotEmpty')
  364. ) {
  365. $this->addError(Mage::helper('customer')->__('Please enter the zip/postal code.'));
  366. }
  367. if (!Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
  368. $this->addError(Mage::helper('customer')->__('Please enter the country.'));
  369. }
  370. if ($this->getCountryModel()->getRegionCollection()->getSize()
  371. && !Zend_Validate::is($this->getRegionId(), 'NotEmpty')
  372. && Mage::helper('directory')->isRegionRequired($this->getCountryId())
  373. ) {
  374. $this->addError(Mage::helper('customer')->__('Please enter the state/province.'));
  375. }
  376. }
  377. /**
  378. * Add error
  379. *
  380. * @param $error
  381. * @return Mage_Customer_Model_Address_Abstract
  382. */
  383. public function addError($error)
  384. {
  385. $this->_errors[] = $error;
  386. return $this;
  387. }
  388. /**
  389. * Retreive errors
  390. *
  391. * @return array
  392. */
  393. protected function _getErrors()
  394. {
  395. return $this->_errors;
  396. }
  397. /**
  398. * Reset errors array
  399. *
  400. * @return Mage_Customer_Model_Address_Abstract
  401. */
  402. protected function _resetErrors()
  403. {
  404. $this->_errors = array();
  405. return $this;
  406. }
  407. }