vladdancer
9/3/2013 - 8:55 AM

Get csv columns for migrate class

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;
}