You are here

function privatemsg_new_submit in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.pages.inc \privatemsg_new_submit()
  2. 7 privatemsg.pages.inc \privatemsg_new_submit()

Submit callback for the privatemsg_new form.

1 string reference to 'privatemsg_new_submit'
_privatemsg_form_base_fields in ./privatemsg.pages.inc
Returns the common fields of the reply and new form.

File

./privatemsg.pages.inc, line 693
User menu callbacks for Privatemsg.

Code

function privatemsg_new_submit($form, &$form_state) {
  $message = $form_state['validate_built_message'];
  field_attach_submit('privatemsg_message', $message, $form, $form_state);

  // Format each recipient.
  $recipient_names = array();
  foreach ($message->recipients as $recipient) {
    $recipient_names[] = privatemsg_recipient_format($recipient);
  }
  try {
    $message = _privatemsg_send($message);
    _privatemsg_handle_recipients($message->mid, $message->recipients);
    drupal_set_message(t('A message has been sent to !recipients.', array(
      '!recipients' => implode(', ', $recipient_names),
    )));

    // Only redirect on new threads.
    if ($message->mid == $message->thread_id || variable_get('privatemsg_default_redirect_reply', FALSE)) {
      $redirect = variable_get('privatemsg_default_redirect', '<new-message>');
      if ($redirect == '<new-message>' || !empty($_REQUEST['destination']) && $_REQUEST['destination'] == '[new-message]') {
        if (!empty($_REQUEST['destination']) && $_REQUEST['destination'] == '[new-message]') {

          // Remove GET param so that drupal_goto() uses the redirect from
          // $form_state.
          unset($_GET['destination']);
        }

        // Forward to the new message in the thread.
        $form_state['redirect'] = array(
          'messages/view/' . $message->thread_id,
          array(
            'fragment' => 'privatemsg-mid-' . $message->mid,
          ),
        );
      }
      elseif (!empty($redirect)) {
        $form_state['redirect'] = $redirect;
      }
    }
  } catch (Exception $e) {
    if (error_displayable()) {
      require_once DRUPAL_ROOT . '/includes/errors.inc';
      $variables = _drupal_decode_exception($e);
      drupal_set_message(t('Failed to send a message to !recipients. %type: !message in %function (line %line of %file).', array(
        '!recipients' => implode(', ', $recipient_names),
      ) + $variables), 'error');
    }
    else {
      drupal_set_message(t('Failed to send a message to !recipients. Contact your site administrator.', array(
        '!recipients' => implode(', ', $recipient_names),
      )), 'error');
    }
  }
}