示例#1
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     if (isset($fields['ProductReview']['ID'])) {
         $instance = ActiveRecordModel::getInstanceByID('ProductReview', $record[$fields['ProductReview']['ID']], true);
     } else {
         if (isset($fields['Product']['ID'])) {
             $parent = ActiveRecordModel::getInstanceByID('Product', $record[$fields['Product']['ID']], true);
         } else {
             if (isset($fields['Product']['sku'])) {
                 $parent = Product::getInstanceBySku($record[$fields['Product']['sku']]);
             } else {
                 return;
             }
         }
     }
     if (empty($instance) && empty($parent)) {
         return;
     }
     if (empty($instance)) {
         $instance = ProductReview::getNewInstance($parent, User::getNewInstance(''));
         $instance->isEnabled->set(true);
     }
     return $instance;
 }
示例#2
0
 public function testSimpleImport()
 {
     $lv = ActiveRecordModel::getNewInstance('Language');
     $lv->setID('xx');
     $lv->save();
     $profile = new CsvImportProfile('Product');
     $profile->setField(0, 'Product.sku');
     $profile->setField(1, 'Product.name', array('language' => 'en'));
     $profile->setField(2, 'Product.name', array('language' => 'xx'));
     $profile->setField(3, 'Product.shippingWeight');
     $profile->setParam('delimiter', ';');
     $csvFile = ClassLoader::getRealPath('cache.') . 'testDataImport.csv';
     file_put_contents($csvFile, 'test; "Test Product"; "Parbaudes Produkts"; 15' . "\n" . 'another; "Another Test"; "Vel Viens"; 12.44');
     $import = new ProductImport($this->getApplication());
     $csv = $profile->getCsvFile($csvFile);
     $cnt = $import->importFile($csv, $profile);
     $this->assertEquals($cnt, 2);
     $test = Product::getInstanceBySKU('test');
     $this->assertTrue($test instanceof Product);
     $this->assertEquals($test->shippingWeight->get(), '15');
     $this->assertEquals($test->getValueByLang('name', 'en'), 'Test Product');
     $another = Product::getInstanceBySKU('another');
     $this->assertEquals($another->getValueByLang('name', 'xx'), 'Vel Viens');
     unlink($csvFile);
 }
示例#3
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     if (isset($fields['NewsPost']['ID'])) {
         try {
             $instance = ActiveRecordModel::getInstanceByID('NewsPost', $record[$fields['NewsPost']['ID']], true);
         } catch (ARNotFoundException $e) {
         }
     }
     if (empty($instance)) {
         $instance = ActiveRecordModel::getNewInstance('NewsPost');
     }
     //$this->setLastImportedRecordName($instance->getID());
     return $instance;
 }
示例#4
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     if (isset($fields['User']['ID'])) {
         $instance = User::getInstanceByID($record[$fields['User']['ID']], true);
     } else {
         if (isset($fields['User']['email'])) {
             $instance = User::getInstanceByEmail($record[$fields['User']['email']]);
         }
     }
     if (empty($instance)) {
         $instance = User::getNewInstance('');
         $instance->isEnabled->set(true);
     }
     $this->setLastImportedRecordName($instance->email->get());
     return $instance;
 }
示例#5
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     if (isset($fields['Currency']['ID'])) {
         try {
             $instance = Currency::getInstanceByID($record[$fields['Currency']['ID']], true);
         } catch (ARNotFoundException $e) {
         }
     } else {
         return;
     }
     if (empty($instance)) {
         $instance = Currency::getNewInstance($record[$fields['Currency']['ID']]);
     }
     $this->setLastImportedRecordName($instance->getID());
     return $instance;
 }
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     if (isset($fields['NewsletterSubscriber']['ID'])) {
         $instance = ActiveRecordModel::getInstanceByID('NewsletterSubscriber', $record[$fields['NewsletterSubscriber']['ID']], true);
     } else {
         if (isset($fields['NewsletterSubscriber']['email'])) {
             $instance = NewsletterSubscriber::getInstanceByEmail($record[$fields['NewsletterSubscriber']['email']]);
         } else {
             return;
         }
     }
     if (empty($instance)) {
         $instance = NewsletterSubscriber::getNewInstanceByEmail($record[$fields['NewsletterSubscriber']['email']]);
     }
     $this->setLastImportedRecordName($instance->email->get());
     return $instance;
 }
