You are here

function taxonomy_csv_import_submit in Taxonomy CSV import/export 5

Handles CSV import form submission.

File

./taxonomy_csv.module, line 173
Quick import of lists of terms from a csv file.

Code

function taxonomy_csv_import_submit($form_id, $form_values) {
  $options = $form_values;
  $file = file_check_upload();
  if ($options['delimiter'] == TAXONOMY_CSV_SEMICOLON) {
    $delimiter = ';';
  }
  else {
    $delimiter = ',';
  }
  $options['vocabulary'] = taxonomy_get_vocabulary($options['vid']);
  if (!$options['vocabulary']->hierarchy && $options['columns'] == TAXONOMY_CSV_CHILDREN) {
    $options['columns'] = TAXONOMY_CSV_IGNORE;
  }

  // Automatically detect line endings.
  ini_set('auto_detect_line_endings', '1');
  $handle = fopen($file->filepath, 'r');
  $first = TRUE;
  while ($line = fgetcsv($handle, 4096, $delimiter)) {
    if (empty($line) || count($line) == 1 && $line[0] == NULL) {
      continue;
    }

    // Skip UTF-8 byte order mark.
    if ($first) {
      if (strncmp($line[0], "", 3) === 0) {
        $line[0] = substr($line[0], 3);
      }
      $first = FALSE;
    }
    taxonomy_csv_import_line($line, $options);

    // Keep alive by restarting execution time limit.
    set_time_limit(30);
  }
  fclose($handle);
  drupal_set_message(t('The CSV file has been imported.'));
}