You are here

function _user_import_process in User Import 5

Same name and namespace in other branches
  1. 8 user_import.import.inc \_user_import_process()
  2. 5.2 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
Implementation of hook_cron().
user_import_edit_form_submit in ./user_import.module
_user_import_initialise_import in ./user_import.module

File

./user_import.module, line 1403
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']);
  $update_setting = _user_import_update_user_check($settings['options']['update_user']);
  $update_setting_per_module = $settings['options']['update_user'];
  ini_set('auto_detect_line_endings', true);
  $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, $update_setting, $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();
        $existing_account = FALSE;
        $updated = FALSE;

        // 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 ($update_setting && !empty($fields['user']['email'][0])) {
          $existing_account = user_load(array(
            'mail' => $fields['user']['email'][0],
          ));
          if ($existing_account) {
            $account = (array) $existing_account;
          }
        }

        // if $account['uid'] is not empty then we can assume the account is being updated
        $account_additions = module_invoke_all('user_import_pre_save', $settings, $account, $fields, $errors, $update_setting_per_module);
        foreach ($account_additions as $field_name => $value) {
          $account[$field_name] = $value;
        }
        if (empty($errors)) {
          if ($settings['setting'] == 'import') {
            if ($existing_account) {
              $account = user_save($existing_account, $account);
              $updated = TRUE;
            }
            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']);
              $password = $account['pass'];
              $account = user_save('', $account);
            }
            module_invoke_all('user_import_after_save', $settings, $account, $password, $fields, $updated, $update_setting_per_module);
            $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;
}