getConfigFor($this); $state = $source->state(FEEDS_PARSE); // Load and configure parser. feeds_include_library('ParserCSV.inc', 'ParserCSV'); $parser = new ParserCSV(); $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter']; $parser->setDelimiter($delimiter); $iterator = new ParserCSVIterator($fetcher_result->getFilePath()); if (empty($source_config['no_headers'])) { // Get first line and use it for column names, convert them to lower case. $header = $this->parseHeader($parser, $iterator); if (!$header) { return; } $parser->setColumnNames($header); } // Determine section to parse, parse. $start = $state->pointer ? $state->pointer : $parser->lastLinePos(); $limit = $source->importer->getLimit(); $rows = $this->parseItems($parser, $iterator, $start, $limit); // Report progress. $state->total = filesize($fetcher_result->getFilePath()); $state->pointer = $parser->lastLinePos(); $progress = $parser->lastLinePos() ? $parser->lastLinePos() : $state->total; $state->progress($state->total, $progress); // Create a result object and return it. return new FeedsParserResult($rows, $source->feed_nid); } /** * Get first line and use it for column names, convert them to lower case. * Be aware that the $parser and iterator objects can be modified in this * function since they are passed in by reference * * @param ParserCSV $parser * @param ParserCSVIterator $iterator * @return * An array of lower-cased column names to use as keys for the parsed items. */ protected function parseHeader(ParserCSV $parser, ParserCSVIterator $iterator) { $parser->setLineLimit(1); $rows = $parser->parse($iterator); if (!count($rows)) { return FALSE; } $header = array_shift($rows); foreach ($header as $i => $title) { $header[$i] = trim(drupal_strtolower($title)); } return $header; } /** * Parse all of the items from the CSV. * * @param ParserCSV $parser * @param ParserCSVIterator $iterator * @return * An array of rows of the CSV keyed by the column names previously set */ protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator, $start = 0, $limit = 0) { $parser->setLineLimit($limit); $parser->setStartByte($start); $rows = $parser->parse($iterator); return $rows; } /** * Override parent::getMappingSources(). */ public function getMappingSources() { return FALSE; } /** * Override parent::getSourceElement() to use only lower keys. */ public function getSourceElement(FeedsSource $source, FeedsParserResult $result, $element_key) { return parent::getSourceElement($source, $result, drupal_strtolower($element_key)); } /** * Define defaults. */ public function sourceDefaults() { return array( 'delimiter' => $this->config['delimiter'], 'no_headers' => $this->config['no_headers'], ); } /** * Source form. * * Show mapping configuration as a guidance for import form users. */ public function sourceForm($source_config) { $form = array(); $form['#weight'] = -10; $mappings = feeds_importer($this->id)->processor->config['mappings']; $sources = $uniques = array(); foreach ($mappings as $mapping) { $sources[] = check_plain($mapping['source']); if ($mapping['unique']) { $uniques[] = check_plain($mapping['source']); } } $output = t('Import !csv_files with one or more of these columns: !columns.',array('!csv_files' => l(t('CSV files'), 'http://en.wikipedia.org/wiki/Comma-separated_values'), '!columns' => implode(', ', $sources))); $items = array(); $items[] = format_plural(count($uniques), t('Column !column is mandatory and considered unique: only one item per !column value will be created.', array('!column' => implode(', ', $uniques))), t('Columns !columns are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array('!columns' => implode(', ', $uniques)))); $items[] = l(t('Download a template'), 'import/' . $this->id . '/template'); $form['help']['#markup'] = '
' . $output . '
' . theme('item_list', array('items' => $items)) . '