You are here

public function FeedsCSVParser::getTemplate in Feeds 7.2

File

plugins/FeedsCSVParser.inc, line 289
Contains the FeedsCSVParser class.

Class

FeedsCSVParser
Parses a given file as a CSV file.

Code

public function getTemplate() {
  $mappings = feeds_importer($this->id)->processor->config['mappings'];
  $sources = $uniques = array();
  foreach ($mappings as $mapping) {
    if (in_array($mapping['source'], $uniques) || in_array($mapping['source'], $sources)) {

      // Skip columns we've already seen.
      continue;
    }
    if (!empty($mapping['unique'])) {
      $uniques[] = $mapping['source'];
    }
    else {
      $sources[] = $mapping['source'];
    }
  }
  $sep = $this
    ->getDelimiterChar($this->config);
  $columns = array();
  foreach (array_merge($uniques, $sources) as $col) {
    if (strpos($col, $sep) !== FALSE) {
      $col = '"' . str_replace('"', '""', $col) . '"';
    }

    // Prevent columns without headers from being added to the template.
    if (strlen(trim($col))) {
      $columns[] = $col;
    }
  }
  $template_file_details = $this
    ->getTemplateFileDetails($this->config);
  $filename = "{$this->id}_template.{$template_file_details['extension']}";
  $cache_control = 'max-age=60, must-revalidate';
  $content_disposition = 'attachment; filename="' . $filename . '"';
  $content_type = "{$template_file_details['mime_type']}; charset=utf-8";
  drupal_add_http_header('Cache-Control', $cache_control);
  drupal_add_http_header('Content-Disposition', $content_disposition);
  drupal_add_http_header('Content-type', $content_type);
  print implode($sep, $columns);
}