示例#7
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     // get delivery zone
     if (isset($fields['DeliveryZone']['ID'])) {
         try {
             $zone = DeliveryZone::getInstanceByID($record[$fields['DeliveryZone']['ID']], true);
         } catch (ARNotFoundException $e) {
             $zone = DeliveryZone::getDefaultZoneInstance();
         }
     } else {
         $zone = DeliveryZone::getDefaultZoneInstance();
     }
     // get shipping service
     $f = select(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('ShippingService', 'name'), $this->application->getDefaultLanguageCode()), $record[$fields['ShippingService']['name']]));
     if ($zone->isDefault()) {
         $f->mergeCondition(new IsNullCond(f('ShippingService.deliveryZoneID')));
     } else {
         $f->mergeCondition(eq(f('ShippingService.deliveryZoneID'), $zone->getID()));
     }
     $services = ActiveRecordModel::getRecordSet('ShippingService', $f);
     if ($services->get(0)) {
         $service = $services->get(0);
         // temporary
         $service->deleteRelatedRecordSet('ShippingRate');
     } else {
         $service = ShippingService::getNewInstance($zone, '', 0);
         $service->rangeType->set(ShippingService::SUBTOTAL_BASED);
     }
     $this->importInstance($record, $profile, $service);
     $this->setLastImportedRecordName($service->getValueByLang('name'));
     // get rate instance
     $rate = ShippingRate::getNewInstance($service, 0, 1000000);
     $rate->subtotalRangeStart->set(0);
     $rate->subtotalRangeEnd->set(1000000);
     return $rate;
 }
示例#8
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     pp('User address import get instance');
     $fields = $profile->getSortedFields();
     if (isset($fields['UserAddress']['ID'])) {
         $instance = ActiveRecordModel::getInstanceByID('UserAddress', $record[$fields['UserAddress']['ID']], true);
     } else {
         if (isset($fields['AddressUser']['ID'])) {
             $owner = User::getInstanceByID($record[$fields['AddressUser']['ID']], true);
         } else {
             if (isset($fields['AddressUser']['email'])) {
                 $owner = User::getInstanceByEmail($record[$fields['AddressUser']['email']]);
             }
         }
     }
     if (isset($owner)) {
         if ($profile->isColumnSet('AddressUser.isShipping')) {
             $type = $this->evalBool(strtolower($record[$profile->getColumnIndex('AddressUser.isShipping')])) ? 'ShippingAddress' : 'BillingAddress';
         } else {
             $type = 'BillingAddress';
         }
         $owner->loadAddresses();
     }
     if (empty($instance)) {
         if (empty($owner)) {
             return;
         }
         $isDefault = $profile->isColumnSet('AddressUser.isDefault') && $this->evalBool(strtolower($record[$profile->getColumnIndex('AddressUser.isDefault')]));
         if ($isDefault) {
             $field = 'default' . $type;
             $addressType = $owner->{$field}->get();
             $instance = $addressType->userAddress->get();
         }
         if (empty($addressType)) {
             $instance = UserAddress::getNewInstance();
             $addressType = call_user_func_array(array($type, 'getNewInstance'), array($owner, $instance));
             if ($isDefault) {
                 $owner->{$field}->set($addressType);
             }
         }
         $addressType->userAddress->set($instance);
         $instance->addressType = $addressType;
     }
     return $instance;
 }
示例#9
0
 public function import()
 {
     $options = unserialize(base64_decode($this->request->get('options')));
     $response = new JSONResponse(null);
     if (file_exists($this->getCancelFile())) {
         unlink($this->getCancelFile());
     }
     if (!$this->request->get('continue')) {
         $this->clearCacheProgress();
     }
     $import = $this->getImportInstance();
     set_time_limit(0);
     ignore_user_abort(true);
     $profile = new CsvImportProfile($import->getClassName());
     // map CSV fields to LiveCart fields
     $params = $this->request->get('params');
     foreach ($this->request->get('column') as $key => $value) {
         if ($value) {
             $fieldParams = !empty($params[$key]) ? $params[$key] : array();
             $profile->setField($key, $value, array_filter($fieldParams));
         }
     }
     $profile->setParam('isHead', $this->request->get('firstHeader'));
     if ($this->request->get('saveProfile')) {
         $path = $this->getProfileDirectory($import) . $this->request->get('profileName') . '.ini';
         $profile->setFileName($path);
         $profile->save();
     }
     // get import root category
     if ($import->isRootCategory()) {
         $profile->setParam('category', $this->request->get('category'));
     }
     $import->beforeImport($profile);
     $csv = new CsvFile($this->request->get('file'), $this->request->get('delimiter'));
     $total = $csv->getRecordCount();
     if ($this->request->get('firstHeader')) {
         $total -= 1;
     }
     if ($this->request->get('firstHeader')) {
         $import->skipHeader($csv);
         $import->skipHeader($csv);
     }
     $progress = 0;
     $processed = 0;
     if ($this->request->get('continue')) {
         $import->setImportPosition($csv, $this->getCacheProgress() + 1);
         $progress = $this->getCacheProgress();
     } else {
         if (!empty($options['transaction'])) {
             ActiveRecord::beginTransaction();
         }
     }
     if (empty($options['transaction'])) {
         $this->request->set('continue', true);
     }
     $import->setOptions($options);
     if ($uid = $this->request->get('uid')) {
         $import->setUID($uid);
     }
     do {
         $progress += $import->importFileChunk($csv, $profile, 1);
         // continue timed-out import
         if ($this->request->get('continue')) {
             $this->setCacheProgress($progress);
         }
         ActiveRecord::clearPool();
         if ($progress % self::PROGRESS_FLUSH_INTERVAL == 0 || $total == $progress) {
             $response->flush($this->getResponse(array('progress' => $progress, 'total' => $total, 'uid' => $import->getUID(), 'lastName' => $import->getLastImportedRecordName())));
             //echo '|' . round(memory_get_usage() / (1024*1024), 1) . '|' . count($categories) . "\n";
         }
         // test non-transactional mode
         //if (!$this->request->get('continue')) exit;
         if (connection_aborted()) {
             if ($this->request->get('continue')) {
                 exit;
             } else {
                 $this->cancel();
             }
         }
     } while (!$import->isCompleted($csv));
     if (!empty($options['missing']) && 'keep' != $options['missing']) {
         $filter = $import->getMissingRecordFilter($profile);
         if ('disable' == $options['missing']) {
             $import->disableRecords($filter);
         } else {
             if ('delete' == $options['missing']) {
                 $import->deleteRecords($filter);
             }
         }
     }
     $import->afterImport();
     if (!$this->request->get('continue')) {
         //ActiveRecord::rollback();
         ActiveRecord::commit();
     }
     $response->flush($this->getResponse(array('progress' => 0, 'total' => $total)));
     //echo '|' . round(memory_get_usage() / (1024*1024), 1);
     exit;
 }
