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_Customer extends Mage_Core_Model_Abstract
35: {
36: 37: 38:
39: const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';
40: const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';
41: const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';
42: const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';
43: const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
44:
45: const XML_PATH_DEFAULT_EMAIL_DOMAIN = 'customer/create_account/email_domain';
46: const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
47: const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
48: const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';
49: const XML_PATH_GENERATE_HUMAN_FRIENDLY_ID = 'customer/create_account/generate_human_friendly_id';
50:
51: 52: 53:
54: const EXCEPTION_EMAIL_NOT_CONFIRMED = 1;
55: const EXCEPTION_INVALID_EMAIL_OR_PASSWORD = 2;
56: const EXCEPTION_EMAIL_EXISTS = 3;
57: const EXCEPTION_INVALID_RESET_PASSWORD_LINK_TOKEN = 4;
58:
59: const SUBSCRIBED_YES = 'yes';
60: const SUBSCRIBED_NO = 'no';
61:
62: 63: 64: 65: 66:
67: protected $_eventPrefix = 'customer';
68:
69: 70: 71: 72: 73:
74: protected $_eventObject = 'customer';
75:
76: 77: 78: 79: 80:
81: protected $_errors = array();
82:
83: 84: 85: 86: 87:
88: protected $_attributes;
89:
90: 91: 92: 93: 94: 95:
96: protected $_addresses = null;
97:
98: 99: 100: 101: 102:
103: protected $_addressesCollection;
104:
105: 106: 107: 108: 109:
110: protected $_isDeleteable = true;
111:
112: 113: 114: 115: 116:
117: protected $_isReadonly = false;
118:
119: 120: 121: 122: 123:
124: private static $_isConfirmationRequired;
125:
126: 127: 128:
129: function _construct()
130: {
131: $this->_init('customer/customer');
132: }
133:
134: 135: 136: 137: 138:
139: public function getSharingConfig()
140: {
141: return Mage::getSingleton('customer/config_share');
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151: 152:
153: public function authenticate($login, $password)
154: {
155: $this->loadByEmail($login);
156: if ($this->getConfirmation() && $this->isConfirmationRequired()) {
157: throw Mage::exception('Mage_Core', Mage::helper('customer')->__('This account is not confirmed.'),
158: self::EXCEPTION_EMAIL_NOT_CONFIRMED
159: );
160: }
161: if (!$this->validatePassword($password)) {
162: throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Invalid login or password.'),
163: self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
164: );
165: }
166: Mage::dispatchEvent('customer_customer_authenticated', array(
167: 'model' => $this,
168: 'password' => $password,
169: ));
170:
171: return true;
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function loadByEmail($customerEmail)
181: {
182: $this->_getResource()->loadByEmail($this, $customerEmail);
183: return $this;
184: }
185:
186:
187: 188: 189: 190: 191:
192: protected function _beforeSave()
193: {
194: parent::_beforeSave();
195:
196: $storeId = $this->getStoreId();
197: if ($storeId === null) {
198: $this->setStoreId(Mage::app()->getStore()->getId());
199: }
200:
201: $this->getGroupId();
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function changePassword($newPassword)
212: {
213: $this->_getResource()->changePassword($this, $newPassword);
214: return $this;
215: }
216:
217: 218: 219: 220: 221:
222: public function getName()
223: {
224: $name = '';
225: $config = Mage::getSingleton('eav/config');
226: if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
227: $name .= $this->getPrefix() . ' ';
228: }
229: $name .= $this->getFirstname();
230: if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
231: $name .= ' ' . $this->getMiddlename();
232: }
233: $name .= ' ' . $this->getLastname();
234: if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
235: $name .= ' ' . $this->getSuffix();
236: }
237: return $name;
238: }
239:
240: 241: 242: 243: 244: 245:
246: public function addAddress(Mage_Customer_Model_Address $address)
247: {
248: $this->getAddressesCollection()->addItem($address);
249: $this->getAddresses();
250: $this->_addresses[] = $address;
251: return $this;
252: }
253:
254: 255: 256: 257: 258: 259:
260: public function getAddressById($addressId)
261: {
262: return Mage::getModel('customer/address')
263: ->load($addressId);
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function getAddressItemById($addressId)
273: {
274: return $this->getAddressesCollection()->getItemById($addressId);
275: }
276:
277: 278: 279: 280: 281:
282: public function getAddressCollection()
283: {
284: return Mage::getResourceModel('customer/address_collection');
285: }
286:
287: 288: 289: 290: 291:
292: public function getAddressesCollection()
293: {
294: if ($this->_addressesCollection === null) {
295: $this->_addressesCollection = $this->getAddressCollection()
296: ->setCustomerFilter($this)
297: ->addAttributeToSelect('*');
298: foreach ($this->_addressesCollection as $address) {
299: $address->setCustomer($this);
300: }
301: }
302:
303: return $this->_addressesCollection;
304: }
305:
306: 307: 308: 309: 310:
311: public function getAddresses()
312: {
313: $this->_addresses = $this->getAddressesCollection()->getItems();
314: return $this->_addresses;
315: }
316:
317: 318: 319: 320: 321:
322: public function getAttributes()
323: {
324: if ($this->_attributes === null) {
325: $this->_attributes = $this->_getResource()
326: ->loadAllAttributes($this)
327: ->getSortedAttributes();
328: }
329: return $this->_attributes;
330: }
331:
332: 333: 334: 335: 336: 337:
338: public function getAttribute($attributeCode)
339: {
340: $this->getAttributes();
341: if (isset($this->_attributes[$attributeCode])) {
342: return $this->_attributes[$attributeCode];
343: }
344: return null;
345: }
346:
347: 348: 349: 350: 351: 352:
353: public function setPassword($password)
354: {
355: $this->setData('password', $password);
356: $this->setPasswordHash($this->hashPassword($password));
357: return $this;
358: }
359:
360: 361: 362: 363: 364: 365: 366:
367: public function hashPassword($password, $salt = null)
368: {
369: return Mage::helper('core')->getHash($password, !is_null($salt) ? $salt : 2);
370: }
371:
372: 373: 374: 375: 376: 377:
378: public function generatePassword($length = 8)
379: {
380: $chars = Mage_Core_Helper_Data::CHARS_PASSWORD_LOWERS
381: . Mage_Core_Helper_Data::CHARS_PASSWORD_UPPERS
382: . Mage_Core_Helper_Data::CHARS_PASSWORD_DIGITS
383: . Mage_Core_Helper_Data::CHARS_PASSWORD_SPECIALS;
384: return Mage::helper('core')->getRandomString($length, $chars);
385: }
386:
387: 388: 389: 390: 391: 392:
393: public function validatePassword($password)
394: {
395: $hash = $this->getPasswordHash();
396: if (!$hash) {
397: return false;
398: }
399: return Mage::helper('core')->validateHash($password, $hash);
400: }
401:
402:
403: 404: 405: 406: 407: 408:
409: public function encryptPassword($password)
410: {
411: return Mage::helper('core')->encrypt($password);
412: }
413:
414: 415: 416: 417: 418: 419:
420: public function decryptPassword($password)
421: {
422: return Mage::helper('core')->decrypt($password);
423: }
424:
425: 426: 427: 428: 429: 430:
431: public function getPrimaryAddress($attributeCode)
432: {
433: $primaryAddress = $this->getAddressesCollection()->getItemById($this->getData($attributeCode));
434:
435: return $primaryAddress ? $primaryAddress : false;
436: }
437:
438: 439: 440: 441: 442:
443: public function getPrimaryBillingAddress()
444: {
445: return $this->getPrimaryAddress('default_billing');
446: }
447:
448: 449: 450: 451: 452:
453: public function getDefaultBillingAddress()
454: {
455: return $this->getPrimaryBillingAddress();
456: }
457:
458: 459: 460: 461: 462:
463: public function getPrimaryShippingAddress()
464: {
465: return $this->getPrimaryAddress('default_shipping');
466: }
467:
468: 469: 470: 471: 472:
473: public function getDefaultShippingAddress()
474: {
475: return $this->getPrimaryShippingAddress();
476: }
477:
478: 479: 480: 481: 482:
483: public function getPrimaryAddressIds()
484: {
485: $ids = array();
486: if ($this->getDefaultBilling()) {
487: $ids[] = $this->getDefaultBilling();
488: }
489: if ($this->getDefaultShipping()) {
490: $ids[] = $this->getDefaultShipping();
491: }
492: return $ids;
493: }
494:
495: 496: 497: 498: 499:
500: public function getPrimaryAddresses()
501: {
502: $addresses = array();
503: $primaryBilling = $this->getPrimaryBillingAddress();
504: if ($primaryBilling) {
505: $addresses[] = $primaryBilling;
506: $primaryBilling->setIsPrimaryBilling(true);
507: }
508:
509: $primaryShipping = $this->getPrimaryShippingAddress();
510: if ($primaryShipping) {
511: if ($primaryBilling->getId() == $primaryShipping->getId()) {
512: $primaryBilling->setIsPrimaryShipping(true);
513: } else {
514: $primaryShipping->setIsPrimaryShipping(true);
515: $addresses[] = $primaryShipping;
516: }
517: }
518: return $addresses;
519: }
520:
521: 522: 523: 524: 525:
526: public function getAdditionalAddresses()
527: {
528: $addresses = array();
529: $primatyIds = $this->getPrimaryAddressIds();
530: foreach ($this->getAddressesCollection() as $address) {
531: if (!in_array($address->getId(), $primatyIds)) {
532: $addresses[] = $address;
533: }
534: }
535: return $addresses;
536: }
537:
538: 539: 540: 541: 542: 543:
544: public function isAddressPrimary(Mage_Customer_Model_Address $address)
545: {
546: if (!$address->getId()) {
547: return false;
548: }
549: return ($address->getId() == $this->getDefaultBilling()) || ($address->getId() == $this->getDefaultShipping());
550: }
551:
552: 553: 554: 555: 556: 557: 558: 559: 560:
561: public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeId = '0')
562: {
563: $types = array(
564: 'registered' => self::XML_PATH_REGISTER_EMAIL_TEMPLATE,
565: 'confirmed' => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE,
566: 'confirmation' => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
567: );
568: if (!isset($types[$type])) {
569: Mage::throwException(Mage::helper('customer')->__('Wrong transactional account email type'));
570: }
571:
572: if (!$storeId) {
573: $storeId = $this->_getWebsiteStoreId($this->getSendemailStoreId());
574: }
575:
576: $this->_sendEmailTemplate($types[$type], self::XML_PATH_REGISTER_EMAIL_IDENTITY,
577: array('customer' => $this, 'back_url' => $backUrl), $storeId);
578:
579: return $this;
580: }
581:
582: 583: 584: 585: 586:
587: public function isConfirmationRequired()
588: {
589: if ($this->canSkipConfirmation()) {
590: return false;
591: }
592: if (self::$_isConfirmationRequired === null) {
593: $storeId = $this->getStoreId() ? $this->getStoreId() : null;
594: self::$_isConfirmationRequired = (bool)Mage::getStoreConfig(self::XML_PATH_IS_CONFIRM, $storeId);
595: }
596:
597: return self::$_isConfirmationRequired;
598: }
599:
600: 601: 602: 603: 604:
605: public function getRandomConfirmationKey()
606: {
607: return md5(uniqid());
608: }
609:
610: 611: 612: 613: 614:
615: public function sendPasswordReminderEmail()
616: {
617: $storeId = $this->getStoreId();
618: if (!$storeId) {
619: $storeId = $this->_getWebsiteStoreId();
620: }
621:
622: $this->_sendEmailTemplate(self::XML_PATH_REMIND_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
623: array('customer' => $this), $storeId);
624:
625: return $this;
626: }
627:
628: 629: 630: 631: 632: 633: 634: 635: 636:
637: protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
638: {
639:
640: $mailer = Mage::getModel('core/email_template_mailer');
641: $emailInfo = Mage::getModel('core/email_info');
642: $emailInfo->addTo($this->getEmail(), $this->getName());
643: $mailer->addEmailInfo($emailInfo);
644:
645:
646: $mailer->setSender(Mage::getStoreConfig($sender, $storeId));
647: $mailer->setStoreId($storeId);
648: $mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
649: $mailer->setTemplateParams($templateParams);
650: $mailer->send();
651: return $this;
652: }
653:
654: 655: 656: 657: 658:
659: public function sendPasswordResetConfirmationEmail()
660: {
661: $storeId = $this->getStoreId();
662: if (!$storeId) {
663: $storeId = $this->_getWebsiteStoreId();
664: }
665:
666: $this->_sendEmailTemplate(self::XML_PATH_FORGOT_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
667: array('customer' => $this), $storeId);
668:
669: return $this;
670: }
671:
672: 673: 674: 675: 676:
677: public function getGroupId()
678: {
679: if (!$this->hasData('group_id')) {
680: $storeId = $this->getStoreId() ? $this->getStoreId() : Mage::app()->getStore()->getId();
681: $groupId = Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $storeId);
682: $this->setData('group_id', $groupId);
683: }
684: return $this->getData('group_id');
685: }
686:
687: 688: 689: 690: 691:
692: public function getTaxClassId()
693: {
694: if (!$this->getData('tax_class_id')) {
695: $this->setTaxClassId(Mage::getModel('customer/group')->getTaxClassId($this->getGroupId()));
696: }
697: return $this->getData('tax_class_id');
698: }
699:
700: 701: 702: 703: 704: 705:
706: public function isInStore($store)
707: {
708: if ($store instanceof Mage_Core_Model_Store) {
709: $storeId = $store->getId();
710: } else {
711: $storeId = $store;
712: }
713:
714: $availableStores = $this->getSharedStoreIds();
715: return in_array($storeId, $availableStores);
716: }
717:
718: 719: 720: 721: 722:
723: public function getStore()
724: {
725: return Mage::app()->getStore($this->getStoreId());
726: }
727:
728: 729: 730: 731: 732:
733: public function getSharedStoreIds()
734: {
735: $ids = $this->_getData('shared_store_ids');
736: if ($ids === null) {
737: $ids = array();
738: if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
739: $ids = Mage::app()->getWebsite($this->getWebsiteId())->getStoreIds();
740: } else {
741: foreach (Mage::app()->getStores() as $store) {
742: $ids[] = $store->getId();
743: }
744: }
745: $this->setData('shared_store_ids', $ids);
746: }
747:
748: return $ids;
749: }
750:
751: 752: 753: 754: 755:
756: public function getSharedWebsiteIds()
757: {
758: $ids = $this->_getData('shared_website_ids');
759: if ($ids === null) {
760: $ids = array();
761: if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
762: $ids[] = $this->getWebsiteId();
763: } else {
764: foreach (Mage::app()->getWebsites() as $website) {
765: $ids[] = $website->getId();
766: }
767: }
768: $this->setData('shared_website_ids', $ids);
769: }
770: return $ids;
771: }
772:
773: 774: 775: 776: 777: 778:
779: public function setStore(Mage_Core_Model_Store $store)
780: {
781: $this->setStoreId($store->getId());
782: $this->setWebsiteId($store->getWebsite()->getId());
783: return $this;
784: }
785:
786: 787: 788: 789: 790: 791:
792: public function validate()
793: {
794: $errors = array();
795: if (!Zend_Validate::is( trim($this->getFirstname()) , 'NotEmpty')) {
796: $errors[] = Mage::helper('customer')->__('The first name cannot be empty.');
797: }
798:
799: if (!Zend_Validate::is( trim($this->getLastname()) , 'NotEmpty')) {
800: $errors[] = Mage::helper('customer')->__('The last name cannot be empty.');
801: }
802:
803: if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
804: $errors[] = Mage::helper('customer')->__('Invalid email address "%s".', $this->getEmail());
805: }
806:
807: $password = $this->getPassword();
808: if (!$this->getId() && !Zend_Validate::is($password , 'NotEmpty')) {
809: $errors[] = Mage::helper('customer')->__('The password cannot be empty.');
810: }
811: if (strlen($password) && !Zend_Validate::is($password, 'StringLength', array(6))) {
812: $errors[] = Mage::helper('customer')->__('The minimum password length is %s', 6);
813: }
814: $confirmation = $this->getConfirmation();
815: if ($password != $confirmation) {
816: $errors[] = Mage::helper('customer')->__('Please make sure your passwords match.');
817: }
818:
819: $entityType = Mage::getSingleton('eav/config')->getEntityType('customer');
820: $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'dob');
821: if ($attribute->getIsRequired() && '' == trim($this->getDob())) {
822: $errors[] = Mage::helper('customer')->__('The Date of Birth is required.');
823: }
824: $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'taxvat');
825: if ($attribute->getIsRequired() && '' == trim($this->getTaxvat())) {
826: $errors[] = Mage::helper('customer')->__('The TAX/VAT number is required.');
827: }
828: $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'gender');
829: if ($attribute->getIsRequired() && '' == trim($this->getGender())) {
830: $errors[] = Mage::helper('customer')->__('Gender is required.');
831: }
832:
833: if (empty($errors)) {
834: return true;
835: }
836: return $errors;
837: }
838:
839: 840: 841: 842: 843: 844:
845: public function importFromTextArray(array $row)
846: {
847: $this->resetErrors();
848: $line = $row['i'];
849: $row = $row['row'];
850:
851: $regions = Mage::getResourceModel('directory/region_collection');
852:
853: $website = Mage::getModel('core/website')->load($row['website_code'], 'code');
854:
855: if (!$website->getId()) {
856: $this->addError(Mage::helper('customer')->__('Invalid website, skipping the record, line: %s', $line));
857:
858: } else {
859: $row['website_id'] = $website->getWebsiteId();
860: $this->setWebsiteId($row['website_id']);
861: }
862:
863:
864: if (empty($row['email'])) {
865: $this->addError(Mage::helper('customer')->__('Missing email, skipping the record, line: %s', $line));
866: } else {
867: $this->loadByEmail($row['email']);
868: }
869:
870: if (empty($row['entity_id'])) {
871: if ($this->getData('entity_id')) {
872: $this->addError(Mage::helper('customer')->__('The customer email (%s) already exists, skipping the record, line: %s', $row['email'], $line));
873: }
874: } else {
875: if ($row['entity_id'] != $this->getData('entity_id')) {
876: $this->addError(Mage::helper('customer')->__('The customer ID and email did not match, skipping the record, line: %s', $line));
877: } else {
878: $this->unsetData();
879: $this->load($row['entity_id']);
880: if (isset($row['store_view'])) {
881: $storeId = Mage::app()->getStore($row['store_view'])->getId();
882: if ($storeId) $this->setStoreId($storeId);
883: }
884: }
885: }
886:
887: if (empty($row['website_code'])) {
888: $this->addError(Mage::helper('customer')->__('Missing website, skipping the record, line: %s', $line));
889: }
890:
891: if (empty($row['group'])) {
892: $row['group'] = 'General';
893: }
894:
895: if (empty($row['firstname'])) {
896: $this->addError(Mage::helper('customer')->__('Missing first name, skipping the record, line: %s', $line));
897: }
898: if (empty($row['lastname'])) {
899: $this->addError(Mage::helper('customer')->__('Missing last name, skipping the record, line: %s', $line));
900: }
901:
902: if (!empty($row['password_new'])) {
903: $this->setPassword($row['password_new']);
904: unset($row['password_new']);
905: if (!empty($row['password_hash'])) unset($row['password_hash']);
906: }
907:
908: $errors = $this->getErrors();
909: if ($errors) {
910: $this->unsetData();
911: $this->printError(implode('<br />', $errors));
912: return;
913: }
914:
915: foreach ($row as $field => $value) {
916: $this->setData($field, $value);
917: }
918:
919: if (!$this->validateAddress($row, 'billing')) {
920: $this->printError(Mage::helper('customer')->__('Invalid billing address for (%s)', $row['email']), $line);
921: } else {
922:
923: $billingAddress = $this->getPrimaryBillingAddress();
924: if (!$billingAddress instanceof Mage_Customer_Model_Address) {
925: $billingAddress = Mage::getModel('customer/address');
926: }
927:
928: $regions->addRegionNameFilter($row['billing_region'])->load();
929: if ($regions) foreach($regions as $region) {
930: $regionId = intval($region->getId());
931: }
932:
933: $billingAddress->setFirstname($row['firstname']);
934: $billingAddress->setLastname($row['lastname']);
935: $billingAddress->setCity($row['billing_city']);
936: $billingAddress->setRegion($row['billing_region']);
937: if (isset($regionId)) {
938: $billingAddress->setRegionId($regionId);
939: }
940: $billingAddress->setCountryId($row['billing_country']);
941: $billingAddress->setPostcode($row['billing_postcode']);
942: if (isset($row['billing_street2'])) {
943: $billingAddress->setStreet(array($row['billing_street1'], $row['billing_street2']));
944: } else {
945: $billingAddress->setStreet(array($row['billing_street1']));
946: }
947: if (isset($row['billing_telephone'])) {
948: $billingAddress->setTelephone($row['billing_telephone']);
949: }
950:
951: if (!$billingAddress->getId()) {
952: $billingAddress->setIsDefaultBilling(true);
953: if ($this->getDefaultBilling()) {
954: $this->setData('default_billing', '');
955: }
956: $this->addAddress($billingAddress);
957: }
958: }
959:
960: if (!$this->validateAddress($row, 'shipping')) {
961: $this->printError(Mage::helper('customer')->__('Invalid shipping address for (%s)', $row['email']), $line);
962: } else {
963:
964: $shippingAddress = $this->getPrimaryShippingAddress();
965: if (!$shippingAddress instanceof Mage_Customer_Model_Address) {
966: $shippingAddress = Mage::getModel('customer/address');
967: }
968:
969: $regions->addRegionNameFilter($row['shipping_region'])->load();
970:
971: if ($regions) foreach($regions as $region) {
972: $regionId = intval($region->getId());
973: }
974:
975: $shippingAddress->setFirstname($row['firstname']);
976: $shippingAddress->setLastname($row['lastname']);
977: $shippingAddress->setCity($row['shipping_city']);
978: $shippingAddress->setRegion($row['shipping_region']);
979: if (isset($regionId)) {
980: $shippingAddress->setRegionId($regionId);
981: }
982: $shippingAddress->setCountryId($row['shipping_country']);
983: $shippingAddress->setPostcode($row['shipping_postcode']);
984: if (isset($row['shipping_street2'])) {
985: $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2']));
986: } else {
987: $shippingAddress->setStreet(array($row['shipping_street1']));
988: }
989: if (!empty($row['shipping_telephone'])) {
990: $shippingAddress->setTelephone($row['shipping_telephone']);
991: }
992:
993: if (!$shippingAddress->getId()) {
994: $shippingAddress->setIsDefaultShipping(true);
995: $this->addAddress($shippingAddress);
996: }
997:
998: }
999: if (!empty($row['is_subscribed'])) {
1000: $isSubscribed = (bool)strtolower($row['is_subscribed']) == self::SUBSCRIBED_YES;
1001: $this->setIsSubscribed($isSubscribed);
1002: }
1003: unset($row);
1004: return $this;
1005: }
1006:
1007: 1008: 1009: 1010: 1011:
1012: function unsetSubscription()
1013: {
1014: if (isset($this->_isSubscribed)) {
1015: unset($this->_isSubscribed);
1016: }
1017: return $this;
1018: }
1019:
1020: 1021: 1022: 1023: 1024:
1025: function cleanAllAddresses() {
1026: $this->_addressesCollection = null;
1027: $this->_addresses = null;
1028: }
1029:
1030: 1031: 1032: 1033: 1034:
1035: function addError($error)
1036: {
1037: $this->_errors[] = $error;
1038: return $this;
1039: }
1040:
1041: 1042: 1043: 1044: 1045:
1046: function getErrors()
1047: {
1048: return $this->_errors;
1049: }
1050:
1051: 1052: 1053: 1054: 1055:
1056: function resetErrors()
1057: {
1058: $this->_errors = array();
1059: return $this;
1060: }
1061:
1062: 1063: 1064: 1065: 1066: 1067: 1068:
1069: function printError($error, $line = null)
1070: {
1071: if ($error == null) {
1072: return false;
1073: }
1074:
1075: $liStyle = 'background-color: #FDD; ';
1076: echo '<li style="' . $liStyle . '">';
1077: echo '<img src="' . Mage::getDesign()->getSkinUrl('images/error_msg_icon.gif') . '" class="v-middle"/>';
1078: echo $error;
1079: if ($line) {
1080: echo '<small>, Line: <b>' . $line . '</b></small>';
1081: }
1082: echo '</li>';
1083: }
1084:
1085: 1086: 1087: 1088: 1089: 1090: 1091:
1092: function validateAddress(array $data, $type = 'billing')
1093: {
1094: $fields = array('city', 'country', 'postcode', 'telephone', 'street1');
1095: $usca = array('US', 'CA');
1096: $prefix = $type ? $type . '_' : '';
1097:
1098: if ($data) {
1099: foreach($fields as $field) {
1100: if (!isset($data[$prefix . $field])) {
1101: return false;
1102: }
1103: if ($field == 'country'
1104: && in_array(strtolower($data[$prefix . $field]), array('US', 'CA'))) {
1105:
1106: if (!isset($data[$prefix . 'region'])) {
1107: return false;
1108: }
1109:
1110: $region = Mage::getModel('directory/region')
1111: ->loadByName($data[$prefix . 'region']);
1112: if (!$region->getId()) {
1113: return false;
1114: }
1115: unset($region);
1116: }
1117: }
1118: unset($data);
1119: return true;
1120: }
1121: return false;
1122: }
1123:
1124: 1125: 1126:
1127: protected function _beforeDelete()
1128: {
1129: $this->_protectFromNonAdmin();
1130: return parent::_beforeDelete();
1131: }
1132:
1133: 1134: 1135: 1136: 1137:
1138: public function getCreatedAtTimestamp()
1139: {
1140: $date = $this->getCreatedAt();
1141: if ($date) {
1142: return Varien_Date::toTimestamp($date);
1143: }
1144: return null;
1145: }
1146:
1147: 1148: 1149: 1150: 1151:
1152: public function reset()
1153: {
1154: $this->setData(array());
1155: $this->setOrigData();
1156: $this->_attributes = null;
1157:
1158: return $this;
1159: }
1160:
1161: 1162: 1163: 1164: 1165:
1166: public function isDeleteable()
1167: {
1168: return $this->_isDeleteable;
1169: }
1170:
1171: 1172: 1173: 1174: 1175: 1176:
1177: public function setIsDeleteable($value)
1178: {
1179: $this->_isDeleteable = (bool)$value;
1180: return $this;
1181: }
1182:
1183: 1184: 1185: 1186: 1187:
1188: public function isReadonly()
1189: {
1190: return $this->_isReadonly;
1191: }
1192:
1193: 1194: 1195: 1196: 1197: 1198:
1199: public function setIsReadonly($value)
1200: {
1201: $this->_isReadonly = (bool)$value;
1202: return $this;
1203: }
1204:
1205: 1206: 1207: 1208: 1209:
1210: public function canSkipConfirmation()
1211: {
1212: return $this->getId() && $this->hasSkipConfirmationIfEmail()
1213: && strtolower($this->getSkipConfirmationIfEmail()) === strtolower($this->getEmail());
1214: }
1215:
1216: 1217: 1218:
1219: public function __clone()
1220: {
1221: $newAddressCollection = $this->getPrimaryAddresses();
1222: $newAddressCollection = array_merge($newAddressCollection, $this->getAdditionalAddresses());
1223: $this->setId(null);
1224: $this->cleanAllAddresses();
1225: foreach ($newAddressCollection as $address) {
1226: $this->addAddress(clone $address);
1227: }
1228: }
1229:
1230: 1231: 1232: 1233: 1234:
1235: public function getEntityType()
1236: {
1237: return $this->_getResource()->getEntityType();
1238: }
1239:
1240: 1241: 1242: 1243: 1244:
1245: public function getEntityTypeId()
1246: {
1247: $entityTypeId = $this->getData('entity_type_id');
1248: if (!$entityTypeId) {
1249: $entityTypeId = $this->getEntityType()->getId();
1250: $this->setData('entity_type_id', $entityTypeId);
1251: }
1252: return $entityTypeId;
1253: }
1254:
1255: 1256: 1257: 1258: 1259: 1260: 1261:
1262: protected function _getWebsiteStoreId($defaultStoreId = null)
1263: {
1264: if ($this->getWebsiteId() != 0 && empty($defaultStoreId)) {
1265: $storeIds = Mage::app()->getWebsite($this->getWebsiteId())->getStoreIds();
1266: reset($storeIds);
1267: $defaultStoreId = current($storeIds);
1268: }
1269: return $defaultStoreId;
1270: }
1271:
1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279:
1280: public function changeResetPasswordLinkToken($newResetPasswordLinkToken) {
1281: if (!is_string($newResetPasswordLinkToken) || empty($newResetPasswordLinkToken)) {
1282: throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Invalid password reset token.'),
1283: self::EXCEPTION_INVALID_RESET_PASSWORD_LINK_TOKEN);
1284: }
1285: $this->_getResource()->changeResetPasswordLinkToken($this, $newResetPasswordLinkToken);
1286: return $this;
1287: }
1288:
1289: 1290: 1291: 1292: 1293:
1294: public function isResetPasswordLinkTokenExpired()
1295: {
1296: $resetPasswordLinkToken = $this->getRpToken();
1297: $resetPasswordLinkTokenCreatedAt = $this->getRpTokenCreatedAt();
1298:
1299: if (empty($resetPasswordLinkToken) || empty($resetPasswordLinkTokenCreatedAt)) {
1300: return true;
1301: }
1302:
1303: $tokenExpirationPeriod = Mage::helper('customer')->getResetPasswordLinkExpirationPeriod();
1304:
1305: $currentDate = Varien_Date::now();
1306: $currentTimestamp = Varien_Date::toTimestamp($currentDate);
1307: $tokenTimestamp = Varien_Date::toTimestamp($resetPasswordLinkTokenCreatedAt);
1308: if ($tokenTimestamp > $currentTimestamp) {
1309: return true;
1310: }
1311:
1312: $dayDifference = floor(($currentTimestamp - $tokenTimestamp) / (24 * 60 * 60));
1313: if ($dayDifference >= $tokenExpirationPeriod) {
1314: return true;
1315: }
1316:
1317: return false;
1318: }
1319: }
1320: