You are here

function _og_massadd_adduser in Organic Groups Mass Add 7

Same name and namespace in other branches
  1. 6 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 254
The og_massadd module file

Code

function _og_massadd_adduser($user, $group_type, $group_ids) {
  $newuser = array();
  if (count($user) == 1) {
    $firstname = "";
    $lastname = "";
    $mail = reset($user);
    $desiredname = "";
  }
  elseif (count($user) == 2) {
    $firstname = reset($user);
    $lastname = "";
    $mail = end($user);
    $desiredname = "";
  }
  elseif (count($user) == 3) {
    $firstname = reset($user);
    $lastname = next($user);
    $mail = end($user);
    $desiredname = "";
  }
  elseif (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)) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('mail', check_plain($mail));
    $result = $query
      ->execute();
    if (!empty($result)) {
      $uids = array_keys($result['user']);
      $account = user_load_multiple($uids);
      $account = array_shift($account);
    }
  }

  // If not, try to check for usernames
  if (!isset($account) && drupal_strlen($mail)) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('name', check_plain($mail));
    $result = $query
      ->execute();
    if (!empty($result)) {
      $uids = array_keys($result['user']);
      $account = user_load_multiple($uids);
      $account = array_shift($account);
    }
  }

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

    //dpm($mail, 'Not valid email address');
    return FALSE;
  }
  elseif (!isset($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_query("SELECT count(*) FROM {users} WHERE name = :name", array(
        ':name' => check_plain($newuser['name']),
      ))
        ->fetchField()) {
        $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;
    }
  }
  if (variable_get('og_massadd_createunknowns', '')) {
    $fname = variable_get('og_massadd_profilefname', '');
    $lname = variable_get('og_massadd_profilelname', '');
    if (drupal_strlen($fname) && field_info_field($fname)) {
      $account->{$fname}['und'][0]['value'] = $firstname;
    }
    if (drupal_strlen($lname) && field_info_field($lname)) {
      $account->{$lname}['und'][0]['value'] = $lastname;
    }
    user_save($account);

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

  // Add user to groups
  foreach ($group_ids as $gid) {
    if ($gid) {
      $field_names = og_get_group_audience_fields();
      $field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : key($field_names);

      /* user already belongs to the group */
      if (og_get_membership($group_type, $gid, 'user', $account->uid)) {
        return FALSE;
      }
      $ret = og_membership_create($group_type, $gid, 'user', $account->uid, $field_name);
      $ret
        ->save();

      /*
           $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;
}