forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Customer.php
1305 lines (1172 loc) · 37.7 KB
/
Customer.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Customer
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Customer model
*
* @category Mage
* @package Mage_Customer
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Customer_Model_Customer extends Mage_Core_Model_Abstract
{
/**
* Configuration pathes for email templates and identities
*/
const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';
const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';
const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';
const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
const XML_PATH_DEFAULT_EMAIL_DOMAIN = 'customer/create_account/email_domain';
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';
const XML_PATH_GENERATE_HUMAN_FRIENDLY_ID = 'customer/create_account/generate_human_friendly_id';
/**
* Codes of exceptions related to customer model
*/
const EXCEPTION_EMAIL_NOT_CONFIRMED = 1;
const EXCEPTION_INVALID_EMAIL_OR_PASSWORD = 2;
const EXCEPTION_EMAIL_EXISTS = 3;
const EXCEPTION_INVALID_RESET_PASSWORD_LINK_TOKEN = 4;
const SUBSCRIBED_YES = 'yes';
const SUBSCRIBED_NO = 'no';
/**
* Model event prefix
*
* @var string
*/
protected $_eventPrefix = 'customer';
/**
* Name of the event object
*
* @var string
*/
protected $_eventObject = 'customer';
/**
* List of errors
*
* @var array
*/
protected $_errors = array();
/**
* Assoc array of customer attributes
*
* @var array
*/
protected $_attributes;
/**
* Customer addresses collection
*
* @var Mage_Customer_Model_Resource_Address_Collection
*/
protected $_addressesCollection;
/**
* Is model deleteable
*
* @var boolean
*/
protected $_isDeleteable = true;
/**
* Is model readonly
*
* @var boolean
*/
protected $_isReadonly = false;
/**
* Confirmation requirement flag
*
* @var boolean
*/
private static $_isConfirmationRequired;
/**
* Initialize customer model
*/
function _construct()
{
$this->_init('Mage_Customer_Model_Resource_Customer');
}
/**
* Retrieve customer sharing configuration model
*
* @return Mage_Customer_Model_Config_Share
*/
public function getSharingConfig()
{
return Mage::getSingleton('Mage_Customer_Model_Config_Share');
}
/**
* Authenticate customer
*
* @param string $login
* @param string $password
* @throws Mage_Core_Exception
* @return true
*
*/
public function authenticate($login, $password)
{
$this->loadByEmail($login);
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
throw Mage::exception('Mage_Core', Mage::helper('Mage_Customer_Helper_Data')->__('This account is not confirmed.'),
self::EXCEPTION_EMAIL_NOT_CONFIRMED
);
}
if (!$this->validatePassword($password)) {
throw Mage::exception('Mage_Core', Mage::helper('Mage_Customer_Helper_Data')->__('Invalid login or password.'),
self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
);
}
Mage::dispatchEvent('customer_customer_authenticated', array(
'model' => $this,
'password' => $password,
));
return true;
}
/**
* Load customer by email
*
* @param string $customerEmail
* @return Mage_Customer_Model_Customer
*/
public function loadByEmail($customerEmail)
{
$this->_getResource()->loadByEmail($this, $customerEmail);
return $this;
}
/**
* Processing object before save data
*
* @return Mage_Customer_Model_Customer
*/
protected function _beforeSave()
{
parent::_beforeSave();
$storeId = $this->getStoreId();
if ($storeId === null) {
$this->setStoreId(Mage::app()->getStore()->getId());
}
$this->getGroupId();
return $this;
}
/**
* Change customer password
*
* @param string $newPassword
* @return Mage_Customer_Model_Customer
*/
public function changePassword($newPassword)
{
$this->_getResource()->changePassword($this, $newPassword);
return $this;
}
/**
* Get full customer name
*
* @return string
*/
public function getName()
{
$name = '';
$config = Mage::getSingleton('Mage_Eav_Model_Config');
if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
$name .= $this->getPrefix() . ' ';
}
$name .= $this->getFirstname();
if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
$name .= ' ' . $this->getMiddlename();
}
$name .= ' ' . $this->getLastname();
if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
$name .= ' ' . $this->getSuffix();
}
return $name;
}
/**
* Add address to address collection
*
* @param Mage_Customer_Model_Address $address
* @return Mage_Customer_Model_Customer
*/
public function addAddress(Mage_Customer_Model_Address $address)
{
$this->getAddressesCollection()->addItem($address);
return $this;
}
/**
* Retrieve customer address by address id
*
* @param int $addressId
* @return Mage_Customer_Model_Address
*/
public function getAddressById($addressId)
{
return Mage::getModel('Mage_Customer_Model_Address')
->load($addressId);
}
/**
* Getting customer address object from collection by identifier
*
* @param int $addressId
* @return Mage_Customer_Model_Address
*/
public function getAddressItemById($addressId)
{
return $this->getAddressesCollection()->getItemById($addressId);
}
/**
* Retrieve not loaded address collection
*
* @return Mage_Customer_Model_Resource_Address_Collection
*/
public function getAddressCollection()
{
return Mage::getResourceModel('Mage_Customer_Model_Resource_Address_Collection');
}
/**
* Customer addresses collection
*
* @return Mage_Customer_Model_Resource_Address_Collection
*/
public function getAddressesCollection()
{
if ($this->_addressesCollection === null) {
$this->_addressesCollection = $this->getAddressCollection()
->setCustomerFilter($this)
->addAttributeToSelect('*');
foreach ($this->_addressesCollection as $address) {
$address->setCustomer($this);
}
}
return $this->_addressesCollection;
}
/**
* Retrieve customer address array
*
* @return array
*/
public function getAddresses()
{
return $this->getAddressesCollection()->getItems();
}
/**
* Retrieve all customer attributes
*
* @return array
*/
public function getAttributes()
{
if ($this->_attributes === null) {
$this->_attributes = $this->_getResource()
->loadAllAttributes($this)
->getSortedAttributes();
}
return $this->_attributes;
}
/**
* Get customer attribute model object
*
* @param string $attributeCode
* @return Mage_Customer_Model_Resource_Attribute | null
*/
public function getAttribute($attributeCode)
{
$this->getAttributes();
if (isset($this->_attributes[$attributeCode])) {
return $this->_attributes[$attributeCode];
}
return null;
}
/**
* Set plain and hashed password
*
* @param string $password
* @return Mage_Customer_Model_Customer
*/
public function setPassword($password)
{
$this->setData('password', $password);
$this->setPasswordHash($this->hashPassword($password));
return $this;
}
/**
* Hash customer password
*
* @param string $password
* @param int $salt
* @return string
*/
public function hashPassword($password, $salt = null)
{
return Mage::helper('Mage_Core_Helper_Data')->getHash($password, !is_null($salt) ? $salt : 2);
}
/**
* Retrieve random password
*
* @param int $length
* @return string
*/
public function generatePassword($length = 6)
{
return Mage::helper('Mage_Core_Helper_Data')->getRandomString($length);
}
/**
* Validate password with salted hash
*
* @param string $password
* @return boolean
*/
public function validatePassword($password)
{
$hash = $this->getPasswordHash();
if (!$hash) {
return false;
}
return Mage::helper('Mage_Core_Helper_Data')->validateHash($password, $hash);
}
/**
* Encrypt password
*
* @param string $password
* @return string
*/
public function encryptPassword($password)
{
return Mage::helper('Mage_Core_Helper_Data')->encrypt($password);
}
/**
* Decrypt password
*
* @param string $password
* @return string
*/
public function decryptPassword($password)
{
return Mage::helper('Mage_Core_Helper_Data')->decrypt($password);
}
/**
* Retrieve default address by type(attribute)
*
* @param string $attributeCode address type attribute code
* @return Mage_Customer_Model_Address
*/
public function getPrimaryAddress($attributeCode)
{
$primaryAddress = $this->getAddressesCollection()->getItemById($this->getData($attributeCode));
return $primaryAddress ? $primaryAddress : false;
}
/**
* Get customer default billing address
*
* @return Mage_Customer_Model_Address
*/
public function getPrimaryBillingAddress()
{
return $this->getPrimaryAddress('default_billing');
}
/**
* Get customer default billing address
*
* @return Mage_Customer_Model_Address
*/
public function getDefaultBillingAddress()
{
return $this->getPrimaryBillingAddress();
}
/**
* Get default customer shipping address
*
* @return Mage_Customer_Model_Address
*/
public function getPrimaryShippingAddress()
{
return $this->getPrimaryAddress('default_shipping');
}
/**
* Get default customer shipping address
*
* @return Mage_Customer_Model_Address
*/
public function getDefaultShippingAddress()
{
return $this->getPrimaryShippingAddress();
}
/**
* Retrieve ids of default addresses
*
* @return array
*/
public function getPrimaryAddressIds()
{
$ids = array();
if ($this->getDefaultBilling()) {
$ids[] = $this->getDefaultBilling();
}
if ($this->getDefaultShipping()) {
$ids[] = $this->getDefaultShipping();
}
return $ids;
}
/**
* Retrieve all customer default addresses
*
* @return array
*/
public function getPrimaryAddresses()
{
$addresses = array();
$primaryBilling = $this->getPrimaryBillingAddress();
if ($primaryBilling) {
$addresses[] = $primaryBilling;
$primaryBilling->setIsPrimaryBilling(true);
}
$primaryShipping = $this->getPrimaryShippingAddress();
if ($primaryShipping) {
if ($primaryBilling->getId() == $primaryShipping->getId()) {
$primaryBilling->setIsPrimaryShipping(true);
} else {
$primaryShipping->setIsPrimaryShipping(true);
$addresses[] = $primaryShipping;
}
}
return $addresses;
}
/**
* Retrieve not default addresses
*
* @return array
*/
public function getAdditionalAddresses()
{
$addresses = array();
$primatyIds = $this->getPrimaryAddressIds();
foreach ($this->getAddressesCollection() as $address) {
if (!in_array($address->getId(), $primatyIds)) {
$addresses[] = $address;
}
}
return $addresses;
}
/**
* Check if address is primary
*
* @param Mage_Customer_Model_Address $address
* @return boolean
*/
public function isAddressPrimary(Mage_Customer_Model_Address $address)
{
if (!$address->getId()) {
return false;
}
return ($address->getId() == $this->getDefaultBilling()) || ($address->getId() == $this->getDefaultShipping());
}
/**
* Send email with new account related information
*
* @param string $type
* @param string $backUrl
* @param string $storeId
* @throws Mage_Core_Exception
* @return Mage_Customer_Model_Customer
*/
public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeId = '0')
{
$types = array(
'registered' => self::XML_PATH_REGISTER_EMAIL_TEMPLATE, // welcome email, when confirmation is disabled
'confirmed' => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE, // welcome email, when confirmation is enabled
'confirmation' => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE, // email with confirmation link
);
if (!isset($types[$type])) {
Mage::throwException(Mage::helper('Mage_Customer_Helper_Data')->__('Wrong transactional account email type'));
}
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId($this->getSendemailStoreId());
}
$this->_sendEmailTemplate($types[$type], self::XML_PATH_REGISTER_EMAIL_IDENTITY,
array('customer' => $this, 'back_url' => $backUrl), $storeId);
return $this;
}
/**
* Check if accounts confirmation is required in config
*
* @return bool
*/
public function isConfirmationRequired()
{
if ($this->canSkipConfirmation()) {
return false;
}
if (self::$_isConfirmationRequired === null) {
$storeId = $this->getStoreId() ? $this->getStoreId() : null;
self::$_isConfirmationRequired = (bool)Mage::getStoreConfig(self::XML_PATH_IS_CONFIRM, $storeId);
}
return self::$_isConfirmationRequired;
}
/**
* Generate random confirmation key
*
* @return string
*/
public function getRandomConfirmationKey()
{
return md5(uniqid());
}
/**
* Send email with new customer password
*
* @return Mage_Customer_Model_Customer
*/
public function sendPasswordReminderEmail()
{
$storeId = $this->getStoreId();
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId();
}
$this->_sendEmailTemplate(self::XML_PATH_REMIND_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
array('customer' => $this), $storeId);
return $this;
}
/**
* Send corresponding email template
*
* @param string $emailTemplate configuration path of email template
* @param string $emailSender configuration path of email identity
* @param array $templateParams
* @param int|null $storeId
* @return Mage_Customer_Model_Customer
*/
protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
{
/** @var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('Mage_Core_Model_Email_Template_Mailer');
$emailInfo = Mage::getModel('Mage_Core_Model_Email_Info');
$emailInfo->addTo($this->getEmail(), $this->getName());
$mailer->addEmailInfo($emailInfo);
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig($sender, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
$mailer->setTemplateParams($templateParams);
$mailer->send();
return $this;
}
/**
* Send email with reset password confirmation link
*
* @return Mage_Customer_Model_Customer
*/
public function sendPasswordResetConfirmationEmail()
{
$storeId = $this->getStoreId();
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId();
}
$this->_sendEmailTemplate(self::XML_PATH_FORGOT_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
array('customer' => $this), $storeId);
return $this;
}
/**
* Retrieve customer group identifier
*
* @return int
*/
public function getGroupId()
{
if (!$this->hasData('group_id')) {
$storeId = $this->getStoreId() ? $this->getStoreId() : Mage::app()->getStore()->getId();
$groupId = Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $storeId);
$this->setData('group_id', $groupId);
}
return $this->getData('group_id');
}
/**
* Retrieve customer tax class identifier
*
* @return int
*/
public function getTaxClassId()
{
if (!$this->getData('tax_class_id')) {
$this->setTaxClassId(Mage::getModel('Mage_Customer_Model_Group')->getTaxClassId($this->getGroupId()));
}
return $this->getData('tax_class_id');
}
/**
* Check store availability for customer
*
* @param Mage_Core_Model_Store | int $store
* @return bool
*/
public function isInStore($store)
{
if ($store instanceof Mage_Core_Model_Store) {
$storeId = $store->getId();
} else {
$storeId = $store;
}
$availableStores = $this->getSharedStoreIds();
return in_array($storeId, $availableStores);
}
/**
* Retrieve store where customer was created
*
* @return Mage_Core_Model_Store
*/
public function getStore()
{
return Mage::app()->getStore($this->getStoreId());
}
/**
* Retrieve shared store ids
*
* @return array
*/
public function getSharedStoreIds()
{
$ids = $this->_getData('shared_store_ids');
if ($ids === null) {
$ids = array();
if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
$ids = Mage::app()->getWebsite($this->getWebsiteId())->getStoreIds();
} else {
foreach (Mage::app()->getStores() as $store) {
$ids[] = $store->getId();
}
}
$this->setData('shared_store_ids', $ids);
}
return $ids;
}
/**
* Retrive shared website ids
*
* @return array
*/
public function getSharedWebsiteIds()
{
$ids = $this->_getData('shared_website_ids');
if ($ids === null) {
$ids = array();
if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
$ids[] = $this->getWebsiteId();
} else {
foreach (Mage::app()->getWebsites() as $website) {
$ids[] = $website->getId();
}
}
$this->setData('shared_website_ids', $ids);
}
return $ids;
}
/**
* Set store to customer
*
* @param Mage_Core_Model_Store $store
* @return Mage_Customer_Model_Customer
*/
public function setStore(Mage_Core_Model_Store $store)
{
$this->setStoreId($store->getId());
$this->setWebsiteId($store->getWebsite()->getId());
return $this;
}
/**
* Validate customer attribute values.
* For existing customer password + confirmation will be validated only when password is set (i.e. its change is requested)
*
* @return bool
*/
public function validate()
{
$errors = array();
$customerHelper = Mage::helper('Mage_Customer_Helper_Data');
if (!Zend_Validate::is( trim($this->getFirstname()) , 'NotEmpty')) {
$errors[] = $customerHelper->__('The first name cannot be empty.');
}
if (!Zend_Validate::is( trim($this->getLastname()) , 'NotEmpty')) {
$errors[] = $customerHelper->__('The last name cannot be empty.');
}
if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
$errors[] = $customerHelper->__('Invalid email address "%s".', $this->getEmail());
}
$password = $this->getPassword();
if (!$this->getId() && !Zend_Validate::is($password , 'NotEmpty')) {
$errors[] = $customerHelper->__('The password cannot be empty.');
}
if (strlen($password) && !Zend_Validate::is($password, 'StringLength', array(6))) {
$errors[] = $customerHelper->__('The minimum password length is %s', 6);
}
$confirmation = $this->getConfirmation();
if ($password != $confirmation) {
$errors[] = $customerHelper->__('Please make sure your passwords match.');
}
$entityType = Mage::getSingleton('Mage_Eav_Model_Config')->getEntityType('customer');
$attribute = Mage::getModel('Mage_Customer_Model_Attribute')->loadByCode($entityType, 'dob');
if ($attribute->getIsRequired() && '' == trim($this->getDob())) {
$errors[] = $customerHelper->__('The Date of Birth is required.');
}
$attribute = Mage::getModel('Mage_Customer_Model_Attribute')->loadByCode($entityType, 'taxvat');
if ($attribute->getIsRequired() && '' == trim($this->getTaxvat())) {
$errors[] = $customerHelper->__('The TAX/VAT number is required.');
}
$attribute = Mage::getModel('Mage_Customer_Model_Attribute')->loadByCode($entityType, 'gender');
if ($attribute->getIsRequired() && '' == trim($this->getGender())) {
$errors[] = $customerHelper->__('Gender is required.');
}
if (empty($errors)) {
return true;
}
return $errors;
}
/**
* Import customer data from text array
*
* @param array $row
* @return Mage_Customer_Model_Customer
*/
public function importFromTextArray(array $row)
{
$this->resetErrors();
$hlp = Mage::helper('Mage_Customer_Helper_Data');
$line = $row['i'];
$row = $row['row'];
$regions = Mage::getResourceModel('Mage_Directory_Model_Resource_Region_Collection');
$website = Mage::getModel('Mage_Core_Model_Website')->load($row['website_code'], 'code');
if (!$website->getId()) {
$this->addError($hlp->__('Invalid website, skipping the record, line: %s', $line));
} else {
$row['website_id'] = $website->getWebsiteId();
$this->setWebsiteId($row['website_id']);
}
// Validate Email
if (empty($row['email'])) {
$this->addError($hlp->__('Missing email, skipping the record, line: %s', $line));
} else {
$this->loadByEmail($row['email']);
}
if (empty($row['entity_id'])) {
if ($this->getData('entity_id')) {
$this->addError($hlp->__('The customer email (%s) already exists, skipping the record, line: %s', $row['email'], $line));
}
} else {
if ($row['entity_id'] != $this->getData('entity_id')) {
$this->addError($hlp->__('The customer ID and email did not match, skipping the record, line: %s', $line));
} else {
$this->unsetData();
$this->load($row['entity_id']);
if (isset($row['store_view'])) {
$storeId = Mage::app()->getStore($row['store_view'])->getId();
if ($storeId) $this->setStoreId($storeId);
}
}
}
if (empty($row['website_code'])) {
$this->addError($hlp->__('Missing website, skipping the record, line: %s', $line));
}
if (empty($row['group'])) {
$row['group'] = 'General';
}
if (empty($row['firstname'])) {
$this->addError($hlp->__('Missing first name, skipping the record, line: %s', $line));
}
if (empty($row['lastname'])) {
$this->addError($hlp->__('Missing last name, skipping the record, line: %s', $line));
}
if (!empty($row['password_new'])) {
$this->setPassword($row['password_new']);
unset($row['password_new']);
if (!empty($row['password_hash'])) unset($row['password_hash']);
}
$errors = $this->getErrors();
if ($errors) {
$this->unsetData();
$this->printError(implode('<br />', $errors));
return;
}
foreach ($row as $field => $value) {
$this->setData($field, $value);
}
if (!$this->validateAddress($row, 'billing')) {
$this->printError($hlp->__('Invalid billing address for (%s)', $row['email']), $line);
} else {
// Handling billing address
$billingAddress = $this->getPrimaryBillingAddress();
if (!$billingAddress instanceof Mage_Customer_Model_Address) {
$billingAddress = Mage::getModel('Mage_Customer_Model_Address');
}
$regions->addRegionNameFilter($row['billing_region'])->load();
if ($regions) foreach($regions as $region) {
$regionId = intval($region->getId());
}
$billingAddress->setFirstname($row['firstname']);
$billingAddress->setLastname($row['lastname']);
$billingAddress->setCity($row['billing_city']);
$billingAddress->setRegion($row['billing_region']);
if (isset($regionId)) {
$billingAddress->setRegionId($regionId);
}
$billingAddress->setCountryId($row['billing_country']);
$billingAddress->setPostcode($row['billing_postcode']);
if (isset($row['billing_street2'])) {
$billingAddress->setStreet(array($row['billing_street1'], $row['billing_street2']));
} else {
$billingAddress->setStreet(array($row['billing_street1']));
}
if (isset($row['billing_telephone'])) {
$billingAddress->setTelephone($row['billing_telephone']);
}
if (!$billingAddress->getId()) {
$billingAddress->setIsDefaultBilling(true);
if ($this->getDefaultBilling()) {
$this->setData('default_billing', '');
}
$this->addAddress($billingAddress);
} // End handling billing address
}
if (!$this->validateAddress($row, 'shipping')) {
$this->printError($hlp->__('Invalid shipping address for (%s)', $row['email']), $line);
} else {
// Handling shipping address
$shippingAddress = $this->getPrimaryShippingAddress();
if (!$shippingAddress instanceof Mage_Customer_Model_Address) {
$shippingAddress = Mage::getModel('Mage_Customer_Model_Address');
}
$regions->addRegionNameFilter($row['shipping_region'])->load();
if ($regions) foreach($regions as $region) {
$regionId = intval($region->getId());
}
$shippingAddress->setFirstname($row['firstname']);
$shippingAddress->setLastname($row['lastname']);
$shippingAddress->setCity($row['shipping_city']);
$shippingAddress->setRegion($row['shipping_region']);
if (isset($regionId)) {
$shippingAddress->setRegionId($regionId);
}
$shippingAddress->setCountryId($row['shipping_country']);
$shippingAddress->setPostcode($row['shipping_postcode']);
if (isset($row['shipping_street2'])) {
$shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2']));
} else {
$shippingAddress->setStreet(array($row['shipping_street1']));
}
if (!empty($row['shipping_telephone'])) {
$shippingAddress->setTelephone($row['shipping_telephone']);
}
if (!$shippingAddress->getId()) {
$shippingAddress->setIsDefaultShipping(true);
$this->addAddress($shippingAddress);
}
// End handling shipping address
}
if (!empty($row['is_subscribed'])) {
$isSubscribed = (bool)strtolower($row['is_subscribed']) == self::SUBSCRIBED_YES;
$this->setIsSubscribed($isSubscribed);
}
unset($row);
return $this;
}
/**
* Unset subscription
*
* @return Mage_Customer_Model_Customer
*/
function unsetSubscription()
{