You are here

function invite_form_submit in Invite 7.2

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

Forms API callback; process submitted form data.

File

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

Code

function invite_form_submit($form, &$form_state) {
  global $user, $language;
  $show_only = $form_state['clicked_button']['#value'] == t('Show registration link');

  // Set this now, so other modules can change it later.
  // $form_state['redirect'] = 'invite';
  $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);
  }
  $processed_invites = array();
  foreach ($form_state['values']['valid_emails'] as $email) {

    // Create or load the invite object.
    if (!empty($form_state['values']['reg_code'])) {
      $invite = invite_load($form_state['values']['reg_code']);
      $invite->expiry = REQUEST_TIME + variable_get('invite_expiry', 30) * 60 * 60 * 24;
    }
    else {
      $invite = invite_create();
      $invite->email = $email;
    }
    $invite->data = array(
      'subject' => trim($form_state['values']['subject']),
      'message' => '',
    );
    if (variable_get('invite_mail_template_editable', TRUE)) {
      $message = $form_state['values']['message'];
    }
    else {
      $message = _invite_get_mail_template();
      $invite->data['user_message'] = $form_state['values']['message'];
    }
    $invite->data['message'] = token_replace($message, invite_token_data($invite));
    if (invite_send($invite, !$show_only)) {
      $num_succeeded++;
      $processed_invites[] = $invite;
    }
    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 (!$show_only) {
      $message = format_plural($num_succeeded, 'Your invitation has been successfully sent.' . ($user->uid ? ' You will be notified when the invitee joins the site.' : ''), '@count invitations have been successfully sent.' . ($user->uid ? ' You will be notified when any invitee joins the site.' : ''));
      drupal_set_message($message);
    }
    else {
      $form_state['invite_show_reg_links'] = TRUE;
      $form_state['invite_processed_invites'] = $processed_invites;
      $form_state['rebuild'] = TRUE;
      drupal_set_message('Your invitation has been successfully created.');
    }
  }
  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');
  }
  elseif (!$show_only && user_access('track invitations') && $user->uid) {
    $form_state['redirect'] = "user/{$user->uid}/invites/pending";
  }
}