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); }
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; }