00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class Mage_Customer_Model_Convert_Parser_Customer
00030 extends Mage_Eav_Model_Convert_Parser_Abstract
00031 {
00032 const MULTI_DELIMITER = ' , ';
00033
00034 protected $_resource;
00035
00036
00037
00038
00039
00040
00041 protected $_collections;
00042
00043 protected $_customerModel;
00044 protected $_customerAddressModel;
00045 protected $_newsletterModel;
00046 protected $_store;
00047 protected $_storeId;
00048
00049 protected $_stores;
00050
00051
00052
00053
00054
00055
00056 protected $_websites;
00057 protected $_attributes = array();
00058
00059 protected $_fields;
00060
00061 public function getFields()
00062 {
00063 if (!$this->_fields) {
00064 $this->_fields = Mage::getConfig()->getFieldset('customer_dataflow', 'admin');
00065 }
00066 return $this->_fields;
00067 }
00068
00069
00070
00071
00072
00073
00074 public function getCustomerModel()
00075 {
00076 if (is_null($this->_customerModel)) {
00077 $object = Mage::getModel('customer/customer');
00078 $this->_customerModel = Mage::objects()->save($object);
00079 }
00080 return Mage::objects()->load($this->_customerModel);
00081 }
00082
00083
00084
00085
00086
00087
00088 public function getCustomerAddressModel()
00089 {
00090 if (is_null($this->_customerAddressModel)) {
00091 $object = Mage::getModel('customer/address');
00092 $this->_customerAddressModel = Mage::objects()->save($object);
00093 }
00094 return Mage::objects()->load($this->_customerAddressModel);
00095 }
00096
00097
00098
00099
00100
00101
00102 public function getNewsletterModel()
00103 {
00104 if (is_null($this->_newsletterModel)) {
00105 $object = Mage::getModel('newsletter/subscriber');
00106 $this->_newsletterModel = Mage::objects()->save($object);
00107 }
00108 return Mage::objects()->load($this->_newsletterModel);
00109 }
00110
00111
00112
00113
00114
00115
00116 public function getStore()
00117 {
00118 if (is_null($this->_store)) {
00119 try {
00120 $store = Mage::app()->getStore($this->getVar('store'));
00121 }
00122 catch (Exception $e) {
00123 $this->addException(Mage::helper('catalog')->__('Invalid store specified'), Varien_Convert_Exception::FATAL);
00124 throw $e;
00125 }
00126 $this->_store = $store;
00127 }
00128 return $this->_store;
00129 }
00130
00131
00132
00133
00134
00135
00136 public function getStoreId()
00137 {
00138 if (is_null($this->_storeId)) {
00139 $this->_storeId = $this->getStore()->getId();
00140 }
00141 return $this->_storeId;
00142 }
00143
00144 public function getStoreById($storeId)
00145 {
00146 if (is_null($this->_stores)) {
00147 $this->_stores = Mage::app()->getStores(true);
00148 }
00149 if (isset($this->_stores[$storeId])) {
00150 return $this->_stores[$storeId];
00151 }
00152 return false;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 public function getWebsiteById($websiteId)
00162 {
00163 if (is_null($this->_websites)) {
00164 $this->_websites = Mage::app()->getWebsites(true);
00165 }
00166 if (isset($this->_websites[$websiteId])) {
00167 return $this->_websites[$websiteId];
00168 }
00169 return false;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public function getAttribute($code)
00179 {
00180 if (!isset($this->_attributes[$code])) {
00181 $this->_attributes[$code] = $this->getCustomerModel()->getResource()->getAttribute($code);
00182 }
00183 return $this->_attributes[$code];
00184 }
00185
00186
00187
00188
00189 public function getResource()
00190 {
00191 if (!$this->_resource) {
00192 $this->_resource = Mage::getResourceSingleton('catalog_entity/convert');
00193 #->loadStores()
00194 #->loadProducts()
00195 #->loadAttributeSets()
00196 #->loadAttributeOptions();
00197 }
00198 return $this->_resource;
00199 }
00200
00201 public function getCollection($storeId)
00202 {
00203 if (!isset($this->_collections[$storeId])) {
00204 $this->_collections[$storeId] = Mage::getResourceModel('customer/customer_collection');
00205 $this->_collections[$storeId]->getEntity()->setStore($storeId);
00206 }
00207 return $this->_collections[$storeId];
00208 }
00209
00210 public function unparse()
00211 {
00212 $systemFields = array();
00213 foreach ($this->getFields() as $code=>$node) {
00214 if ($node->is('system')) {
00215 $systemFields[] = $code;
00216 }
00217 }
00218
00219 $entityIds = $this->getData();
00220
00221 foreach ($entityIds as $i => $entityId) {
00222 $customer = $this->getCustomerModel()
00223 ->setData(array())
00224 ->load($entityId);
00225
00226
00227 $position = Mage::helper('catalog')->__('Line %d, Email: %s', ($i+1), $customer->getEmail());
00228 $this->setPosition($position);
00229
00230 $row = array();
00231
00232 foreach ($customer->getData() as $field => $value) {
00233 if ($field == 'website_id') {
00234 $website = $this->getWebsiteById($value);
00235 if ($website === false) {
00236 $website = $this->getWebsiteById(0);
00237 }
00238 $row['website'] = $website->getCode();
00239 continue;
00240 }
00241
00242 if (in_array($field, $systemFields) || is_object($value)) {
00243 continue;
00244 }
00245
00246 $attribute = $this->getAttribute($field);
00247 if (!$attribute) {
00248 continue;
00249 }
00250
00251 if ($attribute->usesSource()) {
00252
00253 $option = $attribute->getSource()->getOptionText($value);
00254 if ($value && empty($option)) {
00255 $message = Mage::helper('catalog')->__("Invalid option id specified for %s (%s), skipping the record", $field, $value);
00256 $this->addException($message, Mage_Dataflow_Model_Convert_Exception::ERROR);
00257 continue;
00258 }
00259 if (is_array($option)) {
00260 $value = join(self::MULTI_DELIMITER, $option);
00261 } else {
00262 $value = $option;
00263 }
00264 unset($option);
00265 }
00266 elseif (is_array($value)) {
00267 continue;
00268 }
00269 $row[$field] = $value;
00270 }
00271
00272 $defaultBillingId = $customer->getDefaultBilling();
00273 $defaultShippingId = $customer->getDefaultShipping();
00274
00275 $customerAddress = $this->getCustomerAddressModel();
00276
00277 if (!$defaultBillingId) {
00278 foreach ($this->getFields() as $code=>$node) {
00279 if ($node->is('billing')) {
00280 $row['billing_'.$code] = null;
00281 }
00282 }
00283 }
00284 else {
00285 $customerAddress->load($defaultBillingId);
00286
00287 foreach ($this->getFields() as $code=>$node) {
00288 if ($node->is('billing')) {
00289 $row['billing_'.$code] = $customerAddress->getDataUsingMethod($code);
00290 }
00291 }
00292 }
00293
00294 if (!$defaultShippingId) {
00295 foreach ($this->getFields() as $code=>$node) {
00296 if ($node->is('shipping')) {
00297 $row['shipping_'.$code] = null;
00298 }
00299 }
00300 }
00301 else {
00302 if ($defaultShippingId != $defaultBillingId) {
00303 $customerAddress->load($defaultShippingId);
00304 }
00305 foreach ($this->getFields() as $code=>$node) {
00306 if ($node->is('shipping')) {
00307 $row['shipping_'.$code] = $customerAddress->getDataUsingMethod($code);
00308 }
00309 }
00310 }
00311
00312 $store = $this->getStoreById($customer->getStoreId());
00313 if ($store === false) {
00314 $store = $this->getStoreById(0);
00315 }
00316 $row['created_in'] = $store->getCode();
00317
00318 $newsletter = $this->getNewsletterModel()
00319 ->loadByCustomer($customer);
00320 $row['is_subscribed'] = ($newsletter->getId()
00321 && $newsletter->getSubscriberStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED)
00322 ? 1 : 0;
00323
00324 $batchExport = $this->getBatchExportModel()
00325 ->setId(null)
00326 ->setBatchId($this->getBatchModel()->getId())
00327 ->setBatchData($row)
00328 ->setStatus(1)
00329 ->save();
00330 }
00331
00332 return $this;
00333 }
00334
00335 public function getExternalAttributes()
00336 {
00337 $internal = array(
00338 'store_id',
00339 'entity_id',
00340 'website_id',
00341 'group_id',
00342 'created_in',
00343 'default_billing',
00344 'default_shipping',
00345 'country_id'
00346 );
00347
00348 $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId();
00349 $customerAttributes = Mage::getResourceModel('eav/entity_attribute_collection')
00350 ->setEntityTypeFilter($entityTypeId)
00351 ->load()->getIterator();
00352
00353 $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer_address')->getId();
00354 $addressAttributes = Mage::getResourceModel('eav/entity_attribute_collection')
00355 ->setEntityTypeFilter($entityTypeId)
00356 ->load()->getIterator();
00357
00358 $attributes = array(
00359 'website' => 'website',
00360 'email' => 'email',
00361 'group' => 'group',
00362 'create_in' => 'create_in',
00363 'is_subscribed' => 'is_subscribed'
00364 );
00365
00366 foreach ($customerAttributes as $attr) {
00367 $code = $attr->getAttributeCode();
00368 if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
00369 continue;
00370 }
00371 $attributes[$code] = $code;
00372 }
00373 $attributes['password_hash'] = 'password_hash';
00374
00375 foreach ($addressAttributes as $attr) {
00376 $code = $attr->getAttributeCode();
00377 if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
00378 continue;
00379 }
00380 $attributes['billing_'.$code] = 'billing_'.$code;
00381 }
00382 $attributes['billing_country'] = 'billing_country';
00383
00384 foreach ($addressAttributes as $attr) {
00385 $code = $attr->getAttributeCode();
00386 if (in_array($code, $internal) || $attr->getFrontendInput()=='hidden') {
00387 continue;
00388 }
00389 $attributes['shipping_'.$code] = 'shipping_'.$code;
00390 }
00391 $attributes['shipping_country'] = 'shipping_country';
00392
00393 return $attributes;
00394 }
00395
00396
00397
00398 public function unparse__OLD()
00399 {
00400 $collections = $this->getData();
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 $data = array();
00413
00414 foreach ($collections->getIterator() as $i=>$model) {
00415 $this->setPosition('Line: '.($i+1).', Email: '.$model->getEmail());
00416
00417
00418
00419
00420 $row = array(
00421 'store_view'=>$this->getStoreCode($this->getVar('store') ? $this->getVar('store') : $storeId),
00422 );
00423
00424 foreach ($model->getData() as $field=>$value) {
00425
00426 if ($field == 'website_id') {
00427 $row['website_code'] = Mage::getModel('core/website')->load($value)->getCode();
00428 continue;
00429 }
00430
00431 if (in_array($field, $systemFields)) {
00432 continue;
00433 }
00434
00435 $attribute = $model->getResource()->getAttribute($field);
00436 if (!$attribute) {
00437 continue;
00438 }
00439
00440 if ($attribute->usesSource()) {
00441 $option = $attribute->getSource()->getOptionText($value);
00442
00443 if (false===$option) {
00444 $this->addException(Mage::helper('customer')->__("Invalid option id specified for %s (%s), skipping the record", $field, $value), Mage_Dataflow_Model_Convert_Exception::ERROR);
00445 continue;
00446 }
00447 if (is_array($option)) {
00448 $value = $option['label'];
00449 } else {
00450 $value = $option;
00451 }
00452 }
00453 $row[$field] = $value;
00454
00455 $billingAddress = $model->getDefaultBillingAddress();
00456 if($billingAddress instanceof Mage_Customer_Model_Address){
00457 $billingAddress->explodeStreetAddress();
00458 $row['billing_street1'] = $billingAddress->getStreet1();
00459 $row['billing_street2'] = $billingAddress->getStreet2();
00460 $row['billing_city'] = $billingAddress->getCity();
00461 $row['billing_region'] = $billingAddress->getRegion();
00462 $row['billing_country'] = $billingAddress->getCountry();
00463 $row['billing_postcode'] = $billingAddress->getPostcode();
00464 $row['billing_telephone'] = $billingAddress->getTelephone();
00465 }
00466
00467 $shippingAddress = $model->getDefaultShippingAddress();
00468 if($shippingAddress instanceof Mage_Customer_Model_Address){
00469 $shippingAddress->explodeStreetAddress();
00470 $row['shipping_street1'] = $shippingAddress->getStreet1();
00471 $row['shipping_street2'] = $shippingAddress->getStreet2();
00472 $row['shipping_city'] = $shippingAddress->getCity();
00473 $row['shipping_region'] = $shippingAddress->getRegion();
00474 $row['shipping_country'] = $shippingAddress->getCountry();
00475 $row['shipping_postcode'] = $shippingAddress->getPostcode();
00476 $row['shipping_telephone'] = $shippingAddress->getTelephone();
00477 }
00478
00479 if($model->getGroupId()){
00480 $group = Mage::getResourceModel('customer/group_collection')
00481 ->addFilter('customer_group_id',$model->getGroupId())
00482 ->load();
00483 $row['group']=$group->getFirstItem()->getData('customer_group_code');
00484 }
00485 }
00486 $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($model);
00487 if ($subscriber->getId()) {
00488 $row['is_subscribed'] = $subscriber->getSubscriberStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED ? Mage_Customer_Model_Customer::SUBSCRIBED_YES : Mage_Customer_Model_Customer::SUBSCRIBED_NO;
00489 }
00490 if(!isset($row['created_in'])){
00491 $row['created_in'] = 'Admin';
00492 }
00493 $data[] = $row;
00494
00495 }
00496
00497 $this->setData($data);
00498 return $this;
00499 }
00500
00501
00502
00503
00504 public function parse()
00505 {
00506 $data = $this->getData();
00507
00508 $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId();
00509 $result = array();
00510 foreach ($data as $i=>$row) {
00511 $this->setPosition('Line: '.($i+1));
00512 try {
00513
00514
00515 if (empty($row['email'])) {
00516 $this->addException(Mage::helper('customer')->__('Missing email, skipping the record'), Varien_Convert_Exception::ERROR);
00517 continue;
00518 }
00519 $this->setPosition('Line: '.($i+1).', email: '.$row['email']);
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 if (empty($row['attribute_set'])) {
00530 $row['attribute_set'] = 'Default';
00531 }
00532
00533
00534 $row['attribute_set_id'] = $this->getAttributeSetId($entityTypeId, $row['attribute_set']);
00535 if (!$row['attribute_set_id']) {
00536 $this->addException(Mage::helper('customer')->__("Invalid attribute set specified, skipping the record"), Varien_Convert_Exception::ERROR);
00537 continue;
00538 }
00539
00540 if (empty($row['group'])) {
00541 $row['group'] = 'General';
00542 }
00543
00544 if (empty($row['firstname'])) {
00545 $this->addException(Mage::helper('customer')->__('Missing firstname, skipping the record'), Varien_Convert_Exception::ERROR);
00546 continue;
00547 }
00548
00549
00550 if (empty($row['lastname'])) {
00551 $this->addException(Mage::helper('customer')->__('Missing lastname, skipping the record'), Varien_Convert_Exception::ERROR);
00552 continue;
00553 }
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 $storeIds = $this->getStoreIds(isset($row['store']) ? $row['store'] : $this->getVar('store'));
00567 if (!$storeIds) {
00568 $this->addException(Mage::helper('customer')->__("Invalid store specified, skipping the record"), Varien_Convert_Exception::ERROR);
00569 continue;
00570 }
00571
00572
00573 $rowError = false;
00574 foreach ($storeIds as $storeId) {
00575 $collection = $this->getCollection($storeId);
00576
00577 $entity = $collection->getEntity();
00578
00579 $model = Mage::getModel('customer/customer');
00580 $model->setStoreId($storeId);
00581 if (!empty($row['entity_id'])) {
00582 $model->load($row['entity_id']);
00583 }
00584 foreach ($row as $field=>$value) {
00585 $attribute = $entity->getAttribute($field);
00586 if (!$attribute) {
00587 continue;
00588 #$this->addException(Mage::helper('catalog')->__("Unknown attribute: %s", $field), Varien_Convert_Exception::ERROR);
00589
00590 }
00591
00592 if ($attribute->usesSource()) {
00593 $source = $attribute->getSource();
00594 $optionId = $this->getSourceOptionId($source, $value);
00595 if (is_null($optionId)) {
00596 $rowError = true;
00597 $this->addException(Mage::helper('customer')->__("Invalid attribute option specified for attribute %s (%s), skipping the record", $field, $value), Varien_Convert_Exception::ERROR);
00598 continue;
00599 }
00600 $value = $optionId;
00601 }
00602 $model->setData($field, $value);
00603
00604 }
00605
00606
00607 $billingAddress = $model->getPrimaryBillingAddress();
00608 $customer = Mage::getModel('customer/customer')->load($model->getId());
00609
00610
00611 if (!$billingAddress instanceof Mage_Customer_Model_Address) {
00612 $billingAddress = new Mage_Customer_Model_Address();
00613 if ($customer->getId() && $customer->getDefaultBilling()) {
00614 $billingAddress->setId($customer->getDefaultBilling());
00615 }
00616 }
00617
00618 $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['billing_region'])->load();
00619 if ($regions) foreach($regions as $region) {
00620 $regionId = $region->getId();
00621 }
00622
00623 $billingAddress->setFirstname($row['firstname']);
00624 $billingAddress->setLastname($row['lastname']);
00625 $billingAddress->setCity($row['billing_city']);
00626 $billingAddress->setRegion($row['billing_region']);
00627 $billingAddress->setRegionId($regionId);
00628 $billingAddress->setCountryId($row['billing_country']);
00629 $billingAddress->setPostcode($row['billing_postcode']);
00630 $billingAddress->setStreet(array($row['billing_street1'],$row['billing_street2']));
00631 if (!empty($row['billing_telephone'])) {
00632 $billingAddress->setTelephone($row['billing_telephone']);
00633 }
00634
00635 if (!$model->getDefaultBilling()) {
00636 $billingAddress->setCustomerId($model->getId());
00637 $billingAddress->setIsDefaultBilling(true);
00638 $billingAddress->save();
00639 $model->setDefaultBilling($billingAddress->getId());
00640 $model->addAddress($billingAddress);
00641 if ($customer->getDefaultBilling()) {
00642 $model->setDefaultBilling($customer->getDefaultBilling());
00643 } else {
00644 $shippingAddress->save();
00645 $model->setDefaultShipping($billingAddress->getId());
00646 $model->addAddress($billingAddress);
00647
00648 }
00649 }
00650
00651 $shippingAddress = $model->getPrimaryShippingAddress();
00652 if (!$shippingAddress instanceof Mage_Customer_Model_Address) {
00653 $shippingAddress = new Mage_Customer_Model_Address();
00654 if ($customer->getId() && $customer->getDefaultShipping()) {
00655 $shippingAddress->setId($customer->getDefaultShipping());
00656 }
00657 }
00658
00659 $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['shipping_region'])->load();
00660 if ($regions) foreach($regions as $region) {
00661 $regionId = $region->getId();
00662 }
00663
00664 $shippingAddress->setFirstname($row['firstname']);
00665 $shippingAddress->setLastname($row['lastname']);
00666 $shippingAddress->setCity($row['shipping_city']);
00667 $shippingAddress->setRegion($row['shipping_region']);
00668 $shippingAddress->setRegionId($regionId);
00669 $shippingAddress->setCountryId($row['shipping_country']);
00670 $shippingAddress->setPostcode($row['shipping_postcode']);
00671 $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2']));
00672 $shippingAddress->setCustomerId($model->getId());
00673 if (!empty($row['shipping_telephone'])) {
00674 $shippingAddress->setTelephone($row['shipping_telephone']);
00675 }
00676
00677 if (!$model->getDefaultShipping()) {
00678 if ($customer->getDefaultShipping()) {
00679 $model->setDefaultShipping($customer->getDefaultShipping());
00680 } else {
00681 $shippingAddress->save();
00682 $model->setDefaultShipping($shippingAddress->getId());
00683 $model->addAddress($shippingAddress);
00684
00685 }
00686 $shippingAddress->setIsDefaultShipping(true);
00687 }
00688
00689 if (!$rowError) {
00690 $collection->addItem($model);
00691 }
00692
00693 }
00694
00695 } catch (Exception $e) {
00696 if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
00697 $this->addException(Mage::helper('customer')->__("Error during retrieval of option value: %s", $e->getMessage()), Mage_Dataflow_Model_Convert_Exception::FATAL);
00698 }
00699 }
00700 }
00701 $this->setData($this->_collections);
00702 return $this;
00703 }
00704 }