You are here

function uif_validate_user_file in User Import Framework 7

Same name and namespace in other branches
  1. 6 uif.admin.inc \uif_validate_user_file()

Read the user import file and validate on the way.

Parameters

$uri: filepath to the user import file @param $data returns with array of users @return FALSE if no errors found array of error strings if error found

1 call to uif_validate_user_file()
uif_import_form_validate in ./uif.admin.inc
Validate the import data.

File

./uif.admin.inc, line 178
Simple, extensible user import from a CSV file.

Code

function uif_validate_user_file($uri, &$data, $form_state) {
  $data = array();
  $data['user'] = array();
  $line = 0;
  $delimiter = $form_state['values']['field_delimiter'];

  // Without this fgetcsv() fails for Mac-created files
  ini_set('auto_detect_line_endings', TRUE);
  if ($fp = fopen($uri, 'r')) {

    // Read the header and allow alterations
    $header_row = fgetcsv($fp, NULL, $delimiter);
    $header_row = uif_normalize_header($header_row);
    uif_adjust_header_values($header_row);
    drupal_alter('uif_header', $header_row);
    $line++;
    $errors = module_invoke_all('uif_validate_header', $header_row, $form_state);
    uif_add_line_number($errors, $line);
    if (!empty($errors)) {
      return $errors;
    }
    $data['header'] = $header_row;

    // Gather core and entity field info
    $data['fields'] = uif_get_field_info($header_row);

    // Read the data
    $errors = array();
    while (!feof($fp) && count($errors) < 20) {

      // Read a row and allow alterations
      $row = fgetcsv($fp, NULL, $delimiter);
      drupal_alter('uif_row', $row, $header_row);
      $line++;
      if (uif_row_has_data($row)) {
        $user_row = uif_clean_and_key_row($header_row, $row, $line);
        $args = array(
          ':mail' => db_like($user_row['mail']),
        );
        $uid = db_query_range('SELECT uid FROM {users} WHERE mail LIKE :mail', 0, 1, $args)
          ->fetchField();
        $more_errors = module_invoke_all('uif_validate_user', $user_row, $uid, $header_row, $form_state);
        uif_add_line_number($more_errors, $line);
        $errors = array_merge($errors, $more_errors);
        $data['user'][] = $user_row;
      }
    }

    // Any errors?
    if (!empty($errors)) {
      return $errors;
    }
  }
  else {
    return t('Cannot open that import file.');
  }

  // Final validation opportunity after header and all users validated individually.
  $errors = module_invoke_all('uif_validate_all_users', $data['user'], $form_state);
  if (!empty($errors)) {
    return $errors;
  }
}