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_Parser_Customer
29: extends Mage_Eav_Model_Convert_Parser_Abstract
30: {
31: const MULTI_DELIMITER = ' , ';
32:
33: protected $_resource;
34:
35: 36: 37: 38: 39:
40: protected $_collections;
41:
42: protected $_customerModel;
43: protected $_customerAddressModel;
44: protected $_newsletterModel;
45: protected $_store;
46: protected $_storeId;
47:
48: protected $_stores;
49:
50: 51: 52: 53: 54:
55: protected $_websites;
56: protected $_attributes = array();
57:
58: protected $_fields;
59:
60: 61: 62: 63:
64: protected $_customerGroups = null;
65:
66: public function getFields()
67: {
68: if (!$this->_fields) {
69: $this->_fields = Mage::getConfig()->getFieldset('customer_dataflow', 'admin');
70: }
71: return $this->_fields;
72: }
73:
74: 75: 76: 77: 78:
79: public function getCustomerModel()
80: {
81: if (is_null($this->_customerModel)) {
82: $object = Mage::getModel('customer/customer');
83: $this->_customerModel = Mage::objects()->save($object);
84: }
85: return Mage::objects()->load($this->_customerModel);
86: }
87:
88: 89: 90: 91: 92:
93: public function getCustomerAddressModel()
94: {
95: if (is_null($this->_customerAddressModel)) {
96: $object = Mage::getModel('customer/address');
97: $this->_customerAddressModel = Mage::objects()->save($object);
98: }
99: return Mage::objects()->load($this->_customerAddressModel);
100: }
101:
102: 103: 104: 105: 106:
107: public function getNewsletterModel()
108: {
109: if (is_null($this->_newsletterModel)) {
110: $object = Mage::getModel('newsletter/subscriber');
111: $this->_newsletterModel = Mage::objects()->save($object);
112: }
113: return Mage::objects()->load($this->_newsletterModel);
114: }
115:
116: 117: 118: 119: 120:
121: public function getStore()
122: {
123: if (is_null($this->_store)) {
124: try {
125: $store = Mage::app()->getStore($this->getVar('store'));
126: }
127: catch (Exception $e) {
128: $this->addException(Mage::helper('catalog')->__('An invalid store was specified.'), Varien_Convert_Exception::FATAL);
129: throw $e;
130: }
131: $this->_store = $store;
132: }
133: return $this->_store;
134: }
135:
136: 137: 138: 139: 140:
141: public function getStoreId()
142: {
143: if (is_null($this->_storeId)) {
144: $this->_storeId = $this->getStore()->getId();
145: }
146: return $this->_storeId;
147: }
148:
149: public function getStoreById($storeId)
150: {
151: if (is_null($this->_stores)) {
152: $this->_stores = Mage::app()->getStores(true);
153: }
154: if (isset($this->_stores[$storeId])) {
155: return $this->_stores[$storeId];
156: }
157: return false;
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function getWebsiteById($websiteId)
167: {
168: if (is_null($this->_websites)) {
169: $this->_websites = Mage::app()->getWebsites(true);
170: }
171: if (isset($this->_websites[$websiteId])) {
172: return $this->_websites[$websiteId];
173: }
174: return false;
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function getAttribute($code)
184: {
185: if (!isset($this->_attributes[$code])) {
186: $this->_attributes[$code] = $this->getCustomerModel()->getResource()->getAttribute($code);
187: }
188: return $this->_attributes[$code];
189: }
190:
191: 192: 193:
194: public function getResource()
195: {
196: if (!$this->_resource) {
197: $this->_resource = Mage::getResourceSingleton('catalog_entity/convert');
198:
199:
200:
201:
202: }
203: return $this->_resource;
204: }
205:
206: public function getCollection($storeId)
207: {
208: if (!isset($this->_collections[$storeId])) {
209: $this->_collections[$storeId] = Mage::getResourceModel('customer/customer_collection');
210: $this->_collections[$storeId]->getEntity()->setStore($storeId);
211: }
212: return $this->_collections[$storeId];
213: }
214:
215: public function unparse()
216: {
217: $systemFields = array();
218: foreach ($this->getFields() as $code=>$node) {
219: if ($node->is('system')) {
220: $systemFields[] = $code;
221: }
222: }
223:
224: $entityIds = $this->getData();
225:
226: foreach ($entityIds as $i => $entityId) {
227: $customer = $this->getCustomerModel()
228: ->setData(array())
229: ->load($entityId);
230:
231:
232: $position = Mage::helper('catalog')->__('Line %d, Email: %s', ($i+1), $customer->getEmail());
233: $this->setPosition($position);
234:
235: $row = array();
236:
237: foreach ($customer->getData() as $field => $value) {
238: if ($field == 'website_id') {
239: $website = $this->getWebsiteById($value);
240: if ($website === false) {
241: $website = $this->getWebsiteById(0);
242: }
243: $row['website'] = $website->getCode();
244: continue;
245: }
246:
247: if (in_array($field, $systemFields) || is_object($value)) {
248: continue;
249: }
250:
251: $attribute = $this->getAttribute($field);
252: if (!$attribute) {
253: continue;
254: }
255:
256: if ($attribute->usesSource()) {
257:
258: $option = $attribute->getSource()->getOptionText($value);
259: if ($value && empty($option)) {
260: $message = Mage::helper('catalog')->__("An invalid option ID is specified for %s (%s), skipping the record.", $field, $value);
261: $this->addException($message, Mage_Dataflow_Model_Convert_Exception::ERROR);
262: continue;
263: }
264: if (is_array($option)) {
265: $value = join(self::MULTI_DELIMITER, $option);
266: } else {
267: $value = $option;
268: }
269: unset($option);
270: }
271: elseif (is_array($value)) {
272: continue;
273: }
274: $row[$field] = $value;
275: }
276:
277: $defaultBillingId = $customer->getDefaultBilling();
278: $defaultShippingId = $customer->getDefaultShipping();
279:
280: $customerAddress = $this->getCustomerAddressModel();
281:
282: if (!$defaultBillingId) {
283: foreach ($this->getFields() as $code=>$node) {
284: if ($node->is('billing')) {
285: $row['billing_'.$code] = null;
286: }
287: }
288: }
289: else {
290: $customerAddress->load($defaultBillingId);
291:
292: foreach ($this->getFields() as $code=>$node) {
293: if ($node->is('billing')) {
294: $row['billing_'.$code] = $customerAddress->getDataUsingMethod($code);
295: }
296: }
297: }
298:
299: if (!$defaultShippingId) {
300: foreach ($this->getFields() as $code=>$node) {
301: if ($node->is('shipping')) {
302: $row['shipping_'.$code] = null;
303: }
304: }
305: }
306: else {
307: if ($defaultShippingId != $defaultBillingId) {
308: $customerAddress->load($defaultShippingId);
309: }
310: foreach ($this->getFields() as $code=>$node) {
311: if ($node->is('shipping')) {
312: $row['shipping_'.$code] = $customerAddress->getDataUsingMethod($code);
313: }
314: }
315: }
316:
317: $store = $this->getStoreById($customer->getStoreId());
318: if ($store === false) {
319: $store = $this->getStoreById(0);
320: }
321: $row['created_in'] = $store->getCode();
322:
323: $newsletter = $this->getNewsletterModel()
324: ->setData(array())
325: ->loadByCustomer($customer);
326: $row['is_subscribed'] = ($newsletter->getId()
327: && $newsletter->getSubscriberStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED)
328: ? 1 : 0;
329:
330: if($customer->getGroupId()){
331: $groupCode = $this->_getCustomerGroupCode($customer);
332: if (is_null($groupCode)) {
333: $this->addException(
334: Mage::helper('catalog')->__("An invalid group ID is specified, skipping the record."),
335: Mage_Dataflow_Model_Convert_Exception::ERROR
336: );
337: continue;
338: } else {
339: $row['group'] = $groupCode;
340: }
341: }
342:
343: $batchExport = $this->getBatchExportModel()
344: ->setId(null)
345: ->setBatchId($this->getBatchModel()->getId())
346: ->setBatchData($row)
347: ->setStatus(1)
348: ->save();
349: }
350:
351: return $this;
352: }
353:
354: public function getExternalAttributes()
355: {
356: $internal = array(
357: 'store_id',
358: 'entity_id',
359: 'website_id',
360: 'group_id',
361: 'created_in',
362: 'default_billing',
363: 'default_shipping',
364: 'country_id'
365: );
366:
367: $customerAttributes = Mage::getResourceModel('customer/attribute_collection')
368: ->load()->getIterator();
369:
370: $addressAttributes = Mage::getResourceModel('customer/address_attribute_collection')
371: ->load()->getIterator();
372:
373: $attributes = array(
374: 'website' => 'website',
375: 'email' => 'email',
376: 'group' => 'group',
377: 'create_in' => 'create_in',
378: 'is_subscribed' => 'is_subscribed'
379: );
380:
381: foreach ($customerAttributes as $attr) {
382: $code = $attr->getAttributeCode();
383: if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
384: continue;
385: }
386: $attributes[$code] = $code;
387: }
388: $attributes['password_hash'] = 'password_hash';
389:
390: foreach ($addressAttributes as $attr) {
391: $code = $attr->getAttributeCode();
392: if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
393: continue;
394: }
395:
396: if ($code == 'street') {
397: $attributes['billing_'.$code.'_full'] = 'billing_'.$code;
398: } else {
399: $attributes['billing_'.$code] = 'billing_'.$code;
400: }
401: }
402: $attributes['billing_country'] = 'billing_country';
403:
404: foreach ($addressAttributes as $attr) {
405: $code = $attr->getAttributeCode();
406: if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
407: continue;
408: }
409:
410: if ($code == 'street') {
411: $attributes['shipping_'.$code.'_full'] = 'shipping_'.$code;
412: } else {
413: $attributes['shipping_'.$code] = 'shipping_'.$code;
414: }
415: }
416: $attributes['shipping_country'] = 'shipping_country';
417:
418: return $attributes;
419: }
420:
421: 422: 423: 424: 425: 426:
427: protected function _getCustomerGroupCode($customer)
428: {
429: if (is_null($this->_customerGroups)) {
430: $groups = Mage::getResourceModel('customer/group_collection')
431: ->load();
432:
433: foreach ($groups as $group) {
434: $this->_customerGroups[$group->getId()] = $group->getData('customer_group_code');
435: }
436: }
437:
438: if (isset($this->_customerGroups[$customer->getGroupId()])) {
439: return $this->_customerGroups[$customer->getGroupId()];
440: } else {
441: return null;
442: }
443: }
444:
445:
446:
447: public function unparse__OLD()
448: {
449: $collections = $this->getData();
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461: $data = array();
462:
463: foreach ($collections->getIterator() as $i=>$model) {
464: $this->setPosition('Line: '.($i+1).', Email: '.$model->getEmail());
465:
466:
467:
468:
469: $row = array(
470: 'store_view'=>$this->getStoreCode($this->getVar('store') ? $this->getVar('store') : $storeId),
471: );
472:
473: foreach ($model->getData() as $field=>$value) {
474:
475: if ($field == 'website_id') {
476: $row['website_code'] = Mage::getModel('core/website')->load($value)->getCode();
477: continue;
478: }
479:
480: if (in_array($field, $systemFields)) {
481: continue;
482: }
483:
484: $attribute = $model->getResource()->getAttribute($field);
485: if (!$attribute) {
486: continue;
487: }
488:
489: if ($attribute->usesSource()) {
490: $option = $attribute->getSource()->getOptionText($value);
491:
492: if (false===$option) {
493: $this->addException(Mage::helper('customer')->__("An invalid option ID is specified for %s (%s), skipping the record.", $field, $value), Mage_Dataflow_Model_Convert_Exception::ERROR);
494: continue;
495: }
496: if (is_array($option)) {
497: $value = $option['label'];
498: } else {
499: $value = $option;
500: }
501: }
502: $row[$field] = $value;
503:
504: $billingAddress = $model->getDefaultBillingAddress();
505: if($billingAddress instanceof Mage_Customer_Model_Address){
506: $billingAddress->explodeStreetAddress();
507: $row['billing_street1'] = $billingAddress->getStreet1();
508: $row['billing_street2'] = $billingAddress->getStreet2();
509: $row['billing_city'] = $billingAddress->getCity();
510: $row['billing_region'] = $billingAddress->getRegion();
511: $row['billing_country'] = $billingAddress->getCountry();
512: $row['billing_postcode'] = $billingAddress->getPostcode();
513: $row['billing_telephone'] = $billingAddress->getTelephone();
514: }
515:
516: $shippingAddress = $model->getDefaultShippingAddress();
517: if($shippingAddress instanceof Mage_Customer_Model_Address){
518: $shippingAddress->explodeStreetAddress();
519: $row['shipping_street1'] = $shippingAddress->getStreet1();
520: $row['shipping_street2'] = $shippingAddress->getStreet2();
521: $row['shipping_city'] = $shippingAddress->getCity();
522: $row['shipping_region'] = $shippingAddress->getRegion();
523: $row['shipping_country'] = $shippingAddress->getCountry();
524: $row['shipping_postcode'] = $shippingAddress->getPostcode();
525: $row['shipping_telephone'] = $shippingAddress->getTelephone();
526: }
527:
528: if($model->getGroupId()){
529: $group = Mage::getResourceModel('customer/group_collection')
530: ->addFilter('customer_group_id',$model->getGroupId())
531: ->load();
532: $row['group']=$group->getFirstItem()->getData('customer_group_code');
533: }
534: }
535: $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($model);
536: if ($subscriber->getId()) {
537: if ($subscriber->getSubscriberStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED) {
538: $row['is_subscribed'] = Mage_Customer_Model_Customer::SUBSCRIBED_YES;
539: } else {
540: $row['is_subscribed'] = Mage_Customer_Model_Customer::SUBSCRIBED_NO;
541: }
542: }
543: if(!isset($row['created_in'])){
544: $row['created_in'] = 'Admin';
545: }
546: $data[] = $row;
547:
548: }
549:
550: $this->setData($data);
551: return $this;
552: }
553:
554: 555: 556:
557: public function parse()
558: {
559: $data = $this->getData();
560:
561: $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId();
562: $result = array();
563: foreach ($data as $i=>$row) {
564: $this->setPosition('Line: '.($i+1));
565: try {
566:
567:
568: if (empty($row['email'])) {
569: $this->addException(Mage::helper('customer')->__('Missing email, skipping the record.'), Varien_Convert_Exception::ERROR);
570: continue;
571: }
572: $this->setPosition('Line: '.($i+1).', email: '.$row['email']);
573:
574:
575: 576: 577: 578: 579:
580:
581:
582: if (empty($row['attribute_set'])) {
583: $row['attribute_set'] = 'Default';
584: }
585:
586:
587: $row['attribute_set_id'] = $this->getAttributeSetId($entityTypeId, $row['attribute_set']);
588: if (!$row['attribute_set_id']) {
589: $this->addException(Mage::helper('customer')->__("Invalid attribute set specified, skipping the record."), Varien_Convert_Exception::ERROR);
590: continue;
591: }
592:
593: if (empty($row['group'])) {
594: $row['group'] = 'General';
595: }
596:
597: if (empty($row['firstname'])) {
598: $this->addException(Mage::helper('customer')->__('Missing firstname, skipping the record.'), Varien_Convert_Exception::ERROR);
599: continue;
600: }
601:
602:
603: if (empty($row['lastname'])) {
604: $this->addException(Mage::helper('customer')->__('Missing lastname, skipping the record.'), Varien_Convert_Exception::ERROR);
605: continue;
606: }
607:
608:
609: 610: 611: 612: 613: 614: 615: 616:
617:
618:
619: $storeIds = $this->getStoreIds(isset($row['store']) ? $row['store'] : $this->getVar('store'));
620: if (!$storeIds) {
621: $this->addException(Mage::helper('customer')->__("Invalid store specified, skipping the record."), Varien_Convert_Exception::ERROR);
622: continue;
623: }
624:
625:
626: $rowError = false;
627: foreach ($storeIds as $storeId) {
628: $collection = $this->getCollection($storeId);
629:
630: $entity = $collection->getEntity();
631:
632: $model = Mage::getModel('customer/customer');
633: $model->setStoreId($storeId);
634: if (!empty($row['entity_id'])) {
635: $model->load($row['entity_id']);
636: }
637: foreach ($row as $field=>$value) {
638: $attribute = $entity->getAttribute($field);
639: if (!$attribute) {
640: continue;
641:
642:
643: }
644:
645: if ($attribute->usesSource()) {
646: $source = $attribute->getSource();
647: $optionId = $this->getSourceOptionId($source, $value);
648: if (is_null($optionId)) {
649: $rowError = true;
650: $this->addException(Mage::helper('customer')->__("Invalid attribute option specified for attribute %s (%s), skipping the record.", $field, $value), Varien_Convert_Exception::ERROR);
651: continue;
652: }
653: $value = $optionId;
654: }
655: $model->setData($field, $value);
656:
657: }
658:
659:
660: $billingAddress = $model->getPrimaryBillingAddress();
661: $customer = Mage::getModel('customer/customer')->load($model->getId());
662:
663:
664: if (!$billingAddress instanceof Mage_Customer_Model_Address) {
665: $billingAddress = Mage::getModel('customer/address');
666: if ($customer->getId() && $customer->getDefaultBilling()) {
667: $billingAddress->setId($customer->getDefaultBilling());
668: }
669: }
670:
671: $regions = Mage::getResourceModel('directory/region_collection')
672: ->addRegionNameFilter($row['billing_region'])
673: ->load();
674: if ($regions) foreach($regions as $region) {
675: $regionId = $region->getId();
676: }
677:
678: $billingAddress->setFirstname($row['firstname']);
679: $billingAddress->setLastname($row['lastname']);
680: $billingAddress->setCity($row['billing_city']);
681: $billingAddress->setRegion($row['billing_region']);
682: $billingAddress->setRegionId($regionId);
683: $billingAddress->setCountryId($row['billing_country']);
684: $billingAddress->setPostcode($row['billing_postcode']);
685: $billingAddress->setStreet(array($row['billing_street1'],$row['billing_street2']));
686: if (!empty($row['billing_telephone'])) {
687: $billingAddress->setTelephone($row['billing_telephone']);
688: }
689:
690: if (!$model->getDefaultBilling()) {
691: $billingAddress->setCustomerId($model->getId());
692: $billingAddress->setIsDefaultBilling(true);
693: $billingAddress->save();
694: $model->setDefaultBilling($billingAddress->getId());
695: $model->addAddress($billingAddress);
696: if ($customer->getDefaultBilling()) {
697: $model->setDefaultBilling($customer->getDefaultBilling());
698: } else {
699: $shippingAddress->save();
700: $model->setDefaultShipping($billingAddress->getId());
701: $model->addAddress($billingAddress);
702:
703: }
704: }
705:
706: $shippingAddress = $model->getPrimaryShippingAddress();
707: if (!$shippingAddress instanceof Mage_Customer_Model_Address) {
708: $shippingAddress = Mage::getModel('customer/address');
709: if ($customer->getId() && $customer->getDefaultShipping()) {
710: $shippingAddress->setId($customer->getDefaultShipping());
711: }
712: }
713:
714: $regions = Mage::getResourceModel('directory/region_collection')
715: ->addRegionNameFilter($row['shipping_region'])
716: ->load();
717: if ($regions) foreach($regions as $region) {
718: $regionId = $region->getId();
719: }
720:
721: $shippingAddress->setFirstname($row['firstname']);
722: $shippingAddress->setLastname($row['lastname']);
723: $shippingAddress->setCity($row['shipping_city']);
724: $shippingAddress->setRegion($row['shipping_region']);
725: $shippingAddress->setRegionId($regionId);
726: $shippingAddress->setCountryId($row['shipping_country']);
727: $shippingAddress->setPostcode($row['shipping_postcode']);
728: $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2']));
729: $shippingAddress->setCustomerId($model->getId());
730: if (!empty($row['shipping_telephone'])) {
731: $shippingAddress->setTelephone($row['shipping_telephone']);
732: }
733:
734: if (!$model->getDefaultShipping()) {
735: if ($customer->getDefaultShipping()) {
736: $model->setDefaultShipping($customer->getDefaultShipping());
737: } else {
738: $shippingAddress->save();
739: $model->setDefaultShipping($shippingAddress->getId());
740: $model->addAddress($shippingAddress);
741:
742: }
743: $shippingAddress->setIsDefaultShipping(true);
744: }
745:
746: if (!$rowError) {
747: $collection->addItem($model);
748: }
749:
750: }
751:
752: } catch (Exception $e) {
753: if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
754: $this->addException(Mage::helper('customer')->__('An error occurred while retrieving the option value: %s.', $e->getMessage()), Mage_Dataflow_Model_Convert_Exception::FATAL);
755: }
756: }
757: }
758: $this->setData($this->_collections);
759: return $this;
760: }
761: }
762: