You are here

function pm_block_user_privatemsg_block_message in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 pm_block_user/pm_block_user.module \pm_block_user_privatemsg_block_message()
  2. 6 pm_block_user/pm_block_user.module \pm_block_user_privatemsg_block_message()
  3. 7 pm_block_user/pm_block_user.module \pm_block_user_privatemsg_block_message()

Implements hook_privatemsg_block_message.

File

pm_block_user/pm_block_user.module, line 253
Allows users to block other users from sending them any messages

Code

function pm_block_user_privatemsg_block_message($author, $recipients, $context = array()) {
  $blocked = array();

  // Loop through each recipient and ensure there is no rule blocking this
  // author from sending them private messages. Use a reference, so when
  // privatemsg_user_load_multiple() is needed here the array is updated, to
  // avoid additional checks.
  foreach (array_keys($recipients) as $id) {

    // Only recipients of type user are currently supported.
    if (isset($recipients[$id]->type) && $recipients[$id]->type != 'user') {
      continue;
    }

    // Ensure we have a recipient user object which includes roles.
    if (!isset($recipients[$id]->roles)) {
      $uid = str_replace('user_', '', $id);
      $recipients[$id] = array_shift(privatemsg_user_load_multiple($uid));
    }

    // Note: this is checks whether the author may send the message (see third
    // parameter). Further below is a check whether the recipient may block it.
    if (_pm_block_user_rule_exists($author, $recipients[$id], PM_BLOCK_USER_DISALLOW_SENDING)) {
      $blocked[] = array(
        'recipient' => $id,
        'message' => t('You are not permitted to send messages to !user.', array(
          '!user' => privatemsg_recipient_format($recipients[$id]),
        )),
      );
    }
  }

  // Only user recipients are supported for now, remove others.
  $user_recipients = array();
  foreach ($recipients as $recipient) {
    if (empty($recipient->type)) {
      $recipient->type = 'user';
      $recipient->recipient = $recipient->uid;
    }
    if ($recipient->type == 'user') {
      $user_recipients[$recipient->recipient] = $recipient;
    }
  }
  if (empty($user_recipients)) {
    return $blocked;
  }
  $args = array(
    ':author' => $author->uid,
    ':recipients' => array_keys($user_recipients),
  );
  $result = db_query('SELECT recipient FROM {pm_block_user} WHERE author = :author AND recipient IN (:recipients) GROUP BY recipient', $args);
  foreach ($result as $row) {
    $recipient = $user_recipients[$row->recipient];

    // If there's a rule disallowing blocking of this message, send it anyway.
    if (_pm_block_user_rule_exists($author, $recipient, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
      continue;
    }
    $blocked[] = array(
      'recipient' => privatemsg_recipient_key($recipient),
      'message' => t('%name has chosen to block messages from you.', array(
        '%name' => privatemsg_recipient_format($recipient, array(
          'plain' => TRUE,
        )),
      )),
    );
  }
  return $blocked;
}