function privatemsg_new

1 privatemsg.module privatemsg_new(&$form_state, $recipients = array(), $subject = '', $thread_id = NULL, $read_all = FALSE)
7-1 privatemsg.pages.inc privatemsg_new($form, &$form_state, $recipients = array(), $subject = '', $thread_id = NULL, $read_all = FALSE)
7-2 privatemsg.pages.inc privatemsg_new($form, &$form_state, $recipients = '', $subject = '')
6-2 privatemsg.pages.inc privatemsg_new(&$form_state, $recipients = '', $subject = '')

Form builder function; Write a new private message.

2 string references to 'privatemsg_new'
privatemsg_menu in ./privatemsg.module
Implements hook_menu().
privatemsg_mollom_form_list in ./privatemsg.module
Implements hook_mollom_form_list().

File

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

Code

function privatemsg_new($form, &$form_state, $recipients = '', $subject = '') {

  // Convert recipients to array of user objects.
  $unique = FALSE;
  if (!empty($recipients) && is_string($recipients) || is_int($recipients)) {
    $unique = TRUE;
    $recipients = _privatemsg_generate_user_array($recipients);
  }
  else {
    $recipients = array();
  }

  // Subject has / encoded twice if clean urls are enabled to get it through
  // mod_rewrite and the menu system. Decode it once more.
  $subject = str_replace('%2F', '/', $subject);

  if (isset($form_state['values'])) {
    if (isset($form_state['values']['recipient'])) {
      $recipients_plain = $form_state['values']['recipient'];
    }
    $subject = $form_state['values']['subject'];
  }
  else {
    $to = _privatemsg_get_allowed_recipients($recipients);

    $recipients_plain = '';
    if (!empty($to)) {
      $to_plain = array();
      $to_title = array();
      foreach ($to as $recipient) {
        // Load user(s) with that name
        $to_user = _privatemsg_parse_userstring($recipient->name);
        // If the count of duplicated is more than 0 that means the recipient name is used by the other recipient type,
        // so we should use unique.
        $to_plain[] = privatemsg_recipient_format($recipient, array('plain' => TRUE, 'unique' => (count($to_user[2]) > 0)));
        $to_title[] = privatemsg_recipient_format($recipient, array('plain' => TRUE));
      }
      $recipients_plain = implode(', ', $to_plain);
      $recipients_title = implode(', ', $to_title);
    }
  }

  if (!empty($recipients_title)) {
    drupal_set_title(t('Write new message to %recipient', array('%recipient' => $recipients_title)), PASS_THROUGH);
  }
  else {
    drupal_set_title(t('Write new message'));
  }

  $form = array('#access' => privatemsg_user_access('write privatemsg'));

  $form += _privatemsg_form_base_fields($form, $form_state);

  $description_array = array();
  foreach (privatemsg_recipient_get_types() as $name => $type) {
    if (privatemsg_recipient_access($name, 'write')) {
      $description_array[] = $type['description'];
    }
  }
  $description = t('Enter the recipient, separate recipients with commas.');
  $description .= theme('item_list', array('items' => $description_array));

  $form['recipient'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#description' => $description,
    '#default_value' => $recipients_plain,
    '#required' => TRUE,
    '#weight' => -10,
    '#size' => 50,
    '#autocomplete_path' => 'messages/autocomplete',
    // Disable #maxlength, make it configurable by number of recipients, not
    // their name length.
    '#after_build' => array('privatemsg_disable_maxlength'),
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#size' => 50,
    '#maxlength' => 255,
    '#default_value' => $subject,
    '#weight' => -5,
  );

  $url = 'messages';
  if (isset($_REQUEST['destination'])) {
    $url = $_REQUEST['destination'];
  }

  $form['actions']['cancel'] = array(
    '#value' => l(t('Cancel'), $url, array('attributes' => array('id' => 'edit-cancel'))),
    '#weight' => 20,
  );

  return $form;
}