/** * @param string $filepath * @param boolean $preview */ protected function processAll($filepath, $preview = false) { $results = new BulkLoader_Result(); $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure); // ColumnMap has two uses, depending on whether hasHeaderRow is set if ($this->columnMap) { // if the map goes to a callback, use the same key value as the map // value, rather than function name as multiple keys may use the // same callback foreach ($this->columnMap as $k => $v) { if (strpos($v, "->") === 0) { $map[$k] = $k; } else { $map[$k] = $v; } } if ($this->hasHeaderRow) { $csv->mapColumns($map); } else { $csv->provideHeaderRow($map); } } foreach ($csv as $row) { $this->processRecord($row, $this->columnMap, $results, $preview); } return $results; }
function testParsingWithHeadersAndColumnMap() { /* By default, a CSV file will be interpreted as having headers */ $csv = new CSVParser($this->getCurrentRelativePath() . '/CsvBulkLoaderTest_PlayersWithHeader.csv'); /* We can set up column remapping. The keys are case-insensitive. */ $csv->mapColumns(array( 'FirstName' => '__fn', 'bIoGrApHy' => '__BG', )); $firstNames = $birthdays = $biographies = $registered = array(); foreach($csv as $record) { /* Each row in the CSV file will be keyed with the renamed columns. Any unmapped column names will be left as-is. */ $this->assertEquals(array('__fn','__BG','Birthday','IsRegistered'), array_keys($record)); $firstNames[] = $record['__fn']; $biographies[] = $record['__BG']; $birthdays[] = $record['Birthday']; $registered[] = $record['IsRegistered']; } $this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames); $this->assertEquals(array( "He's a good guy", "She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography", "Pretty old, with an escaped comma", "Unicode FTW"), $biographies); $this->assertEquals(array("31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays); $this->assertEquals(array('1', '0', '1', '1'), $registered); }
protected function processAll($filepath, $preview = false) { $results = new BulkLoader_Result(); $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure); // ColumnMap has two uses, depending on whether hasHeaderRow is set if ($this->columnMap) { if ($this->hasHeaderRow) { $csv->mapColumns($this->columnMap); } else { $csv->provideHeaderRow($this->columnMap); } } foreach ($csv as $row) { $this->processRecord($row, $this->columnMap, $results, $preview); } return $results; }
/** * ----------------------------------------------------------------------------------- * Overload the processAll method of the CsvBulkLoader class. * Used so that the session vars can be cleared once they are no longer needed. * * @return BulkLoader_Result See {@link self::processAll()} * ----------------------------------------------------------------------------------- */ protected function processAll($filepath, $preview = false) { $results = new BulkLoader_Result(); // NOTE: CSVParser BUGFIX should be present in the remapHeader method to allow spaces before/after headings in a CSV file. $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure); // ColumnMap has two uses, depending on whether hasHeaderRow is set if ($this->columnMap) { if ($this->hasHeaderRow) { $csv->mapColumns($this->columnMap); } else { $csv->provideHeaderRow($this->columnMap); } } foreach ($csv as $row) { $this->processRecord($row, $this->columnMap, $results, $preview); } // Clear the session variables we set in the load method Session::clear('MenuPageID'); Session::clear('SectionImageIDs'); // Return the process results //die(); return $results; }
/** * Process every record in the file * * @param string $filepath Absolute path to the file we're importing (with UTF8 content) * @param boolean $preview If true, we'll just output a summary of changes but not actually do anything * * @return int * * @author Sascha Koehler <*****@*****.**> * @since 16.08.2011 */ public function processAll($filepath, $preview = false) { $pluginResult = SilvercartPlugin::call($this, 'overwriteProcessAll', array($filepath, $preview), false, 'DataObject'); if ($pluginResult) { return $pluginResult; } $results = new BulkLoader_Result(); $result = 0; $currPointer = 0; $csvParser = new CSVParser($filepath, $this->delimiter, $this->enclosure); $this->Log('product import start ---------------------------------------------------------------------'); // -------------------------------------------------------------------- // Insert header row if configured so // -------------------------------------------------------------------- if ($this->columnMap) { if ($this->hasHeaderRow) { $csvParser->mapColumns($this->columnMap); } else { $csvParser->provideHeaderRow($this->columnMap); } } // -------------------------------------------------------------------- // Process data range // -------------------------------------------------------------------- foreach ($csvParser as $row) { $status = $this->processRecord($row, $this->columnMap, $results, $preview); if ($status) { $results->addCreated($status); } $currPointer++; usleep(1000); } $this->Log('product import end ---------------------------------------------------------------------'); return $results; }