/**
  * @deprecated not used anymore
  */
 public function parse()
 {
     $data = $this->getData();
     $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId();
     $result = array();
     foreach ($data as $i => $row) {
         $this->setPosition('Line: ' . ($i + 1));
         try {
             // validate SKU
             if (empty($row['email'])) {
                 $this->addException(Mage::helper('customer')->__('Missing email, skipping the record'), Varien_Convert_Exception::ERROR);
                 continue;
             }
             $this->setPosition('Line: ' . ($i + 1) . ', email: ' . $row['email']);
             // try to get entity_id by sku if not set
             /*
             if (empty($row['entity_id'])) {
                 $row['entity_id'] = $this->getResource()->getProductIdBySku($row['email']);
             }
             */
             // if attribute_set not set use default
             if (empty($row['attribute_set'])) {
                 $row['attribute_set'] = 'Default';
             }
             // get attribute_set_id, if not throw error
             $row['attribute_set_id'] = $this->getAttributeSetId($entityTypeId, $row['attribute_set']);
             if (!$row['attribute_set_id']) {
                 $this->addException(Mage::helper('customer')->__("Invalid attribute set specified, skipping the record"), Varien_Convert_Exception::ERROR);
                 continue;
             }
             if (empty($row['group'])) {
                 $row['group'] = 'General';
             }
             if (empty($row['firstname'])) {
                 $this->addException(Mage::helper('customer')->__('Missing firstname, skipping the record'), Varien_Convert_Exception::ERROR);
                 continue;
             }
             //$this->setPosition('Line: '.($i+1).', Firstname: '.$row['firstname']);
             if (empty($row['lastname'])) {
                 $this->addException(Mage::helper('customer')->__('Missing lastname, skipping the record'), Varien_Convert_Exception::ERROR);
                 continue;
             }
             //$this->setPosition('Line: '.($i+1).', Lastname: '.$row['lastname']);
             /*
             // get product type_id, if not throw error
             $row['type_id'] = $this->getProductTypeId($row['type']);
             if (!$row['type_id']) {
                 $this->addException(Mage::helper('catalog')->__("Invalid product type specified, skipping the record"), Varien_Convert_Exception::ERROR);
                 continue;
             }
             */
             // get store ids
             $storeIds = $this->getStoreIds(isset($row['store']) ? $row['store'] : $this->getVar('store'));
             if (!$storeIds) {
                 $this->addException(Mage::helper('customer')->__("Invalid store specified, skipping the record"), Varien_Convert_Exception::ERROR);
                 continue;
             }
             // import data
             $rowError = false;
             foreach ($storeIds as $storeId) {
                 $collection = $this->getCollection($storeId);
                 //print_r($collection);
                 $entity = $collection->getEntity();
                 $model = Mage::getModel('customer/customer');
                 $model->setStoreId($storeId);
                 if (!empty($row['entity_id'])) {
                     $model->load($row['entity_id']);
                 }
                 foreach ($row as $field => $value) {
                     $attribute = $entity->getAttribute($field);
                     if (!$attribute) {
                         continue;
                         #$this->addException(Mage::helper('catalog')->__("Unknown attribute: %s", $field), Varien_Convert_Exception::ERROR);
                     }
                     if ($attribute->usesSource()) {
                         $source = $attribute->getSource();
                         $optionId = $this->getSourceOptionId($source, $value);
                         if (is_null($optionId)) {
                             $rowError = true;
                             $this->addException(Mage::helper('customer')->__("Invalid attribute option specified for attribute %s (%s), skipping the record", $field, $value), Varien_Convert_Exception::ERROR);
                             continue;
                         }
                         $value = $optionId;
                     }
                     $model->setData($field, $value);
                 }
                 //foreach ($row as $field=>$value)
                 $billingAddress = $model->getPrimaryBillingAddress();
                 $customer = Mage::getModel('customer/customer')->load($model->getId());
                 if (!$billingAddress instanceof Mage_Customer_Model_Address) {
                     $billingAddress = new Mage_Customer_Model_Address();
                     if ($customer->getId() && $customer->getDefaultBilling()) {
                         $billingAddress->setId($customer->getDefaultBilling());
                     }
                 }
                 $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['billing_region'])->load();
                 if ($regions) {
                     foreach ($regions as $region) {
                         $regionId = $region->getId();
                     }
                 }
                 $billingAddress->setFirstname($row['firstname']);
                 $billingAddress->setLastname($row['lastname']);
                 $billingAddress->setCity($row['billing_city']);
                 $billingAddress->setRegion($row['billing_region']);
                 $billingAddress->setRegionId($regionId);
                 $billingAddress->setCountryId($row['billing_country']);
                 $billingAddress->setPostcode($row['billing_postcode']);
                 $billingAddress->setStreet(array($row['billing_street1'], $row['billing_street2']));
                 if (!empty($row['billing_telephone'])) {
                     $billingAddress->setTelephone($row['billing_telephone']);
                 }
                 if (!$model->getDefaultBilling()) {
                     $billingAddress->setCustomerId($model->getId());
                     $billingAddress->setIsDefaultBilling(true);
                     $billingAddress->save();
                     $model->setDefaultBilling($billingAddress->getId());
                     $model->addAddress($billingAddress);
                     if ($customer->getDefaultBilling()) {
                         $model->setDefaultBilling($customer->getDefaultBilling());
                     } else {
                         $shippingAddress->save();
                         $model->setDefaultShipping($billingAddress->getId());
                         $model->addAddress($billingAddress);
                     }
                 }
                 $shippingAddress = $model->getPrimaryShippingAddress();
                 if (!$shippingAddress instanceof Mage_Customer_Model_Address) {
                     $shippingAddress = new Mage_Customer_Model_Address();
                     if ($customer->getId() && $customer->getDefaultShipping()) {
                         $shippingAddress->setId($customer->getDefaultShipping());
                     }
                 }
                 $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['shipping_region'])->load();
                 if ($regions) {
                     foreach ($regions as $region) {
                         $regionId = $region->getId();
                     }
                 }
                 $shippingAddress->setFirstname($row['firstname']);
                 $shippingAddress->setLastname($row['lastname']);
                 $shippingAddress->setCity($row['shipping_city']);
                 $shippingAddress->setRegion($row['shipping_region']);
                 $shippingAddress->setRegionId($regionId);
                 $shippingAddress->setCountryId($row['shipping_country']);
                 $shippingAddress->setPostcode($row['shipping_postcode']);
                 $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2']));
                 $shippingAddress->setCustomerId($model->getId());
                 if (!empty($row['shipping_telephone'])) {
                     $shippingAddress->setTelephone($row['shipping_telephone']);
                 }
                 if (!$model->getDefaultShipping()) {
                     if ($customer->getDefaultShipping()) {
                         $model->setDefaultShipping($customer->getDefaultShipping());
                     } else {
                         $shippingAddress->save();
                         $model->setDefaultShipping($shippingAddress->getId());
                         $model->addAddress($shippingAddress);
                     }
                     $shippingAddress->setIsDefaultShipping(true);
                 }
                 if (!$rowError) {
                     $collection->addItem($model);
                 }
             }
             //foreach ($storeIds as $storeId)
         } catch (Exception $e) {
             if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
                 $this->addException(Mage::helper('customer')->__("Error during retrieval of option value: %s", $e->getMessage()), Mage_Dataflow_Model_Convert_Exception::FATAL);
             }
         }
     }
     $this->setData($this->_collections);
     return $this;
 }
