You are here

function invite_form_submit in Invite 5

Same name and namespace in other branches
  1. 5.2 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; processes the incoming form data.

Sends out invitation e-mails.

File

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

Code

function invite_form_submit($form_id, $edit) {
  $failed_emails = array();
  $count_failed = $count_success = 0;
  if (isset($_SESSION['invite_failed_emails'])) {
    $failed_emails = (array) unserialize($_SESSION['invite_failed_emails']);
    $count_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
    $invite = _invite_substitutions(array(
      'email' => $email,
      'code' => invite_regcode(),
      '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
      $args = array(
        'inviter' => $invite->inviter,
        'code' => $invite->code,
      );
      module_invoke_all('invite', 'invite', $args);
      $count_success++;
    }
    else {
      $failed_emails[] = $email;
    }
  }
  if ($failed_emails) {
    $_SESSION['invite_failed_emails'] = serialize($failed_emails);
  }
  if ($count_success) {
    $message = format_plural($count_success, '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 ($count_failed) {
    $message = format_plural($count_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')) {
      return 'invite/list';
    }
  }
  return 'invite';
}