From 9084fea87b979c03185f64e89cecd356fc08369e Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros+test@gmail.com>
Date: Sun, 13 Mar 2011 19:40:23 +0100
Subject: [PATCH] Issue #728552 by Berdir: Added relationship recipient type for private messages and moved existing privatemsg-related settings into separate sub-module.

---
 .../user_relationship_privatemsg.info              |   10 +
 .../user_relationship_privatemsg.install           |   35 ++
 .../user_relationship_privatemsg.module            |  330 ++++++++++++++++++++
 .../user_relationship_privatemsg.test              |  174 ++++++++++
 .../user_relationships_api.module                  |   12 +-
 .../user_relationships_api.privatemsg.inc          |   75 -----
 .../user_relationships_ui.admin.inc                |   31 --
 user_relationships_ui/user_relationships_ui.module |   19 --
 8 files changed, 553 insertions(+), 133 deletions(-)
 create mode 100644 user_relationship_privatemsg/user_relationship_privatemsg.info
 create mode 100644 user_relationship_privatemsg/user_relationship_privatemsg.install
 create mode 100644 user_relationship_privatemsg/user_relationship_privatemsg.module
 create mode 100644 user_relationship_privatemsg/user_relationship_privatemsg.test
 delete mode 100644 user_relationships_api/user_relationships_api.privatemsg.inc

