You are here

function invite_form_submit in Invite 6.2

Same name and namespace in other branches
  1. 5.2 invite.module \invite_form_submit()
  2. 5 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 1071
Allows your users to send and track invitations to join your site.

Code

function invite_form_submit($form, &$form_state) {
  global $user, $language;

  // 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);
  }
  $subject = isset($form_state['values']['subject']) ? trim($form_state['values']['subject']) : invite_get_subject();
  $message = isset($form_state['values']['message']) ? trim($form_state['values']['message']) : NULL;
  if (!variable_get('invite_use_users_email', 0)) {
    $from = variable_get('invite_manual_from', '');
  }
  elseif ($user->uid) {
    $from = $user->mail;
  }
  if (!$from) {

    // Never pass an empty string to drupal_mail()
    $from = NULL;
  }
  foreach ($form_state['values']['valid_emails'] as $email) {

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

    // Send e-mail.
    $params = array(
      'invite' => $invite,
    );
    $result = drupal_mail('invite', 'invite', $email, $language, $params, $from, TRUE);
    if ($result['result']) {

      // Save invite.
      invite_save($invite);

      // Notify other modules.
      if (!$form_state['values']['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($form_state['values']['remaining_invites'])) {

      // Update user property if user is limited.
      user_save($user, array(
        'invites' => $form_state['values']['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');
  }
  elseif (user_access('track invitations') && $user->uid) {

    // Everything went well: redirect to pending invites page.
    $form_state['redirect'] = "user/{$user->uid}/invites/pending";
  }
}