You are here

function _node_import_start_validate in Node import 5

File

./node_import.module, line 185
This modules provides a wizard at "administer >> content >> import" to import a CSV file with nodes.

Code

function _node_import_start_validate($op, &$edit) {
  global $user;
  global $base_url;

  // Delete an existing file if needed.
  if ($edit['op'] == t('Use a different file')) {
    file_delete($edit['file']->filepath);
    foreach (array(
      'file',
      'filename',
      'file_format',
      'errors',
    ) as $key) {
      unset($edit[$key]);
      unset($_SESSION['node_import'][$key]);
    }
  }
  else {
    if ($edit['op'] == t('Next (mapping)')) {

      // If there is an uploaded file, save it to
      // drupal.node_import.{site_url}.{uid} in the temporary directory.
      $file = file_save_upload('file');
      if ($file) {
        $edit['filename'] = $file->filename;
        file_move($file, 'drupal.node_import.' . strtr($base_url, array(
          'http://' => '',
          '/' => '.',
        )) . '.' . $user->uid, 1);
        $edit['file'] = $file;
      }
      if (!$edit['file']) {
        form_set_error('file', t('You must select a file to import.'));
        return;
      }

      // Autodetect the fileformat if needed.
      if ($edit['file_format'] == '') {
        $format = _node_import_autodetect_file_format($edit['file']->filepath);
        if ($format == '') {
          form_set_error('file_format', t('Could not detect the file format.'));
          return;
        }
        $edit['file_format'] = $format;
      }
      $formats = _node_import_get_file_formats();
      if (!isset($formats[$edit['file_format']])) {
        form_set_error('file_format', t('You need to select a format from the list.'));
      }
      if (!$edit['type']) {
        form_set_error('type', t('You must select a content type.'));
        return;
      }
      if ($edit['type'] == 'node_import') {
        $_SESSION['node_import_page'] = '_node_import_preview';
      }
      else {
        $_SESSION['node_import_page'] = '_node_import_mapping';
      }
    }
  }
}