You are here

function uif_validate_user_file in User Import Framework 6

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

Read the user import file and validate on the way.

Parameters

$filepath: 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 156
Simple, extensible user import from a CSV file.

Code

function uif_validate_user_file($filepath, &$data, $form_state) {
  $data = array();
  $data['user'] = array();
  $line = 0;

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

    // Read the header and allow alterations
    $header_row = fgetcsv($fp);
    drupal_alter('uif_header', $header_row);
    $header_row = uif_normalize_header(array_map('trim', $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;

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

      // Read a row and allow alterations
      $row = fgetcsv($fp);
      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);
        $uid = db_result(db_query_range("SELECT uid FROM {users} WHERE mail LIKE '%s'", $user_row['email'], 0, 1));
        $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;
  }
}