You are here

function hook_privatemsg_block_message in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.api.php \hook_privatemsg_block_message()
  2. 7.2 privatemsg.api.php \hook_privatemsg_block_message()
  3. 7 privatemsg.api.php \hook_privatemsg_block_message()

Check if the author can send a message to the recipients.

This can be used to limit who can write whom based on other modules and/or settings.

Parameters

$author: Author of the message to be sent

$recipients: Recipients of the message

Return value

An indexed array of arrays with the keys uid and message (The reason why the recipient has been blocked).

Related topics

1 function implements hook_privatemsg_block_message()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

pm_block_user_privatemsg_block_message in pm_block_user/pm_block_user.module
Implements hook_privatemsg_block_message().
3 invocations of hook_privatemsg_block_message()
privatemsg_get_link in ./privatemsg.module
Returns a link to send message form for a specific users.
privatemsg_new in ./privatemsg.module
_privatemsg_validate_message in ./privatemsg.module

File

./privatemsg.api.php, line 386
Privatemsg API Documentation

Code

function hook_privatemsg_block_message($author, $recipients) {
  $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
  // user_load() is needed here the array is updated, negating the need for
  // further calls to user_load() later in the code.
  foreach (array_keys($recipients) as $uid) {

    // Ensure we have a recipient user object which includes roles.
    if (!isset($recipients[$uid]->roles)) {
      $recipients[$uid] = user_load($uid);
    }

    // Note: this is checked 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[$uid], PM_BLOCK_USER_DISALLOW_SENDING)) {
      $blocked[] = array(
        'uid' => $uid,
        'message' => t('Sorry, private messaging rules forbid sending messages to !name.', array(
          '!name' => $recipients[$uid]->name,
        )),
      );
    }
  }
  $args = array_merge(array(
    $author->uid,
  ), array_keys($recipients));
  $result = db_query('SELECT recipient FROM {pm_block_user} WHERE author = %d AND recipient IN (' . db_placeholders($recipients) . ') GROUP BY recipient', $args);
  while ($row = db_fetch_array($result)) {
    $recipient = $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(
      'uid' => $row['recipient'],
      'message' => t('%name has chosen to not recieve any more messages from you.', array(
        '%name' => $recipients[$row['recipient']]->name,
      )),
    );
  }
  return $blocked;
}