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: class Mage_Customer_Model_Convert_Adapter_Customer
29: extends Mage_Eav_Model_Convert_Adapter_Entity
30: {
31: const MULTI_DELIMITER = ' , ';
32:
33: 34: 35: 36: 37:
38: protected $_customerModel;
39: protected $_stores;
40: protected $_attributes = array();
41: protected $_customerGroups;
42:
43: protected $_billingAddressModel;
44: protected $_shippingAddressModel;
45:
46: protected $_requiredFields = array();
47:
48: protected $_ignoreFields = array();
49:
50: protected $_billingFields = array();
51:
52: protected $_billingMappedFields = array();
53:
54: protected $_billingStreetFields = array();
55:
56: protected $_billingRequiredFields = array();
57:
58: protected $_shippingFields = array();
59:
60: protected $_shippingMappedFields = array();
61:
62: protected $_shippingStreetFields= array();
63:
64: protected $_shippingRequiredFields = array();
65:
66: protected $_addressFields = array();
67:
68: protected $_regions;
69: protected $_websites;
70:
71: protected $_customer = null;
72: protected $_address = null;
73:
74: protected $_customerId = '';
75:
76: 77: 78: 79: 80:
81: public function getCustomerModel()
82: {
83: if (is_null($this->_customerModel)) {
84: $object = Mage::getModel('customer/customer');
85: $this->_customerModel = Mage::objects()->save($object);
86: }
87: return Mage::objects()->load($this->_customerModel);
88: }
89:
90: 91: 92: 93: 94:
95: public function getBillingAddressModel()
96: {
97: if (is_null($this->_billingAddressModel)) {
98: $object = Mage::getModel('customer/address');
99: $this->_billingAddressModel = Mage::objects()->save($object);
100: }
101: return Mage::objects()->load($this->_billingAddressModel);
102: }
103:
104: 105: 106: 107: 108:
109: public function getShippingAddressModel()
110: {
111: if (is_null($this->_shippingAddressModel)) {
112: $object = Mage::getModel('customer/address');
113: $this->_shippingAddressModel = Mage::objects()->save($object);
114: }
115: return Mage::objects()->load($this->_shippingAddressModel);
116: }
117:
118: 119: 120: 121: 122: 123:
124: public function getStoreByCode($store)
125: {
126: if (is_null($this->_stores)) {
127: $this->_stores = Mage::app()->getStores(true, true);
128: }
129: if (isset($this->_stores[$store])) {
130: return $this->_stores[$store];
131: }
132: return false;
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function getWebsiteByCode($websiteCode)
142: {
143: if (is_null($this->_websites)) {
144: $this->_websites = Mage::app()->getWebsites(true, true);
145: }
146: if (isset($this->_websites[$websiteCode])) {
147: return $this->_websites[$websiteCode];
148: }
149: return false;
150: }
151:
152: 153: 154: 155: 156: 157:
158: public function getAttribute($code)
159: {
160: if (!isset($this->_attributes[$code])) {
161: $this->_attributes[$code] = $this->getCustomerModel()->getResource()->getAttribute($code);
162: }
163: return $this->_attributes[$code];
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: public function getRegionId($country, $regionName)
174: {
175: if (is_null($this->_regions)) {
176: $this->_regions = array();
177:
178: $collection = Mage::getModel('directory/region')
179: ->getCollection();
180: foreach ($collection as $region) {
181: if (!isset($this->_regions[$region->getCountryId()])) {
182: $this->_regions[$region->getCountryId()] = array();
183: }
184:
185: $this->_regions[$region->getCountryId()][$region->getDefaultName()] = $region->getId();
186: }
187: }
188:
189: if (isset($this->_regions[$country][$regionName])) {
190: return $this->_regions[$country][$regionName];
191: }
192:
193: return 0;
194: }
195:
196: 197: 198: 199: 200:
201: public function getCustomerGroups()
202: {
203: if (is_null($this->_customerGroups)) {
204: $this->_customerGroups = array();
205: $collection = Mage::getModel('customer/group')
206: ->getCollection()
207: ->addFieldToFilter('customer_group_id', array('gt'=> 0));
208: foreach ($collection as $group) {
209: $this->_customerGroups[$group->getCustomerGroupCode()] = $group->getId();
210: }
211: }
212: return $this->_customerGroups;
213: }
214:
215: 216: 217: 218: 219:
220: public function getCustomerGoups()
221: {
222: return $this->getCustomerGroups();
223: }
224:
225: public function __construct()
226: {
227: $this->setVar('entity_type', 'customer/customer');
228:
229: if (!Mage::registry('Object_Cache_Customer')) {
230: $this->setCustomer(Mage::getModel('customer/customer'));
231: }
232:
233:
234: foreach (Mage::getConfig()->getFieldset('customer_dataflow', 'admin') as $code=>$node) {
235: if ($node->is('ignore')) {
236: $this->_ignoreFields[] = $code;
237: }
238: if ($node->is('billing')) {
239: $this->_billingFields[] = 'billing_'.$code;
240: }
241: if ($node->is('shipping')) {
242: $this->_shippingFields[] = 'shipping_'.$code;
243: }
244:
245: if ($node->is('billing') && $node->is('shipping')) {
246: $this->_addressFields[] = $code;
247: }
248:
249: if ($node->is('mapped') || $node->is('billing_mapped')) {
250: $this->_billingMappedFields['billing_'.$code] = $code;
251: }
252: if ($node->is('mapped') || $node->is('shipping_mapped')) {
253: $this->_shippingMappedFields['shipping_'.$code] = $code;
254: }
255: if ($node->is('street')) {
256: $this->_billingStreetFields[] = 'billing_'.$code;
257: $this->_shippingStreetFields[] = 'shipping_'.$code;
258: }
259: if ($node->is('required')) {
260: $this->_requiredFields[] = $code;
261: }
262: if ($node->is('billing_required')) {
263: $this->_billingRequiredFields[] = 'billing_'.$code;
264: }
265: if ($node->is('shipping_required')) {
266: $this->_shippingRequiredFields[] = 'shipping_'.$code;
267: }
268: }
269: }
270:
271: public function load()
272: {
273: $addressType = $this->getVar('filter/adressType');
274: if ($addressType=='both') {
275: $addressType = array('default_billing','default_shipping');
276: }
277: $attrFilterArray = array();
278: $attrFilterArray ['firstname'] = 'like';
279: $attrFilterArray ['lastname'] = 'like';
280: $attrFilterArray ['email'] = 'like';
281: $attrFilterArray ['group'] = 'eq';
282: $attrFilterArray ['customer_address/telephone'] = array(
283: 'type' => 'like',
284: 'bind' => $addressType
285: );
286: $attrFilterArray ['customer_address/postcode'] = array(
287: 'type' => 'like',
288: 'bind' => $addressType
289: );
290: $attrFilterArray ['customer_address/country'] = array(
291: 'type' => 'eq',
292: 'bind' => $addressType
293: );
294: $attrFilterArray ['customer_address/region'] = array(
295: 'type' => 'like',
296: 'bind' => $addressType
297: );
298: $attrFilterArray ['created_at'] = 'datetimeFromTo';
299:
300: 301: 302:
303: if ($var = $this->getVar('filter/created_at/from')) {
304: $this->setVar('filter/created_at/from', $var . ' 00:00:00');
305: }
306:
307: if ($var = $this->getVar('filter/created_at/to')) {
308: $this->setVar('filter/created_at/to', $var . ' 23:59:59');
309: }
310:
311: $attrToDb = array(
312: 'group' => 'group_id',
313: 'customer_address/country' => 'customer_address/country_id',
314: );
315:
316:
317: if ($storeId = $this->getStoreId()) {
318: $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
319: if ($websiteId) {
320: $this->_filter[] = array(
321: 'attribute' => 'website_id',
322: 'eq' => $websiteId
323: );
324: }
325: }
326:
327: parent::setFilter($attrFilterArray, $attrToDb);
328: return parent::load();
329: }
330:
331: 332: 333:
334: public function parse()
335: {
336: $batchModel = Mage::getSingleton('dataflow/batch');
337:
338:
339: $batchImportModel = $batchModel->getBatchImportModel();
340: $importIds = $batchImportModel->getIdCollection();
341:
342: foreach ($importIds as $importId) {
343: $batchImportModel->load($importId);
344: $importData = $batchImportModel->getBatchData();
345:
346: $this->saveRow($importData);
347: }
348: }
349:
350: public function setCustomer(Mage_Customer_Model_Customer $customer)
351: {
352: $id = Mage::objects()->save($customer);
353: Mage::register('Object_Cache_Customer', $id);
354: }
355:
356: public function getCustomer()
357: {
358: return Mage::objects()->load(Mage::registry('Object_Cache_Customer'));
359: }
360:
361: public function save()
362: {
363: $stores = array();
364: foreach (Mage::getConfig()->getNode('stores')->children() as $storeNode) {
365: $stores[(int)$storeNode->system->store->id] = $storeNode->getName();
366: }
367:
368: $collections = $this->getData();
369: if ($collections instanceof Mage_Customer_Model_Entity_Customer_Collection) {
370: $collections = array($collections->getEntity()->getStoreId()=>$collections);
371: } elseif (!is_array($collections)) {
372: $this->addException(Mage::helper('customer')->__('No customer collections found'), Mage_Dataflow_Model_Convert_Exception::FATAL);
373: }
374:
375: foreach ($collections as $storeId=>$collection) {
376: $this->addException(Mage::helper('customer')->__('Records for %s store found.', $stores[$storeId]));
377:
378: if (!$collection instanceof Mage_Customer_Model_Entity_Customer_Collection) {
379: $this->addException(Mage::helper('customer')->__('Customer collection expected.'), Mage_Dataflow_Model_Convert_Exception::FATAL);
380: }
381: try {
382: $i = 0;
383: foreach ($collection->getIterator() as $model) {
384: $new = false;
385:
386: if (!$model->getId()) {
387: $new = true;
388: $model->save();
389:
390: }
391: if (!$new || 0!==$storeId) {
392:
393:
394:
395:
396:
397: $model->save();
398: }
399: $i++;
400: }
401: $this->addException(Mage::helper('customer')->__("Saved %d record(s)", $i));
402: } catch (Exception $e) {
403: if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
404: $this->addException(Mage::helper('customer')->__('An error occurred while saving the collection, aborting. Error: %s', $e->getMessage()),
405: Mage_Dataflow_Model_Convert_Exception::FATAL);
406: }
407: }
408: }
409: return $this;
410: }
411:
412: 413: 414: 415: 416: 417:
418: public function saveRow($importData)
419: {
420: $customer = $this->getCustomerModel();
421: $customer->setId(null);
422:
423: if (empty($importData['website'])) {
424: $message = Mage::helper('customer')->__('Skipping import row, required field "%s" is not defined.', 'website');
425: Mage::throwException($message);
426: }
427:
428: $website = $this->getWebsiteByCode($importData['website']);
429:
430: if ($website === false) {
431: $message = Mage::helper('customer')->__('Skipping import row, website "%s" field does not exist.', $importData['website']);
432: Mage::throwException($message);
433: }
434: if (empty($importData['email'])) {
435: $message = Mage::helper('customer')->__('Skipping import row, required field "%s" is not defined.', 'email');
436: Mage::throwException($message);
437: }
438:
439: $customer->setWebsiteId($website->getId())
440: ->loadByEmail($importData['email']);
441: if (!$customer->getId()) {
442: $customerGroups = $this->getCustomerGroups();
443: 444: 445:
446: if (empty($importData['group']) || !isset($customerGroups[$importData['group']])) {
447: $value = isset($importData['group']) ? $importData['group'] : '';
448: $message = Mage::helper('catalog')->__('Skipping import row, the value "%s" is not valid for the "%s" field.', $value, 'group');
449: Mage::throwException($message);
450: }
451: $customer->setGroupId($customerGroups[$importData['group']]);
452:
453: foreach ($this->_requiredFields as $field) {
454: if (!isset($importData[$field])) {
455: $message = Mage::helper('catalog')->__('Skip import row, required field "%s" for the new customer is not defined.', $field);
456: Mage::throwException($message);
457: }
458: }
459:
460: $customer->setWebsiteId($website->getId());
461:
462: if (empty($importData['created_in']) || !$this->getStoreByCode($importData['created_in'])) {
463: $customer->setStoreId(0);
464: }
465: else {
466: $customer->setStoreId($this->getStoreByCode($importData['created_in'])->getId());
467: }
468:
469: if (empty($importData['password_hash'])) {
470: $customer->setPasswordHash($customer->hashPassword($customer->generatePassword(8)));
471: }
472: }
473: elseif (!empty($importData['group'])) {
474: $customerGroups = $this->getCustomerGroups();
475: 476: 477:
478: if (isset($customerGroups[$importData['group']])) {
479: $customer->setGroupId($customerGroups[$importData['group']]);
480: }
481: }
482:
483: foreach ($this->_ignoreFields as $field) {
484: if (isset($importData[$field])) {
485: unset($importData[$field]);
486: }
487: }
488:
489: foreach ($importData as $field => $value) {
490: if (in_array($field, $this->_billingFields)) {
491: continue;
492: }
493: if (in_array($field, $this->_shippingFields)) {
494: continue;
495: }
496:
497: $attribute = $this->getAttribute($field);
498: if (!$attribute) {
499: continue;
500: }
501:
502: $isArray = false;
503: $setValue = $value;
504:
505: if ($attribute->getFrontendInput() == 'multiselect') {
506: $value = explode(self::MULTI_DELIMITER, $value);
507: $isArray = true;
508: $setValue = array();
509: }
510:
511: if ($attribute->usesSource()) {
512: $options = $attribute->getSource()->getAllOptions(false);
513:
514: if ($isArray) {
515: foreach ($options as $item) {
516: if (in_array($item['label'], $value)) {
517: $setValue[] = $item['value'];
518: }
519: }
520: }
521: else {
522: $setValue = null;
523: foreach ($options as $item) {
524: if ($item['label'] == $value) {
525: $setValue = $item['value'];
526: }
527: }
528: }
529: }
530:
531: $customer->setData($field, $setValue);
532: }
533:
534: if (isset($importData['is_subscribed'])) {
535: $customer->setData('is_subscribed', $importData['is_subscribed']);
536: }
537:
538: $importBillingAddress = $importShippingAddress = true;
539: $savedBillingAddress = $savedShippingAddress = false;
540:
541: 542: 543:
544: foreach ($this->_billingRequiredFields as $field) {
545: if (empty($importData[$field])) {
546: $importBillingAddress = false;
547: }
548: }
549:
550: 551: 552:
553: foreach ($this->_shippingRequiredFields as $field) {
554: if (empty($importData[$field])) {
555: $importShippingAddress = false;
556: }
557: }
558:
559: $onlyAddress = false;
560:
561: 562: 563:
564: if ($importBillingAddress && $importShippingAddress) {
565: $onlyAddress = true;
566: foreach ($this->_addressFields as $field) {
567: if (!isset($importData['billing_'.$field]) && !isset($importData['shipping_'.$field])) {
568: continue;
569: }
570: if (!isset($importData['billing_'.$field]) || !isset($importData['shipping_'.$field])) {
571: $onlyAddress = false;
572: break;
573: }
574: if ($importData['billing_'.$field] != $importData['shipping_'.$field]) {
575: $onlyAddress = false;
576: break;
577: }
578: }
579:
580: if ($onlyAddress) {
581: $importShippingAddress = false;
582: }
583: }
584:
585: 586: 587:
588: if ($importBillingAddress) {
589: $billingAddress = $this->getBillingAddressModel();
590: if ($customer->getDefaultBilling()) {
591: $billingAddress->load($customer->getDefaultBilling());
592: }
593: else {
594: $billingAddress->setData(array());
595: }
596:
597: foreach ($this->_billingFields as $field) {
598: $cleanField = Mage::helper('core/string')->substr($field, 8);
599:
600: if (isset($importData[$field])) {
601: $billingAddress->setDataUsingMethod($cleanField, $importData[$field]);
602: }
603: elseif (isset($this->_billingMappedFields[$field])
604: && isset($importData[$this->_billingMappedFields[$field]])) {
605: $billingAddress->setDataUsingMethod($cleanField, $importData[$this->_billingMappedFields[$field]]);
606: }
607: }
608:
609: $street = array();
610: foreach ($this->_billingStreetFields as $field) {
611: if (!empty($importData[$field])) {
612: $street[] = $importData[$field];
613: }
614: }
615: if ($street) {
616: $billingAddress->setDataUsingMethod('street', $street);
617: }
618:
619: $billingAddress->setCountryId($importData['billing_country']);
620: $regionName = isset($importData['billing_region']) ? $importData['billing_region'] : '';
621: if ($regionName) {
622: $regionId = $this->getRegionId($importData['billing_country'], $regionName);
623: $billingAddress->setRegionId($regionId);
624: }
625:
626: if ($customer->getId()) {
627: $billingAddress->setCustomerId($customer->getId());
628:
629: $billingAddress->save();
630: $customer->setDefaultBilling($billingAddress->getId());
631:
632: if ($onlyAddress) {
633: $customer->setDefaultShipping($billingAddress->getId());
634: }
635:
636: $savedBillingAddress = true;
637: }
638: }
639:
640: 641: 642:
643: if ($importShippingAddress) {
644: $shippingAddress = $this->getShippingAddressModel();
645: if ($customer->getDefaultShipping() && $customer->getDefaultBilling() != $customer->getDefaultShipping()) {
646: $shippingAddress->load($customer->getDefaultShipping());
647: }
648: else {
649: $shippingAddress->setData(array());
650: }
651:
652: foreach ($this->_shippingFields as $field) {
653: $cleanField = Mage::helper('core/string')->substr($field, 9);
654:
655: if (isset($importData[$field])) {
656: $shippingAddress->setDataUsingMethod($cleanField, $importData[$field]);
657: }
658: elseif (isset($this->_shippingMappedFields[$field])
659: && isset($importData[$this->_shippingMappedFields[$field]])) {
660: $shippingAddress->setDataUsingMethod($cleanField, $importData[$this->_shippingMappedFields[$field]]);
661: }
662: }
663:
664: $street = array();
665: foreach ($this->_shippingStreetFields as $field) {
666: if (!empty($importData[$field])) {
667: $street[] = $importData[$field];
668: }
669: }
670: if ($street) {
671: $shippingAddress->setDataUsingMethod('street', $street);
672: }
673:
674: $shippingAddress->setCountryId($importData['shipping_country']);
675: $regionName = isset($importData['shipping_region']) ? $importData['shipping_region'] : '';
676: if ($regionName) {
677: $regionId = $this->getRegionId($importData['shipping_country'], $regionName);
678: $shippingAddress->setRegionId($regionId);
679: }
680:
681: if ($customer->getId()) {
682: $shippingAddress->setCustomerId($customer->getId());
683: $shippingAddress->save();
684: $customer->setDefaultShipping($shippingAddress->getId());
685:
686: $savedShippingAddress = true;
687: }
688: }
689:
690: $customer->setImportMode(true);
691: $customer->save();
692: $saveCustomer = false;
693:
694: if ($importBillingAddress && !$savedBillingAddress) {
695: $saveCustomer = true;
696: $billingAddress->setCustomerId($customer->getId());
697: $billingAddress->save();
698: $customer->setDefaultBilling($billingAddress->getId());
699: if ($onlyAddress) {
700: $customer->setDefaultShipping($billingAddress->getId());
701: }
702: }
703: if ($importShippingAddress && !$savedShippingAddress) {
704: $saveCustomer = true;
705: $shippingAddress->setCustomerId($customer->getId());
706: $shippingAddress->save();
707: $customer->setDefaultShipping($shippingAddress->getId());
708: }
709: if ($saveCustomer) {
710: $customer->save();
711: }
712:
713: return $this;
714: }
715:
716: public function getCustomerId()
717: {
718: return $this->_customerId;
719: }
720:
721:
722:
723: public function saveRow__OLD()
724: {
725:
726: $mem = memory_get_usage(); $origMem = $mem; $memory = $mem;
727: $customer = $this->getCustomer();
728: @set_time_limit(240);
729: $row = $args;
730: $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
731: $customer->importFromTextArray($row);
732:
733: if (!$customer->getData()) {
734: return;
735: }
736:
737: $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
738: try {
739: $customer->save();
740: $this->_customerId = $customer->getId();
741: $customer->unsetData();
742: $customer->cleanAllAddresses();
743: $customer->unsetSubscription();
744: $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
745:
746: } catch (Exception $e) {
747: }
748: unset($row);
749: return array('memory'=>$memory);
750: }
751: }
752: