You are here

function _privatemsg_handle_recipients in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 privatemsg.module \_privatemsg_handle_recipients()
  2. 7.2 privatemsg.module \_privatemsg_handle_recipients()

Handle the non-user recipients of a new message.

Either process them directly if they have less than a certain amount of users or, if enabled, add them to a batch.

Parameters

$mid: Message id for which the recipients are processed.

$recipients: Array of recipients.

$use_batch: Use batch API to process recipients.

3 calls to _privatemsg_handle_recipients()
privatemsg_new_submit in ./privatemsg.pages.inc
Submit callback for the privatemsg_new form.
privatemsg_new_thread in ./privatemsg.module
Send a new message.
privatemsg_reply in ./privatemsg.module
Send a reply message

File

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

Code

function _privatemsg_handle_recipients($mid, $recipients, $use_batch = TRUE) {
  $batch = array(
    'title' => t('Processing recipients'),
    'operations' => array(),
    'file' => drupal_get_path('module', 'privatemsg') . '/privatemsg.pages.inc',
    'progress_message' => t('Processing recipients'),
  );
  $small_threshold = variable_get('privatemsg_recipient_small_threshold', 100);
  foreach ($recipients as $recipient) {

    // Add a batch operation to press non-user recipient types.
    if ($recipient->type != 'user' && $recipient->type != 'hidden') {
      $type = privatemsg_recipient_get_type($recipient->type);

      // Count the recipients, if there are less than small_threshold, process
      // them right now.
      $count_function = $type['count'];
      if (!is_callable($count_function)) {
        db_update('pm_index')
          ->fields(array(
          'is_new' => PRIVATEMSG_READ,
        ))
          ->condition('mid', $mid)
          ->condition('recipient', $recipient->recipient)
          ->condition('type', $recipient->type)
          ->execute();
        drupal_set_message(t('Recipient type %type is not correctly implemented', array(
          '%type' => $recipient->type,
        )), 'error');
        continue;
      }
      $count = $count_function($recipient);
      if ($count < $small_threshold) {
        $load_function = $type['generate recipients'];
        if (!is_callable($load_function)) {
          db_update('pm_index')
            ->fields(array(
            'is_new' => PRIVATEMSG_READ,
          ))
            ->condition('mid', $mid)
            ->condition('recipient', $recipient->recipient)
            ->condition('type', $recipient->type)
            ->execute();
          drupal_set_message(t('Recipient type %type is not correctly implemented', array(
            '%type' => $recipient->type,
          )), 'error');
          continue;
        }
        $uids = $load_function($recipient, $small_threshold, 0);
        if (!empty($uids)) {
          foreach ($uids as $uid) {
            privatemsg_message_change_recipient($mid, $uid, 'hidden');
          }
        }
        db_update('pm_index')
          ->fields(array(
          'is_new' => PRIVATEMSG_READ,
        ))
          ->condition('mid', $mid)
          ->condition('recipient', $recipient->recipient)
          ->condition('type', $recipient->type)
          ->execute();
        continue;
      }
      if ($use_batch) {
        $batch['operations'][] = array(
          'privatemsg_load_recipients',
          array(
            $mid,
            $recipient,
          ),
        );
      }
    }
  }

  // Set batch if there are outstanding operations.
  if ($use_batch && !empty($batch['operations'])) {
    batch_set($batch);
  }
}