View source
<?php
class FeedsCSVParser extends FeedsParser {
public function parse(FeedsImportBatch $batch, FeedsSource $source) {
feeds_include_library('ParserCSV.inc', 'ParserCSV');
$iterator = new ParserCSVIterator(realpath($batch
->getFilePath()));
$source_config = $source
->getConfigFor($this);
$parser = new ParserCSV();
$delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
$parser
->setDelimiter($delimiter);
if (empty($source_config['no_headers'])) {
$header = $this
->parseHeader($parser, $iterator);
if (!$header) {
return;
}
$parser
->setColumnNames($header);
}
$batch->items = $this
->parseItems($parser, $iterator);
}
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;
}
protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator) {
$parser
->setLineLimit(0);
$parser
->setStartByte($parser
->lastLinePos());
$rows = $parser
->parse($iterator);
return $rows;
}
public function getMappingSources() {
return FALSE;
}
public function getSourceElement(FeedsImportBatch $batch, $element_key) {
return parent::getSourceElement($batch, drupal_strtolower($element_key));
}
public function sourceDefaults() {
return array(
'delimiter' => $this->config['delimiter'],
'no_headers' => $this->config['no_headers'],
);
}
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']);
}
}
$items = array(
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),
)),
format_plural(count($uniques), t('Column <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array(
'!column' => implode(', ', $uniques),
)), t('Columns <strong>!columns</strong> 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),
))),
);
$form['help']['#value'] = '<div class="help">' . theme('item_list', $items) . '</div>';
$form['delimiter'] = array(
'#type' => 'select',
'#title' => t('Delimiter'),
'#description' => t('The character that delimits fields in the CSV file.'),
'#options' => array(
',' => ',',
';' => ';',
'TAB' => 'TAB',
),
'#default_value' => isset($source_config['delimiter']) ? $source_config['delimiter'] : ',',
);
$form['no_headers'] = array(
'#type' => 'checkbox',
'#title' => t('No Headers'),
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
'#default_value' => isset($source_config['no_headers']) ? $source_config['no_headers'] : 0,
);
return $form;
}
public function configDefaults() {
return array(
'delimiter' => ',',
'no_headers' => 0,
);
}
public function configForm(&$form_state) {
$form = array();
$form['delimiter'] = array(
'#type' => 'select',
'#title' => t('Default delimiter'),
'#description' => t('Default field delimiter.'),
'#options' => array(
',' => ',',
';' => ';',
'TAB' => 'TAB',
),
'#default_value' => $this->config['delimiter'],
);
$form['no_headers'] = array(
'#type' => 'checkbox',
'#title' => t('No headers'),
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
'#default_value' => $this->config['no_headers'],
);
return $form;
}
}