/** * @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 testParsingWithExplicitHeaderRow() { /* If your CSV file doesn't have a header row */ $csv = new CSVParser($this->getCurrentRelativePath() .'/CsvBulkLoaderTest_PlayersWithHeader.csv'); $csv->provideHeaderRow(array('__fn','__bio','__bd','__reg')); $firstNames = $birthdays = $biographies = $registered = array(); foreach($csv as $record) { /* Each row in the CSV file will be keyed with the header row that you gave */ $this->assertEquals(array('__fn','__bio','__bd','__reg'), array_keys($record)); $firstNames[] = $record['__fn']; $biographies[] = $record['__bio']; $birthdays[] = $record['__bd']; $registered[] = $record['__reg']; } /* And the first row will be returned in the data */ $this->assertEquals(array('FirstName','John','Jane','Jamie','Järg'), $firstNames); $this->assertEquals(array( 'Biography', "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("Birthday","31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays); $this->assertEquals(array('IsRegistered', '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; }