You are here

function privatemsg_new in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 privatemsg.module \privatemsg_new()
  2. 7.2 privatemsg.pages.inc \privatemsg_new()
  3. 7 privatemsg.pages.inc \privatemsg_new()

Form builder function; Write a new private message.

1 string reference to 'privatemsg_new'
privatemsg_menu in ./privatemsg.module
Implements hook_menu().

File

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

Code

function privatemsg_new(&$form_state, $recipients = '', $subject = '') {
  global $user;

  // 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,
    )));
  }
  else {
    drupal_set_title(t('Write new message'));
  }
  $form = array(
    '#access' => privatemsg_user_access('write privatemsg'),
  );
  $form += _privatemsg_form_base_fields($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', $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 = privatemsg_get_dynamic_url_prefix();
  if (isset($_REQUEST['destination'])) {
    $url = $_REQUEST['destination'];
  }
  $form['cancel'] = array(
    '#value' => l(t('Cancel'), $url, array(
      'attributes' => array(
        'id' => 'edit-cancel',
      ),
    )),
    '#weight' => 20,
  );
  return $form;
}