diff --git a/user_relationship_privatemsg/user_relationship_privatemsg.info b/user_relationship_privatemsg/user_relationship_privatemsg.info
new file mode 100644
index 0000000..16c0200
--- /dev/null
+++ b/user_relationship_privatemsg/user_relationship_privatemsg.info
@@ -0,0 +1,10 @@
+name = UR-Private message integration
+description = Allows to send message to relationships.
+package = "User Relationships"
+core = 7.x
+dependencies[] = privatemsg
+dependencies[] = user_relationships_api
+dependencies[] = user_relationships_ui
+files[]=user_relationship_privatemsg.test
+
+
diff --git a/user_relationship_privatemsg/user_relationship_privatemsg.install b/user_relationship_privatemsg/user_relationship_privatemsg.install
new file mode 100644
index 0000000..cefc878
--- /dev/null
+++ b/user_relationship_privatemsg/user_relationship_privatemsg.install
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @file
+ * Install time hooks for user_relationship_privatemsg module.
+ */
+
+/***
+ * Implements hook_schema().
+ */
+function user_relationship_privatemsg_schema() {
+  $schema['user_relationship_privatemsg'] = array(
+    'fields' => array(
+      'urpid'   => array(
+        'type'     => 'serial',
+        'not NULL' => TRUE,
+      ),
+      'rtid'   => array(
+        'type'     => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'author' => array(
+        'type'     => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+    ),
+    'primary key' => array('urpid'),
+    'unique keys' => array(
+      'rtid_author' => array('rtid', 'author'),
+    ),
+  );
+
+  return $schema;
+}
diff --git a/user_relationship_privatemsg/user_relationship_privatemsg.module b/user_relationship_privatemsg/user_relationship_privatemsg.module
new file mode 100644
index 0000000..f43197a
--- /dev/null
+++ b/user_relationship_privatemsg/user_relationship_privatemsg.module
@@ -0,0 +1,330 @@
+<?php
+/**
+ * @file
+ * Allows to send messages to all members of a role.
+ */
+
+/**
+ * Implements hook_perm().
+ */
+function user_relationship_privatemsg_permission() {
+  return array(
+    'write privatemsg to relationships' => array(
+      'title' => t('Write private messages to relationships'),
+      'description' => t('Users with this permission are allowed to write a private message to all related users.'),
+    ),
+    'view relationship recipients' => array(
+      'title' => t('View relationship recipients'),
+      'description' => t('Users with this permission will be able to see that a message has been sent to a relationship.'),
+    ),
+  );
+}
+
+function user_relationship_privatemsg_theme() {
+  return array(
+    'user_relationship_privatemsg_format' => array(
+      'variables' => array('recipient' => NULL, 'options' => array()),
+    ),
+  );
+}
+
+/**
+ * Implements hook_privatemsg_recipient_types_info().
+ */
+function user_relationship_privatemsg_privatemsg_recipient_type_info() {
+  $types = user_relationships_types_load();
+
+  // If there is no relationship defined, don't expose it as a recipient type.
+  if (empty($types)) {
+    return;
+  }
+
+  return array(
+    'user_relationship' => array(
+      'name' => t('User relationship'),
+      'description' => t('Enter the name of a user relationship to write a message to all related users. Example: %example.', array('%example' => reset($types)->plural_name)),
+      'format' => 'user_relationship_privatemsg_format',
+      'load' => 'user_relationship_privatemsg_load_multiple',
+      'autocomplete' => 'user_relationship_privatemsg_autocomplete',
+      'generate recipients' => 'user_relationship_privatemsg_load_recipients',
+      'count' => 'user_relationship_privatemsg_count_recipients',
+      'write callback' => 'user_relationship_privatemsg_check_write_access',
+      'view access' => 'view relationship recipients',
+    ),
+  );
+}
+
+function user_relationship_privatemsg_check_write_access($relationship = NULL) {
+  global $user;
+  // Users are only allowed to write their own related users.
+  if ($relationship) {
+    if (!isset($relationship->account)) {
+      $author = db_query('SELECT author FROM {user_relationship_privatemsg} WHERE urpid = :urpid', array(':urpid' => $relationship->recipient))->fetchField();
+      $relationship->account = user_load($author);
+    }
+    if ($relationship->account->uid != $user->uid) {
+      return FALSE;
+    }
+  }
+  return user_access('write privatemsg to relationships');
+}
+
+/**
+ * Format a relationship for displaying as recipient.
+ */
+function theme_user_relationship_privatemsg_format($variables) {
+  global $user;
+  $relationship = $variables['recipient'];
+  $options = $variables['options'];
+
+  if (!empty($options['plain'])) {
+    return $relationship->plural_name;
+  }
+
+  if ($relationship->account->uid == $user->uid) {
+    return l(t('Your %relationship (relationship)', array('%relationship' => $relationship->plural_name)), 'relationships/' . $relationship->rtid, array('html' => TRUE));
+  }
+  $name = t('%relationship of @username (relationship)', array('%relationship' => $relationship->plural_name, '@username' => format_username($relationship->account)));
+  if (user_relationships_ui_check_access(array('user'), $relationship->account)) {
+    return l($name, 'user/' . $relationship->account->uid . '/relationships/' . $relationship->rtid, array('html' => TRUE));
+  }
+  return $name;
+}
+
+/**
+ * Load relationships based on their rtid.
+ */
+function user_relationship_privatemsg_load_multiple($urpids) {
+  global $user;
+  $relationships = array();
+  $result = db_query('SELECT * FROM {user_relationship_privatemsg} WHERE urpid IN (:urpids)', array(':urpids' => $urpids));
+  foreach ($result as $row) {
+    if ($relationship = user_relationships_type_load($row->rtid)) {
+      $relationship->type = 'user_relationship';
+      $relationship->recipient = $row->urpid;
+      $relationship->account = user_load($row->author);
+      $relationships[privatemsg_recipient_key($relationship)] = $relationship;
+    }
+  }
+  return $relationships;
+}
+
+/**
+ * Returns the recipient id for a rtid - author combination.
+ */
+function _user_relationship_privatemsg_get_recipient_id($rtid, $uid) {
+  $urpid = db_query('SELECT urpid FROM {user_relationship_privatemsg} WHERE rtid = :rtid AND author = :author', array(':rtid' => $rtid, ':author' => $uid))->fetchField();
+  if ($urpid) {
+    return $urpid;
+  }
+  return db_insert('user_relationship_privatemsg')
+    ->fields(array(
+      'rtid' => $rtid,
+      'author' => $uid,
+    ))
+    ->execute();
+}
+
+/**
+ * Load a number of recipient user ids.
+ */
+function user_relationship_privatemsg_load_recipients($relationship, $limit, $offset) {
+  $recipients = array();
+  $relationships = user_relationships_load(array('user' => $relationship->account->uid, 'rtid' => $relationship->rtid), array('limit' => $limit, 'offset' => $offset));
+  foreach ($relationships as $row) {
+    if ($row->requester_id == $relationship->account->uid) {
+      $recipients[] = $row->requestee_id;
+    }
+    else {
+      $recipients[] = $row->requester_id;
+    }
+  }
+  return $recipients;
+}
+
+/**
+ * Return the number of users to which the author is related.
+ */
+function user_relationship_privatemsg_count_recipients($relationship) {
+  return user_relationships_load(array('user' => $relationship->account->uid, 'rtid' => $relationship->rtid), array('count' => TRUE));
+}
+
+/**
+ * Return relationship autocomplete suggestions.
+ */
+function user_relationship_privatemsg_autocomplete($search, $names, $limit) {
+  global $user;
+  $matches = array();
+  $types = user_relationships_types_load();
+  foreach ($types as $type) {
+    // Only look for relationship types which start with $search but search
+    // case insensitive.
+    if (stripos($type->plural_name, $search) === 0 && !in_array($type->plural_name, $names)) {
+      $type->type = 'user_relationship';
+      $type->recipient = _user_relationship_privatemsg_get_recipient_id($type->rtid, $user->uid);
+      $type->account = $user;
+      $matches[privatemsg_recipient_key($type)] = $type;
+      if (count($matches) >= $limit) {
+        break;
+      }
+    }
+  }
+  return $matches;
+}
+
+/**
+ * Implements hook_privatemsg_name_lookup().
+ */
+function user_relationship_privatemsg_privatemsg_name_lookup($string) {
+  global $user;
+  $relationship = str_replace(t('[user_relationship]'), '', $string);
+  if ($recipient = user_relationships_type_load(array('plural_name' => $relationship))) {
+    $recipient->type = 'user_relationship';
+    $recipient->recipient = _user_relationship_privatemsg_get_recipient_id($recipient->rtid, $user->uid);
+    $recipient->account = $user;
+    return array(privatemsg_recipient_key($recipient) => $recipient);
+  }
+}
+
+/**
+ * Implements hook_query_privatemsg_autocomplete_alter().
+ */
+function user_relationship_privatemsg_query_privatemsg_autocomplete_alter($query) {
+  global $user;
+  // Check if $author needs to be restricted.
+  if (!variable_get('user_relationships_privatemsg_autocomplete_alter', 0) || !user_relationship_privatemsg_restrict($user, FALSE)) {
+    return;
+  }
+
+  $query->innerJoin('user_relationships', 'ur', 'u.uid = ur.requestee_id');
+  $query
+    ->condition('ur.approved', 1)
+    ->condition('ur.requester_id', $user->uid);
+}
+
+
+/**
+ * Implements hook_privatemsg_block_message().
+ */
+function user_relationship_privatemsg_privatemsg_block_message($author, $recipients) {
+  // Check if $author needs to be restricted.
+  if (!user_relationship_privatemsg_restrict($author)) {
+    return;
+  }
+
+  $blocked = array();
+  foreach ($recipients as $recipient) {
+    if (isset($recipient->type) && $recipient->type != 'user' && $recipient->type != 'hidden') {
+      continue;
+    }
+
+    // Don't block if the user is writing himself.
+    if ($author->uid == $recipient->uid) {
+      continue;
+    }
+
+    //block if user is only receiving pm's from his relationships, and author is not one of them
+    $user_setting = isset($recipient->data['user_relationships_allow_private_message']) ? $recipient->data['user_relationships_allow_private_message'] : 'on all users';
+    $setting = variable_get('user_relationships_restrict_privatemsg', 'all');
+    if ($setting == 'relationships' || ($setting == 'all_overridable' && $user_setting == 'on in relations')) {
+      // Check if author is related.
+      $relations = user_relationships_load(array('between' => array($author->uid, $recipient->uid)));
+      if (empty($relations)) {
+        $blocked[] = array(
+          'recipient' => privatemsg_recipient_key($recipient),
+          'message' => t('!name does not have an established relationship with you.', array('!name' => privatemsg_recipient_format($recipient))),
+        );
+      }
+    }
+  }
+  return $blocked;
+}
+
+/**
+ * Check if the current user should be restricted.
+ *
+ * @param $author
+ *   User object of the message author.
+ * @param $check_permission
+ *   If TRUE, this only applies if all users are restricted by default.
+ *
+ * @return
+ *   FALSE if the current user should be not be restricted, TRUE otherwise.
+ */
+function user_relationship_privatemsg_restrict($author, $check_permission = TRUE) {
+  //#522078 admin killswitch, always ignore this for user 1.
+  if (($check_permission && variable_get('user_relationships_restrict_privatemsg', 'all') == 'all') || $author->uid == 1) {
+    return FALSE;
+  }
+
+  $exlude_roles = array_filter(variable_get('user_relationships_privatemsg_role_exclusions', array()));
+  // First, make sure that we have a user object with roles, and then check if
+  // the users does have any of the exluded roles.
+  if (!empty($author->roles) || $author = user_load($author->uid)) {
+    $is_excluded = array_intersect(array_keys($author->roles), $exlude_roles);
+    if (!empty($is_excluded)) {
+      return FALSE;
+    }
+  }
+  return TRUE;
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function user_relationship_privatemsg_form_user_relationships_ui_settings_alter(&$form, &$form_state) {
+  $form['privatemsg'] = array(
+    '#type'   => 'fieldset',
+    '#title'  => t('Private Message Integration'),
+    '#description'    => t('Configure integration with the Private Message module. These settings apply to all users except the default admin user.'),
+    '#group' => 'settings',
+  );
+  $form['privatemsg']['user_relationships_restrict_privatemsg'] = array(
+    '#type'           => 'radios',
+    '#title'          => t('Permitted message recipients'),
+    '#default_value'  => variable_get('user_relationships_restrict_privatemsg', 'all'),
+    '#options' => array(
+      'all' => t('Allow sending messages to all users'),
+      'all_overridable' => t('Allow sending messages to all users, but provide users with an option to only receive messages from their confirmed relationships.'),
+      'relationships' => t('Only allow sending messages between confirmed relationships.'),
+    ),
+  );
+  $form['privatemsg']['user_relationships_privatemsg_autocomplete_alter'] = array(
+    '#type'           => 'checkbox',
+    '#title'          => t('Only suggest confirmed relationships as message recipients'),
+    '#description'    => t('When sending a private message, only display confirmed relationships in the "To" autocomplete field.'),
+    '#default_value'  => variable_get('user_relationships_privatemsg_autocomplete_alter', 0),
+  );
+  $form['privatemsg']['user_relationships_privatemsg_role_exclusions'] = array(
+    '#type' => 'checkboxes',
+    '#options' => user_roles(TRUE),
+    '#default_value' => variable_get('user_relationships_privatemsg_role_exclusions', array()),
+    '#title' => t('Role exceptions'),
+    '#description' => t('Any roles checked below are exempt from the above restrictions and may send private messages to all users.'),
+  );
+}
+
+/**
+ * Implements hook_form_alter().
+ */
+function user_relationship_privatemsg_form_alter(&$form, &$form_state, $form_id) {
+  if (($form_id == 'user_register_form' || $form_id == 'user_profile_form') && $form['#user_category'] == 'account') {
+    // #257748 #458046 for adding the functionality of allowing/disallowing
+    // private messages.
+    if (variable_get('user_relationships_restrict_privatemsg', 'all') == 'all_overridable' && user_access('maintain own relationships', $form['#user'])) {
+      $options = drupal_map_assoc(array('on in relations', 'on all users'));
+      $form['privatemsg']['user_relationships_allow_private_message'] = array(
+        '#type' => 'radios',
+        '#title' => t('Allow private messages from...'),
+        '#description' => t('Choose who can send you private messages.'),
+        '#options' => array('on all users' => t('Everyone'), 'on in relations' => t('Only those who have an established relationship with me')),
+        '#default_value' => !empty($form['#user']->data['user_relationships_allow_private_message'])? $form['#user']->data['user_relationships_allow_private_message'] : 'on all users',
+        '#states' => array(
+          'visible' => array(
+            ':input[name="pm_enable"]' => array('checked' => TRUE),
+          ),
+        ),
+      );
+    }
+  }
+}
diff --git a/user_relationship_privatemsg/user_relationship_privatemsg.test b/user_relationship_privatemsg/user_relationship_privatemsg.test
new file mode 100644
index 0000000..4344eb0
--- /dev/null
+++ b/user_relationship_privatemsg/user_relationship_privatemsg.test
@@ -0,0 +1,174 @@
+<?php
+/**
+ * @file
+ * User relationship privatemsg tests.
+ */
+
+/**
+ * Tests cases to send messages to related users.
+ */
+class UserRelationshipsPrivatemsgRecipientTestCase extends DrupalWebTestCase {
+
+  /**
+   * Implements getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('User Relationships Privatemsg recipients'),
+      'description' => t('Check relationship recipient type for privatemsg'),
+      'group' => t('User Relationships'),
+      'dependencies' => array('privatemsg'),
+    );
+  }
+
+  /**
+   * Enable required modules.
+   */
+  function setUp() {
+    return parent::setUp('privatemsg', 'user_relationships_api', 'user_relationships_ui', 'user_relationship_privatemsg');
+  }
+
+  /**
+   * Tests sending a message to related users.
+   */
+  function testSendtoRelationship() {
+    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'can have relationships', 'view relationship recipients', 'administer user relationships', 'maintain own relationships', 'write privatemsg to relationships', 'access user profiles'));
+    $userA = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'can have relationships', 'view relationship recipients', 'write privatemsg to relationships'));
+    $userB = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'can have relationships', 'view relationship recipients'));
+
+    $this->drupalLogin($admin);
+
+    // Only allow messages between related users.
+    $edit = array(
+      'user_relationships_restrict_privatemsg' => 'relationships',
+    );
+    $this->drupalPost('admin/config/people/relationships/settings', $edit, t('Save configuration'));
+
+    // Create relationship.
+    $relationship = array(
+      'name' => $this->randomName(),
+      'plural_name' => $this->randomName(),
+      'requires_approval' => FALSE,
+    );
+    $this->drupalPost('admin/config/people/relationships/add', $relationship, t('Submit'));
+
+    // Become related to user A and B.
+    $this->drupalGet('relationship/' . $userA->uid . '/request/1');
+    $this->drupalPost(NULL, array(), t('Send'));
+    $this->drupalGet('relationship/' . $userB->uid . '/request/1');
+    $this->drupalPost(NULL, array(), t('Send'));
+
+    // Send a message to related users.
+    $this->drupalGet('messages/new');
+    $this->assertText(t('Enter the name of a user relationship to write a message to all related users. Example: @relationship.', array('@relationship' => $relationship['plural_name'])), t('Description is displayed.'));
+
+    $message = array(
+      'recipient' => $relationship['plural_name'],
+      'subject' => $this->randomName(),
+      'body[value]' => $this->randomName(50),
+    );
+    $this->drupalPost(NULL, $message, t('Send message'));
+    $this->assertText(t('A message has been sent to Your @relationship (relationship).', array('@relationship' => $relationship['plural_name'])));
+
+    // Check if userA recieved the message and is able to respond.
+    $this->drupalLogin($userA);
+    $this->drupalGet('messages');
+    $this->clickLink($message['subject']);
+    $this->assertText($message['body[value]']);
+
+    // Send a reply
+    $reply = array('body[value]' => $this->randomName(50));
+    $this->drupalPost(NULL, $reply, t('Send message'));
+
+    // Login as userB and make sure the original message is displayed but not the message nor the username of userA.
+    $this->drupalLogin($userB);
+    $this->drupalGet('messages');
+    $this->clickLink($message['subject']);
+    $this->assertText($message['body[value]']);
+    $this->assertNoText($userA->name);
+
+    // Log in as admin and check that the response of user A is visible.
+    $this->drupalLogin($admin);
+    $this->drupalGet('messages');
+    $this->clickLink($message['subject']);
+    $this->assertText($reply['body[value]']);
+
+    $reply2 = array('body[value]' => $this->randomName(50));
+    $this->drupalPost(NULL, $reply2, t('Send message'));
+
+    // Log in as user B again and check that the reply but not user a is visible.
+    $this->drupalLogin($userB);
+    $this->drupalGet('messages');
+    $this->clickLink($message['subject']);
+    $this->assertText($reply2['body[value]']);
+    // @todo: does not yet work.
+    //$this->assertNoText($userA->name);
+
+    // Test reverse relationship.
+    $this->drupalLogin($userA);
+    $this->drupalGet('messages/new');
+    $this->assertText(t('Enter the name of a user relationship to write a message to all related users. Example: @relationship.', array('@relationship' => $relationship['plural_name'])), t('Description is displayed.'));
+
+    $message = array(
+      'recipient' => $relationship['plural_name'],
+      'subject' => $this->randomName(),
+      'body[value]' => $this->randomName(50),
+    );
+    $this->drupalPost(NULL, $message, t('Send message'));
+    $this->assertText(t('A message has been sent to Your @relationship (relationship).', array('@relationship' => $relationship['plural_name'])));
+
+    // Log in as admin and check that the response of user A is visible.
+    $this->drupalLogin($admin);
+    $this->drupalGet('messages');
+    $this->clickLink($message['subject']);
+    $this->assertText($message['body[value]']);
+  }
+
+  /**
+   * Test a relationship with bath api processing.
+   */
+  function dtestManyRelations() {
+    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'can have relationships', 'view relationship recipients', 'administer user relationships', 'maintain own relationships', 'write privatemsg to relationships', 'access user profiles'));
+
+    $this->drupalLogin($admin);
+
+    // Create relationship.
+    $relationship = array(
+      'name' => $this->randomName(),
+      'plural_name' => $this->randomName(),
+      'requires_approval' => FALSE,
+    );
+    $this->drupalPost('admin/config/people/relationships/add', $relationship, t('Submit'));
+
+    // Create 25 users (more than are allowed to be process directly) and relate
+    // them to the admin.
+    $users = array();
+    for ($i = 0; $i < 25; $i++) {
+      $users[$i] = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'can have relationships', 'view relationship recipients', 'write privatemsg to relationships'));
+      // Become related to that user.
+      $this->drupalGet('relationship/' . $users[$i]->uid . '/request/1');
+      $this->drupalPost(NULL, array(), t('Send'));
+    }
+    variable_set('privatemsg_recipient_small_threshold', 20);
+
+    // Send a message to related users.
+    $this->drupalGet('messages/new');
+    $this->assertText(t('Enter the name of a user relationship to write a message to all related users. Example: @relationship.', array('@relationship' => $relationship['plural_name'])), t('Description is displayed.'));
+
+    $message = array(
+      'recipient' => $relationship['plural_name'],
+      'subject' => $this->randomName(),
+      'body[value]' => $this->randomName(50),
+    );
+    $this->drupalPost(NULL, $message, t('Send message'));
+    $this->assertText(t('A message has been sent to Your @relationship (relationship).', array('@relationship' => $relationship['plural_name'])));
+
+    // Test if all recipients see the message.
+    foreach ($users as $user) {
+      $this->drupalLogin($user);
+      $this->drupalGet('messages');
+      $this->clickLink($message['subject']);
+      $this->assertText($message['body[value]']);
+    }
+  }
+}
diff --git a/user_relationships_api/user_relationships_api.module b/user_relationships_api/user_relationships_api.module
index 49159e2..57bd9d9 100644
--- a/user_relationships_api/user_relationships_api.module
+++ b/user_relationships_api/user_relationships_api.module
@@ -11,13 +11,8 @@
 define('UR_OK', 0x0);
 define('UR_BANNED', 0x1);
 
