You are here

function _user_import_process in User Import 5.2

Same name and namespace in other branches
  1. 8 user_import.import.inc \_user_import_process()
  2. 5 user_import.module \_user_import_process()
  3. 6.4 user_import.import.inc \_user_import_process()
  4. 6.2 user_import.import.inc \_user_import_process()
  5. 7.3 user_import.import.inc \_user_import_process()
  6. 7 user_import.import.inc \_user_import_process()
  7. 7.2 user_import.import.inc \_user_import_process()
4 calls to _user_import_process()
user_import_continue in ./user_import.module
user_import_cron in ./user_import.module
user_import_edit_form_submit in ./user_import.module
_user_import_initialise_import in ./user_import.module

File

./user_import.module, line 1330
Import users from a comma separated file (csv).

Code

function _user_import_process($settings) {
  user_import_load_supported();
  $line_max = variable_get('user_import_line_max', 1000);
  $import_max = variable_get('user_import_max', 250);
  $field_match = _user_import_unconcatenate_field_match($settings['field_match']);
  $handle = @fopen($settings['filepath'], "r");

  // move pointer to where test/import last finished
  if ($settings['pointer'] != 0) {
    fseek($handle, $settings['pointer']);
  }

  // start count of imports on this cron run
  $send_counter = 0;
  while ($data = fgetcsv($handle, $line_max, ',')) {
    $errors = user_import_errors(FALSE, TRUE);

    // if importing, check we are not over max number of imports per cron
    if ($settings['setting'] == 'import' && $send_counter > $import_max) {
      $finished = TRUE;
      break;
    }

    // don't process empty lines
    $line_filled = count($data) == 1 && drupal_strlen($data[0]) == 0 ? FALSE : TRUE;
    if ($line_filled) {

      // check if this is first line - if so should we skip?
      if (!empty($settings['first_line_skip']) && $settings['processed'] == 0) {

        // reset to false on second process
        $first_line_skip = empty($first_line_skip) ? TRUE : FALSE;
      }
      if (!$first_line_skip) {
        unset($password, $errors, $fields);
        reset($field_match);
        foreach ($field_match as $column_id => $column_settings) {
          $type = $column_settings['type'];
          $field_id = $column_settings['field_id'];

          // Skip if this is a field used as part of a username but
          // not otherwise mapped for import.
          if ($type != 'username_part') {
            $fields[$type][$field_id] = module_invoke_all('user_import_data', $settings, $column_settings, $type, $field_id, $data, $column_id);
          }

          // Read in data if present for concatenating a user name.
          if ($column_settings['username'] > 0) {
            $username_data[$column_id] = $data[$column_id];
            $username_order[$column_id] = $column_settings['username'];
            $username_abbreviate[$column_id] = $column_settings['abbreviate'];
          }
        }
        $errors = user_import_errors();
        $account = array();
        $account_additions = module_invoke_all('user_import_pre_save', $settings, $account, NULL, $fields, $errors);
        foreach ($account_additions as $field_name => $value) {
          $account[$field_name] = $value;
        }
        if (empty($errors)) {
          if ($settings['setting'] == 'import') {

            // If we update existing users matched by email (and
            // therefore passed validation even if this email
            // already exists), look for and use an existing account.
            if (!empty($settings['options']['update_users']) && ($existing = user_load(array(
              'mail' => $account['mail'],
            )))) {
              $account = user_save($existing, $account);
            }
            else {

              // Only set a user name if we are not updating an existing record.
              $account['name'] = _user_import_create_username($username_order, $username_data, $username_abbreviate, $settings['username_space']);
              $account = user_save('', $account);
            }
            module_invoke_all('user_import_after_save', $settings, $account, $password, $fields);
            $send_counter++;
          }
          $settings['valid']++;
        }
        $settings['processed']++;
      }
      $settings['pointer'] = ftell($handle);

      // save lines that have fatal errors
      if (!empty($errors)) {
        _user_import_errors_display_save($settings['import_id'], $fields, $account['email'], $errors);
      }
    }
  }
  fclose($handle);
  if ($settings['setting'] == 'import' && !$finished) {
    $settings['setting'] = 'imported';
  }
  if ($settings['setting'] == 'test') {
    $settings['setting'] = 'tested';
  }
  _user_import_settings_update($settings['pointer'], $settings['processed'], $settings['valid'], $settings['setting'], $settings['import_id']);
  return $settings;
}