示例#10
0
 public function extractSection($section)
 {
     $profile = new CsvImportProfile($section);
     $sorted = $this->getSortedFields();
     if (isset($sorted[$section])) {
         foreach ($sorted[$section] as $column => $key) {
             $field = $this->fields[$key];
             $profile->setField($key, $field['name'], $field['params']);
         }
     }
     return $profile;
 }
示例#11
0
 private function getRoot(CsvImportProfile $profile)
 {
     $id = $profile->getParam('category');
     $cat = $id ? Category::getInstanceByID($id, true) : Category::getRootNode();
     return $cat;
 }
示例#12
0
 public function getColumnValue($record, CsvImportProfile $profile, $fieldName)
 {
     if ($profile->isColumnSet($fieldName)) {
         return $record[$profile->getColumnIndex($fieldName)];
     }
 }
示例#13
0
 /**
  *  Import or update an ordered product
  */
 protected function set_OrderedItem_sku($instance, $value, $record, CsvImportProfile $profile)
 {
     if (!$value) {
         return;
     }
     $product = Product::getInstanceBySKU($value);
     if (!$product) {
         return;
     }
     $items = $instance->getItemsByProduct($product);
     // create initial shipment
     if (!$instance->getShipments()->size()) {
         $shipment = Shipment::getNewInstance($instance);
         $shipment->save();
     }
     // any particular shipment?
     $shipment = $item = null;
     if ($profile->isColumnSet('OrderedItem.shipment')) {
         // internal indexes are 0-based, but the import references are 1-based
         $shipmentNo = $this->getColumnValue($record, $profile, 'OrderedItem.shipment') - 1;
         if (is_numeric($this->getColumnValue($record, $profile, 'OrderedItem.shipment'))) {
             foreach ($instance->getShipments() as $key => $shipment) {
                 if ($key == $shipmentNo) {
                     break;
                 }
                 $shipment = null;
             }
             // create a new shipment
             if (!$shipment) {
                 $shipment = Shipment::getNewInstance($instance);
                 $shipment->save();
             }
             foreach ($items as $item) {
                 if ($item->shipment->get() == $shipment) {
                     break;
                 }
                 unset($item);
             }
         }
     }
     if (!$item) {
         $item = array_shift($items);
     }
     if (!$item) {
         $count = $this->getColumnValue($record, $profile, 'OrderedItem.count');
         $item = OrderedItem::getNewInstance($instance, $product, max(1, $count));
         $instance->addItem($item);
     }
     if ($profile->isColumnSet('OrderedItem.count')) {
         $count = $this->getColumnValue($record, $profile, 'OrderedItem.count');
         $item->count->set(max(1, $count));
     }
     if ($profile->isColumnSet('OrderedItem.price')) {
         $item->price->set($this->getColumnValue($record, $profile, 'OrderedItem.price'));
     }
     if (!$shipment) {
         $shipment = $instance->getShipments()->get(0);
     }
     $item->shipment->set($shipment);
     $item->save();
     $instance->finalize(array('customPrice' => true, 'allowRefinalize' => true));
 }