-// Pre Loading files that will be required in this module
-foreach (array('api', 'actions', 'socnet') as $file) {
-  module_load_include("inc", "user_relationships_api", "user_relationships_api.{$file}");
-}
-if (module_exists('privatemsg')) {
-  module_load_include('inc', 'user_relationships_api', 'user_relationships_api.privatemsg');
-}
+// @todo Move functions into this file and remove the explicit include.
+module_load_include("inc", "user_relationships_api", "user_relationships_api.api");
 
 /**
  * Helper function to generate the main and count queries from a list of parameters and options
@@ -130,7 +125,8 @@ function _user_relationships_generate_query($param = array(), $options = array()
     }
   }
   if (!empty($limit)) {
-    $query->range(0, $limit);
+    $offset = !empty($offset) ? $offset : 0;
+    $query->range($offset, $limit);
   }
   if (!empty($only_count)) {
     return $count;
diff --git a/user_relationships_api/user_relationships_api.privatemsg.inc b/user_relationships_api/user_relationships_api.privatemsg.inc
deleted file mode 100644
index 2f24008..0000000
--- a/user_relationships_api/user_relationships_api.privatemsg.inc
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-
-/**
- * @file
- * Integration with Privatemsg module
- * @author mansspams http://drupal.org/user/293179
- * @author alex.k http://drupal.org/user/183217
- */
-
-/**
- * Fills new Privatemsg autocomplete To: field with friends only.
- */
-function user_relationships_api_query_privatemsg_autocomplete_alter($query) {
-  global $user;
-  // Check if $author needs to be restricted.
-  if (!variable_get('user_relationships_privatemsg_autocomplete_alter', 0) || !user_relationships_api_privatemsg_restrict($user, FALSE)) {
-    return;
-  }
-
-  $query->innerJoin('user_relationships', 'ur', 'u.uid = ur.requestee_id');
-  $query
-    ->condition('ur.approved', 1)
-    ->condition('ur.requester_id', $user->uid);
-}
-
-
-/**
- * Blocks messages to users that are not in relationships with sender.
- * @see hook_privatemsg_block_message()
- */
-function user_relationships_api_privatemsg_block_message($author, $recipients) {
-  // Check if $author needs to be restricted.
-  if (!user_relationships_api_privatemsg_restrict($author)) {
-    return;
-  }
-
-  $blocked = array();
-  foreach ($recipients as $recipient) {
-    if (isset($recipient->type) && $recipient->type != 'user' && $recipient->type != 'hidden') {
-      continue;
-    }
-    //block if user is only receiving pm's from his relationships, and author is not one of them
-    $user_setting = isset($recipient->data['user_relationships_allow_private_message']) ? $recipient->data['user_relationships_allow_private_message'] : 'on all users';
-    if (
-        (variable_get('user_relationships_restrict_privatemsg', 'all') == 'relationships'
-         || (variable_get('user_relationships_restrict_privatemsg', 'all') == 'all_overridable'
-             && $user_setting == 'on in relations'))
-        && !module_invoke_all('socnet_is_related', $author->uid, $recipient->recipient)
-     ) {
-      $blocked[] = array(
-        'recipient' => privatemsg_recipient_key($recipient),
-        'message' => t('!name does not have an established relationship with you.', array('!name' => privatemsg_recipient_format($recipient))),
-      );
-    }
-  }
-  return $blocked;
-}
-
-function user_relationships_api_privatemsg_restrict($author, $check_permission = TRUE) {
-    //#522078 admin killswitch, always ignore this for user 1.
-  if (($check_permission && variable_get('user_relationships_restrict_privatemsg', 'all') == 'all') || $author->uid == 1) {
-    return FALSE;
-  }
-
-  $exlude_roles = array_filter(variable_get('user_relationships_privatemsg_role_exclusions', array()));
-  // First, make sure that we have a user object with roles, and then check if
-  // the users does have any of the exluded roles.
-  if (!empty($author->roles) || $author = user_load($author->uid)) {
-    $is_excluded = array_intersect(array_keys($author->roles), $exlude_roles);
-    if (!empty($is_excluded)) {
-      return FALSE;
-    }
-  }
-  return TRUE;
-}
diff --git a/user_relationships_ui/user_relationships_ui.admin.inc b/user_relationships_ui/user_relationships_ui.admin.inc
index d71215b..2317b73 100644
--- a/user_relationships_ui/user_relationships_ui.admin.inc
+++ b/user_relationships_ui/user_relationships_ui.admin.inc
@@ -55,37 +55,6 @@ function user_relationships_ui_settings() {
       'is_numeric' => array('msg' => t('The relationships per page setting is not an integer'))
     )))
   );
