You are here

function privatemsg_new in Privatemsg 7

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

File

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

Code

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

  // 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);
  }
  elseif (is_object($recipients)) {
    $recipients = array(
      $recipients,
    );
  }
  elseif (empty($recipients) && is_string($recipients)) {
    $recipients = array();
  }
  $usercount = 0;
  $to = array();
  $to_plain = array();
  $blocked_messages = array();
  foreach ($recipients as $recipient) {

    // Allow to pass in normal user objects.
    if (empty($recipient->type)) {
      $recipient->type = 'user';
      $recipient->recipient = $recipient->uid;
    }
    if ($recipient->type == 'hidden') {
      continue;
    }
    if (isset($to[privatemsg_recipient_key($recipient)])) {

      // We already added the recipient to the list, skip him.
      continue;
    }
    if (!privatemsg_recipient_access($recipient->type, 'write', $recipient)) {

      // User does not have access to write to this recipient, continue.
      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(
      privatemsg_recipient_key($recipient) => $recipient,
    ), array(
      'thread_id' => $thread_id,
    ));
    if (!count($user_blocked) != 0 && $recipient->recipient) {
      if ($recipient->type == 'user' && $recipient->recipient == $user->uid) {
        $usercount++;

        // Skip putting author in the recipients list for now.
        continue;
      }
      $to[privatemsg_recipient_key($recipient)] = privatemsg_recipient_format($recipient);
      $to_plain[privatemsg_recipient_key($recipient)] = privatemsg_recipient_format($recipient, array(
        'plain' => TRUE,
        'unique' => $unique,
      ));
    }
    else {

      // Store blocked messages. These are only displayed if all recipients
      // are blocked.
      $first_reason = reset($user_blocked);
      $blocked_messages[] = $first_reason['message'];
    }
  }
  if (empty($to) && $usercount >= 1 && empty($blocked_messages)) {

    // 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_' . $user->uid] = privatemsg_recipient_format($user);
    $to_plain['user_' . $user->uid] = privatemsg_recipient_format($user, array(
      'plain' => TRUE,
    ));
  }

  // 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 (!empty($to)) {
    $recipients_plain = implode(', ', $to_plain);
  }
  if (isset($form_state['values'])) {
    if (isset($form_state['values']['recipient'])) {
      $recipients_plain = $form_state['values']['recipient'];
    }
    if (isset($form_state['values']['subject'])) {
      $subject = $form_state['values']['subject'];
    }
    if (isset($form_state['values']['body'])) {
      $body = $form_state['values']['body'];
    }
  }
  if (!$thread_id && !empty($recipients_plain)) {
    drupal_set_title(t('Write new message to @recipient', array(
      '@recipient' => $recipients_plain,
    )));
  }
  elseif (!$thread_id) {
    drupal_set_title(t('Write new message'));
  }
  $form = array(
    '#access' => privatemsg_user_access('write privatemsg') || privatemsg_user_access('reply only privatemsg'),
  );
  if (isset($form_state['privatemsg_preview'])) {
    $preview_subject = '';

    // Only display subject on preview for new messages.
    if (empty($form_state['validate_built_message']->thread_id)) {
      $preview_subject = check_plain($form_state['validate_built_message']->subject);

      // If message has tokens, replace them.
      if ($form_state['validate_built_message']->has_tokens) {
        $preview_subject = privatemsg_token_replace($preview_subject, array(
          'privatemsg_message' => $form_state['validate_built_message'],
        ), array(
          'sanitize' => TRUE,
          'privatemsg-show-span' => FALSE,
        ));
      }
    }
    $form['message_header'] = array(
      '#type' => 'fieldset',
      '#title' => !empty($preview_subject) ? $preview_subject : t('Preview'),
      '#attributes' => array(
        'class' => array(
          'preview',
        ),
      ),
      '#weight' => -20,
    );
    $form['message_header']['message_preview'] = $form_state['privatemsg_preview'];
  }
  $form['author'] = array(
    '#type' => 'value',
    '#value' => $user,
  );
  if (is_null($thread_id)) {
    $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',
    );
  }
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#size' => 50,
    '#maxlength' => 255,
    '#default_value' => $subject,
    '#weight' => -5,
  );

  // 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['body'] = array(
    '#type' => 'text_format',
    '#title' => t('Message'),
    '#rows' => 6,
    '#weight' => -3,
    '#default_value' => $body,
    '#resizable' => TRUE,
    '#format' => isset($format) ? $format : NULL,
    '#after_build' => array(
      'privatemsg_check_format_access',
    ),
  );
  if (privatemsg_user_access('use tokens in privatemsg') && module_exists('token')) {
    $form['token'] = array(
      '#type' => 'fieldset',
      '#title' => t('Token browser'),
      '#weight' => -1,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['token']['browser'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'privatemsg_message',
      ),
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  if (variable_get('privatemsg_display_preview_button', FALSE)) {
    $form['actions']['preview'] = array(
      '#type' => 'submit',
      '#value' => t('Preview message'),
      '#submit' => array(
        'privatemsg_new_preview',
      ),
      '#weight' => 48,
    );
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send message'),
    '#weight' => 49,
  );
  $url = 'messages';
  $title = t('Cancel');
  if (isset($_REQUEST['destination'])) {
    $url = $_REQUEST['destination'];
  }
  elseif (!is_null($thread_id)) {
    $url = $_GET['q'];
    $title = t('Clear');
  }
  $form['actions']['cancel'] = array(
    '#markup' => l($title, $url, array(
      'attributes' => array(
        'id' => 'edit-cancel',
      ),
    )),
    '#weight' => 50,
  );
  if (!is_null($thread_id)) {
    $form['thread_id'] = array(
      '#type' => 'value',
      '#value' => $thread_id,
    );
    $form['subject'] = array(
      '#type' => 'value',
      '#default_value' => $subject,
    );
    $form['reply'] = array(
      '#markup' => '<h2 class="privatemsg-reply">' . t('Reply') . '</h2>',
      '#weight' => -10,
    );
    if (empty($to)) {

      // If there are no valid recipients, hide all visible parts of the form.
      foreach (element_children($form) as $element) {
        $form[$element]['#access'] = FALSE;
      }

      // Display a message if some users are blocked.
      if (!empty($blocked_messages)) {
        $form['blocked'] = array(
          '#theme' => 'item_list',
          '#items' => $blocked_messages,
          '#title' => t('You can not reply to this conversation because all recipients are blocked.'),
        );
      }
    }
  }

  // Only set read all if it is a boolean TRUE. It might also be an integer set
  // through the URL.
  $form['read_all'] = array(
    '#type' => 'value',
    '#value' => $read_all === TRUE,
  );

  // Attach field widgets.
  $message = (object) array();
  if (isset($form_state['validate_built_message'])) {
    $message = $form_state['validate_built_message'];
  }

  // If a module (e.g. OG) adds a validate or submit callback to the form in
  // field_attach_form, the form system will not add ours automatically
  // anymore. Therefore, explicitly adding them here.
  $form['#submit'] = array(
    'privatemsg_new_submit',
  );
  $form['#validate'] = array(
    'privatemsg_new_validate',
  );
  field_attach_form('privatemsg_message', $message, $form, $form_state);
  return $form;
}