You are here

public function FeedsCSVParser::sourceForm in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsCSVParser.inc \FeedsCSVParser::sourceForm()
  2. 7 plugins/FeedsCSVParser.inc \FeedsCSVParser::sourceForm()

Source form.

Show mapping configuration as a guidance for import form users.

Overrides FeedsPlugin::sourceForm

File

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

Class

FeedsCSVParser
Parses a given file as a CSV file.

Code

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) {
    if (strpos($mapping['source'], ',') !== FALSE) {
      $sources[] = '"' . $mapping['source'] . '"';
    }
    elseif (strlen(trim($mapping['source']))) {
      $sources[] = $mapping['source'];
    }
    if (!empty($mapping['unique']) && strlen(trim($mapping['source']))) {
      $uniques[] = $mapping['source'];
    }
  }
  $sources = array_unique($sources);
  $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();
  if ($uniques) {
    $items[] = format_plural(count($uniques), 'Column <strong>@columns</strong> is mandatory and considered unique: only one item per @columns value will be created.', '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),
    ));
  }
  else {
    $items[] = t('No columns are unique. The import will only create new items, no items will be updated.');
  }
  $items[] = l(t('Download a template'), 'import/' . $this->id . '/template');
  $form['help'] = array(
    '#prefix' => '<div class="help">',
    '#suffix' => '</div>',
    'description' => array(
      '#prefix' => '<p>',
      '#markup' => $output,
      '#suffix' => '</p>',
    ),
    'list' => array(
      '#theme' => 'item_list',
      '#items' => $items,
    ),
  );
  $form['delimiter'] = array(
    '#type' => 'select',
    '#title' => t('Delimiter'),
    '#description' => t('The character that delimits fields in the CSV file.'),
    '#options' => $this
      ->getAllDelimiterTypes(),
    '#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,
  );
  $form['encoding'] = $this
    ->configEncodingForm();
  if (isset($source_config['encoding'])) {
    $form['encoding']['#default_value'] = $source_config['encoding'];
  }
  return $form;
}