You are here

function invite_form_submit in Invite 5.2

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

Forms API callback; process submitted form data.

File

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

Code

function invite_form_submit($form_id, $edit) {
  global $user;
  $failed_emails = array();
  $num_failed = $num_succeeded = 0;

  // Get e-mails that failed validation.
  if (isset($_SESSION['invite_failed_emails'])) {
    $failed_emails = (array) unserialize($_SESSION['invite_failed_emails']);
    $num_failed = count($failed_emails);
  }
  $subject = isset($edit['subject']) ? trim($edit['subject']) : invite_get_subject();
  $message = isset($edit['message']) ? trim($edit['message']) : NULL;
  foreach ($edit['valid_emails'] as $email) {

    // Create the invite object.
    $code = $edit['reg_code'] ? $edit['reg_code'] : invite_generate_code();
    $invite = _invite_substitutions(array(
      'email' => $email,
      'code' => $code,
      'resent' => $edit['resent'],
      'data' => array(
        'subject' => $subject,
        'message' => $message,
      ),
    ));

    // Perform token replacement on mail body.
    $body = token_replace_multiple(t(_invite_get_mail_template()), _invite_token_types($invite));

    // Send e-mail.
    if (invite_send_invite($email, $subject, $body)) {

      // Save invite.
      invite_save($invite);

      // Notify other modules.
      if (!$edit['resent']) {
        $args = array(
          'inviter' => $invite->inviter,
          'email' => $invite->email,
          'code' => $invite->code,
        );
        module_invoke_all('invite', 'invite', $args);
      }
      $num_succeeded++;
    }
    else {
      $failed_emails[] = $email;
    }
  }

  // Store failed e-mails for re-display.
  if ($failed_emails) {
    $_SESSION['invite_failed_emails'] = serialize($failed_emails);
  }
  if ($num_succeeded) {
    if (isset($edit['remaining_invites'])) {

      // Update user property if user is limited.
      user_save($user, array(
        'invites' => $edit['remaining_invites'] - $num_succeeded,
      ));
    }
    $message = format_plural($num_succeeded, 'Your invitation has been successfully sent. You will be notified when the invitee joins the site.', '@count invitations have been successfully sent. You will be notified when any invitee joins the site.');
    drupal_set_message($message);
  }
  if ($num_failed) {
    $message = format_plural($num_failed, 'The entered e-mail address is invalid. Please correct it.', '@count entered e-mail addresses are invalid. Please correct them.');
    drupal_set_message($message, 'error');
  }
  else {
    if (user_access('track invitations') && $user->uid) {

      // Everything went well: redirect to pending invites page.
      return "user/{$user->uid}/invites/pending";
    }
  }
  return 'invite';
}