You are here

function user_relationships_request_relationship in User Relationships 6

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. 5.2 user_relationships_api.inc \user_relationships_request_relationship()
  4. 7 user_relationships.module \user_relationships_request_relationship()

Request a new user relationship

Parameters

$requester: object or UID of the requester

$requestee: object or UID of the requestee

$type: object or RTID of the relationship type

$approved: boolean status of the relationship

Return value

object of the newly created relationship

13 calls to user_relationships_request_relationship()
UserRelationshipsApiSocnetTestCase::testAllPendingFrom in user_relationships_api/tests/user_relationships_api.socnet.test
UserRelationshipsApiSocnetTestCase::testAllPendingTo in user_relationships_api/tests/user_relationships_api.socnet.test
UserRelationshipsApiSocnetTestCase::testGetRelatedUsers in user_relationships_api/tests/user_relationships_api.socnet.test
UserRelationshipsApiSocnetTestCase::testIsPending in user_relationships_api/tests/user_relationships_api.socnet.test
UserRelationshipsApiSocnetTestCase::testIsRelated in user_relationships_api/tests/user_relationships_api.socnet.test

... See full list

File

user_relationships_api/user_relationships_api.api.inc, line 165
User Relationships API. Data abstraction layer @author Jeff Smick (creator) @author Alex Karshakevich (maintainer) http://drupal.org/user/183217

Code

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

  //#578372 check if this is a "new" type of invocation, where first argument is a relationship object
  if (is_object($requester) && isset($requester->requester) && isset($requester->requestee) && isset($requester->type)) {
    $relationship = $requester;
    $requester = $relationship->requester;
    $requestee = $relationship->requestee;
    $type = $relationship->type;
    if (isset($relationship->approved)) {
      $approved = $relationship->approved;
    }
  }
  else {
    $relationship = new stdClass();
  }

  // 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($value);
    }
  }

  //check requester is allowed to create this rtype
  if (!user_relationships_api_can_request($requester, $type)) {
    watchdog('user_relationships_api', 'User %user has no suitable roles to request a %rtype relationship', array(
      '%user' => $requester->name,
      '%rtype' => $type->name,
    ), WATCHDOG_WARNING);

    //it would be good to return a reason why save is denied, but it's the API portion, it does not expose anything user-visible
    return FALSE;
  }

  //check requestee is allowed to receive this rtype
  if (!user_relationships_api_can_receive($requestee, $type)) {
    watchdog('user_relationships_api', 'User %user has no suitable roles to receive a %rtype relationship', array(
      '%user' => $requestee->name,
      '%rtype' => $type->name,
    ), WATCHDOG_WARNING);

    //it would be good to return a reason why save is denied, but it's the API portion, it does not expose anything user-visible
    return FALSE;
  }

  // check whether this relationship aleady exists
  if (!$type->is_oneway || !$type->is_reciprocal) {
    $existing = user_relationships_load(array(
      'between' => array(
        $requester->uid,
        $requestee->uid,
      ),
      'rtid' => $type->rtid,
    ), array(
      'count' => TRUE,
    ));
  }
  else {
    $existing = user_relationships_load(array(
      'requester_id' => $requester->uid,
      'requestee_id' => $requestee->uid,
      'rtid' => $type->rtid,
    ), array(
      'count' => TRUE,
    ));
  }
  if (!$existing) {
    $relationship->requester_id = $requester->uid;
    $relationship->requestee_id = $requestee->uid;
    $relationship->approved = $approved || !$type->requires_approval || isset($requestee->user_relationships_ui_auto_approve) && $requestee->user_relationships_ui_auto_approve[$type->rtid];
    $relationship->rtid = $type->rtid;
    return user_relationships_save_relationship($relationship);
  }
}