You are here

function privatemsg_new_validate in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 privatemsg.pages.inc \privatemsg_new_validate()
  2. 7.2 privatemsg.pages.inc \privatemsg_new_validate()
1 string reference to 'privatemsg_new_validate'
privatemsg_new in ./privatemsg.pages.inc

File

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

Code

function privatemsg_new_validate($form, &$form_state) {

  // The actual message that is being sent, we create this during validation and
  // pass to submit to send out.
  $message = (object) $form_state['values'];
  $message->mid = 0;
  $message->format = $message->body['format'];
  $message->body = $message->body['value'];
  $message->timestamp = REQUEST_TIME;

  // Avoid subjects which only consist of a space as these can not be clicked.
  $message->subject = trim($message->subject);
  $trimmed_body = trim(truncate_utf8(strip_tags($message->body), 50, TRUE, TRUE));
  if (empty($message->subject) && !empty($trimmed_body)) {
    $message->subject = $trimmed_body;
  }

  // Only parse the user string for a new thread.
  if (!isset($message->thread_id)) {
    list($message->recipients, $invalid, $duplicates, $denieds) = _privatemsg_parse_userstring($message->recipient);
  }
  else {

    // Load participants. Limit recipients to visible unless read_all is TRUE.
    $message->recipients = _privatemsg_load_thread_participants($message->thread_id, $message->read_all ? FALSE : $message->author);
  }
  if (!empty($invalid)) {

    // Display information about invalid recipients.
    drupal_set_message(t('The following users will not receive this private message: @invalid.', array(
      '@invalid' => implode(", ", $invalid),
    )), 'error');
  }
  if (!empty($denieds)) {

    // Display information about denied recipients.
    drupal_set_message(t('You do not have access to write these recipients: @denieds.', array(
      '@denieds' => implode(", ", $denieds),
    )), 'error');
  }
  if (!empty($duplicates)) {

    // Add JS and CSS to allow choosing the recipient.
    drupal_add_js(drupal_get_path('module', 'privatemsg') . '/privatemsg-alternatives.js');

    // Display information about recipients that couldn't be identified
    // uniquely.
    $js_duplicates = array();
    foreach ($duplicates as $string => $duplicate) {
      $alternatives = array();
      foreach ($duplicate as $match) {
        $formatted_match = privatemsg_recipient_format($match, array(
          'plain' => TRUE,
          'unique' => TRUE,
        ));
        $js_duplicates[$formatted_match] = $string;
        $alternatives[] = '<span class="privatemsg-recipient-alternative">' . $formatted_match . '</span>';
      }

      // Build a formatted list of possible recipients.
      $alternatives = theme('item_list', array(
        'items' => $alternatives,
        'attributes' => array(
          'class' => array(
            'action-links',
          ),
        ),
      ));
      form_set_error('recipient', '<span class="privatemsg-alternative-description">' . t('The site has multiple recipients named %string. Please choose your intended recipient: !list', array(
        '%string' => $string,
        '!list' => $alternatives,
      )) . '</span>');
    }

    // Also make that information available to the javascript replacement code.
    drupal_add_js(array(
      'privatemsg_duplicates' => $js_duplicates,
    ), 'setting');
  }
  $validated = _privatemsg_validate_message($message, TRUE);
  foreach ($validated['messages'] as $type => $texts) {
    foreach ($texts as $text) {
      drupal_set_message($text, $type);
    }
  }
  $form_state['validate_built_message'] = $message;
}