Skip to content

Instantly share code, notes, and snippets.

@vladdancer
Created September 3, 2013 08:55
Show Gist options
  • Save vladdancer/6421340 to your computer and use it in GitHub Desktop.
Save vladdancer/6421340 to your computer and use it in GitHub Desktop.
Get csv columns for migrate class
function getCSVheader() {
$index = 1;
$numempty = 0;
$rows = _data_import_read_file($this->importArguments['file'], $this->importArguments['csvOptions']['delimiter']);
$header = array_shift($rows);
foreach($header as $title) {
if (!$this->importArguments['csvOptions']['header_rows']) {
$columns["col_$index"] = t('Column #!column', array('!column' => $index));
$index++;
}
else {
if (empty($title)) {
$title = 'EmptyColumn'. $numempty;
$numempty++;
}
$columns[$title] = ucfirst($title);
}
}
return $columns;
}
function _data_import_read_file($file, $delimiter) {
$rows = array();
$stream_wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
if (is_object($stream_wrapper) && ($stream_wrapper->stream_open($file->uri, "r", STREAM_REPORT_ERRORS, $opened_path)) !== FALSE) {
while (($data = fgetcsv($stream_wrapper->handle, 1000, $delimiter)) !== FALSE) {
$rows[] = $data;
}
$stream_wrapper->stream_close();
}
return $rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment