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_Adapter_Customer
00030 extends Mage_Eav_Model_Convert_Adapter_Entity
00031 {
00032 const MULTI_DELIMITER = ' , ';
00033
00034
00035
00036
00037
00038
00039 protected $_customerModel;
00040 protected $_stores;
00041 protected $_attributes = array();
00042 protected $_customerGroups;
00043
00044 protected $_billingAddressModel;
00045 protected $_shippingAddressModel;
00046
00047 protected $_requiredFields = array();
00048
00049 protected $_ignoreFields = array();
00050
00051 protected $_billingFields = array();
00052
00053 protected $_billingMappedFields = array();
00054
00055 protected $_billingStreetFields = array();
00056
00057 protected $_billingRequiredFields = array();
00058
00059 protected $_shippingFields = array();
00060
00061 protected $_shippingMappedFields = array();
00062
00063 protected $_shippingStreetFields= array();
00064
00065 protected $_shippingRequiredFields = array();
00066
00067 protected $_addressFields = array();
00068
00069 protected $_regions;
00070 protected $_websites;
00071
00072 protected $_customer = null;
00073 protected $_address = null;
00074
00075 protected $_customerId = '';
00076
00077
00078
00079
00080
00081
00082 public function getCustomerModel()
00083 {
00084 if (is_null($this->_customerModel)) {
00085 $object = Mage::getModel('customer/customer');
00086 $this->_customerModel = Mage::objects()->save($object);
00087 }
00088 return Mage::objects()->load($this->_customerModel);
00089 }
00090
00091
00092
00093
00094
00095
00096 public function getBillingAddressModel()
00097 {
00098 if (is_null($this->_billingAddressModel)) {
00099 $object = Mage::getModel('customer/address');
00100 $this->_billingAddressModel = Mage::objects()->save($object);
00101 }
00102 return Mage::objects()->load($this->_billingAddressModel);
00103 }
00104
00105
00106
00107
00108
00109
00110 public function getShippingAddressModel()
00111 {
00112 if (is_null($this->_shippingAddressModel)) {
00113 $object = Mage::getModel('customer/address');
00114 $this->_shippingAddressModel = Mage::objects()->save($object);
00115 }
00116 return Mage::objects()->load($this->_shippingAddressModel);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 public function getStoreByCode($store)
00126 {
00127 if (is_null($this->_stores)) {
00128 $this->_stores = Mage::app()->getStores(true, true);
00129 }
00130 if (isset($this->_stores[$store])) {
00131 return $this->_stores[$store];
00132 }
00133 return false;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 public function getWebsiteByCode($websiteCode)
00143 {
00144 if (is_null($this->_websites)) {
00145 $this->_websites = Mage::app()->getWebsites(true, true);
00146 }
00147 if (isset($this->_websites[$websiteCode])) {
00148 return $this->_websites[$websiteCode];
00149 }
00150 return false;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 public function getAttribute($code)
00160 {
00161 if (!isset($this->_attributes[$code])) {
00162 $this->_attributes[$code] = $this->getCustomerModel()->getResource()->getAttribute($code);
00163 }
00164 return $this->_attributes[$code];
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174 public function getRegionId($country, $regionName)
00175 {
00176 if (is_null($this->_regions)) {
00177 $this->_regions = array();
00178
00179 $collection = Mage::getModel('directory/region')
00180 ->getCollection();
00181 foreach ($collection as $region) {
00182 if (!isset($this->_regions[$region->getCountryId()])) {
00183 $this->_regions[$region->getCountryId()] = array();
00184 }
00185
00186 $this->_regions[$region->getCountryId()][$region->getDefaultName()] = $region->getId();
00187 }
00188 }
00189
00190 if (isset($this->_regions[$country][$regionName])) {
00191 return $this->_regions[$country][$regionName];
00192 }
00193
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getCustomerGoups()
00203 {
00204 if (is_null($this->_customerGroups)) {
00205 $this->_customerGroups = array();
00206 $collection = Mage::getModel('customer/group')
00207 ->getCollection()
00208 ->addFieldToFilter('customer_group_id', array('gt'=> 0));
00209 foreach ($collection as $group) {
00210 $this->_customerGroups[$group->getCustomerGroupCode()] = $group->getId();
00211 }
00212 }
00213 return $this->_customerGroups;
00214 }
00215
00216
00217
00218 public function __construct()
00219 {
00220 $this->setVar('entity_type', 'customer/customer');
00221
00222 if (!Mage::registry('Object_Cache_Customer')) {
00223 $this->setCustomer(Mage::getModel('customer/customer'));
00224 }
00225
00226
00227 foreach (Mage::getConfig()->getFieldset('customer_dataflow', 'admin') as $code=>$node) {
00228 if ($node->is('ignore')) {
00229 $this->_ignoreFields[] = $code;
00230 }
00231 if ($node->is('billing')) {
00232 $this->_billingFields[] = 'billing_'.$code;
00233 }
00234 if ($node->is('shipping')) {
00235 $this->_shippingFields[] = 'shipping_'.$code;
00236 }
00237
00238 if ($node->is('billing') && $node->is('shipping')) {
00239 $this->_addressFields[] = $code;
00240 }
00241
00242 if ($node->is('mapped') || $node->is('billing_mapped')) {
00243 $this->_billingMappedFields['billing_'.$code] = $code;
00244 }
00245 if ($node->is('mapped') || $node->is('shipping_mapped')) {
00246 $this->_shippingMappedFields['shipping_'.$code] = $code;
00247 }
00248 if ($node->is('street')) {
00249 $this->_billingStreetFields[] = 'billing_'.$code;
00250 $this->_shippingStreetFields[] = 'shipping_'.$code;
00251 }
00252 if ($node->is('required')) {
00253 $this->_requiredFields[] = $code;
00254 }
00255 if ($node->is('billing_required')) {
00256 $this->_billingRequiredFields[] = 'billing_'.$code;
00257 }
00258 if ($node->is('shipping_required')) {
00259 $this->_shippingRequiredFields[] = 'shipping_'.$code;
00260 }
00261 }
00262 }
00263
00264 public function load()
00265 {
00266 $addressType = $this->getVar('filter/addressType');
00267 if ($addressType=='both') {
00268 $addressType = array('default_billing','default_shipping');
00269 }
00270 $attrFilterArray = array();
00271 $attrFilterArray ['firstname'] = 'like';
00272 $attrFilterArray ['lastname'] = 'like';
00273 $attrFilterArray ['email'] = 'like';
00274 $attrFilterArray ['group'] = 'eq';
00275 $attrFilterArray ['customer_address/telephone'] = array(
00276 'type' => 'like',
00277 'bind' => $addressType
00278 );
00279 $attrFilterArray ['customer_address/postcode'] = array(
00280 'type' => 'like',
00281 'bind' => $addressType
00282 );
00283 $attrFilterArray ['customer_address/country'] = array(
00284 'type' => 'eq',
00285 'bind' => $addressType
00286 );
00287 $attrFilterArray ['customer_address/region'] = array(
00288 'type' => 'like',
00289 'bind' => $addressType
00290 );
00291 $attrFilterArray ['created_at'] = 'datetimeFromTo';
00292
00293
00294
00295
00296 if ($var = $this->getVar('filter/created_at/from')) {
00297 $this->setVar('filter/created_at/from', $var . ' 00:00:00');
00298 }
00299
00300 if ($var = $this->getVar('filter/created_at/to')) {
00301 $this->setVar('filter/created_at/to', $var . ' 23:59:59');
00302 }
00303
00304 $attrToDb = array(
00305 'group' => 'group_id',
00306 'customer_address/country' => 'customer_address/country_id',
00307 );
00308
00309
00310 if ($storeId = $this->getStoreId()) {
00311 $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
00312 if ($websiteId) {
00313 $this->_filter[] = array(
00314 'attribute' => 'website_id',
00315 'eq' => $websiteId
00316 );
00317 }
00318 }
00319
00320 parent::setFilter($attrFilterArray, $attrToDb);
00321 return parent::load();
00322 }
00323
00324
00325
00326
00327 public function parse()
00328 {
00329 $batchModel = Mage::getSingleton('dataflow/batch');
00330
00331
00332 $batchImportModel = $batchModel->getBatchImportModel();
00333 $importIds = $batchImportModel->getIdCollection();
00334
00335 foreach ($importIds as $importId) {
00336 $batchImportModel->load($importId);
00337 $importData = $batchImportModel->getBatchData();
00338
00339 $this->saveRow($importData);
00340 }
00341 }
00342
00343 public function setCustomer(Mage_Customer_Model_Customer $customer)
00344 {
00345 $id = Mage::objects()->save($customer);
00346 Mage::register('Object_Cache_Customer', $id);
00347 }
00348
00349 public function getCustomer()
00350 {
00351 return Mage::objects()->load(Mage::registry('Object_Cache_Customer'));
00352 }
00353
00354 public function save()
00355 {
00356 $stores = array();
00357 foreach (Mage::getConfig()->getNode('stores')->children() as $storeNode) {
00358 $stores[(int)$storeNode->system->store->id] = $storeNode->getName();
00359 }
00360
00361 $collections = $this->getData();
00362 if ($collections instanceof Mage_Customer_Model_Entity_Customer_Collection) {
00363 $collections = array($collections->getEntity()->getStoreId()=>$collections);
00364 } elseif (!is_array($collections)) {
00365 $this->addException(Mage::helper('customer')->__('No product collections found'), Mage_Dataflow_Model_Convert_Exception::FATAL);
00366 }
00367
00368 foreach ($collections as $storeId=>$collection) {
00369 $this->addException(Mage::helper('customer')->__('Records for "'.$stores[$storeId].'" store found'));
00370
00371 if (!$collection instanceof Mage_Customer_Model_Entity_Customer_Collection) {
00372 $this->addException(Mage::helper('customer')->__('Customer collection expected'), Mage_Dataflow_Model_Convert_Exception::FATAL);
00373 }
00374 try {
00375 $i = 0;
00376 foreach ($collection->getIterator() as $model) {
00377 $new = false;
00378
00379 if (!$model->getId()) {
00380 $new = true;
00381 $model->save();
00382 #Mage::getResourceSingleton('catalog_entity/convert')->addProductToStore($model->getId(), 0);
00383 }
00384 if (!$new || 0!==$storeId) {
00385
00386
00387
00388
00389
00390 $model->save();
00391 }
00392 $i++;
00393 }
00394 $this->addException(Mage::helper('customer')->__("Saved ".$i." record(s)"));
00395 } catch (Exception $e) {
00396 if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
00397 $this->addException(Mage::helper('customer')->__('Problem saving the collection, aborting. Error: %s', $e->getMessage()),
00398 Mage_Dataflow_Model_Convert_Exception::FATAL);
00399 }
00400 }
00401 }
00402 return $this;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411 public function saveRow($importData)
00412 {
00413 $customer = $this->getCustomerModel();
00414 $customer->setId(null);
00415 $customer->setImportMode(true);
00416
00417 if (empty($importData['website'])) {
00418 $message = Mage::helper('customer')->__('Skip import row, required field "%s" not defined', 'website');
00419 Mage::throwException($message);
00420 }
00421
00422 $website = $this->getWebsiteByCode($importData['website']);
00423
00424 if ($website === false) {
00425 $message = Mage::helper('customer')->__('Skip import row, website "%s" field not exists', $importData['website']);
00426 Mage::throwException($message);
00427 }
00428 if (empty($importData['email'])) {
00429 $message = Mage::helper('customer')->__('Skip import row, required field "%s" not defined', 'email');
00430 Mage::throwException($message);
00431 }
00432
00433 $customer->setWebsiteId($website->getId())
00434 ->loadByEmail($importData['email']);
00435 if (!$customer->getId()) {
00436 $customerGroups = $this->getCustomerGoups();
00437
00438
00439
00440 if (empty($importData['group_id']) || !isset($customerGroups[$importData['group_id']])) {
00441 $value = isset($importData['group_id']) ? $importData['group_id'] : '';
00442 $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'group_id');
00443 Mage::throwException($message);
00444 }
00445 $customer->setGroupId($customerGroups[$importData['group_id']]);
00446
00447 foreach ($this->_requiredFields as $field) {
00448 if (!isset($importData[$field])) {
00449 $message = Mage::helper('catalog')->__('Skip import row, required field "%s" for new customer not defined', $field);
00450 Mage::throwException($message);
00451 }
00452 }
00453
00454 $customer->setWebsiteId($website->getId());
00455
00456 if (empty($importData['created_in']) || !$this->getStoreByCode($importData['created_in'])) {
00457 $customer->setStoreId(0);
00458 }
00459 else {
00460 $customer->setStoreId($this->getStoreByCode($importData['created_in'])->getId());
00461 }
00462
00463 if (empty($importData['password_hash'])) {
00464 $customer->setPasswordHash($customer->hashPassword($customer->generatePassword(8)));
00465 }
00466 }
00467 elseif (!empty($importData['group_id'])) {
00468 $customerGroups = $this->getCustomerGoups();
00469
00470
00471
00472 if (isset($customerGroups[$importData['group_id']])) {
00473 $customer->setGroupId($customerGroups[$importData['group_id']]);
00474 }
00475 }
00476
00477 foreach ($this->_ignoreFields as $field) {
00478 if (isset($importData[$field])) {
00479 unset($importData[$field]);
00480 }
00481 }
00482
00483 foreach ($importData as $field => $value) {
00484 if (in_array($field, $this->_billingFields)) {
00485 continue;
00486 }
00487 if (in_array($field, $this->_shippingFields)) {
00488 continue;
00489 }
00490
00491 $attribute = $this->getAttribute($field);
00492 if (!$attribute) {
00493 continue;
00494 }
00495
00496 $isArray = false;
00497 $setValue = $value;
00498
00499 if ($attribute->getFrontendInput() == 'multiselect') {
00500 $value = split(self::MULTI_DELIMITER, $value);
00501 $isArray = true;
00502 $setValue = array();
00503 }
00504
00505 if ($attribute->usesSource()) {
00506 $options = $attribute->getSource()->getAllOptions(false);
00507
00508 if ($isArray) {
00509 foreach ($options as $item) {
00510 if (in_array($item['label'], $value)) {
00511 $setValue[] = $item['value'];
00512 }
00513 }
00514 }
00515 else {
00516 $setValue = null;
00517 foreach ($options as $item) {
00518 if ($item['label'] == $value) {
00519 $setValue = $item['value'];
00520 }
00521 }
00522 }
00523 }
00524
00525 $customer->setData($field, $setValue);
00526 }
00527
00528 if (isset($importData['is_subscribed'])) {
00529 $customer->setData('is_subscribed', $importData['is_subscribed']);
00530 }
00531
00532 $importBillingAddress = $importShippingAddress = true;
00533 $savedBillingAddress = $savedShippingAddress = false;
00534
00535
00536
00537
00538 foreach ($this->_billingRequiredFields as $field) {
00539 if (empty($importData[$field])) {
00540 $importBillingAddress = false;
00541 }
00542 }
00543
00544
00545
00546
00547 foreach ($this->_shippingRequiredFields as $field) {
00548 if (empty($importData[$field])) {
00549 $importShippingAddress = false;
00550 }
00551 }
00552
00553 $onlyAddress = false;
00554
00555
00556
00557
00558 if ($importBillingAddress && $importShippingAddress) {
00559 $onlyAddress = true;
00560 foreach ($this->_addressFields as $field) {
00561 if (!isset($importData['billing_'.$field]) && !isset($importData['shipping_'.$field])) {
00562 continue;
00563 }
00564 if (!isset($importData['billing_'.$field]) || !isset($importData['shipping_'.$field])) {
00565 $onlyAddress = false;
00566 break;
00567 }
00568 if ($importData['billing_'.$field] != $importData['shipping_'.$field]) {
00569 $onlyAddress = false;
00570 break;
00571 }
00572 }
00573
00574 if ($onlyAddress) {
00575 $importShippingAddress = false;
00576 }
00577 }
00578
00579
00580
00581
00582 if ($importBillingAddress) {
00583 $billingAddress = $this->getBillingAddressModel();
00584 if ($customer->getDefaultBilling()) {
00585 $billingAddress->load($customer->getDefaultBilling());
00586 }
00587 else {
00588 $billingAddress->setData(array());
00589 }
00590
00591 foreach ($this->_billingFields as $field) {
00592 $cleanField = Mage::helper('core/string')->substr($field, 8);
00593
00594 if (isset($importData[$field])) {
00595 $billingAddress->setDataUsingMethod($cleanField, $importData[$field]);
00596 }
00597 elseif (isset($this->_billingMappedFields[$field])
00598 && isset($importData[$this->_billingMappedFields[$field]])) {
00599 $billingAddress->setDataUsingMethod($cleanField, $importData[$this->_billingMappedFields[$field]]);
00600 }
00601 }
00602
00603 $street = array();
00604 foreach ($this->_billingStreetFields as $field) {
00605 if (!empty($importData[$field])) {
00606 $street[] = $importData[$field];
00607 }
00608 }
00609 if ($street) {
00610 $billingAddress->setDataUsingMethod('street', $street);
00611 }
00612
00613 $billingAddress->setCountryId($importData['billing_country']);
00614 $regionName = isset($importData['billing_region']) ? $importData['billing_region'] : '';
00615 if ($regionName) {
00616 $regionId = $this->getRegionId($importData['billing_country'], $regionName);
00617 $billingAddress->setRegionId($regionId);
00618 }
00619
00620 if ($customer->getId()) {
00621 $billingAddress->setCustomerId($customer->getId());
00622
00623 $billingAddress->save();
00624 $customer->setDefaultBilling($billingAddress->getId());
00625
00626 if ($onlyAddress) {
00627 $customer->setDefaultShipping($billingAddress->getId());
00628 }
00629
00630 $savedBillingAddress = true;
00631 }
00632 }
00633
00634
00635
00636
00637 if ($importShippingAddress) {
00638 $shippingAddress = $this->getShippingAddressModel();
00639 if ($customer->getDefaultShipping() && $customer->getDefaultBilling() != $customer->getDefaultShipping()) {
00640 $shippingAddress->load($customer->getDefaultShipping());
00641 }
00642 else {
00643 $shippingAddress->setData(array());
00644 }
00645
00646 foreach ($this->_shippingFields as $field) {
00647 $cleanField = Mage::helper('core/string')->substr($field, 9);
00648
00649 if (isset($importData[$field])) {
00650 $shippingAddress->setDataUsingMethod($cleanField, $importData[$field]);
00651 }
00652 elseif (isset($this->_shippingMappedFields[$field])
00653 && isset($importData[$this->_shippingMappedFields[$field]])) {
00654 $shippingAddress->setDataUsingMethod($cleanField, $importData[$this->_shippingMappedFields[$field]]);
00655 }
00656 }
00657
00658 $street = array();
00659 foreach ($this->_shippingStreetFields as $field) {
00660 if (!empty($importData[$field])) {
00661 $street[] = $importData[$field];
00662 }
00663 }
00664 if ($street) {
00665 $shippingAddress->setDataUsingMethod('street', $street);
00666 }
00667
00668 $shippingAddress->setCountryId($importData['shipping_country']);
00669 $regionName = isset($importData['shipping_region']) ? $importData['shipping_region'] : '';
00670 if ($regionName) {
00671 $regionId = $this->getRegionId($importData['shipping_country'], $regionName);
00672 $shippingAddress->setRegionId($regionId);
00673 }
00674
00675 if ($customer->getId()) {
00676 $shippingAddress->setCustomerId($customer->getId());
00677 $shippingAddress->save();
00678 $customer->setDefaultShipping($shippingAddress->getId());
00679
00680 $savedShippingAddress = true;
00681 }
00682 }
00683
00684 $customer->save();
00685 $saveCustomer = false;
00686
00687 if ($importBillingAddress && !$savedBillingAddress) {
00688 $saveCustomer = true;
00689 $billingAddress->setCustomerId($customer->getId());
00690 $billingAddress->save();
00691 $customer->setDefaultBilling($billingAddress->getId());
00692 if ($onlyAddress) {
00693 $customer->setDefaultShipping($billingAddress->getId());
00694 }
00695 }
00696 if ($importShippingAddress && !$savedShippingAddress) {
00697 $saveCustomer = true;
00698 $shippingAddress->setCustomerId($customer->getId());
00699 $shippingAddress->save();
00700 $customer->setDefaultShipping($shippingAddress->getId());
00701 }
00702 if ($saveCustomer) {
00703 $customer->save();
00704 }
00705
00706 return $this;
00707 }
00708
00709 public function getCustomerId()
00710 {
00711 return $this->_customerId;
00712 }
00713
00714
00715
00716 public function saveRow__OLD()
00717 {
00718
00719 $mem = memory_get_usage(); $origMem = $mem; $memory = $mem;
00720 $customer = $this->getCustomer();
00721 @set_time_limit(240);
00722 $row = $args;
00723 $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
00724 $customer->importFromTextArray($row);
00725
00726 if (!$customer->getData()) {
00727 return;
00728 }
00729
00730 $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
00731 try {
00732 $customer->save();
00733 $this->_customerId = $customer->getId();
00734 $customer->unsetData();
00735 $customer->cleanAllAddresses();
00736 $customer->unsetSubscription();
00737 $newMem = memory_get_usage(); $memory .= ', '.($newMem-$mem); $mem = $newMem;
00738
00739 } catch (Exception $e) {
00740 }
00741 unset($row);
00742 return array('memory'=>$memory);
00743 }
00744 }