-  if (module_exists('privatemsg')) {
-    $form['privatemsg'] = array(
-      '#type'   => 'fieldset',
-      '#title'  => t('Private Message Integration'),
-      '#description'    => t('Configure integration with the Private Message module. These settings apply to all users except the default admin user.'),
-      '#group' => 'settings',
-    );
-    $form['privatemsg']['user_relationships_restrict_privatemsg'] = array(
-      '#type'           => 'radios',
-      '#title'          => t('Permitted message recipients'),
-      '#default_value'  => variable_get('user_relationships_restrict_privatemsg', 'all'),
-      '#options' => array(
-        'all' => t('Allow sending messages to all users'),
-        'all_overridable' => t('Allow sending messages to all users, but provide users with an option to only receive messages from their confirmed relationships.'),
-        'relationships' => t('Only allow sending messages between confirmed relationships.'),
-      ),
-    );
-    $form['privatemsg']['user_relationships_privatemsg_autocomplete_alter'] = array(
-      '#type'           => 'checkbox',
-      '#title'          => t('Only suggest confirmed relationships as message recipients'),
-      '#description'    => t('When sending a private message, only display confirmed relationships in the "To" autocomplete field.'),
-      '#default_value'  => variable_get('user_relationships_privatemsg_autocomplete_alter', 0),
-    );
-    $form['privatemsg']['user_relationships_privatemsg_role_exclusions'] = array(
-      '#type' => 'checkboxes',
-      '#options' => user_roles(TRUE),
-      '#default_value' => variable_get('user_relationships_privatemsg_role_exclusions', array()),
-      '#title' => t('Role exceptions'),
-      '#description' => t('Any roles checked below are exempt from the above restrictions and may send private messages to all users.'),
-    );
-  }
   $form['positioning'] = array(
     '#type'   => 'fieldset',
     '#title'  => t('AJAX Popup Positioning'),
diff --git a/user_relationships_ui/user_relationships_ui.module b/user_relationships_ui/user_relationships_ui.module
index 13e6571..c55e67b 100644
--- a/user_relationships_ui/user_relationships_ui.module
+++ b/user_relationships_ui/user_relationships_ui.module
@@ -754,25 +754,6 @@ function user_relationships_ui_form_alter(&$form, &$form_state, $form_id) {
         }
       }
     }
-    //#257748 #458046 for adding the functionality of allowing/disallowing private messages.
-    if (variable_get('user_relationships_restrict_privatemsg', 'all') == 'all_overridable'
-        && user_access('maintain own relationships', $form['#user'])
-        && module_exists('privatemsg')
-    ) { //check if privatemsg is installed
-      $options = drupal_map_assoc(array('on in relations', 'on all users'));
-      $form['privatemsg']['user_relationships_allow_private_message'] = array(
-        '#type' => 'radios',
-        '#title' => t('Allow private messages from...'),
-        '#description' => t('Choose who can send you private messages.'),
-        '#options' => array('on all users' => t('Everyone'), 'on in relations' => t('Only those who have an established relationship with me')),
-        '#default_value' => isset($form['#user']->data['user_relationships_allow_private_message'])? $form['#user']->data['user_relationships_allow_private_message'] : 'on all users',
-        '#states' => array(
-          'visible' => array(
-            ':input[name="pm_enable"]' => array('checked' => TRUE),
-          ),
-        ),
-      );
-    }
 
     // No options have been set so don't display it
     if (isset($form['user_relationships_ui_settings']) && sizeof($form['user_relationships_ui_settings']) == 3) {
-- 
1.7.4.1

