You are here

function privatemsg_new_form in Privatemsg 5

Same name and namespace in other branches
  1. 5.3 privatemsg.module \privatemsg_new_form()

Provides a form to write a private message.

2 string references to 'privatemsg_new_form'
privatemsg_menu in ./privatemsg.module
Implementation of hook_menu().
privatemsg_view in ./privatemsg.module
Display a private message to a user.

File

./privatemsg.module, line 1426

Code

function privatemsg_new_form($message = NULL) {
  global $user;
  if (!isset($message)) {
    $message = 0;
    $op = arg(1);
    if ($op == 'reply') {
      $message = arg(2);
    }
    else {
      if (($uid = arg(2)) && ($msg->recipient = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid)))) {
        if (!privatemsg_message_allowed($uid, $user->uid)) {
          drupal_set_message(t('You cannot contact %recipient', array(
            '%recipient' => $msg->recipient,
          )));
          drupal_goto("user/{$uid}");
        }
        $message = $msg;
      }
    }
  }
  if ($message && !is_object($message)) {

    // This is a reply to another message
    $message = db_fetch_object(db_query('SELECT thread, subject, message, u.name AS recipient FROM {privatemsg} p INNER JOIN {users} u ON u.uid = p.author WHERE id = %d AND recipient = %d', $message, $user->uid));
    if (!stristr($message->subject, t('Re:'))) {
      $message->subject = t('Re: ') . $message->subject;
    }

    // quoting; [quote] if default input format uses bbcode or quote, else > quoting
    foreach (filter_list_format(filter_resolve_format(FILTER_FORMAT_DEFAULT)) as $filter) {
      if ($filter->module == 'bbcode' || $filter->module == 'quote') {
        $bbcode = TRUE;
        break;
      }
    }
    if (isset($bbcode)) {
      $message->message = "\n\n[quote=" . $message->recipient . ']' . $message->message . '[/quote]';
    }
    else {
      $message->message = "\n\n\n" . str_replace("\n", "\n> ", "\n" . $message->message);
    }
  }
  if (!is_object($message)) {
    $message = new stdClass();
  }

  // The first if can provide a partial message, so we fill it in to avoid
  // notices. Also we remove the message if it's a reply in a threaded view,
  // the original message will be seen anyways.
  if (!isset($message->subject) || variable_get('privatemsg_threaded_view', 0)) {
    $message->message = '';
  }
  if (isset($_SESSION['recipients'])) {
    $recipient = implode(', ', $_SESSION['recipients']);
    unset($_SESSION['recipients']);
  }
  $form['thread'] = array(
    '#type' => 'value',
    '#value' => isset($message->thread) ? $message->thread : 0,
  );
  $form['header']['#theme'] = 'privatemsg_new_msg_header';
  $form['header']['recipient'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#description' => t('Separate multiple names with commas.'),
    '#default_value' => isset($recipient) ? $recipient : $message->recipient,
    '#autocomplete_path' => 'privatemsg/autocomplete',
    '#size' => 50,
    '#maxlength' => 1000,
    '#required' => TRUE,
  );
  $form['header']['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => $message->subject,
    '#size' => 50,
    '#maxlength' => 64,
    '#required' => TRUE,
  );
  $form['privatemsgbody'] = array(
    '#type' => 'textarea',
    '#title' => empty($message->thread) ? t('Message') : t('Reply'),
    '#default_value' => $message->message,
    '#cols' => 80,
    '#rows' => 6,
  );
  $form['filter_form'] = filter_form($message->format);
  $form['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview'),
    '#prefix' => '<div class="pm-controls">',
  );
  $form['send'] = array(
    '#type' => 'submit',
    '#value' => t('Send private message'),
  );
  $form['cancel'] = array(
    '#value' => l(t('Cancel'), arg(1) == 'reply' ? 'privatemsg/view/' . arg(2) : 'privatemsg'),
    '#suffix' => '</div>',
  );
  $form['#after_build'] = array(
    '_privatemsg_new_preview',
  );
  drupal_add_js(drupal_get_path('module', 'privatemsg') . '/privatemsg.js');
  return $form;
}