You are here

function privatemsg_new in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.pages.inc \privatemsg_new()
  2. 7.2 privatemsg.pages.inc \privatemsg_new()
  3. 7 privatemsg.pages.inc \privatemsg_new()
2 string references to 'privatemsg_new'
privatemsg_menu in ./privatemsg.module
Implements hook_menu().
privatemsg_view in ./privatemsg.module
Menu callback for viewing a thread.

File

./privatemsg.module, line 817
Allows users to send private messages to other users.

Code

function privatemsg_new(&$form_state, $recipients = array(), $subject = '', $thread_id = NULL, $read_all = FALSE) {
  global $user;
  $recipients_string = '';
  $body = '';

  // convert recipients to array of user objects
  if (!empty($recipients) && is_string($recipients) || is_int($recipients)) {
    $recipients = _privatemsg_generate_user_array($recipients);
  }
  elseif (is_object($recipients)) {
    $recipients = array(
      $recipients,
    );
  }
  elseif (empty($recipients) && is_string($recipients)) {
    $recipients = array();
  }
  $usercount = 0;
  $to = array();
  $to_themed = array();
  $blocked = FALSE;
  foreach ($recipients as $recipient) {
    if (in_array($recipient->name, $to)) {

      // We already added the recipient to the list, skip him.
      continue;
    }

    // Check if another module is blocking the sending of messages to the recipient by current user.
    $user_blocked = module_invoke_all('privatemsg_block_message', $user, array(
      $recipient->uid => $recipient,
    ));
    if (!count($user_blocked) != 0 && $recipient->uid) {
      if ($recipient->uid == $user->uid) {
        $usercount++;

        // Skip putting author in the recipients list for now.
        continue;
      }
      $to[] = $recipient->name;
      $to_themed[$recipient->uid] = theme('username', $recipient);
    }
    else {

      // Recipient list contains blocked users.
      $blocked = TRUE;
    }
  }
  if (empty($to) && $usercount >= 1 && !$blocked) {

    // Assume the user sent message to own account as if the usercount is one or less, then the user sent a message but not to self.
    $to[] = $user->name;
    $to_themed[$user->uid] = theme('username', $user);
  }
  if (!empty($to)) {
    $recipients_string = implode(', ', $to);
  }
  if (isset($form_state['values'])) {
    if (isset($form_state['values']['recipient'])) {
      $recipients_string = $form_state['values']['recipient'];
    }
    $subject = $form_state['values']['subject'];
    $body = $form_state['values']['body'];
  }
  if (!$thread_id && !empty($recipients_string)) {
    drupal_set_title(t('Write new message to %recipient', array(
      '%recipient' => $recipients_string,
    )));
  }
  elseif (!$thread_id) {
    drupal_set_title(t('Write new message'));
  }
  $form = array();
  if (isset($form_state['privatemsg_preview'])) {
    $form['message_header'] = array(
      '#type' => 'fieldset',
      '#attributes' => array(
        'class' => 'preview',
      ),
    );
    $form['message_header']['message_preview'] = array(
      '#value' => $form_state['privatemsg_preview'],
    );
  }
  $form['privatemsg'] = array(
    '#type' => 'fieldset',
    '#access' => privatemsg_user_access('write privatemsg'),
  );
  $form['privatemsg']['author'] = array(
    '#type' => 'value',
    '#value' => $user,
  );
  if (is_null($thread_id)) {
    $form['privatemsg']['recipient'] = array(
      '#type' => 'textfield',
      '#title' => t('To'),
      '#description' => t('Separate multiple names with commas.'),
      '#default_value' => $recipients_string,
      '#required' => TRUE,
      '#weight' => -10,
      '#size' => 50,
      '#autocomplete_path' => 'messages/user-name-autocomplete',
    );
  }
  $form['privatemsg']['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#size' => 50,
    '#maxlength' => 255,
    '#default_value' => $subject,
    '#weight' => -5,
  );
  $form['privatemsg']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#rows' => 6,
    '#weight' => 0,
    '#default_value' => $body,
    '#resizable' => TRUE,
  );
  $format = FILTER_FORMAT_DEFAULT;

  // The input filter widget looses the format during preview, specify it
  // explicitly.
  if (isset($form_state['values']) && array_key_exists('format', $form_state['values'])) {
    $format = $form_state['values']['format'];
  }
  $form['privatemsg']['format'] = filter_form($format);
  $form['privatemsg']['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview message'),
    '#submit' => array(
      'pm_preview',
    ),
    '#validate' => array(
      'pm_send_validate',
    ),
    '#weight' => 10,
  );
  $form['privatemsg']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send message'),
    '#submit' => array(
      'pm_send',
    ),
    '#validate' => array(
      'pm_send_validate',
    ),
    '#weight' => 15,
  );
  $url = 'messages';
  $title = t('Cancel');
  if (isset($_REQUEST['destination'])) {
    $url = $_REQUEST['destination'];
  }
  elseif (!is_null($thread_id)) {
    $url = $_GET['q'];
    $title = t('Clear');
  }
  $form['privatemsg']['cancel'] = array(
    '#value' => l($title, $url, array(
      'attributes' => array(
        'id' => 'edit-cancel',
      ),
    )),
    '#weight' => 20,
  );
  if (!is_null($thread_id)) {
    $form['privatemsg']['thread_id'] = array(
      '#type' => 'value',
      '#value' => $thread_id,
    );
    $form['privatemsg']['subject'] = array(
      '#type' => 'value',
      '#default_value' => $subject,
    );
    $recipients_string_themed = implode(', ', $to_themed);
    $form['privatemsg']['recipient_display'] = array(
      '#value' => '<p>' . t('<strong>Reply to thread</strong>:<br /> Recipients: !to', array(
        '!to' => $recipients_string_themed,
      )) . '</p>',
      '#weight' => -10,
    );
    if (empty($recipients_string)) {

      // If there are no valid recipients, unset the message reply form.
      $form['privatemsg']['#access'] = FALSE;
    }
  }
  $form['privatemsg']['read_all'] = array(
    '#type' => 'value',
    '#value' => $read_all,
  );
  return $form;
}