You are here

function _og_massadd_adduser in Organic Groups Mass Add 6

Same name and namespace in other branches
  1. 7 og_massadd.module \_og_massadd_adduser()

Actually adding something Returns FALSE if the user couldn't be created, 0 if the user existed and 1 if it was created.

1 call to _og_massadd_adduser()
_og_massadd_fromstring in ./og_massadd.module
Mass adding users from string input

File

./og_massadd.module, line 198
The og_massadd module file

Code

function _og_massadd_adduser($user, $groups) {
  $newuser = array();
  if (count($user) == 1) {
    $firstname = "";
    $lastname = "";
    $mail = reset($user);
    $desiredname = "";
  }
  else {
    if (count($user) == 2) {
      $firstname = reset($user);
      $lastname = "";
      $mail = end($user);
      $desiredname = "";
    }
    else {
      if (count($user) == 3) {
        $firstname = reset($user);
        $lastname = next($user);
        $mail = end($user);
        $desiredname = "";
      }
      else {
        if (count($user) >= 4) {
          $firstname = reset($user);
          $lastname = next($user);
          $mail = next($user);
          $desiredname = end($user);
        }
        else {

          //dpm('Unable to count fields');
          return FALSE;
        }
      }
    }
  }
  $didcreate = 0;

  // If it looks like a mail address, try to look up user
  if (valid_email_address($mail)) {
    $account = user_load(array(
      'mail' => check_plain($mail),
    ));
  }

  // If not, try to check for usernames
  if (!$account && strlen($mail)) {
    $account = user_load(array(
      'name' => check_plain($mail),
    ));
  }

  // Create user if necessary
  if (!$account && !valid_email_address($mail)) {

    //dpm($mail, 'Not valid email address');
    return FALSE;
  }
  else {
    if (!$account && valid_email_address($mail)) {
      if (!user_access('mass add and create users') || !variable_get("og_massadd_createunknowns", FALSE)) {

        //dpm('Not allowed to create unknowns');
        return FALSE;
      }
      $newuser['mail'] = $mail;
      $newuser['pass'] = user_password();
      $newuser['init'] = $newuser['mail'];
      $newuser['status'] = 1;

      // 20 attempts should be enough for anybody (tm)
      for ($attempt = 0; $attempt <= 20; $attempt++) {
        $newuser['name'] = _og_massadd_createusername($mail, $firstname, $lastname, $desiredname, $attempt);
        if ($newuser['name'] !== FALSE && !db_result(db_query("SELECT count(*) FROM {users} WHERE name = '%s'", check_plain($newuser['name'])))) {
          $account = user_save(NULL, $newuser);
          if ($account !== FALSE) {
            $account->password = $newuser['pass'];
            _user_mail_notify('register_no_approval_required', $account);
            $didcreate = 1;
            break;

            // Got user, end tries
          }
        }
      }
      if (!$account) {

        // Exhausted attempts, give up

        //dpm('Unable to find available username');
        return FALSE;
      }
    }
  }
  $profilenode = module_exists('content_profile') ? variable_get('og_massadd_profilenode', '') : '';
  if ($profilenode) {
    $fname = variable_get('og_massadd_profilefname', 'firstname');
    $lname = variable_get('og_massadd_profilelname', 'lastname');
    $profilenodes = array_keys(content_profile_get_types('names'));

    // Create content profile
    $profile_node = new stdClass();
    $profile_node->title = $account->name;
    $profile_node->body = '';
    $profile_node->type = $profilenode;

    // Your specified content type
    $profile_node->created = time();
    $profile_node->changed = time();
    $profile_node->status = 1;
    $profile_node->promote = 0;
    $profile_node->sticky = 0;
    $profile_node->uid = $account->uid;

    // UID of content owner
    if (strlen($fname) && content_fields($fname, $profilenode)) {
      $profile_node->{$fname}[0]['value'] = $firstname;
    }
    if (strlen($lname) && content_fields($lname, $profilenode)) {
      $profile_node->{$lname}[0]['value'] = $lastname;
    }
    node_save($profile_node);

    // Actually, we don't care if this fails for now. We need better field-handling first.
  }

  // Add user to groups
  foreach ($groups as $gid) {
    if ($gid) {
      og_save_subscription($gid, $account->uid, array(
        'is_active' => 1,
      ));
      $groupnode = node_load($gid);
      $variables = array(
        '@title' => $groupnode->title,
        '!group_url' => url("node/{$groupnode->nid}", array(
          'absolute' => TRUE,
        )),
      );
      drupal_mail('og', 'approve_user', $account->mail, user_preferred_language($account), $variables);
    }
  }
  return $didcreate;
}