Example #2
0
 /**
  * Importing customer data from text array
  *
  * @param array $row
  * @return uMage_Customer_Model_Customer
  */
 public function importFromTextArray(array $row)
 {
     $this->resetErrors();
     $hlp = Mage::helper('customer');
     $line = $row['i'];
     $row = $row['row'];
     $regions = Mage::getResourceModel('directory/region_collection');
     //        $config = Mage::getSingleton('eav/config')->getEntityType('customer');
     $website = Mage::getModel('core/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']);
         }
     }
     if ($errors = $this->getErrors()) {
         $this->unsetData();
         $this->printError(join("<br />", $errors));
         return;
     }
     //        $entity = $this->getResource();
     foreach ($row as $field => $value) {
         //            $attribute = $entity->getAttribute($field);
         //            if (!$attribute) {
         //                echo $field;
         //                continue;
         //            }
         //            if ($attribute->usesSource()) {
         //                $source = $attribute->getSource();
         //                $optionId = $config->getSourceOptionId($source, $value);
         //                if (is_null($optionId)) {
         //                    $this->printError($hlp->__("Invalid attribute option specified for attribute attribute %s (%s).", $field, $value), $line);
         //                }
         //                $value = $optionId;
         //            }
         $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 = new Mage_Customer_Model_Address();
         }
         $regions->addRegionNameFilter($row['billing_region'])->load();
         if ($regions) {
             foreach ($regions as $region) {
                 $regionId = $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 = new Mage_Customer_Model_Address();
         }
         $regions->addRegionNameFilter($row['shipping_region'])->load();
         if ($regions) {
             foreach ($regions as $region) {
                 $regionId = $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'])) {
         $this->setIsSubscribed(strtolower($row['is_subscribed']) == self::SUBSCRIBED_YES ? 1 : 0);
     }
     unset($row);
     return $this;
 }