You are here

function ginvite_by_mail_form_validate in Group 7

Validate handler for ginvite_by_mail_form().

See also

group_form_ginvite_by_mail_form_alter()

File

modules/ginvite/forms/ginvite.inc, line 121
Group member invite UI.

Code

function ginvite_by_mail_form_validate($form, &$form_state) {
  $group = $form_state['build_info']['args'][0];
  $mail = $form_state['values']['mail'];
  if (!valid_email_address($mail)) {
    form_set_error('mail', t('Invalid e-mail address.'));
  }

  // Validation if the e-mail address is already registered.
  if ($account = user_load_by_mail($mail)) {

    // First check for invite by user access.
    if (!group_access('invite members by user', $group)) {
      $message = '%email is already registered on the website. ';
      $message .= 'You do not, however, have the permission to invite existing users.';
      form_set_error('mail', t($message, array(
        '%email' => $mail,
      )));
    }
    elseif ($group
      ->getMember($account->uid)) {
      $message = '%email is already registered to a member of this group.';
      form_set_error('mail', t($message, array(
        '%email' => $mail,
      )));
    }
    else {
      $form_state['values']['uid'] = $account->uid;
      $form_state['values']['user'] = format_username($account);
    }
  }
  else {

    // Get all invites for the e-mail address.
    $invites = ginvite_get_invites($mail);

    // Check for an existing invite for this group.
    if (!empty($invites[$group->gid])) {
      $message = 'There is already an invite pending for %email.';
      form_set_error('mail', t($message, array(
        '%email' => $mail,
      )));
    }
  }
}