You are here

function user_relationships_request_relationship in User Relationships 5.2

Same name and namespace in other branches
  1. 5.3 user_relationships_api/user_relationships_api.api.inc \user_relationships_request_relationship()
  2. 5 user_relationships_api.inc \user_relationships_request_relationship()
  3. 6 user_relationships_api/user_relationships_api.api.inc \user_relationships_request_relationship()
  4. 7 user_relationships.module \user_relationships_request_relationship()

Public API for creating a relationship.

Parameters

$requester: object or ID of the requester

$requestee: object or ID of the requestee

$type: object or ID of the relationship type

$approved: boolean status of the relationship

Return value

object of the newly created relationship

6 calls to user_relationships_request_relationship()
user_relationships_request in ./user_relationships_forms.inc
Request a new relationship with another user
user_relationships_request_submit in ./user_relationships_actions.inc
Process relationship request.
user_relationship_defaults_user in plugins/user_relationship_defaults/user_relationship_defaults.module
hook_user()
user_relationship_implications_user_relationships in plugins/user_relationship_implications/user_relationship_implications.module
hook_user_relationships()
user_relationship_invites_user in plugins/user_relationship_invites/user_relationship_invites.module
Implements hook_user()

... See full list

File

./user_relationships_api.inc, line 92

Code

function user_relationships_request_relationship($requester, $requestee, $type, $approved = FALSE) {

  // translate an ID into an object
  foreach (array(
    'requester' => $requester,
    'requestee' => $requestee,
    'type' => $type,
  ) as $key => $value) {
    if (!is_object($value)) {
      ${$key} = $key == 'type' ? user_relationships_type_load($value) : user_load(array(
        'uid' => $value,
      ));
    }
  }
  if (!$requester) {
    $msg = t('Invalid requester in relationship request');
    drupal_set_message($msg);
    error_log($msg);
    return;
  }
  if (!$requestee) {
    $msg = t('Invalid requestee in relationship request');
    drupal_set_message($msg);
    error_log($msg);
    return;
  }
  if (!$type) {
    $msg = t('Invalid type in relationship request');
    drupal_set_message($msg);
    error_log($msg);
    return;
  }

  //Enforce the single relationship restriction, also http://drupal.org/node/271247
  if (!variable_get('user_relationships_allow_multiple', TRUE)) {
    if (user_relationships_load(array(
      'between' => array(
        $requester->uid,
        $requestee->uid,
      ),
    ), FALSE)) {
      drupal_set_message(t('Users are not allowed to have multiple relationships'), 'error');
      return;
    }
  }
  if (user_relationships_load(array(
    'requester_id' => $requester->uid,
    'requestee_id' => $requestee->uid,
    'rtid' => $type->rtid,
  ), TRUE)) {
    return t('This relationship already exists');
  }
  $user_auto_approve = is_array($requestee->user_relationships_auto_approve) && $requestee->user_relationships_auto_approve[$type->rtid];
  if ($user_auto_approve || !$type->requires_approval) {
    $approved = TRUE;
  }
  $relationship = (object) array(
    'requester_id' => $requester->uid,
    'requestee_id' => $requestee->uid,
    'approved' => $approved ? 1 : 0,
    'rtid' => $type->rtid,
  );
  return _user_relationships_save_relationship($relationship, 'request');
}