You are here

function invite_form_validate in Invite 5

Same name and namespace in other branches
  1. 5.2 invite.module \invite_form_validate()
  2. 6.2 invite.module \invite_form_validate()
  3. 7.4 includes/invite.admin.inc \invite_form_validate()
  4. 7.2 invite.module \invite_form_validate()

Forms API callback; validates the incoming form data.

Filters out e-mails that are already registered or have been invited before. Checks the invite limit of the user and the max. number of invites per turn.

File

./invite.module, line 804
Allows your users to send and track invitations to join your site.

Code

function invite_form_validate($form_id, &$edit) {
  global $user;
  $emails = _invite_get_emails($edit['email']);
  if (count($emails) > 0) {

    // Filter out already registered users, but pass validation
    $failed_emails = _invite_validate_emails("SELECT mail AS email FROM {users} WHERE mail IN (%s)", $emails);
    if (count($failed_emails)) {
      $error = format_plural(count($failed_emails), 'The following recipient is already a member:', 'The following recipients are already members:') . '<br />';
      foreach ($failed_emails as $key => $email) {
        $account = user_load(array(
          'mail' => $email,
        ));
        $failed_emails[$key] = theme('username', $account) . ' (' . check_plain($email) . ')';
      }
      $error .= implode(', ', $failed_emails);
      drupal_set_message($error, 'error');
    }

    // Filter out already invited users, but pass validation
    $failed_emails = _invite_validate_emails("SELECT email FROM {invite} WHERE email IN (%s)", $emails);
    if (count($failed_emails)) {
      $error = format_plural(count($failed_emails), 'The following recipient has already been invited:', 'The following recipients have already been invited:') . '<br />';
      $error .= implode(', ', array_map('check_plain', $failed_emails));
      drupal_set_message($error, 'error');
    }

    // Check that there is at least one valid e-mail remaining after filtering
    // out dupes
    if (count($emails) == 0) {
      form_set_error('email');
      return;
    }

    // Check invite limit, fail to let the user choose which ones to send
    if ($edit['remaining_invites'] != INVITE_UNLIMITED_INVITES && count($emails) > $edit['remaining_invites']) {
      form_set_error('email', format_plural($edit['remaining_invites'], 'You have only 1 invite left.', 'You have only @count invites left.'));
      return;
    }

    // Check number of e-mails
    if (!user_access('send mass invitations') && count($emails) > 1) {
      form_set_error('email', t('You cannot send more than one invitation.'));
      return;
    }
  }

  // Save valid emails
  form_set_value(array(
    '#parents' => array(
      'valid_emails',
    ),
  ), $emails);
}